Search in sources :

Example 46 with Workflow

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

the class BasicDB method addGroup.

@Override
public void addGroup(String name, Map<String, String> additionalAttributes, User user, Map<String, Object> request) throws ProvisioningException {
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    if (this.groupMode == BasicDB.GroupManagementMode.Many2Many || this.groupMode == BasicDB.GroupManagementMode.One2Many) {
        String sql = (String) additionalAttributes.get("unison.group.create.sql");
        Connection con = null;
        try {
            con = this.ds.getConnection();
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, name);
            boolean done = false;
            int i = 2;
            StringBuilder b = new StringBuilder();
            while (!done) {
                b.setLength(0);
                String val = (String) additionalAttributes.get(b.append("unison.group.create.param.").append(i).toString());
                if (val != null) {
                    ps.setString(i, val);
                    i++;
                } else {
                    done = true;
                }
            }
            int num = ps.executeUpdate();
            ps.close();
            if (num > 0) {
                this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Add, approvalID, workflow, "group-object", name);
            }
        } catch (SQLException e) {
            throw new ProvisioningException("Could not search for group", e);
        } finally {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    } else {
        throw new ProvisioningException("Not Supported");
    }
}
Also used : SQLException(java.sql.SQLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Connection(java.sql.Connection) Workflow(com.tremolosecurity.provisioning.core.Workflow) PreparedStatement(java.sql.PreparedStatement)

Example 47 with Workflow

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

the class BasicDB method many2manySyncGroups.

private void many2manySyncGroups(User user, boolean addOnly, User foundUser, int userIDnum, Connection con, StringBuffer b, Map<String, Object> request) throws SQLException, Exception {
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    b.setLength(0);
    b.append("SELECT ");
    this.getFieldName(this.groupPrimaryKey, b).append(" FROM ");
    this.getFieldName(this.groupTable, b).append(" WHERE ");
    this.getFieldName(this.groupName, b).append(" = ?");
    PreparedStatement getGroupID = con.prepareStatement(b.toString());
    b.setLength(0);
    b.append("INSERT INTO ").append(this.groupLinkTable).append(" (");
    this.getFieldName(this.groupGroupKey, b).append(",");
    this.getFieldName(this.groupUserKey, b).append(") VALUES (?,?)");
    PreparedStatement addGroup = con.prepareStatement(b.toString());
    b.setLength(0);
    b.append("DELETE FROM ").append(this.groupLinkTable).append(" WHERE ");
    this.getFieldName(this.groupGroupKey, b).append("=? AND ");
    this.getFieldName(this.groupUserKey, b).append("=?");
    PreparedStatement delGroup = con.prepareStatement(b.toString());
    for (String groupName : user.getGroups()) {
        if (!foundUser.getGroups().contains(groupName)) {
            getGroupID.setString(1, groupName);
            ResultSet rs = getGroupID.executeQuery();
            if (!rs.next()) {
                throw new Exception("Group " + groupName + " does not exist");
            }
            int groupID = rs.getInt(this.groupPrimaryKey);
            addGroup.setInt(1, groupID);
            addGroup.setInt(2, userIDnum);
            addGroup.executeUpdate();
            this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "group", groupName);
        }
    }
    if (!addOnly) {
        for (String groupName : foundUser.getGroups()) {
            if (!user.getGroups().contains(groupName)) {
                getGroupID.setString(1, groupName);
                ResultSet rs = getGroupID.executeQuery();
                if (!rs.next()) {
                    throw new Exception("Group " + groupName + " does not exist");
                }
                int groupID = rs.getInt(this.groupPrimaryKey);
                delGroup.setInt(1, groupID);
                delGroup.setInt(2, userIDnum);
                delGroup.executeUpdate();
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Delete, approvalID, workflow, "group", groupName);
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) Workflow(com.tremolosecurity.provisioning.core.Workflow) PreparedStatement(java.sql.PreparedStatement) LDAPException(com.novell.ldap.LDAPException) PropertyVetoException(java.beans.PropertyVetoException) SQLException(java.sql.SQLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException)

Example 48 with Workflow

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

the class BasicDB method deleteGroup.

@Override
public void deleteGroup(String name, User user, Map<String, Object> request) throws ProvisioningException {
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    if (this.groupMode == BasicDB.GroupManagementMode.Many2Many || this.groupMode == BasicDB.GroupManagementMode.One2Many) {
        // String sql = "DELETE FROM " + this.groupTable + " WHERE " + this.groupName + "=?";
        StringBuilder sb = new StringBuilder();
        sb.append("DELETE FROM ");
        if (this.beginEscape != null) {
            sb.append(this.beginEscape);
        }
        sb.append(this.groupTable);
        if (this.endEscape != null) {
            sb.append(this.endEscape);
        }
        sb.append(" WHERE ").append(this.groupName).append("=?");
        String sql = sb.toString();
        Connection con = null;
        try {
            con = this.ds.getConnection();
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, name);
            int num = ps.executeUpdate();
            ps.close();
            if (num > 0) {
                this.cfgMgr.getProvisioningEngine().logAction(name, true, ActionType.Delete, approvalID, workflow, "group-object", name);
            }
        } catch (SQLException e) {
            throw new ProvisioningException("Could not search for group", e);
        } finally {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    } else {
        throw new ProvisioningException("Not Supported");
    }
}
Also used : SQLException(java.sql.SQLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Connection(java.sql.Connection) Workflow(com.tremolosecurity.provisioning.core.Workflow) PreparedStatement(java.sql.PreparedStatement)

Example 49 with Workflow

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

the class BasicDB method deleteUser.

/* (non-Javadoc)
	 * @see com.tremolosecurity.provisioning.core.providers.BasicDB#deleteUser(com.tremolosecurity.provisioning.core.User, java.util.Map)
	 */
@Override
public void deleteUser(User user, Map<String, Object> request) throws ProvisioningException {
    Connection con = null;
    int approvalID = 0;
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    try {
        con = this.ds.getConnection();
        StringBuffer select = new StringBuffer();
        if (this.userSQL != null) {
            select.append(this.userSQL.replaceAll("\\%S", this.userPrimaryKey).replaceAll("\\%L", "?"));
        } else {
            select.append("SELECT ");
            this.getFieldName(this.userPrimaryKey, select).append(" FROM ").append(escapeTableName(this.userTable)).append(" WHERE ");
            this.getFieldName(this.userName, select).append("=?");
        }
        PreparedStatement ps = con.prepareStatement(select.toString());
        ps.setString(1, user.getUserID());
        ResultSet rs = ps.executeQuery();
        if (!rs.next()) {
            throw new ProvisioningException("User not found " + user.getUserID());
        }
        int id = rs.getInt(this.userPrimaryKey);
        rs.close();
        ps.close();
        con.setAutoCommit(false);
        if (this.customDBProvider != null) {
            this.customDBProvider.deleteUser(con, id, request);
        } else {
            select.setLength(0);
            select.append("DELETE FROM ").append(escapeTableName(this.userTable)).append(" WHERE ");
            this.getFieldName(this.userPrimaryKey, select).append("=?");
            ps = con.prepareStatement(select.toString());
            ps.setInt(1, id);
            ps.executeUpdate();
            switch(this.groupMode) {
                case None:
                    break;
                case One2Many:
                    select.setLength(0);
                    select.append("DELETE FROM ").append(escapeTableName(this.groupTable)).append(" WHERE ");
                    this.getFieldName(this.groupUserKey, select).append("=?");
                    ps = con.prepareStatement(select.toString());
                    ps.setInt(1, id);
                    ps.executeUpdate();
                    break;
                case Many2Many:
                    many2manyDeleteGroups(con, select, id);
                    break;
            }
        }
        con.commit();
        this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Delete, approvalID, workflow, "userName", user.getUserID());
    } catch (Exception e) {
        try {
            con.rollback();
        } catch (SQLException e1) {
        }
        throw new ProvisioningException("Could not delete user " + user.getUserID(), e);
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Workflow(com.tremolosecurity.provisioning.core.Workflow) PreparedStatement(java.sql.PreparedStatement) LDAPException(com.novell.ldap.LDAPException) PropertyVetoException(java.beans.PropertyVetoException) SQLException(java.sql.SQLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException)

Example 50 with Workflow

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

the class BasicDB method insertCreate.

private void insertCreate(User user, Set<String> attributes, Map<String, Attribute> attrs, Connection con, Map<String, Object> request) throws SQLException, ProvisioningException {
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    StringBuffer insert = new StringBuffer();
    insert.append("INSERT INTO ").append(this.userTable).append(" (");
    for (String attr : attributes) {
        if (attrs.get(attr) != null) {
            getFieldName(attr, insert).append(",");
        }
    }
    insert.setLength(insert.length() - 1);
    insert.append(") values (");
    for (String attr : attributes) {
        if (attrs.get(attr) != null) {
            insert.append("?,");
        }
    }
    insert.setLength(insert.length() - 1);
    insert.append(")");
    PreparedStatement ps = con.prepareStatement(insert.toString(), Statement.RETURN_GENERATED_KEYS);
    int i = 1;
    for (String attr : attributes) {
        if (attrs.get(attr) != null) {
            Attribute.DataType dataType = attrs.get(attr).getDataType();
            switch(dataType) {
                case string:
                    ps.setString(i, attrs.get(attr).getValues().get(0));
                    break;
                case intNum:
                    ps.setInt(i, Integer.parseInt(attrs.get(attr).getValues().get(0)));
                    break;
                case longNum:
                    ps.setLong(i, Long.parseLong(attrs.get(attr).getValues().get(0)));
                    break;
                case date:
                    ps.setDate(i, new Date(ISODateTimeFormat.date().parseDateTime(attrs.get(attr).getValues().get(0)).getMillis()));
                    break;
                case timeStamp:
                    ps.setTimestamp(i, new Timestamp(ISODateTimeFormat.dateTime().parseDateTime(attrs.get(attr).getValues().get(0)).getMillis()));
                    break;
            }
            i++;
        }
    }
    ps.executeUpdate();
    ResultSet rs = ps.getGeneratedKeys();
    int id;
    if (rs.next() && !this.driver.contains("oracle")) {
        id = (int) rs.getInt(1);
    } else {
        StringBuffer select = new StringBuffer();
        select.append("SELECT ");
        this.getFieldName(this.userPrimaryKey, select).append(" FROM ").append(this.userTable).append(" WHERE ");
        this.getFieldName(this.userName, select).append("=?");
        // con.prepareStatement( + this.userPrimaryKey + " FROM " + this.userTable + " WHERE " + this.userName + "=?");
        PreparedStatement getUserId = con.prepareStatement(select.toString());
        getUserId.setString(1, user.getUserID());
        ResultSet userResult = getUserId.executeQuery();
        userResult.next();
        id = (int) userResult.getInt(this.userPrimaryKey);
        userResult.close();
        getUserId.close();
    }
    this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Add, approvalID, workflow, "userName", user.getUserID());
    for (String attr : attributes) {
        if (attrs.get(attr) != null) {
            this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, attr, attrs.get(attr).getValues().get(0));
        }
    }
    if (user.getGroups().size() > 0) {
        switch(this.groupMode) {
            case None:
                break;
            case One2Many:
                insert.setLength(0);
                insert.append("INSERT INTO ").append(this.groupTable).append(" (").append(this.groupUserKey).append(",").append(this.groupName).append(") VALUES (?,?)");
                ps = con.prepareStatement(insert.toString());
                for (String groupName : user.getGroups()) {
                    ps.setInt(1, id);
                    ps.setString(2, groupName);
                    ps.executeUpdate();
                    this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "group", groupName);
                }
                break;
            case Many2Many:
                many2manySetGroupsCreate(user, insert, con, id, request);
                break;
        }
    }
}
Also used : Attribute(com.tremolosecurity.saml.Attribute) ResultSet(java.sql.ResultSet) Workflow(com.tremolosecurity.provisioning.core.Workflow) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Aggregations

Workflow (com.tremolosecurity.provisioning.core.Workflow)78 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)68 HttpCon (com.tremolosecurity.provisioning.util.HttpCon)32 IOException (java.io.IOException)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)22 ClientProtocolException (org.apache.http.client.ClientProtocolException)21 Attribute (com.tremolosecurity.saml.Attribute)19 ArrayList (java.util.ArrayList)18 LDAPException (com.novell.ldap.LDAPException)17 HashMap (java.util.HashMap)17 User (com.tremolosecurity.provisioning.core.User)16 HashSet (java.util.HashSet)15 ParseException (org.json.simple.parser.ParseException)14 LDAPSearchResults (com.novell.ldap.LDAPSearchResults)12 JSONObject (org.json.simple.JSONObject)12 Gson (com.google.gson.Gson)11 LDAPEntry (com.novell.ldap.LDAPEntry)11 LDAPAttribute (com.novell.ldap.LDAPAttribute)10 GitLabApiException (org.gitlab4j.api.GitLabApiException)10 SQLException (java.sql.SQLException)9