Skip to content

Commit

Permalink
add main role to shared role relationship addition service
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuradhaSK committed Oct 13, 2023
1 parent 7eec955 commit fc23447
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,16 @@ boolean isExistingRoleName(String roleName, String audience, String audienceId,
*/
String getRoleIdByName(String roleName, String audience, String audienceId, String tenantDomain)
throws IdentityRoleManagementException;

/**
* Add the relationship between main role and the shared role.
*
* @param mainRoleUUID Main role UUID.
* @param sharedRoleUUID Shared role UUID.
* @param mainRoleTenantDomain Main role tenant domain.
* @param sharedRoleTenantDomain Shared role tenant domain.
* @throws IdentityRoleManagementException Error occurred while adding the relationship.
*/
void addMainRoleToSharedRoleRelationship(String mainRoleUUID, String sharedRoleUUID, String mainRoleTenantDomain,
String sharedRoleTenantDomain) throws IdentityRoleManagementException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@ public String getRoleIdByName(String roleName, String audience, String audienceI
return roleDAO.getRoleIdByName(roleName, audience, audienceId, tenantDomain);
}

@Override
public void addMainRoleToSharedRoleRelationship(String mainRoleUUID, String sharedRoleUUID,
String mainRoleTenantDomain, String sharedRoleTenantDomain)
throws IdentityRoleManagementException {

roleDAO.addMainRoleToSharedRoleRelationship(mainRoleUUID, sharedRoleUUID, mainRoleTenantDomain,
sharedRoleTenantDomain);
}

private String getUser(String tenantDomain) {

String user = CarbonContext.getThreadLocalCarbonContext().getUsername();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,17 @@ boolean isExistingRoleName(String roleName, String audience, String audienceId,
* @throws IdentityRoleManagementException IdentityRoleManagementException.
*/
Role getRoleWithoutUsers(String roleID, String tenantDomain) throws IdentityRoleManagementException;

/**
* Add shared role to main role relationship.
*
* @param mainRoleUUID Main role UUID.
* @param sharedRoleUUID Shared role UUID.
* @param mainRoleTenantDomain Main role tenant domain.
* @param sharedRoleTenantDomain Shared role tenant domain.
* @throws IdentityRoleManagementException Error occurred while adding shared role to main role relationship.
*/
void addMainRoleToSharedRoleRelationship(String mainRoleUUID, String sharedRoleUUID, String mainRoleTenantDomain,
String sharedRoleTenantDomain)
throws IdentityRoleManagementException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.GET_ROLE_ID_BY_NAME_AND_AUDIENCE_SQL;
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.GET_ROLE_NAME_BY_ID_SQL;
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.GET_ROLE_SCOPE_SQL;
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.GET_ROLE_UM_ID;
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.GET_SHARED_ROLES_SQL;
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.GET_SHARED_ROLE_MAIN_ROLE_ID_SQL;
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.GET_USER_LIST_OF_ROLE_SQL;
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.INSERT_MAIN_TO_SHARED_ROLE_RELATIONSHIP;
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.IS_ROLE_EXIST_SQL;
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.IS_ROLE_ID_EXIST_SQL;
import static org.wso2.carbon.identity.role.v2.mgt.core.dao.SQLQueries.IS_SHARED_ROLE_SQL;
Expand Down Expand Up @@ -638,6 +640,74 @@ public Role getRoleWithoutUsers(String roleID, String tenantDomain) throws Ident
return role;
}

@Override
public void addMainRoleToSharedRoleRelationship(String mainRoleUUID, String sharedRoleUUID,
String mainRoleTenantDomain, String sharedRoleTenantDomain)
throws IdentityRoleManagementException {

String mainRoleName = getRoleNameByID(mainRoleUUID, mainRoleTenantDomain);
int mainRoleAudienceReference = getAudienceRefByID(mainRoleUUID, mainRoleTenantDomain);
int mainRoleTenantId = IdentityTenantUtil.getTenantId(mainRoleTenantDomain);

String sharedRoleName = getRoleNameByID(sharedRoleUUID, sharedRoleTenantDomain);
int sharedRoleAudienceReference = getAudienceRefByID(sharedRoleUUID, sharedRoleTenantDomain);
int sharedRoleTenantId = IdentityTenantUtil.getTenantId(sharedRoleTenantDomain);

int mainRoleUMId = 0;
int sharedRoleUMId = 0;
try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(false)) {
try (NamedPreparedStatement stmt = new NamedPreparedStatement(connection, GET_ROLE_UM_ID)) {
stmt.setString(RoleConstants.RoleTableColumns.UM_ROLE_NAME, mainRoleName);
stmt.setInt(RoleConstants.RoleTableColumns.UM_TENANT_ID, mainRoleTenantId);
stmt.setInt(RoleConstants.RoleTableColumns.UM_AUDIENCE_REF_ID, mainRoleAudienceReference);
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
mainRoleUMId = resultSet.getInt(1);
}
} catch (SQLException e) {
String message = "Error while resolving id of role name: %s in the tenantDomain: %s.";
throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
String.format(message, mainRoleName, mainRoleTenantDomain), e);
}

try (NamedPreparedStatement stmt = new NamedPreparedStatement(connection, GET_ROLE_UM_ID)) {
stmt.setString(RoleConstants.RoleTableColumns.UM_ROLE_NAME, sharedRoleName);
stmt.setInt(RoleConstants.RoleTableColumns.UM_TENANT_ID, sharedRoleTenantId);
stmt.setInt(RoleConstants.RoleTableColumns.UM_AUDIENCE_REF_ID, sharedRoleAudienceReference);
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
sharedRoleUMId = resultSet.getInt(1);
}
} catch (SQLException e) {
String message = "Error while resolving id of role name: %s in the tenantDomain: %s.";
throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
String.format(message, sharedRoleName, sharedRoleTenantDomain), e);
}

