use of org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo in project carbon-identity-framework by wso2.
the class RoleDAOImpl method addRole.
@Override
public RoleBasicInfo addRole(String roleName, List<String> userList, List<String> groupList, List<String> permissions, String tenantDomain) throws IdentityRoleManagementException {
int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
if (log.isDebugEnabled()) {
log.debug("Creating the role: " + roleName + " in the tenantDomain: " + tenantDomain);
}
String primaryDomainName = IdentityUtil.getPrimaryDomainName();
if (primaryDomainName != null) {
primaryDomainName = primaryDomainName.toUpperCase(Locale.ENGLISH);
}
// Remove internal domain before persisting in order to maintain the backward compatibility.
roleName = removeInternalDomain(roleName);
String roleID;
if (!isExistingRoleName(roleName, tenantDomain)) {
try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(true)) {
try {
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, ADD_ROLE_SQL, RoleTableColumns.UM_ID)) {
statement.setString(RoleTableColumns.UM_ROLE_NAME, roleName);
statement.setInt(RoleTableColumns.UM_TENANT_ID, tenantId);
statement.executeUpdate();
}
String databaseProductName = connection.getMetaData().getDatabaseProductName();
// Add users to the created role.
if (CollectionUtils.isNotEmpty(userList)) {
List<String> userNamesList = getUserNamesByIDs(userList, tenantDomain);
String addUsersSQL = ADD_USER_TO_ROLE_SQL;
if (MICROSOFT.equals(databaseProductName)) {
addUsersSQL = ADD_USER_TO_ROLE_SQL_MSSQL;
}
processBatchUpdateForUsers(roleName, userNamesList, tenantId, primaryDomainName, connection, addUsersSQL);
for (String username : userNamesList) {
clearUserRolesCache(username, tenantId);
}
}
// Add groups to the created role.
if (CollectionUtils.isNotEmpty(groupList)) {
Map<String, String> groupIdsToNames = getGroupNamesByIDs(groupList, tenantDomain);
List<String> groupNamesList = new ArrayList<>(groupIdsToNames.values());
String addGroupsSQL = ADD_GROUP_TO_ROLE_SQL;
if (MICROSOFT.equals(databaseProductName)) {
addGroupsSQL = ADD_GROUP_TO_ROLE_SQL_MSSQL;
}
processBatchUpdateForGroups(roleName, groupNamesList, tenantId, primaryDomainName, connection, addGroupsSQL);
}
// Add role ID.
roleID = addRoleID(roleName, tenantDomain);
// Add role permissions.
if (CollectionUtils.isNotEmpty(permissions)) {
setPermissions(roleID, permissions, tenantDomain, roleName);
}
IdentityDatabaseUtil.commitUserDBTransaction(connection);
} catch (SQLException | IdentityRoleManagementException e) {
IdentityDatabaseUtil.rollbackTransaction(connection);
String errorMessage = "Error while creating the role: %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 creating the role: %s in the tenantDomain: %s";
throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, tenantDomain), e);
}
} else {
throw new IdentityRoleManagementClientException(ROLE_ALREADY_EXISTS.getCode(), "Role already exist for the role name: " + roleName);
}
return new RoleBasicInfo(roleID, roleName);
}
use of org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo in project carbon-identity-framework by wso2.
the class RoleDAOTest method testUpdateRoleName.
@Test
public void testUpdateRoleName() throws Exception {
try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
Connection connection2 = DAOUtils.getConnection(DB_NAME);
Connection connection3 = DAOUtils.getConnection(DB_NAME);
Connection connection4 = DAOUtils.getConnection(DB_NAME);
Connection connection5 = DAOUtils.getConnection(DB_NAME);
Connection connection6 = DAOUtils.getConnection(DB_NAME)) {
roleDAO = spy(RoleMgtDAOFactory.getInstance().getRoleDAO());
mockCacheClearing();
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection1);
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection2);
RoleBasicInfo role = addRole("role1");
mockRealmConfiguration();
AuthorizationManager authorizationManager = mock(JDBCAuthorizationManager.class);
when(mockUserRealm.getAuthorizationManager()).thenReturn(authorizationManager);
doNothing().when(authorizationManager).resetPermissionOnUpdateRole(anyString(), anyString());
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection3);
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection4);
doReturn(true).when(roleDAO, "isExistingRoleID", eq(role.getId()), anyString());
doReturn(false).when(roleDAO, "isExistingRoleName", eq("newRole"), anyString());
roleDAO.updateRoleName(role.getId(), "newRole", SAMPLE_TENANT_DOMAIN);
doCallRealMethod().when(roleDAO, "isExistingRoleName", anyString(), anyString());
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection5);
assertFalse(roleDAO.isExistingRoleName("role1", SAMPLE_TENANT_DOMAIN));
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection6);
assertTrue(roleDAO.isExistingRoleName("newRole", SAMPLE_TENANT_DOMAIN));
}
}
use of org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo in project carbon-identity-framework by wso2.
the class RoleDAOTest method testDeleteUser.
@Test
public void testDeleteUser() throws Exception {
try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
Connection connection2 = DAOUtils.getConnection(DB_NAME);
Connection connection3 = DAOUtils.getConnection(DB_NAME);
Connection connection4 = DAOUtils.getConnection(DB_NAME);
Connection connection5 = DAOUtils.getConnection(DB_NAME);
Connection connection6 = DAOUtils.getConnection(DB_NAME);
Connection connection7 = DAOUtils.getConnection(DB_NAME)) {
roleDAO = spy(RoleMgtDAOFactory.getInstance().getRoleDAO());
mockCacheClearing();
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection1);
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection2);
RoleBasicInfo role = addRole("role1");
doReturn(true).when(roleDAO, "isExistingRoleName", anyString(), anyString());
doCallRealMethod().when(roleDAO, "updateUserListOfRole", anyString(), anyCollection(), anyCollection(), anyString());
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection3);
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection4);
mockStatic(IdentityUtil.class);
when(IdentityUtil.getPrimaryDomainName()).thenReturn("PRIMARY");
doReturn(userNamesList).when(roleDAO, "getUserNamesByIDs", eq(userIDsList), anyString());
roleDAO.updateUserListOfRole(role.getId(), userIDsList, null, SAMPLE_TENANT_DOMAIN);
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection5);
doReturn("user1").when(roleDAO, "getUserNameByID", anyString(), anyString());
roleDAO.deleteUser("userID1", SAMPLE_TENANT_DOMAIN);
userNamesList.remove("user1");
mockRealmConfiguration();
mockStatic(UserCoreUtil.class);
when(UserCoreUtil.isEveryoneRole(anyString(), any(RealmConfiguration.class))).thenReturn(false);
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection6);
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection7);
doCallRealMethod().when(UserCoreUtil.class, "addDomainToName", anyString(), anyString());
doReturn("userID1").when(roleDAO, "getUserIDByName", eq(userNamesList.get(0)), anyString());
List<UserBasicInfo> users = roleDAO.getUserListOfRole(role.getId(), SAMPLE_TENANT_DOMAIN);
assertEquals(getUserNamesList(users), userNamesList);
}
}
use of org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo in project carbon-identity-framework by wso2.
the class RoleDAOTest method testGetRoles2.
@Test(dataProvider = "filterData")
public void testGetRoles2(String expectedResult, String filter) throws Exception {
try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
Connection connection2 = DAOUtils.getConnection(DB_NAME);
Connection connection3 = DAOUtils.getConnection(DB_NAME);
Connection connection4 = DAOUtils.getConnection(DB_NAME);
Connection connection5 = DAOUtils.getConnection(DB_NAME);
Connection connection6 = DAOUtils.getConnection(DB_NAME);
Connection connection7 = DAOUtils.getConnection(DB_NAME);
Connection connection8 = DAOUtils.getConnection(DB_NAME)) {
roleDAO = spy(RoleMgtDAOFactory.getInstance().getRoleDAO());
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection1);
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection2);
addRole("login");
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection3);
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection4);
addRole("viewRole");
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection5);
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection6);
addRole("editRole");
List<String> expectedRoles = new ArrayList<>();
expectedRoles.add(expectedResult);
mockRealmConfiguration();
mockStatic(UserCoreUtil.class);
when(UserCoreUtil.isEveryoneRole(anyString(), any(RealmConfiguration.class))).thenReturn(false);
mockStatic(IdentityUtil.class);
when(IdentityUtil.getDefaultItemsPerPage()).thenReturn(IdentityCoreConstants.DEFAULT_ITEMS_PRE_PAGE);
when(IdentityUtil.getMaximumItemPerPage()).thenReturn(IdentityCoreConstants.DEFAULT_MAXIMUM_ITEMS_PRE_PAGE);
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection7);
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection8);
doCallRealMethod().when(IdentityUtil.class, "extractDomainFromName", anyString());
doCallRealMethod().when(UserCoreUtil.class, "removeDomainFromName", anyString());
List<RoleBasicInfo> roles = roleDAO.getRoles(filter, 3, 0, null, null, SAMPLE_TENANT_DOMAIN);
assertEquals(getRoleNamesList(roles), expectedRoles);
}
}
use of org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo in project carbon-identity-framework by wso2.
the class RoleDAOTest method testGetRoleNameByID.
@Test
public void testGetRoleNameByID() throws Exception {
try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
Connection connection2 = DAOUtils.getConnection(DB_NAME);
Connection connection3 = DAOUtils.getConnection(DB_NAME)) {
roleDAO = spy(RoleMgtDAOFactory.getInstance().getRoleDAO());
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection1);
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection2);
RoleBasicInfo role = addRole("role1");
doCallRealMethod().when(roleDAO, "getRoleIDByName", anyString(), anyString());
when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection3);
when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection3);
assertEquals(roleDAO.getRoleNameByID(role.getId(), SAMPLE_TENANT_DOMAIN), "role1");
}
}
Aggregations