Search in sources :

Example 1 with BasicDBInterface

use of com.tremolosecurity.provisioning.core.providers.BasicDBInterface in project OpenUnison by TremoloSecurity.

the class Drupal7GetSequence method doTask.

@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Searching for users.id");
            logger.debug("Looking for user : '" + user.getUserID() + "'");
        }
        User looking = task.getConfigManager().getProvisioningEngine().getTarget(this.targetName).findUser(user.getUserID(), new HashMap<String, Object>());
        if (logger.isDebugEnabled()) {
            logger.debug("User object : '" + looking + "'");
        }
        if (looking == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("User not found");
            }
        }
        if (looking != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("User found, setting to user id : '" + looking.getAttribs().get("uid").getValues().get(0) + "'");
            }
            user.getAttribs().put("drupalid", new Attribute("drupalid", looking.getAttribs().get("uid").getValues().get(0)));
            return true;
        }
    } catch (ProvisioningException pe) {
        // do nothing
        pe.printStackTrace();
    }
    UserStoreProvider provider = task.getConfigManager().getProvisioningEngine().getTarget(this.targetName).getProvider();
    BasicDBInterface dbprovider = (BasicDBInterface) provider;
    Connection con = null;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Getting Connection");
        }
        con = dbprovider.getDS().getConnection();
        if (logger.isDebugEnabled()) {
            logger.debug("Preparing Statement");
        }
        PreparedStatement ps = con.prepareStatement("INSERT INTO sequences () VALUES ()", Statement.RETURN_GENERATED_KEYS);
        if (logger.isDebugEnabled()) {
            logger.debug("Executing Statement");
        }
        ps.executeUpdate();
        if (logger.isDebugEnabled()) {
            logger.debug("Getting key");
        }
        ResultSet rs = ps.getGeneratedKeys();
        rs.next();
        int id = rs.getInt(1);
        if (logger.isDebugEnabled()) {
            logger.debug("ID: '" + id + "'");
        }
        rs.close();
        ps.close();
        user.getAttribs().put("drupalid", new Attribute("drupalid", Integer.toString(id)));
        return true;
    } catch (SQLException e) {
        throw new ProvisioningException("Could not generate userid", e);
    } finally {
        if (con != null) {
            try {
                logger.info("Closing connection");
                con.close();
            } catch (Exception e1) {
            // do nothing
            }
        }
    }
}
Also used : User(com.tremolosecurity.provisioning.core.User) Attribute(com.tremolosecurity.saml.Attribute) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) BasicDBInterface(com.tremolosecurity.provisioning.core.providers.BasicDBInterface) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) SQLException(java.sql.SQLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ResultSet(java.sql.ResultSet) UserStoreProvider(com.tremolosecurity.provisioning.core.UserStoreProvider)

Aggregations

ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)1 User (com.tremolosecurity.provisioning.core.User)1 UserStoreProvider (com.tremolosecurity.provisioning.core.UserStoreProvider)1 BasicDBInterface (com.tremolosecurity.provisioning.core.providers.BasicDBInterface)1 Attribute (com.tremolosecurity.saml.Attribute)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1