if (mainRoleUMId == 0 || sharedRoleUMId == 0) {
String message = "Error while resolving role id.";
throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
message);
}
try (NamedPreparedStatement preparedStatement = new NamedPreparedStatement(connection,
INSERT_MAIN_TO_SHARED_ROLE_RELATIONSHIP)) {
preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_SHARED_ROLE_ID, sharedRoleUMId);
preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_MAIN_ROLE_ID, mainRoleUMId);
preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_SHARED_ROLE_TENANT_ID, sharedRoleTenantId);
preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_MAIN_ROLE_TENANT_ID, mainRoleTenantId);
preparedStatement.executeUpdate();
} catch (SQLException e) {
String message = "Error while adding the role relationship of role: %s.";
throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
String.format(message, sharedRoleName), e);
}
} catch (SQLException e) {
String message = "Error while adding the role relationship of role: %s.";
throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
String.format(message, sharedRoleName), e);
}
}

/**
* Update scim role name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
public class SQLQueries {

public static final String GET_ROLE_UM_ID = "SELECT UM_ID FROM UM_HYBRID_ROLE WHERE " +
"UM_ROLE_NAME=:UM_ROLE_NAME; AND UM_AUDIENCE_REF_ID=:UM_AUDIENCE_REF_ID; AND UM_TENANT_ID=:UM_TENANT_ID;";

public static final String ADD_ROLE_WITH_AUDIENCE_SQL = "INSERT INTO UM_HYBRID_ROLE (UM_ROLE_NAME, " +
"UM_AUDIENCE_REF_ID, UM_UUID, UM_TENANT_ID) VALUES (:UM_ROLE_NAME;, :UM_AUDIENCE_REF_ID;, :UM_UUID;, " +
":UM_TENANT_ID;)";
Expand Down Expand Up @@ -273,4 +276,7 @@ public class SQLQueries {
public static final String GET_GROUP_ID_BY_NAME_SQL = "SELECT ATTR_VALUE FROM IDN_SCIM_GROUP WHERE "
+ "TENANT_ID=:TENANT_ID; AND ROLE_NAME=:ROLE_NAME; AND ATTR_NAME=:ATTR_NAME;";

public static final String INSERT_MAIN_TO_SHARED_ROLE_RELATIONSHIP = "INSERT INTO UM_SHARED_ROLE " +
"(UM_SHARED_ROLE_ID, UM_MAIN_ROLE_ID, UM_SHARED_ROLE_TENANT_ID, UM_MAIN_ROLE_TENANT_ID) " +
"VALUES (:UM_SHARED_ROLE_ID;, :UM_MAIN_ROLE_ID;, :UM_SHARED_ROLE_TENANT_ID;, :UM_MAIN_ROLE_TENANT_ID;)";
}

0 comments on commit fc23447

Please sign in to comment.