Search in sources :

Example 1 with IdentityRoleManagementException

use of org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException in project carbon-identity-framework by wso2.

the class RoleDAOImpl method addRoleID.

protected String addRoleID(String roleName, String tenantDomain) throws IdentityRoleManagementException {
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    String id = UUID.randomUUID().toString();
    // Append internal domain in order to maintain the backward compatibility.
    roleName = appendInternalDomain(roleName);
    if (log.isDebugEnabled()) {
        log.debug("Adding the roleID: " + id + " for the role: " + roleName + " in the tenantDomain: " + tenantDomain);
    }
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, ADD_SCIM_ROLE_ID_SQL)) {
            statement.setInt(RoleTableColumns.TENANT_ID, tenantId);
            statement.setString(RoleTableColumns.ROLE_NAME, roleName);
            statement.setString(RoleTableColumns.ATTR_NAME, RoleConstants.ID_URI);
            statement.setString(RoleTableColumns.ATTR_VALUE, id);
            statement.executeUpdate();
            IdentityDatabaseUtil.commitTransaction(connection);
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            String errorMessage = "Error while adding the the roleID: %s for the role: %s in the tenantDomain: %s";
            throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, id, roleName, tenantDomain), e);
        }
    } catch (SQLException e) {
        String errorMessage = "Error while adding the the roleID: %s for the role: %s in the tenantDomain: %s";
        throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, id, roleName, tenantDomain), e);
    }
    return id;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) IdentityRoleManagementServerException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException) Connection(java.sql.Connection)

Example 2 with IdentityRoleManagementException

use of org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException in project carbon-identity-framework by wso2.

the class RoleDAOImpl method getRole.

@Override
public Role getRole(String roleID, String tenantDomain) throws IdentityRoleManagementException {
    Role role = new Role();
    String roleName = getRoleNameByID(roleID, tenantDomain);
    role.setId(roleID);
    role.setName(roleName);
    role.setTenantDomain(tenantDomain);
    role.setUsers(getUserListOfRole(roleID, tenantDomain));
    role.setGroups(getGroupListOfRole(roleID, tenantDomain));
    role.setPermissions(getPermissionListOfRole(roleID, tenantDomain));
    return role;
}
Also used : Role(org.wso2.carbon.identity.role.mgt.core.Role)

Example 3 with IdentityRoleManagementException

use of org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException in project carbon-identity-framework by wso2.

the class RoleDAOImpl method getGroupListOfRole.

@Override
public List<GroupBasicInfo> getGroupListOfRole(String roleID, String tenantDomain) throws IdentityRoleManagementException {
    if (!isExistingRoleID(roleID, tenantDomain)) {
        throw new IdentityRoleManagementClientException(ROLE_NOT_FOUND.getCode(), "Role id: " + roleID + " does not exist in the system.");
    }
    String roleName = getRoleNameByID(roleID, tenantDomain);
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    List<GroupBasicInfo> groupList = new ArrayList<>();
    List<String> groupNames = new ArrayList<>();
    List<String> disabledDomainName = getDisabledDomainNames();
    String primaryDomainName = IdentityUtil.getPrimaryDomainName();
    if (primaryDomainName != null) {
        primaryDomainName = primaryDomainName.toUpperCase(Locale.ENGLISH);
    }
    try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, GET_GROUP_LIST_OF_ROLE_SQL, RoleTableColumns.UM_ID)) {
            statement.setString(RoleTableColumns.UM_ROLE_NAME, roleName);
            statement.setInt(RoleTableColumns.UM_TENANT_ID, tenantId);
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    String name = resultSet.getString(1);
                    String domain = resultSet.getString(2);
                    if (!disabledDomainName.contains(domain)) {
                        if (!StringUtils.equals(primaryDomainName, domain)) {
                            name = UserCoreUtil.addDomainToName(name, domain);
                        } else {
                            name = primaryDomainName + UserCoreConstants.DOMAIN_SEPARATOR + name;
                        }
                        groupNames.add(name);
                    }
                }
            }
        }
    } catch (SQLException e) {
        String errorMessage = "Error while while getting the group list of role for role name: %s in the " + "tenantDomain: %s";
        throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, tenantDomain), e);
    }
    Map<String, String> groupNamesToIDs = getGroupIDsByNames(groupNames, tenantDomain);
    groupNamesToIDs.forEach((groupName, groupID) -> groupList.add(new GroupBasicInfo(groupID, groupName)));
    return groupList;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) GroupBasicInfo(org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo) IdentityRoleManagementServerException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException) ResultSet(java.sql.ResultSet) IdentityRoleManagementClientException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)

Example 4 with IdentityRoleManagementException

use of org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException in project carbon-identity-framework by wso2.

the class RoleDAOImpl method isExistingRoleName.

@Override
public boolean isExistingRoleName(String roleName, String tenantDomain) throws IdentityRoleManagementException {
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    boolean isExist = false;
    try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, IS_ROLE_EXIST_SQL, RoleTableColumns.UM_ID)) {
            statement.setString(RoleTableColumns.UM_ROLE_NAME, removeInternalDomain(roleName));
            statement.setInt(RoleTableColumns.UM_TENANT_ID, tenantId);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    isExist = resultSet.getInt(1) > 0;
                }
            }
        }
    } catch (SQLException e) {
        String errorMessage = "Error while checking is existing role for role name: %s in the tenantDomain: %s";
        throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, tenantDomain), e);
    }
    if (log.isDebugEnabled()) {
        log.debug("Is roleName: " + roleName + " Exist: " + isExist + " in the tenantDomain: " + tenantDomain);
    }
    return isExist;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) IdentityRoleManagementServerException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 5 with IdentityRoleManagementException

use of org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException in project carbon-identity-framework by wso2.

the class RoleDAOImpl method updateSCIMRoleName.

protected void updateSCIMRoleName(String roleName, String newRoleName, String tenantDomain) throws IdentityRoleManagementException {
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    // Append internal domain in order to maintain the backward compatibility.
    roleName = appendInternalDomain(roleName);
    newRoleName = appendInternalDomain(newRoleName);
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, UPDATE_SCIM_ROLE_NAME_SQL)) {
            statement.setString(RoleTableColumns.NEW_ROLE_NAME, newRoleName);
            statement.setInt(RoleTableColumns.TENANT_ID, tenantId);
            statement.setString(RoleTableColumns.ROLE_NAME, roleName);
            statement.executeUpdate();
            IdentityDatabaseUtil.commitTransaction(connection);
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            String errorMessage = "Error while updating the the roleName: %s in the tenantDomain: " + "%s";
            throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, tenantDomain), e);
        }
    } catch (SQLException e) {
        String errorMessage = "Error while updating the the roleName: %s in the tenantDomain: " + "%s";
        throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, tenantDomain), e);
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) IdentityRoleManagementServerException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException) Connection(java.sql.Connection)

Aggregations

IdentityRoleManagementClientException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)29 IdentityRoleManagementServerException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException)23 RoleBasicInfo (org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Test (org.testng.annotations.Test)20 NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)20 Connection (java.sql.Connection)19 SQLException (java.sql.SQLException)19 Matchers.anyString (org.mockito.Matchers.anyString)14 IdentityRoleManagementException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)14 Role (org.wso2.charon3.core.objects.Role)13 ResultSet (java.sql.ResultSet)12 RoleManagementEventPublisherProxy (org.wso2.carbon.identity.role.mgt.core.RoleManagementEventPublisherProxy)11 ArrayList (java.util.ArrayList)9 CharonException (org.wso2.charon3.core.exceptions.CharonException)8 ExpressionNode (org.wso2.charon3.core.utils.codeutils.ExpressionNode)8 Node (org.wso2.charon3.core.utils.codeutils.Node)8 OperationNode (org.wso2.charon3.core.utils.codeutils.OperationNode)8 UserStoreException (org.wso2.carbon.user.api.UserStoreException)6 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)6