From c2d1d55074769249707d9381c4c3e87b4da11703 Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Mon, 21 Aug 2023 21:32:09 +0530 Subject: [PATCH 01/21] add changes --- .../pom.xml | 110 ++++++ .../role/mgt/ApplicationRoleManager.java | 113 ++++++ .../role/mgt/ApplicationRoleManagerImpl.java | 137 +++++++ .../role/mgt/cache/ApplicationRoleCache.java | 42 +++ .../mgt/cache/ApplicationRoleCacheEntry.java | 46 +++ .../mgt/cache/ApplicationRoleCacheKey.java | 69 ++++ .../ApplicationRoleMgtConstants.java | 85 +++++ .../role/mgt/constants/SQLConstants.java | 89 +++++ .../role/mgt/dao/ApplicationRoleMgtDAO.java | 60 ++++ .../dao/impl/ApplicationRoleMgtDAOImpl.java | 340 ++++++++++++++++++ .../CacheBackedApplicationRoleMgtDAOImpl.java | 164 +++++++++ ...licationRoleManagementClientException.java | 36 ++ .../ApplicationRoleManagementException.java | 79 ++++ ...licationRoleManagementServerException.java | 51 +++ .../ApplicationRoleMgtServiceComponent.java | 66 ++++ ...licationRoleMgtServiceComponentHolder.java | 37 ++ .../role/mgt/model/ApplicationRole.java | 127 +++++++ .../application/role/mgt/model/User.java | 41 +++ .../mgt/util/ApplicationRoleMgtUtils.java | 77 ++++ .../application/role/mgt/util/IDResolver.java | 12 + .../role/mgt/util/UserIDResolver.java | 77 ++++ components/application-role-mgt/pom.xml | 38 ++ .../pom.xml | 70 ++++ .../pom.xml | 121 +++++++ features/application-role-mgt/pom.xml | 40 +++ pom.xml | 13 + 26 files changed, 2140 insertions(+) create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCache.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCacheEntry.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCacheKey.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementClientException.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementException.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementServerException.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponent.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponentHolder.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/User.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java create mode 100644 components/application-role-mgt/pom.xml create mode 100644 features/application-role-mgt/org.wso2.carbon.identity.application.role.mgt.feature/pom.xml create mode 100644 features/application-role-mgt/org.wso2.carbon.identity.application.role.mgt.server.feature/pom.xml create mode 100644 features/application-role-mgt/pom.xml diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml new file mode 100644 index 000000000000..745e74022442 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml @@ -0,0 +1,110 @@ + + + + + + org.wso2.carbon.identity.framework + application-role-mgt + 5.25.287-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.identity.application.role.mgt + Application Role Management Service + bundle + + + + org.apache.felix + org.apache.felix.scr.ds-annotations + provided + + + org.wso2.eclipse.osgi + org.eclipse.osgi.services + + + org.eclipse.osgi + org.eclipse.osgi + + + commons-logging + commons-logging + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.core + + + + org.testng + testng + test + + + org.jacoco + org.jacoco.agent + runtime + test + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.artifactId} + ${project.artifactId} + Application Role Management Service Bundle + org.wso2.carbon.identity.application.role.mgt.internal + + + !org.wso2.carbon.identity.application.role.mgt.internal, + org.wso2.carbon.identity.application.role.mgt.*; + version="${project.version}", + + + javax.sql, + org.osgi.framework; version="${osgi.framework.imp.pkg.version.range}", + org.osgi.service.component; version="${osgi.service.component.imp.pkg.version.range}", + org.apache.commons.lang; version="${commons-lang.wso2.osgi.version.range}", + org.apache.commons.logging; version="${import.package.version.commons.logging}", + org.wso2.carbon.context; version="${carbon.kernel.package.import.version.range}", + org.wso2.carbon.identity.core.*; version="${carbon.identity.package.import.version.range}", + org.wso2.carbon.database.utils.jdbc; + version="${org.wso2.carbon.database.utils.version.range}", + org.wso2.carbon.user.api; version="${carbon.user.api.imp.pkg.version.range}", + org.wso2.carbon.user.core.*;version="${carbon.kernel.package.import.version.range}", + org.wso2.carbon.database.utils.jdbc.exceptions;version="${org.wso2.carbon.database.utils.version.range}", + org.wso2.carbon.utils; version="${carbon.kernel.package.import.version.range}", + + + + + + + + diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java new file mode 100644 index 000000000000..1c64a99c00f0 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt; + +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; +import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; + +import java.util.List; + +/** + * Application Role Manager. + */ +public interface ApplicationRoleManager { + + /** + * Add application role. + * + * @param applicationRole Application role. + * @throws ApplicationRoleManagementException Error occurred while adding application role. + */ + void addApplicationRole(ApplicationRole applicationRole) throws ApplicationRoleManagementException; + + /** + * Update application role. + * + * @param applicationRole Application role. + * @throws ApplicationRoleManagementException Error occurred while updating the application role. + */ + void updateApplicationRole(ApplicationRole applicationRole) throws ApplicationRoleManagementException; + + /** + * Get the application role by role id. + * + * @param roleId Role id. + * @return Application role. + * @throws ApplicationRoleManagementException Error occurred while retrieving the application role. + */ + ApplicationRole getApplicationRoleById(String roleId) throws ApplicationRoleManagementException; + + /** + * Get all the application roles by application id. + * + * @param applicationId Application id. + * @return Application roles. + * @throws ApplicationRoleManagementException Error occurred while retrieving the application roles of a given app. + */ + List getApplicationRoles(String applicationId) throws ApplicationRoleManagementException; + + /** + * Delete application role. + * + * @param roleId Role id. + * @throws ApplicationRoleManagementException Error occurred while deleting the application role. + */ + void deleteApplicationRole(String roleId) throws ApplicationRoleManagementException; + + + /** + * Update the list of assigned users for an application role. + * + * @param roleId Application role ID. + * @param addedUsers List of user IDs to be assigned. + * @param removedUsers List of user IDs to be unassigned. + * @throws ApplicationRoleManagementException Error occurred while updating the application role. + */ + void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers) + throws ApplicationRoleManagementException; + + /** + * Get the list of assigned users of an application role. + * + * @param roleId Application role ID. + * @throws ApplicationRoleManagementException Error occurred while updating the application role. + */ + ApplicationRole getApplicationRoleAssignedUsers(String roleId) throws ApplicationRoleManagementException; + + /** + * Update the list of assigned groups for an application role. + * + * @param roleId Application role ID. + * @param addedGroups List of group IDs to be assigned. + * @param removedGroups List of group IDs to be unassigned. + * @throws ApplicationRoleManagementException Error occurred while updating the application role. + */ + void updateApplicationRoleAssignedGroups(String roleId, List addedGroups, List removedGroups) + throws ApplicationRoleManagementException; + + /** + * Get the list of assigned groups of an application role. + * + * @param roleId Application role ID. + * @throws ApplicationRoleManagementException Error occurred while updating the application role. + */ + ApplicationRole getApplicationRoleAssignedGroups(String roleId) throws ApplicationRoleManagementException; + + +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java new file mode 100644 index 000000000000..3d146733ab1b --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt; + +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.identity.application.role.mgt.dao.ApplicationRoleMgtDAO; +import org.wso2.carbon.identity.application.role.mgt.dao.impl.ApplicationRoleMgtDAOImpl; +import org.wso2.carbon.identity.application.role.mgt.dao.impl.CacheBackedApplicationRoleMgtDAOImpl; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; +import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; + +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_DUPLICATE_ROLE; +import static org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils.handleClientException; + +/** + * Application role management service implementation. + */ +public class ApplicationRoleManagerImpl implements ApplicationRoleManager { + + private static final ApplicationRoleManager instance = new ApplicationRoleManagerImpl(); + + private ApplicationRoleManagerImpl() { + + } + + public static ApplicationRoleManager getInstance() { + + return instance; + } + + private final ApplicationRoleMgtDAO applicationRoleMgtDAO = + new CacheBackedApplicationRoleMgtDAOImpl(new ApplicationRoleMgtDAOImpl()); + + @Override + public void addApplicationRole(ApplicationRole applicationRole) throws ApplicationRoleManagementException { + + String tenantDomain = getTenantDomain(); + boolean existingRole = + applicationRoleMgtDAO.isExistingRole(applicationRole.getApplicationId(), applicationRole.getRoleName(), + tenantDomain); + if (existingRole) { + throw handleClientException(ERROR_CODE_DUPLICATE_ROLE, applicationRole.getRoleName(), + applicationRole.getApplicationId()); + } + applicationRoleMgtDAO.addApplicationRole(applicationRole, tenantDomain); + } + + @Override + public void updateApplicationRole(ApplicationRole applicationRole) throws ApplicationRoleManagementException { + + // TODO : + } + + @Override + public ApplicationRole getApplicationRoleById(String roleId) throws ApplicationRoleManagementException { + + return applicationRoleMgtDAO.getApplicationRoleById(roleId, getTenantDomain()); + } + + @Override + public List getApplicationRoles(String applicationId) throws ApplicationRoleManagementException { + + return applicationRoleMgtDAO.getApplicationRoles(applicationId); + } + + @Override + public void deleteApplicationRole(String roleId) throws ApplicationRoleManagementException { + + applicationRoleMgtDAO.deleteApplicationRole(roleId, getTenantDomain()); + } + + @Override + public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers) + throws ApplicationRoleManagementException { + + removeCommonValues(addedUsers, removedUsers); + applicationRoleMgtDAO.updateApplicationRoleAssignedUsers(roleId, addedUsers, removedUsers, getTenantDomain()); + } + + @Override + public ApplicationRole getApplicationRoleAssignedUsers(String roleId) + throws ApplicationRoleManagementException { + + return applicationRoleMgtDAO.getApplicationRoleAssignedUsers(roleId, getTenantDomain()); + } + + @Override + public void updateApplicationRoleAssignedGroups(String roleId, List addedGroups, List removedGroups) + throws ApplicationRoleManagementException { + + } + + @Override + public ApplicationRole getApplicationRoleAssignedGroups(String roleId) + throws ApplicationRoleManagementException { + + return null; + } + + private static String getTenantDomain() { + + return PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + } + + private void removeCommonValues(List list1, List list2) { + HashSet set = new HashSet<>(list1); + + Iterator iterator = list2.iterator(); + while (iterator.hasNext()) { + String value = iterator.next(); + if (set.contains(value)) { + iterator.remove(); + list1.remove(value); + } + } + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCache.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCache.java new file mode 100644 index 000000000000..8ad3aa33551e --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCache.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.cache; + +import org.wso2.carbon.identity.core.cache.BaseCache; +import org.wso2.carbon.utils.CarbonUtils; + +/** + * Cache implementation for application role cache. + */ +public class ApplicationRoleCache extends BaseCache { + + private static final String CACHE_NAME = "ApplicationRoleCacheById"; + private static final ApplicationRoleCache instance = new ApplicationRoleCache(); + + private ApplicationRoleCache() { + + super(CACHE_NAME); + } + + public static ApplicationRoleCache getInstance() { + + CarbonUtils.checkSecurity(); + return instance; + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCacheEntry.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCacheEntry.java new file mode 100644 index 000000000000..b32cff547484 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCacheEntry.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.cache; + +import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; +import org.wso2.carbon.identity.core.cache.CacheEntry; + +/** + * Cache entry which is kept in the application role cache. + */ +public class ApplicationRoleCacheEntry extends CacheEntry { + + private static final long serialVersionUID = 3112605038259278777L; + private ApplicationRole applicationRole; + + public ApplicationRoleCacheEntry(ApplicationRole applicationRole) { + + this.applicationRole = applicationRole; + } + + public ApplicationRole getApplicationRole() { + + return applicationRole; + } + + public void setApplicationRole(ApplicationRole applicationRole) { + + this.applicationRole = applicationRole; + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCacheKey.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCacheKey.java new file mode 100644 index 000000000000..d2a87897a236 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/cache/ApplicationRoleCacheKey.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.cache; + +import org.wso2.carbon.identity.core.cache.CacheKey; + +/** + * Cache key to lookup application role from cache. + */ +public class ApplicationRoleCacheKey extends CacheKey { + + private static final long serialVersionUID = 8263255365985309443L; + private String applicationRoleId; + + public ApplicationRoleCacheKey(String applicationRoleId) { + + this.applicationRoleId = applicationRoleId; + } + + public String getApplicationRoleId() { + + return applicationRoleId; + } + + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + if (!super.equals(o)) { + return false; + } + + ApplicationRoleCacheKey that = (ApplicationRoleCacheKey) o; + + if (!applicationRoleId.equals(that.applicationRoleId)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + + int result = super.hashCode(); + result = 31 * result + applicationRoleId.hashCode(); + return result; + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java new file mode 100644 index 000000000000..83f782136970 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.constants; + +/** + * Application role management constants. + */ +public class ApplicationRoleMgtConstants { + + private static final String APP_ROLE_MGT_ERROR_CODE_PREFIX = "APM-"; + + /** + * Application role management error message constants. + */ + public enum ErrorMessages { + + // Server Errors. + ERROR_CODE_INSERT_ROLE("65001", "Error occurred while adding the role.", + "Error occurred while adding the role: %s to application: %s."), + ERROR_CODE_GET_ROLE_BY_ID("65002", "Error occurred while retrieving the role.", + "Error occurred while retrieving the role: %s."), + ERROR_CODE_CHECKING_ROLE_EXISTENCE("65003", "Error occurred while checking the role existence.", + "Error occurred while checking whether the role: %s exists in application: %s."), + ERROR_CODE_GET_ROLES_BY_APPLICATION("65004", "Error occurred while retrieving the roles of the application", + "Error occurred while retrieving the roles of application: %s."), + ERROR_CODE_UPDATE_ROLE("65005", "Error occurred while updating the role.", + "Error occurred while updating the role: %s of application: %s."), + ERROR_CODE_DELETE_ROLE("65006", "Error occurred while deleting the role.", + "Error occurred while deleting the role: %s."), + ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS("65007", "Error occurred while assigning users to the role.", + "Error occurred while assigning users to the role: %s."), + ERROR_CODE_GET_ROLE_ASSIGNED_USERS("65008", "Error occurred while retrieving users of the role.", + "Error occurred while retrieving users of the role: %s."), + ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS("65007", "Error occurred while assigning groups to the role.", + "Error occurred while assigning groups to the role: %s."), + ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS("65008", "Error occurred while retrieving groups of the role.", + "Error occurred while retrieving groups of the role: %s."), + + // Client Errors. + ERROR_CODE_DUPLICATE_ROLE("60001", "Role already exists.", + "Role with name: %s already exists in application: %s."); + + private final String code; + private final String message; + private final String description; + + ErrorMessages(String code, String message, String description) { + + this.code = code; + this.message = message; + this.description = description; + } + + public String getCode() { + + return APP_ROLE_MGT_ERROR_CODE_PREFIX + code; + } + + public String getMessage() { + + return message; + } + + public String getDescription() { + + return description; + } + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java new file mode 100644 index 000000000000..51c34557f477 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.constants; + +/** + * Database queries related to application role management CRUD operations. + */ +public class SQLConstants { + + public static final String ADD_APPLICATION_ROLE = "INSERT INTO APP_ROLE (ROLE_ID, ROLE_NAME, TENANT_ID, APP_ID) " + + "VALUES (:" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";, :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_NAME + ";, :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";, :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_APP_ID + ";);"; + + public static final String GET_APPLICATION_ROLE_BY_ID = "SELECT ROLE_ID, ROLE_NAME, TENANT_ID, APP_ID " + + "FROM APP_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";"; + + public static final String GET_APPLICATION_ROLES_OF_APPLICATION = "SELECT ROLE_ID, ROLE_NAME, TENANT_ID, APP_ID " + + "FROM APP_ROLE WHERE APP_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_APP_ID + ";"; + + public static final String IS_APPLICATION_ROLE_EXISTS = "SELECT COUNT(1) FROM APP_ROLE WHERE ROLE_NAME = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_NAME + "; AND APP_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_APP_ID + "; AND TENANT_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + "; "; + + public static final String UPDATE_APPLICATION_ROLE_BY_ID = "UPDATE APP_ROLE SET ROLE_NAME = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_NAME + "; WHERE ROLE_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";"; + public static final String DELETE_APPLICATION_ROLE_BY_ID = "DELETE FROM APP_ROLE WHERE ROLE_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";"; + + public static final String ADD_APPLICATION_ROLE_USER = "INSERT INTO USER_ROLE (ROLE_ID, USER_ID, TENANT_ID) " + + "VALUES (:" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";, :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID + ";, :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";);"; + + public static final String DELETE_ASSIGNED_USER_APPLICATION_ROLE = "DELETE FROM USER_ROLE WHERE ROLE_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + " USER_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID + ";"; + + public static final String GET_ASSIGNED_USERS_OF_APPLICATION_ROLE = "SELECT USER_ID " + + "FROM USER_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";"; + + public static final String ADD_APPLICATION_ROLE_GROUP = "INSERT INTO GROUP_ROLE (ROLE_ID, USER_ID, TENANT_ID) " + + "VALUES (:" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";, :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID + ";, :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";);"; + + public static final String DELETE_ASSIGNED_GROUP_APPLICATION_ROLE = "DELETE FROM GROUP_ROLE WHERE ROLE_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + " GROUP_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID + ";"; + + public static final String GET_ASSIGNED_GROUPS_OF_APPLICATION_ROLE = "SELECT GROUP_ID " + + "FROM GROUP_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + " TENANT_ID = : " + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";"; + + /** + * SQL Placeholders. + */ + public static final class SQLPlaceholders { + + public static final String DB_SCHEMA_COLUMN_NAME_ROLE_ID = "ROLE_ID"; + public static final String DB_SCHEMA_COLUMN_NAME_ROLE_NAME = "ROLE_NAME"; + public static final String DB_SCHEMA_COLUMN_NAME_TENANT_ID = "TENANT_ID"; + public static final String DB_SCHEMA_COLUMN_NAME_APP_ID = "APP_ID"; + public static final String DB_SCHEMA_COLUMN_NAME_USER_ID = "USER_ID"; + public static final String DB_SCHEMA_COLUMN_NAME_GROUP_ID = "GROUP_ID"; + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java new file mode 100644 index 000000000000..e6c93550fe7f --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.dao; + +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; +import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; + +import java.util.List; + +/** + * Application role DAO interface. + */ +public interface ApplicationRoleMgtDAO { + + ApplicationRole addApplicationRole(ApplicationRole applicationRole, String tenantDomain) + throws ApplicationRoleManagementServerException; + + ApplicationRole getApplicationRoleById(String roleId, String tenantDomain) + throws ApplicationRoleManagementServerException; + + List getApplicationRoles(String applicationId) throws ApplicationRoleManagementServerException; + + void updateApplicationRole(String applicationId, String roleId, String tenantDomain) + throws ApplicationRoleManagementServerException; + + void deleteApplicationRole(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException; + + boolean isExistingRole(String applicationId, String roleName, String tenantDomain) + throws ApplicationRoleManagementServerException; + + void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, + String tenantDomain) throws + ApplicationRoleManagementServerException; + + ApplicationRole getApplicationRoleAssignedUsers(String roleId, String tenantDomain) + throws ApplicationRoleManagementException; + + void updateApplicationRoleAssignedGroups(String roleId, List addedGroups, List removedGroups, + String tenantDomain) throws ApplicationRoleManagementException; + + ApplicationRole getApplicationRoleAssignedGroups(String roleId, String tenantDomain) + throws ApplicationRoleManagementException; +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java new file mode 100644 index 000000000000..12fe094bc9c0 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -0,0 +1,340 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; +import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; +import org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException; +import org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants; +import org.wso2.carbon.identity.application.role.mgt.dao.ApplicationRoleMgtDAO; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; +import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; +import org.wso2.carbon.identity.application.role.mgt.model.User; +import org.wso2.carbon.identity.application.role.mgt.util.UserIDResolver; +import org.wso2.carbon.identity.core.util.IdentityTenantUtil; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import java.util.List; + +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_CHECKING_ROLE_EXISTENCE; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_DELETE_ROLE; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLES_BY_APPLICATION; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_ASSIGNED_USERS; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_BY_ID; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_INSERT_ROLE; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_ROLE; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS; +import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_APP_ID; +import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID; +import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID; +import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_NAME; +import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID; +import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID; +import static org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils.getNewTemplate; +import static org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils.handleServerException; + +/** + * Application role DAO implementation. + */ +public class ApplicationRoleMgtDAOImpl implements ApplicationRoleMgtDAO { + + private static final Log LOG = LogFactory.getLog(ApplicationRoleMgtDAOImpl.class); + private UserIDResolver userIDResolver = new UserIDResolver(); + + @Override + public ApplicationRole addApplicationRole(ApplicationRole applicationRole, String tenantDomain) + throws ApplicationRoleManagementServerException { + + int tenantID; + if (tenantDomain != null) { + tenantID = IdentityTenantUtil.getTenantId(tenantDomain); + } else { + tenantID = MultitenantConstants.INVALID_TENANT_ID; + } + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + namedJdbcTemplate.withTransaction(template -> { + template.executeInsert(SQLConstants.ADD_APPLICATION_ROLE, namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, applicationRole.getRoleId()); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_APP_ID, applicationRole.getApplicationId()); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME, applicationRole.getRoleName()); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + }, null, false); + return null; + }); + } catch (TransactionException e) { + throw handleServerException(ERROR_CODE_INSERT_ROLE, e, applicationRole.getRoleName(), + applicationRole.getApplicationId()); + } + return applicationRole; + } + + @Override + public ApplicationRole getApplicationRoleById(String roleId, String tenantDomain) + throws ApplicationRoleManagementServerException { + + int tenantID; + if (tenantDomain != null) { + tenantID = IdentityTenantUtil.getTenantId(tenantDomain); + } else { + tenantID = MultitenantConstants.INVALID_TENANT_ID; + } + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + return namedJdbcTemplate.fetchSingleRecord(SQLConstants.GET_APPLICATION_ROLE_BY_ID, + (resultSet, rowNumber) -> + new ApplicationRole(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_ID), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_APP_ID)), + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + }); + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_GET_ROLE_BY_ID, e, roleId); + } + } + + @Override + public List getApplicationRoles(String applicationId) + throws ApplicationRoleManagementServerException { + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + return namedJdbcTemplate.executeQuery(SQLConstants.GET_APPLICATION_ROLES_OF_APPLICATION, + (resultSet, rowNumber) -> + new ApplicationRole(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_ID), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_APP_ID)), + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_APP_ID, applicationId); + }); + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_GET_ROLES_BY_APPLICATION, e, applicationId); + } + } + + @Override + public void updateApplicationRole(String applicationId, String roleId, String tenantDomain) + throws ApplicationRoleManagementServerException { + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + namedJdbcTemplate.withTransaction(template -> { + template.executeUpdate(SQLConstants.UPDATE_APPLICATION_ROLE_BY_ID, namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + }); + return null; + }); + } catch (TransactionException e) { + throw handleServerException(ERROR_CODE_UPDATE_ROLE, e, roleId, applicationId); + } + } + + @Override + public void deleteApplicationRole(String roleId, String tenantDomain) + throws ApplicationRoleManagementServerException { + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + namedJdbcTemplate.executeQuery(SQLConstants.DELETE_APPLICATION_ROLE_BY_ID, (resultSet, rowNumber) -> null, + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + }); + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_DELETE_ROLE, e, roleId); + } + } + + @Override + public boolean isExistingRole(String applicationId, String roleName, String tenantDomain) + throws ApplicationRoleManagementServerException { + + int tenantID; + if (tenantDomain != null) { + tenantID = IdentityTenantUtil.getTenantId(tenantDomain); + } else { + tenantID = MultitenantConstants.INVALID_TENANT_ID; + } + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + return namedJdbcTemplate.fetchSingleRecord(SQLConstants.IS_APPLICATION_ROLE_EXISTS, + (resultSet, rowNumber) -> resultSet.getInt(1) > 0, + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME, roleName); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_APP_ID, applicationId); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + }); + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_CHECKING_ROLE_EXISTENCE, e, roleName, applicationId); + } + } + + @Override + public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, + String tenantDomain) + throws ApplicationRoleManagementServerException { + + int tenantID; + if (tenantDomain != null) { + tenantID = IdentityTenantUtil.getTenantId(tenantDomain); + } else { + tenantID = MultitenantConstants.INVALID_TENANT_ID; + } + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + String sqlStmt = SQLConstants.ADD_APPLICATION_ROLE_USER; + namedJdbcTemplate.withTransaction(template -> { + namedJdbcTemplate.executeBatchInsert(sqlStmt, (preparedStatement -> { + for (String userId : addedUsers) { + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_USER_ID, userId); + preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + preparedStatement.addBatch(); + } + }), roleId); + for (String userId: removedUsers) { + namedJdbcTemplate.executeQuery(SQLConstants.DELETE_ASSIGNED_USER_APPLICATION_ROLE, + (resultSet, rowNumber) -> null, namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_USER_ID, userId); + }); + } + return null; + }); + } catch (TransactionException e) { + throw handleServerException(ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS, e, roleId); + } + } + + @Override + public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String tenantDomain) + throws ApplicationRoleManagementException { + + int tenantID; + if (tenantDomain != null) { + tenantID = IdentityTenantUtil.getTenantId(tenantDomain); + } else { + tenantID = MultitenantConstants.INVALID_TENANT_ID; + } + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + List users; + users = namedJdbcTemplate.executeQuery(SQLConstants.GET_ASSIGNED_USERS_OF_APPLICATION_ROLE, + (resultSet, rowNumber) -> { + User user = new User(resultSet.getString(DB_SCHEMA_COLUMN_NAME_USER_ID)); + return user; + }, + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + }); + for (User user : users) { + user.setUserName(getUserNamesByID(user.getId(), tenantDomain)); + } + ApplicationRole applicationRole = new ApplicationRole(roleId); + applicationRole.setAssignedUsers(users); + return applicationRole; + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_GET_ROLE_ASSIGNED_USERS, e, roleId); + } + } + + @Override + public void updateApplicationRoleAssignedGroups(String roleId, List addedGroups, List removedGroups, + String tenantDomain) throws ApplicationRoleManagementException { + + int tenantID; + if (tenantDomain != null) { + tenantID = IdentityTenantUtil.getTenantId(tenantDomain); + } else { + tenantID = MultitenantConstants.INVALID_TENANT_ID; + } + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + String sqlStmt = SQLConstants.ADD_APPLICATION_ROLE_GROUP; + namedJdbcTemplate.withTransaction(template -> { + namedJdbcTemplate.executeBatchInsert(sqlStmt, (preparedStatement -> { + for (String groupId : addedGroups) { + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_USER_ID, groupId); + preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + preparedStatement.addBatch(); + } + }), roleId); + for (String groupId: removedGroups) { + namedJdbcTemplate.executeQuery(SQLConstants.DELETE_ASSIGNED_GROUP_APPLICATION_ROLE, + (resultSet, rowNumber) -> null, namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); + }); + } + return null; + }); + } catch (TransactionException e) { + throw handleServerException(ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS, e, roleId); + } + } + + @Override + public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String tenantDomain) + throws ApplicationRoleManagementException { + +// int tenantID; +// if (tenantDomain != null) { +// tenantID = IdentityTenantUtil.getTenantId(tenantDomain); +// } else { +// tenantID = MultitenantConstants.INVALID_TENANT_ID; +// } +// NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); +// try { +// namedJdbcTemplate.executeQuery(SQLConstants.GET_ASSIGNED_GROUPS_OF_APPLICATION_ROLE, +// (resultSet, rowNumber) -> { +// ApplicationRole applicationRole = new ApplicationRole(roleId); +// List groupIds = new ArrayList<>(); +// while (resultSet.next()) { +// groupIds.add(resultSet.getString(DB_SCHEMA_COLUMN_NAME_GROUP_ID)); +// } +// applicationRole.setAssignedGroups(groupIds.toArray(new String[0])); +// return applicationRole; +// }, +// namedPreparedStatement -> { +// namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); +// namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); +// }); +// } catch (DataAccessException e) { +// throw handleServerException(ERROR_CODE_GET_ROLE_ASSIGNED_USERS, e, roleId); +// } + return null; + } + + private String getUserNamesByID(String userIDs, String tenantDomain) + throws ApplicationRoleManagementException { + + return userIDResolver.getNameByID(userIDs, tenantDomain); + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java new file mode 100644 index 000000000000..9cc07ba01da5 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.dao.impl; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.identity.application.role.mgt.cache.ApplicationRoleCache; +import org.wso2.carbon.identity.application.role.mgt.cache.ApplicationRoleCacheEntry; +import org.wso2.carbon.identity.application.role.mgt.cache.ApplicationRoleCacheKey; +import org.wso2.carbon.identity.application.role.mgt.dao.ApplicationRoleMgtDAO; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; +import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; + +import java.util.List; + +/** + * Cache backed application role management DAO implementation. + */ +public class CacheBackedApplicationRoleMgtDAOImpl implements ApplicationRoleMgtDAO { + + private static final Log LOG = LogFactory.getLog(CacheBackedApplicationRoleMgtDAOImpl.class); + + private static ApplicationRoleCache applicationRoleCache; + private final ApplicationRoleMgtDAO applicationRoleMgtDAO; + + public CacheBackedApplicationRoleMgtDAOImpl(ApplicationRoleMgtDAOImpl applicationRoleMgtDAO) { + + this.applicationRoleMgtDAO = applicationRoleMgtDAO; + applicationRoleCache = ApplicationRoleCache.getInstance(); + } + + @Override + public ApplicationRole addApplicationRole(ApplicationRole applicationRole, String tenantDomain) + throws ApplicationRoleManagementServerException { + + return applicationRoleMgtDAO.addApplicationRole(applicationRole, tenantDomain); + } + + @Override + public ApplicationRole getApplicationRoleById(String roleId, String tenantDomain) + throws ApplicationRoleManagementServerException { + + ApplicationRole applicationRole = getApplicationRoleFromCache(roleId, tenantDomain); + if (applicationRole == null) { + applicationRole = applicationRoleMgtDAO.getApplicationRoleById(roleId, tenantDomain); + if (applicationRole != null) { + addToCache(applicationRole, tenantDomain); + } + } + return applicationRole; + } + + @Override + public List getApplicationRoles(String applicationId) + throws ApplicationRoleManagementServerException { + + return applicationRoleMgtDAO.getApplicationRoles(applicationId); + } + + @Override + public void updateApplicationRole(String applicationId, String roleId, String tenantDomain) + throws ApplicationRoleManagementServerException { + + clearFromCache(roleId, tenantDomain); + applicationRoleMgtDAO.updateApplicationRole(applicationId, roleId, tenantDomain); + } + + @Override + public void deleteApplicationRole(String roleId, String tenantDomain) + throws ApplicationRoleManagementServerException { + + clearFromCache(roleId, tenantDomain); + applicationRoleMgtDAO.deleteApplicationRole(roleId, tenantDomain); + } + + @Override + public boolean isExistingRole(String applicationId, String roleName, String tenantDomain) + throws ApplicationRoleManagementServerException { + + // TODO: introduce a cache key with app id and role name. + return applicationRoleMgtDAO.isExistingRole(applicationId, roleName, tenantDomain); + } + + @Override + public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, + String tenantDomain) + throws ApplicationRoleManagementServerException { + + applicationRoleMgtDAO.updateApplicationRoleAssignedUsers(roleId, addedUsers, removedUsers, tenantDomain); + } + + @Override + public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String tenantDomain) + throws ApplicationRoleManagementException { + + return applicationRoleMgtDAO.getApplicationRoleAssignedUsers(roleId, tenantDomain); + } + + @Override + public void updateApplicationRoleAssignedGroups(String roleId, List addedGroups, List removedGroups, + String tenantDomain) throws ApplicationRoleManagementException { + + applicationRoleMgtDAO.updateApplicationRoleAssignedUsers(roleId, addedGroups, removedGroups, tenantDomain); + } + + @Override + public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String tenantDomain) + throws ApplicationRoleManagementException { + + return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, tenantDomain); + } + + private ApplicationRole getApplicationRoleFromCache(String applicationRoleId, String tenantDomain) { + + ApplicationRole applicationRole = null; + if (StringUtils.isNotBlank(applicationRoleId)) { + ApplicationRoleCacheKey cacheKey = new ApplicationRoleCacheKey(applicationRoleId); + ApplicationRoleCacheEntry entry = applicationRoleCache.getValueFromCache(cacheKey, tenantDomain); + if (entry != null) { + applicationRole = entry.getApplicationRole(); + } + } + return applicationRole; + } + + private void addToCache(ApplicationRole applicationRole, String tenantDomain) { + + if (LOG.isDebugEnabled()) { + LOG.debug( + String.format("Add application role: %s in application: %s to cache", applicationRole.getRoleName(), + applicationRole.getApplicationId())); + } + ApplicationRoleCacheKey cacheKey = new ApplicationRoleCacheKey(applicationRole.getApplicationId()); + ApplicationRoleCacheEntry cacheEntry = new ApplicationRoleCacheEntry(applicationRole); + applicationRoleCache.addToCache(cacheKey, cacheEntry, tenantDomain); + } + + private void clearFromCache(String applicationRoleId, String tenantDomain) { + + if (LOG.isDebugEnabled()) { + LOG.debug(String.format("Delete application role: %s from cache", applicationRoleId)); + } + ApplicationRoleCacheKey cacheKey = new ApplicationRoleCacheKey(applicationRoleId); + applicationRoleCache.clearCacheEntry(cacheKey, tenantDomain); + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementClientException.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementClientException.java new file mode 100644 index 000000000000..bcfd79d10026 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementClientException.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.exceptions; + +/** + * Application role management client exception. + */ +public class ApplicationRoleManagementClientException extends ApplicationRoleManagementException { + + public ApplicationRoleManagementClientException(String message, String description, String errorCode) { + + super(message, description, errorCode); + } + + public ApplicationRoleManagementClientException(String message, String description, String errorCode, + Throwable cause) { + + super(message, description, errorCode, cause); + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementException.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementException.java new file mode 100644 index 000000000000..4ff1a782843c --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementException.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.exceptions; + +/** + * Application role management exception. + */ +public class ApplicationRoleManagementException extends Exception { + + private String errorCode; + private String description; + + public ApplicationRoleManagementException(String message) { + + super(message); + } + + public ApplicationRoleManagementException(String message, String errorCode) { + + super(message); + this.errorCode = errorCode; + } + + public ApplicationRoleManagementException(String message, String errorCode, Throwable cause) { + + super(message, cause); + this.errorCode = errorCode; + } + + public ApplicationRoleManagementException(String message, String description, String errorCode) { + + super(message); + this.errorCode = errorCode; + this.description = description; + } + + public ApplicationRoleManagementException(String message, String description, String errorCode, Throwable cause) { + + super(message, cause); + this.errorCode = errorCode; + this.description = description; + } + + public String getErrorCode() { + + return errorCode; + } + + public String getDescription() { + + return description; + } + + public void setErrorCode(String errorCode) { + + this.errorCode = errorCode; + } + + public void setDescription(String description) { + + this.description = description; + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementServerException.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementServerException.java new file mode 100644 index 000000000000..33be365948ac --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/exceptions/ApplicationRoleManagementServerException.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.exceptions; + +/** + * Application role management server exception. + */ +public class ApplicationRoleManagementServerException extends ApplicationRoleManagementException { + + public ApplicationRoleManagementServerException(String message) { + + super(message); + } + + public ApplicationRoleManagementServerException(String message, String errorCode) { + + super(message, errorCode); + } + + public ApplicationRoleManagementServerException(String message, String errorCode, Throwable cause) { + + super(message, errorCode, cause); + } + + public ApplicationRoleManagementServerException(String message, String description, String errorCode) { + + super(message, description, errorCode); + } + + public ApplicationRoleManagementServerException(String message, String description, String errorCode, + Throwable cause) { + + super(message, description, errorCode, cause); + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponent.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponent.java new file mode 100644 index 000000000000..c221b30ee758 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponent.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.framework.BundleContext; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.wso2.carbon.identity.application.role.mgt.ApplicationRoleManager; +import org.wso2.carbon.identity.application.role.mgt.ApplicationRoleManagerImpl; + +/** + * OSGi declarative services component which handled activation and deactivation of Application Role Management. + */ +@Component( + name = "identity.application.role.mgt.component", + immediate = true +) +public class ApplicationRoleMgtServiceComponent { + + private static final Log LOG = LogFactory.getLog(ApplicationRoleMgtServiceComponent.class); + + @Activate + protected void activate(ComponentContext ctxt) { + + try { + BundleContext bundleCtx = ctxt.getBundleContext(); + bundleCtx.registerService(ApplicationRoleManager.class, ApplicationRoleManagerImpl.getInstance(), null); + } catch (Throwable e) { + LOG.error("Error while initializing application role management component.", e); + } + } + + @Deactivate + protected void deactivate(ComponentContext ctxt) { + + try { + BundleContext bundleCtx = ctxt.getBundleContext(); + bundleCtx.ungetService(bundleCtx.getServiceReference(ApplicationRoleManager.class)); + if (LOG.isDebugEnabled()) { + LOG.debug("application role management bundle is deactivated"); + } + } catch (Throwable e) { + LOG.error("Error while deactivating application role management component.", e); + } + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponentHolder.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponentHolder.java new file mode 100644 index 000000000000..a78c7cdc4eae --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponentHolder.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Service component holder class for role management service. + */ +public class ApplicationRoleMgtServiceComponentHolder { + + private static final ApplicationRoleMgtServiceComponentHolder instance = + new ApplicationRoleMgtServiceComponentHolder(); + private static final Log LOG = LogFactory.getLog(ApplicationRoleMgtServiceComponentHolder.class); + + public static ApplicationRoleMgtServiceComponentHolder getInstance() { + + return instance; + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java new file mode 100644 index 000000000000..5923432c7a07 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.model; + +import java.util.List; + +/** + * Application role model. + */ +public class ApplicationRole { + + private String roleId; + private String roleName; + private String[] permissions; + private String applicationId; + private List assignedUsers; + private List assignedGroups; + + public ApplicationRole(String roleId, String roleName, String[] permissions, String applicationId) { + + this.roleId = roleId; + this.roleName = roleName; + this.permissions = permissions; + this.applicationId = applicationId; + } + + public ApplicationRole(String roleId, String roleName, String applicationId) { + + this.roleId = roleId; + this.roleName = roleName; + this.applicationId = applicationId; + } + + public ApplicationRole(String roleName, String[] permissions, String applicationId) { + + this.roleName = roleName; + this.permissions = permissions; + this.applicationId = applicationId; + } + + public ApplicationRole(String roleName, String applicationId) { + + this.roleName = roleName; + this.applicationId = applicationId; + } + + public ApplicationRole(String roleId) { + + this.roleId = roleId; + } + + public String getRoleId() { + + return roleId; + } + + public void setRoleId(String roleId) { + + this.roleId = roleId; + } + + public String getRoleName() { + + return roleName; + } + + public void setRoleName(String roleName) { + + this.roleName = roleName; + } + + public String[] getPermissions() { + + return permissions; + } + + public void setPermissions(String[] permissions) { + + this.permissions = permissions; + } + + public String getApplicationId() { + + return applicationId; + } + + public void setApplicationId(String applicationId) { + + this.applicationId = applicationId; + } + + public List getAssignedUsers() { + + return assignedUsers; + } + + public void setAssignedUsers(List assignedUsers) { + + this.assignedUsers = assignedUsers; + } + + public List getAssignedGroups() { + + return assignedGroups; + } + + public void setAssignedGroups(List assignedGroups) { + + this.assignedGroups = assignedGroups; + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/User.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/User.java new file mode 100644 index 000000000000..8a43f73773cc --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/User.java @@ -0,0 +1,41 @@ +package org.wso2.carbon.identity.application.role.mgt.model; + +/** + * Application role assigned user model. + */ +public class User { + + private String id; + private String userName; + + public User(String id, String userName) { + + this.id = id; + this.userName = userName; + } + + public User(String id) { + + this.id = id; + } + + public String getId() { + + return id; + } + + public void setId(String id) { + + this.id = id; + } + + public String getUserName() { + + return userName; + } + + public void setUserName(String userName) { + + this.userName = userName; + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java new file mode 100644 index 000000000000..98bfd2203113 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.role.mgt.util; + +import org.apache.commons.lang.ArrayUtils; +import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; +import org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementClientException; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; +import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; + +/** + * Application role management util. + */ +public class ApplicationRoleMgtUtils { + + /** + * Get a new Jdbc Template. + * + * @return a new Jdbc Template. + */ + public static NamedJdbcTemplate getNewTemplate() { + + return new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource()); + } + + /** + * Handle server exceptions. + * + * @param error Error message. + * @param e Throwable. + * @param data Data to be replaced in the error description. + * @return ApplicationRoleManagementServerException. + */ + public static ApplicationRoleManagementServerException handleServerException( + ApplicationRoleMgtConstants.ErrorMessages error, Throwable e, String... data) { + + String description = error.getDescription(); + if (ArrayUtils.isNotEmpty(data)) { + description = String.format(description, data); + } + return new ApplicationRoleManagementServerException(error.getMessage(), description, error.getCode(), e); + } + + /** + * Handle client exceptions. + * + * @param error Error message. + * @param data Data to be replaced in the error description. + * @return ApplicationRoleManagementClientException. + */ + public static ApplicationRoleManagementClientException handleClientException( + ApplicationRoleMgtConstants.ErrorMessages error, String... data) { + + String description = error.getDescription(); + if (ArrayUtils.isNotEmpty(data)) { + description = String.format(description, data); + } + return new ApplicationRoleManagementClientException(error.getMessage(), description, error.getCode()); + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java new file mode 100644 index 000000000000..893172d74e6f --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java @@ -0,0 +1,12 @@ +package org.wso2.carbon.identity.application.role.mgt.util; + +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; + +/** + * Id Resolver. + */ +public interface IDResolver { + + String getNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException; + +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java new file mode 100644 index 000000000000..6491199a9082 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java @@ -0,0 +1,77 @@ +package org.wso2.carbon.identity.application.role.mgt.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementClientException; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.user.api.UserStoreManager; +import org.wso2.carbon.user.core.common.AbstractUserStoreManager; + +import java.util.ArrayList; +import java.util.List; + +/** + * UserId Resolver. + */ +public class UserIDResolver implements IDResolver { + + private Log log = LogFactory.getLog(UserIDResolver.class); + + @Override + public String getNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException { + + String userName = resolveUserNameFromUserID(id); + if (userName == null) { + String errorMessage = "A user doesn't exist with id: " + id + " in the tenantDomain: " + tenantDomain; + throw new ApplicationRoleManagementClientException(errorMessage, errorMessage, ""); + } + return userName; + } + + public List getNamesByIDs(List idList, String tenantDomain) + throws ApplicationRoleManagementException { + + List usersList = new ArrayList<>(); + for (String id : idList) { + usersList.add(getNameByID(id, tenantDomain)); + } + return usersList; + } + + /** + * Retrieves the username of the given userID. + * + * @param id userID. + * @return username of the user. + * @throws ApplicationRoleManagementException ApplicationRoleManagementException. + */ + public String resolveUserNameFromUserID(String id) throws ApplicationRoleManagementException { + + try { + UserStoreManager userStoreManager = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() + .getUserStoreManager(); + try { + if (userStoreManager instanceof AbstractUserStoreManager) { + return ((AbstractUserStoreManager) userStoreManager).getUserNameFromUserID(id); + } + if (log.isDebugEnabled()) { + log.debug("Provided user store manager for the userID: " + id + ", is not an instance " + + "of the AbstractUserStore manager"); + } + throw new ApplicationRoleManagementClientException("Unable to get the username of the userID", + "Unable to get the username of the userID: " + id + ".", ""); + } catch (UserStoreException e) { + throw new ApplicationRoleManagementServerException("Error occurred while resolving username for " + + "the userID Error occurred while resolving username for the userID: " + id, ""); + } + } catch (UserStoreException e) { + throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " + + "to resolve username for the userID", "Error occurred while retrieving the userstore manager to " + + "resolve username for the userID: " + id, e); + } + } + +} diff --git a/components/application-role-mgt/pom.xml b/components/application-role-mgt/pom.xml new file mode 100644 index 000000000000..fc33e87650f9 --- /dev/null +++ b/components/application-role-mgt/pom.xml @@ -0,0 +1,38 @@ + + + + + + org.wso2.carbon.identity.framework + identity-framework + 5.25.287-SNAPSHOT + ../../pom.xml + + + 4.0.0 + pom + application-role-mgt + + + org.wso2.carbon.identity.application.role.mgt + + + diff --git a/features/application-role-mgt/org.wso2.carbon.identity.application.role.mgt.feature/pom.xml b/features/application-role-mgt/org.wso2.carbon.identity.application.role.mgt.feature/pom.xml new file mode 100644 index 000000000000..444b97acec84 --- /dev/null +++ b/features/application-role-mgt/org.wso2.carbon.identity.application.role.mgt.feature/pom.xml @@ -0,0 +1,70 @@ + + + + + + org.wso2.carbon.identity.framework + application-role-mgt-feature + 5.25.287-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.identity.application.role.mgt.feature + pom + Identity Application Role Management Feature + http://wso2.org + + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.application.role.mgt.server.feature + zip + + + + + + + org.wso2.maven + carbon-p2-plugin + ${carbon.p2.plugin.version} + + + 4-p2-feature-generation + package + + p2-feature-gen + + + org.wso2.carbon.identity.application.role.mgt + ../../etc/feature.properties + + + org.wso2.carbon.identity.framework:org.wso2.carbon.identity.application.role.mgt.server.feature + + + + + + + + + + diff --git a/features/application-role-mgt/org.wso2.carbon.identity.application.role.mgt.server.feature/pom.xml b/features/application-role-mgt/org.wso2.carbon.identity.application.role.mgt.server.feature/pom.xml new file mode 100644 index 000000000000..704bf0b12388 --- /dev/null +++ b/features/application-role-mgt/org.wso2.carbon.identity.application.role.mgt.server.feature/pom.xml @@ -0,0 +1,121 @@ + + + + + + org.wso2.carbon.identity.framework + application-role-mgt-feature + 5.25.287-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.identity.application.role.mgt.server.feature + pom + Identity Application Role Management Server Feature + http://wso2.org + + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.application.role.mgt + + + + + + + org.wso2.maven + carbon-p2-plugin + ${carbon.p2.plugin.version} + + + 4-p2-feature-generation + package + + p2-feature-gen + + + org.wso2.carbon.identity.application.role.mgt.server + ../../etc/feature.properties + + + org.wso2.carbon.p2.category.type:server + + + + + org.wso2.carbon.identity.framework:org.wso2.carbon.identity.application.role.mgt + + + + + + + maven-resources-plugin + + + prefilter-resources + generate-resources + + copy-resources + + + src/main/resources + + + resources + + **/*.xml + **/*.json + **/*.sql + p2.inf + build.properties + + + + + + + + + org.apache.maven.plugins + maven-antrun-plugin + 1.1 + + + clean_target + install + + + + + + + + + run + + + + + + + + diff --git a/features/application-role-mgt/pom.xml b/features/application-role-mgt/pom.xml new file mode 100644 index 000000000000..88b8d6814d35 --- /dev/null +++ b/features/application-role-mgt/pom.xml @@ -0,0 +1,40 @@ + + + + + + org.wso2.carbon.identity.framework + identity-framework + 5.25.287-SNAPSHOT + ../../pom.xml + + + 4.0.0 + application-role-mgt-feature + pom + WSO2 Carbon - Application Role Management Feature Aggregator Module + http://wso2.org + + + org.wso2.carbon.identity.application.role.mgt.server.feature + org.wso2.carbon.identity.application.role.mgt.feature + + + + diff --git a/pom.xml b/pom.xml index 1aa6b38b4e94..73114bf1994b 100644 --- a/pom.xml +++ b/pom.xml @@ -101,6 +101,8 @@ features/central-logger features/input-validation-mgt features/consent-server-configs-mgt + components/application-role-mgt + features/application-role-mgt @@ -814,6 +816,12 @@ ${project.version} zip + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.application.role.mgt.server.feature + ${project.version} + zip + org.wso2.carbon.identity.framework org.wso2.carbon.identity.application.mgt.ui.feature @@ -1235,6 +1243,11 @@ org.wso2.carbon.identity.application.mgt ${project.version} + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.application.role.mgt + ${project.version} + org.wso2.carbon.identity.framework From c208ffc40b43f6d358dbd1323fcd6958c6a9b96b Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Tue, 22 Aug 2023 18:51:05 +0530 Subject: [PATCH 02/21] add changes --- .../pom.xml | 6 + .../role/mgt/ApplicationRoleManager.java | 7 +- .../role/mgt/ApplicationRoleManagerImpl.java | 39 +++++- .../ApplicationRoleMgtConstants.java | 1 + .../role/mgt/constants/SQLConstants.java | 15 +-- .../role/mgt/dao/ApplicationRoleMgtDAO.java | 9 +- .../dao/impl/ApplicationRoleMgtDAOImpl.java | 114 ++++++++++++------ .../CacheBackedApplicationRoleMgtDAOImpl.java | 17 ++- .../ApplicationRoleMgtServiceComponent.java | 44 +++++++ ...licationRoleMgtServiceComponentHolder.java | 34 ++++++ .../role/mgt/model/ApplicationRole.java | 6 +- .../application/role/mgt/model/Group.java | 47 ++++++++ .../role/mgt/util/GroupIDResolver.java | 49 ++++++++ 13 files changed, 327 insertions(+), 61 deletions(-) create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml index 745e74022442..428cd3b93d28 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml @@ -66,6 +66,10 @@ runtime test + + org.wso2.carbon.identity.framework + org.wso2.carbon.idp.mgt + @@ -100,6 +104,8 @@ org.wso2.carbon.user.core.*;version="${carbon.kernel.package.import.version.range}", org.wso2.carbon.database.utils.jdbc.exceptions;version="${org.wso2.carbon.database.utils.version.range}", org.wso2.carbon.utils; version="${carbon.kernel.package.import.version.range}", + org.wso2.carbon.idp.mgt.*; version="${carbon.identity.package.import.version.range}", + org.wso2.carbon.identity.application.common.*; version="${carbon.identity.package.import.version.range}", diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java index 1c64a99c00f0..c0546e2334ab 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java @@ -98,8 +98,8 @@ void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, * @param removedGroups List of group IDs to be unassigned. * @throws ApplicationRoleManagementException Error occurred while updating the application role. */ - void updateApplicationRoleAssignedGroups(String roleId, List addedGroups, List removedGroups) - throws ApplicationRoleManagementException; + void updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, + List removedGroups) throws ApplicationRoleManagementException; /** * Get the list of assigned groups of an application role. @@ -107,7 +107,8 @@ void updateApplicationRoleAssignedGroups(String roleId, List addedGroups * @param roleId Application role ID. * @throws ApplicationRoleManagementException Error occurred while updating the application role. */ - ApplicationRole getApplicationRoleAssignedGroups(String roleId) throws ApplicationRoleManagementException; + ApplicationRole getApplicationRoleAssignedGroups(String roleId, String idpId) + throws ApplicationRoleManagementException; } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java index 3d146733ab1b..0351bb247d48 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java @@ -19,17 +19,21 @@ package org.wso2.carbon.identity.application.role.mgt; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.identity.application.common.model.IdentityProvider; import org.wso2.carbon.identity.application.role.mgt.dao.ApplicationRoleMgtDAO; import org.wso2.carbon.identity.application.role.mgt.dao.impl.ApplicationRoleMgtDAOImpl; import org.wso2.carbon.identity.application.role.mgt.dao.impl.CacheBackedApplicationRoleMgtDAOImpl; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; +import org.wso2.carbon.identity.application.role.mgt.internal.ApplicationRoleMgtServiceComponentHolder; import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; +import org.wso2.carbon.idp.mgt.IdentityProviderManagementException; import java.util.HashSet; import java.util.Iterator; import java.util.List; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_DUPLICATE_ROLE; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.LOCAL_IDP; import static org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils.handleClientException; /** @@ -105,16 +109,45 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId) } @Override - public void updateApplicationRoleAssignedGroups(String roleId, List addedGroups, List removedGroups) + public void updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, + List removedGroups) throws ApplicationRoleManagementException { + try { + IdentityProvider identityProvider; + if (LOCAL_IDP.equals(idpId)) { + + identityProvider = ApplicationRoleMgtServiceComponentHolder.getInstance() + .getIdentityProviderManager().getResidentIdP(getTenantDomain()); + } else { + identityProvider = ApplicationRoleMgtServiceComponentHolder.getInstance() + .getIdentityProviderManager().getIdPByResourceId(idpId, getTenantDomain(), true); + } + removeCommonValues(addedGroups, removedGroups); + applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, identityProvider.getResourceId(), + addedGroups, removedGroups, getTenantDomain()); + } catch (IdentityProviderManagementException e) { + throw new ApplicationRoleManagementException("Error while retrieving idp", + "Error while retrieving idp for idpId: " + idpId, e); + } } @Override - public ApplicationRole getApplicationRoleAssignedGroups(String roleId) + public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String idpId) throws ApplicationRoleManagementException { - return null; + if (LOCAL_IDP.equals(idpId)) { + return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, getTenantDomain()); + } + IdentityProvider identityProvider; + try { + identityProvider = ApplicationRoleMgtServiceComponentHolder.getInstance() + .getIdentityProviderManager().getIdPByResourceId(idpId, getTenantDomain(), true); + } catch (IdentityProviderManagementException e) { + throw new ApplicationRoleManagementException("Error while retrieving idp", "Error while retrieving idp " + + "for idpId: " + idpId, e); + } + return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, identityProvider, getTenantDomain()); } private static String getTenantDomain() { diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java index 83f782136970..735bd065df80 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java @@ -24,6 +24,7 @@ public class ApplicationRoleMgtConstants { private static final String APP_ROLE_MGT_ERROR_CODE_PREFIX = "APM-"; + public static final String LOCAL_IDP = "local"; /** * Application role management error message constants. diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java index 51c34557f477..07166afca263 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java @@ -54,25 +54,25 @@ public class SQLConstants { SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";);"; public static final String DELETE_ASSIGNED_USER_APPLICATION_ROLE = "DELETE FROM USER_ROLE WHERE ROLE_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + " USER_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + " AND USER_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID + ";"; public static final String GET_ASSIGNED_USERS_OF_APPLICATION_ROLE = "SELECT USER_ID " + "FROM USER_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";"; - public static final String ADD_APPLICATION_ROLE_GROUP = "INSERT INTO GROUP_ROLE (ROLE_ID, USER_ID, TENANT_ID) " + - "VALUES (:" + + public static final String ADD_APPLICATION_ROLE_GROUP = "INSERT INTO GROUP_ROLE (ROLE_ID, GROUP_ID, IDP_ID," + + " TENANT_ID) VALUES (:" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";, :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID + ";, :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_IDP_ID + ";, :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";);"; public static final String DELETE_ASSIGNED_GROUP_APPLICATION_ROLE = "DELETE FROM GROUP_ROLE WHERE ROLE_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + " GROUP_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + " AND GROUP_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID + ";"; - public static final String GET_ASSIGNED_GROUPS_OF_APPLICATION_ROLE = "SELECT GROUP_ID " + - "FROM GROUP_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + " TENANT_ID = : " - + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";"; + public static final String GET_ASSIGNED_GROUPS_OF_APPLICATION_ROLE = "SELECT GROUP_ID, IDP_ID " + + "FROM GROUP_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";"; /** * SQL Placeholders. @@ -85,5 +85,6 @@ public static final class SQLPlaceholders { public static final String DB_SCHEMA_COLUMN_NAME_APP_ID = "APP_ID"; public static final String DB_SCHEMA_COLUMN_NAME_USER_ID = "USER_ID"; public static final String DB_SCHEMA_COLUMN_NAME_GROUP_ID = "GROUP_ID"; + public static final String DB_SCHEMA_COLUMN_NAME_IDP_ID = "IDP_ID"; } } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java index e6c93550fe7f..dccd63bb5592 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java @@ -18,6 +18,7 @@ package org.wso2.carbon.identity.application.role.mgt.dao; +import org.wso2.carbon.identity.application.common.model.IdentityProvider; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; @@ -52,9 +53,13 @@ void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, ApplicationRole getApplicationRoleAssignedUsers(String roleId, String tenantDomain) throws ApplicationRoleManagementException; - void updateApplicationRoleAssignedGroups(String roleId, List addedGroups, List removedGroups, - String tenantDomain) throws ApplicationRoleManagementException; + void updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, + List removedGroups, String tenantDomain) + throws ApplicationRoleManagementException; ApplicationRole getApplicationRoleAssignedGroups(String roleId, String tenantDomain) throws ApplicationRoleManagementException; + + ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, + String tenantDomain) throws ApplicationRoleManagementException; } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java index 12fe094bc9c0..156c46db08b6 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -23,21 +23,28 @@ import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; import org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException; +import org.wso2.carbon.identity.application.common.model.IdPGroup; +import org.wso2.carbon.identity.application.common.model.IdentityProvider; import org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants; import org.wso2.carbon.identity.application.role.mgt.dao.ApplicationRoleMgtDAO; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; +import org.wso2.carbon.identity.application.role.mgt.model.Group; import org.wso2.carbon.identity.application.role.mgt.model.User; +import org.wso2.carbon.identity.application.role.mgt.util.GroupIDResolver; import org.wso2.carbon.identity.application.role.mgt.util.UserIDResolver; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_CHECKING_ROLE_EXISTENCE; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_DELETE_ROLE; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLES_BY_APPLICATION; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_ASSIGNED_USERS; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_BY_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_INSERT_ROLE; @@ -46,6 +53,7 @@ import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_APP_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID; +import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_IDP_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_NAME; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID; @@ -60,6 +68,7 @@ public class ApplicationRoleMgtDAOImpl implements ApplicationRoleMgtDAO { private static final Log LOG = LogFactory.getLog(ApplicationRoleMgtDAOImpl.class); private UserIDResolver userIDResolver = new UserIDResolver(); + private GroupIDResolver groupIDResolver = new GroupIDResolver(); @Override public ApplicationRole addApplicationRole(ApplicationRole applicationRole, String tenantDomain) @@ -235,19 +244,12 @@ public void updateApplicationRoleAssignedUsers(String roleId, List added public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String tenantDomain) throws ApplicationRoleManagementException { - int tenantID; - if (tenantDomain != null) { - tenantID = IdentityTenantUtil.getTenantId(tenantDomain); - } else { - tenantID = MultitenantConstants.INVALID_TENANT_ID; - } NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { List users; users = namedJdbcTemplate.executeQuery(SQLConstants.GET_ASSIGNED_USERS_OF_APPLICATION_ROLE, (resultSet, rowNumber) -> { - User user = new User(resultSet.getString(DB_SCHEMA_COLUMN_NAME_USER_ID)); - return user; + return new User(resultSet.getString(DB_SCHEMA_COLUMN_NAME_USER_ID)); }, namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); @@ -264,8 +266,9 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String ten } @Override - public void updateApplicationRoleAssignedGroups(String roleId, List addedGroups, List removedGroups, - String tenantDomain) throws ApplicationRoleManagementException { + public void updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, + List removedGroups, String tenantDomain) + throws ApplicationRoleManagementException { int tenantID; if (tenantDomain != null) { @@ -281,7 +284,8 @@ public void updateApplicationRoleAssignedGroups(String roleId, List adde namedJdbcTemplate.executeBatchInsert(sqlStmt, (preparedStatement -> { for (String groupId : addedGroups) { preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); - preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_USER_ID, groupId); + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_IDP_ID, idpId); preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); preparedStatement.addBatch(); } @@ -301,40 +305,70 @@ public void updateApplicationRoleAssignedGroups(String roleId, List adde } @Override - public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String tenantDomain) + public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String tenantDomain) throws + ApplicationRoleManagementException { + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + List groups; + groups = namedJdbcTemplate.executeQuery(SQLConstants.GET_ASSIGNED_GROUPS_OF_APPLICATION_ROLE, + (resultSet, rowNumber) -> new Group(resultSet.getString(DB_SCHEMA_COLUMN_NAME_GROUP_ID), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_IDP_ID)), + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + }); + for (Group group : groups) { + group.setGroupName(getGroupNamesByID(group.getGroupId(), tenantDomain)); + } + ApplicationRole applicationRole = new ApplicationRole(roleId); + applicationRole.setAssignedGroups(groups); + return applicationRole; + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS, e, roleId); + } + } + + @Override + public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, + String tenantDomain) throws + ApplicationRoleManagementException { + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + List groups; + groups = namedJdbcTemplate.executeQuery(SQLConstants.GET_ASSIGNED_GROUPS_OF_APPLICATION_ROLE, + (resultSet, rowNumber) -> new Group(resultSet.getString(DB_SCHEMA_COLUMN_NAME_GROUP_ID), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_IDP_ID)), + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + }); + IdPGroup[] idpGroups = identityProvider.getIdPGroupConfig(); + Map idToNameMap = new HashMap<>(); + for (IdPGroup idpGroup : idpGroups) { + idToNameMap.put(idpGroup.getIdpGroupId(), idpGroup.getIdpGroupName()); + } + for (Group group : groups) { + if (idToNameMap.containsKey(group.getGroupId())) { + group.setGroupName(idToNameMap.get(group.getGroupId())); + } + } + ApplicationRole applicationRole = new ApplicationRole(roleId); + applicationRole.setAssignedGroups(groups); + return applicationRole; + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS, e, roleId); + } + } + + private String getUserNamesByID(String userID, String tenantDomain) throws ApplicationRoleManagementException { -// int tenantID; -// if (tenantDomain != null) { -// tenantID = IdentityTenantUtil.getTenantId(tenantDomain); -// } else { -// tenantID = MultitenantConstants.INVALID_TENANT_ID; -// } -// NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); -// try { -// namedJdbcTemplate.executeQuery(SQLConstants.GET_ASSIGNED_GROUPS_OF_APPLICATION_ROLE, -// (resultSet, rowNumber) -> { -// ApplicationRole applicationRole = new ApplicationRole(roleId); -// List groupIds = new ArrayList<>(); -// while (resultSet.next()) { -// groupIds.add(resultSet.getString(DB_SCHEMA_COLUMN_NAME_GROUP_ID)); -// } -// applicationRole.setAssignedGroups(groupIds.toArray(new String[0])); -// return applicationRole; -// }, -// namedPreparedStatement -> { -// namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); -// namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); -// }); -// } catch (DataAccessException e) { -// throw handleServerException(ERROR_CODE_GET_ROLE_ASSIGNED_USERS, e, roleId); -// } - return null; + return userIDResolver.getNameByID(userID, tenantDomain); } - private String getUserNamesByID(String userIDs, String tenantDomain) + private String getGroupNamesByID(String groupID, String tenantDomain) throws ApplicationRoleManagementException { - return userIDResolver.getNameByID(userIDs, tenantDomain); + return groupIDResolver.getNameByID(groupID, tenantDomain); } } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java index 9cc07ba01da5..cca39aeb7f7c 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java @@ -21,6 +21,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.identity.application.common.model.IdentityProvider; import org.wso2.carbon.identity.application.role.mgt.cache.ApplicationRoleCache; import org.wso2.carbon.identity.application.role.mgt.cache.ApplicationRoleCacheEntry; import org.wso2.carbon.identity.application.role.mgt.cache.ApplicationRoleCacheKey; @@ -115,10 +116,12 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String ten } @Override - public void updateApplicationRoleAssignedGroups(String roleId, List addedGroups, List removedGroups, - String tenantDomain) throws ApplicationRoleManagementException { + public void updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, + List removedGroups, String tenantDomain) + throws ApplicationRoleManagementException { - applicationRoleMgtDAO.updateApplicationRoleAssignedUsers(roleId, addedGroups, removedGroups, tenantDomain); + applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, idpId, addedGroups, removedGroups, + tenantDomain); } @Override @@ -128,6 +131,14 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String te return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, tenantDomain); } + @Override + public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, + String tenantDomain) + throws ApplicationRoleManagementException { + + return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, identityProvider, tenantDomain); + } + private ApplicationRole getApplicationRoleFromCache(String applicationRoleId, String tenantDomain) { ApplicationRole applicationRole = null; diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponent.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponent.java index c221b30ee758..4cd54523e31d 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponent.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponent.java @@ -25,8 +25,13 @@ import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; +import org.osgi.service.component.annotations.ReferencePolicy; import org.wso2.carbon.identity.application.role.mgt.ApplicationRoleManager; import org.wso2.carbon.identity.application.role.mgt.ApplicationRoleManagerImpl; +import org.wso2.carbon.idp.mgt.IdpManager; +import org.wso2.carbon.user.core.service.RealmService; /** * OSGi declarative services component which handled activation and deactivation of Application Role Management. @@ -63,4 +68,43 @@ protected void deactivate(ComponentContext ctxt) { LOG.error("Error while deactivating application role management component.", e); } } + + @Reference( + name = "realm.service", + service = RealmService.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetRealmService") + protected void setRealmService(RealmService realmService) { + + if (LOG.isDebugEnabled()) { + LOG.debug("Setting the Realm Service"); + } + ApplicationRoleMgtServiceComponentHolder.getInstance().setRealmService(realmService); + } + + protected void unsetRealmService(RealmService realmService) { + + if (LOG.isDebugEnabled()) { + LOG.debug("Unset the Realm Service."); + } + ApplicationRoleMgtServiceComponentHolder.getInstance().setRealmService(null); + } + + @Reference( + name = "idp.mgt.dscomponent", + service = IdpManager.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetIdentityProviderManager" + ) + protected void setIdentityProviderManager(IdpManager idpMgtService) { + + ApplicationRoleMgtServiceComponentHolder.getInstance().setIdentityProviderManager(idpMgtService); + } + + protected void unsetIdentityProviderManager(IdpManager idpMgtService) { + + ApplicationRoleMgtServiceComponentHolder.getInstance().setIdentityProviderManager(null); + } } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponentHolder.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponentHolder.java index a78c7cdc4eae..f92f7f1aec0e 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponentHolder.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/internal/ApplicationRoleMgtServiceComponentHolder.java @@ -20,6 +20,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.idp.mgt.IdpManager; +import org.wso2.carbon.user.core.service.RealmService; /** * Service component holder class for role management service. @@ -28,10 +30,42 @@ public class ApplicationRoleMgtServiceComponentHolder { private static final ApplicationRoleMgtServiceComponentHolder instance = new ApplicationRoleMgtServiceComponentHolder(); + private RealmService realmService; + private IdpManager identityProviderManager; private static final Log LOG = LogFactory.getLog(ApplicationRoleMgtServiceComponentHolder.class); public static ApplicationRoleMgtServiceComponentHolder getInstance() { return instance; } + + public RealmService getRealmService() { + + return realmService; + } + + public void setRealmService(RealmService realmService) { + + this.realmService = realmService; + } + + /** + * Get IdentityProviderManager osgi service. + * + * @return IdentityProviderManager + */ + public IdpManager getIdentityProviderManager() { + + return identityProviderManager; + } + + /** + * Set IdentityProviderManager osgi service. + * + * @param idpManager IdentityProviderManager. + */ + public void setIdentityProviderManager(IdpManager idpManager) { + + this.identityProviderManager = idpManager; + } } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java index 5923432c7a07..650378f522af 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java @@ -30,7 +30,7 @@ public class ApplicationRole { private String[] permissions; private String applicationId; private List assignedUsers; - private List assignedGroups; + private List assignedGroups; public ApplicationRole(String roleId, String roleName, String[] permissions, String applicationId) { @@ -115,12 +115,12 @@ public void setAssignedUsers(List assignedUsers) { this.assignedUsers = assignedUsers; } - public List getAssignedGroups() { + public List getAssignedGroups() { return assignedGroups; } - public void setAssignedGroups(List assignedGroups) { + public void setAssignedGroups(List assignedGroups) { this.assignedGroups = assignedGroups; } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java new file mode 100644 index 000000000000..1774d85d9dc8 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java @@ -0,0 +1,47 @@ +package org.wso2.carbon.identity.application.role.mgt.model; + +/** + * Application role assigned group model. + */ +public class Group { + + private String groupId; + private String groupName; + private String idpId; + + public Group(String groupId, String idpId) { + + this.groupId = groupId; + this.idpId = idpId; + } + + public String getGroupId() { + + return groupId; + } + + public void setGroupId(String groupId) { + + this.groupId = groupId; + } + + public String getGroupName() { + + return groupName; + } + + public void setGroupName(String groupName) { + + this.groupName = groupName; + } + + public String getIdpId() { + + return idpId; + } + + public void setIdpId(String idpId) { + + this.idpId = idpId; + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java new file mode 100644 index 000000000000..fe5cc862cb7b --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java @@ -0,0 +1,49 @@ +package org.wso2.carbon.identity.application.role.mgt.util; + +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementClientException; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; +import org.wso2.carbon.identity.application.role.mgt.internal.ApplicationRoleMgtServiceComponentHolder; +import org.wso2.carbon.user.api.UserRealm; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.user.core.common.AbstractUserStoreManager; +import org.wso2.carbon.user.core.service.RealmService; + +/** + * GroupId Resolver. + */ +public class GroupIDResolver implements IDResolver { + + @Override + public String getNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException { + + String groupName = resolveGroupNameFromGroupID(id); + if (groupName == null) { + String errorMessage = "A group doesn't exist with id: " + id + " in the tenantDomain: " + tenantDomain; + throw new ApplicationRoleManagementClientException(errorMessage, errorMessage, ""); + } + return groupName; + } + + public String resolveGroupNameFromGroupID(String id) throws ApplicationRoleManagementException { + + AbstractUserStoreManager userStoreManager; + try { + userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + return userStoreManager.getGroupNameByGroupId(id); + } catch (UserStoreException e) { + throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID", "Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID: " + id, e); + } + } + + private AbstractUserStoreManager getUserStoreManager(int tenantId) throws UserStoreException { + + RealmService realmService = ApplicationRoleMgtServiceComponentHolder.getInstance().getRealmService(); + UserRealm tenantUserRealm = realmService.getTenantUserRealm(tenantId); + + return (AbstractUserStoreManager) tenantUserRealm.getUserStoreManager(); + } +} From 6c4b1b1e3404847973863b67a6983fc5a8d91b04 Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Wed, 23 Aug 2023 12:17:25 +0530 Subject: [PATCH 03/21] add changes --- .../role/mgt/ApplicationRoleManagerImpl.java | 17 ++-- .../ApplicationRoleMgtConstants.java | 2 +- .../role/mgt/constants/SQLConstants.java | 7 +- .../role/mgt/dao/ApplicationRoleMgtDAO.java | 3 - .../dao/impl/ApplicationRoleMgtDAOImpl.java | 93 ++++++++----------- .../CacheBackedApplicationRoleMgtDAOImpl.java | 7 -- .../application/role/mgt/model/Group.java | 11 +++ 7 files changed, 66 insertions(+), 74 deletions(-) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java index 0351bb247d48..d36113fedbe7 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java @@ -135,19 +135,20 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis @Override public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String idpId) throws ApplicationRoleManagementException { - - if (LOCAL_IDP.equals(idpId)) { - return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, getTenantDomain()); - } - IdentityProvider identityProvider; try { - identityProvider = ApplicationRoleMgtServiceComponentHolder.getInstance() - .getIdentityProviderManager().getIdPByResourceId(idpId, getTenantDomain(), true); + IdentityProvider identityProvider; + if (LOCAL_IDP.equalsIgnoreCase(idpId)) { + identityProvider = ApplicationRoleMgtServiceComponentHolder.getInstance() + .getIdentityProviderManager().getResidentIdP(getTenantDomain()); + } else { + identityProvider = ApplicationRoleMgtServiceComponentHolder.getInstance() + .getIdentityProviderManager().getIdPByResourceId(idpId, getTenantDomain(), true); + } + return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, identityProvider, getTenantDomain()); } catch (IdentityProviderManagementException e) { throw new ApplicationRoleManagementException("Error while retrieving idp", "Error while retrieving idp " + "for idpId: " + idpId, e); } - return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, identityProvider, getTenantDomain()); } private static String getTenantDomain() { diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java index 735bd065df80..95697f521ecc 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java @@ -24,7 +24,7 @@ public class ApplicationRoleMgtConstants { private static final String APP_ROLE_MGT_ERROR_CODE_PREFIX = "APM-"; - public static final String LOCAL_IDP = "local"; + public static final String LOCAL_IDP = "LOCAL"; /** * Application role management error message constants. diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java index 07166afca263..a4ea631f4313 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java @@ -54,7 +54,7 @@ public class SQLConstants { SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";);"; public static final String DELETE_ASSIGNED_USER_APPLICATION_ROLE = "DELETE FROM USER_ROLE WHERE ROLE_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + " AND USER_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + "; AND USER_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID + ";"; public static final String GET_ASSIGNED_USERS_OF_APPLICATION_ROLE = "SELECT USER_ID " + @@ -68,11 +68,12 @@ public class SQLConstants { SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";);"; public static final String DELETE_ASSIGNED_GROUP_APPLICATION_ROLE = "DELETE FROM GROUP_ROLE WHERE ROLE_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + " AND GROUP_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + "; AND GROUP_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID + ";"; public static final String GET_ASSIGNED_GROUPS_OF_APPLICATION_ROLE = "SELECT GROUP_ID, IDP_ID " + - "FROM GROUP_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";"; + "FROM GROUP_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + + "; AND IDP_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_IDP_ID + ";"; /** * SQL Placeholders. diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java index dccd63bb5592..9e6e368bfdef 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java @@ -57,9 +57,6 @@ void updateApplicationRoleAssignedGroups(String roleId, String idpId, List removedGroups, String tenantDomain) throws ApplicationRoleManagementException; - ApplicationRole getApplicationRoleAssignedGroups(String roleId, String tenantDomain) - throws ApplicationRoleManagementException; - ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, String tenantDomain) throws ApplicationRoleManagementException; } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java index 156c46db08b6..4e55878000b2 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -51,6 +51,7 @@ import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_ROLE; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.LOCAL_IDP; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_APP_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_IDP_ID; @@ -227,8 +228,8 @@ public void updateApplicationRoleAssignedUsers(String roleId, List added } }), roleId); for (String userId: removedUsers) { - namedJdbcTemplate.executeQuery(SQLConstants.DELETE_ASSIGNED_USER_APPLICATION_ROLE, - (resultSet, rowNumber) -> null, namedPreparedStatement -> { + namedJdbcTemplate.executeUpdate(SQLConstants.DELETE_ASSIGNED_USER_APPLICATION_ROLE, + namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_USER_ID, userId); }); @@ -248,9 +249,7 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String ten try { List users; users = namedJdbcTemplate.executeQuery(SQLConstants.GET_ASSIGNED_USERS_OF_APPLICATION_ROLE, - (resultSet, rowNumber) -> { - return new User(resultSet.getString(DB_SCHEMA_COLUMN_NAME_USER_ID)); - }, + (resultSet, rowNumber) -> new User(resultSet.getString(DB_SCHEMA_COLUMN_NAME_USER_ID)), namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); }); @@ -279,23 +278,27 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { - String sqlStmt = SQLConstants.ADD_APPLICATION_ROLE_GROUP; namedJdbcTemplate.withTransaction(template -> { - namedJdbcTemplate.executeBatchInsert(sqlStmt, (preparedStatement -> { - for (String groupId : addedGroups) { - preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); - preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); - preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_IDP_ID, idpId); - preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); - preparedStatement.addBatch(); + if (addedGroups.size() > 0) { + namedJdbcTemplate.executeBatchInsert(SQLConstants.ADD_APPLICATION_ROLE_GROUP, + (preparedStatement -> { + for (String groupId : addedGroups) { + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_IDP_ID, idpId); + preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + preparedStatement.addBatch(); + } + }), roleId); + } + if (removedGroups.size() > 0) { + for (String groupId: removedGroups) { + namedJdbcTemplate.executeUpdate(SQLConstants.DELETE_ASSIGNED_GROUP_APPLICATION_ROLE, + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); + }); } - }), roleId); - for (String groupId: removedGroups) { - namedJdbcTemplate.executeQuery(SQLConstants.DELETE_ASSIGNED_GROUP_APPLICATION_ROLE, - (resultSet, rowNumber) -> null, namedPreparedStatement -> { - namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); - namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); - }); } return null; }); @@ -304,30 +307,6 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis } } - @Override - public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String tenantDomain) throws - ApplicationRoleManagementException { - - NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); - try { - List groups; - groups = namedJdbcTemplate.executeQuery(SQLConstants.GET_ASSIGNED_GROUPS_OF_APPLICATION_ROLE, - (resultSet, rowNumber) -> new Group(resultSet.getString(DB_SCHEMA_COLUMN_NAME_GROUP_ID), - resultSet.getString(DB_SCHEMA_COLUMN_NAME_IDP_ID)), - namedPreparedStatement -> { - namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); - }); - for (Group group : groups) { - group.setGroupName(getGroupNamesByID(group.getGroupId(), tenantDomain)); - } - ApplicationRole applicationRole = new ApplicationRole(roleId); - applicationRole.setAssignedGroups(groups); - return applicationRole; - } catch (DataAccessException e) { - throw handleServerException(ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS, e, roleId); - } - } - @Override public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, String tenantDomain) throws @@ -341,15 +320,25 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityP resultSet.getString(DB_SCHEMA_COLUMN_NAME_IDP_ID)), namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_IDP_ID, + identityProvider.getResourceId()); }); - IdPGroup[] idpGroups = identityProvider.getIdPGroupConfig(); - Map idToNameMap = new HashMap<>(); - for (IdPGroup idpGroup : idpGroups) { - idToNameMap.put(idpGroup.getIdpGroupId(), idpGroup.getIdpGroupName()); - } - for (Group group : groups) { - if (idToNameMap.containsKey(group.getGroupId())) { - group.setGroupName(idToNameMap.get(group.getGroupId())); + if (LOCAL_IDP.equals(identityProvider.getIdentityProviderName())) { + for (Group group : groups) { + group.setGroupName(getGroupNamesByID(group.getGroupId(), tenantDomain)); + group.setIdpName(identityProvider.getIdentityProviderName()); + } + } else { + IdPGroup[] idpGroups = identityProvider.getIdPGroupConfig(); + Map idToNameMap = new HashMap<>(); + for (IdPGroup idpGroup : idpGroups) { + idToNameMap.put(idpGroup.getIdpGroupId(), idpGroup.getIdpGroupName()); + } + for (Group group : groups) { + if (idToNameMap.containsKey(group.getGroupId())) { + group.setGroupName(idToNameMap.get(group.getGroupId())); + group.setIdpName(identityProvider.getIdentityProviderName()); + } } } ApplicationRole applicationRole = new ApplicationRole(roleId); diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java index cca39aeb7f7c..6b9e8164b6b7 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java @@ -124,13 +124,6 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis tenantDomain); } - @Override - public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String tenantDomain) - throws ApplicationRoleManagementException { - - return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, tenantDomain); - } - @Override public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, String tenantDomain) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java index 1774d85d9dc8..d116f282b734 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java @@ -8,6 +8,7 @@ public class Group { private String groupId; private String groupName; private String idpId; + private String idpName; public Group(String groupId, String idpId) { @@ -44,4 +45,14 @@ public void setIdpId(String idpId) { this.idpId = idpId; } + + public String getIdpName() { + + return idpName; + } + + public void setIdpName(String idpName) { + + this.idpName = idpName; + } } From 2f02e6f90fd2e3580eb82b6fc3ffab2ee972d70a Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Thu, 24 Aug 2023 15:28:38 +0530 Subject: [PATCH 04/21] Add validations --- .../role/mgt/ApplicationRoleManager.java | 16 ++++ .../role/mgt/ApplicationRoleManagerImpl.java | 16 +++- .../ApplicationRoleMgtConstants.java | 20 ++-- .../role/mgt/constants/SQLConstants.java | 8 ++ .../role/mgt/dao/ApplicationRoleMgtDAO.java | 8 +- .../dao/impl/ApplicationRoleMgtDAOImpl.java | 93 ++++++++++++++++++- .../CacheBackedApplicationRoleMgtDAOImpl.java | 30 +++++- .../role/mgt/util/GroupIDResolver.java | 21 ++++- .../application/role/mgt/util/IDResolver.java | 2 + .../role/mgt/util/UserIDResolver.java | 70 +++++++------- 10 files changed, 226 insertions(+), 58 deletions(-) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java index c0546e2334ab..89d4438c8614 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java @@ -110,5 +110,21 @@ void updateApplicationRoleAssignedGroups(String roleId, String idpId, List getApplicationRolesByUserId(String userId) throws ApplicationRoleManagementException; + + /** + * Get the list of application roles of a group. + * + * @param groupId group ID. + * @throws ApplicationRoleManagementException Error occurred while updating the application role. + */ + List getApplicationRolesByGroupId(String groupId) throws ApplicationRoleManagementException; + } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java index d36113fedbe7..767cb6ad468c 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java @@ -124,7 +124,7 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis .getIdentityProviderManager().getIdPByResourceId(idpId, getTenantDomain(), true); } removeCommonValues(addedGroups, removedGroups); - applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, identityProvider.getResourceId(), + applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, identityProvider, addedGroups, removedGroups, getTenantDomain()); } catch (IdentityProviderManagementException e) { throw new ApplicationRoleManagementException("Error while retrieving idp", @@ -151,6 +151,20 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String id } } + @Override + public List getApplicationRolesByUserId(String userId) + throws ApplicationRoleManagementException { + + return applicationRoleMgtDAO.getApplicationRolesByUserId(userId); + } + + @Override + public List getApplicationRolesByGroupId(String groupId) + throws ApplicationRoleManagementException { + + return applicationRoleMgtDAO.getApplicationRolesByGroupId(groupId); + } + private static String getTenantDomain() { return PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java index 95697f521ecc..41f414c475e2 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java @@ -44,14 +44,22 @@ public enum ErrorMessages { "Error occurred while updating the role: %s of application: %s."), ERROR_CODE_DELETE_ROLE("65006", "Error occurred while deleting the role.", "Error occurred while deleting the role: %s."), - ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS("65007", "Error occurred while assigning users to the role.", - "Error occurred while assigning users to the role: %s."), + ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS("65007", "Error occurred while assigning users to " + + "the role.", "Error occurred while assigning users to the role: %s."), ERROR_CODE_GET_ROLE_ASSIGNED_USERS("65008", "Error occurred while retrieving users of the role.", "Error occurred while retrieving users of the role: %s."), - ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS("65007", "Error occurred while assigning groups to the role.", - "Error occurred while assigning groups to the role: %s."), - ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS("65008", "Error occurred while retrieving groups of the role.", - "Error occurred while retrieving groups of the role: %s."), + ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS("65007", "Error occurred while assigning groups to " + + "the role.", "Error occurred while assigning groups to the role: %s."), + ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS("65008", "Error occurred while retrieving groups of " + + "the role.", "Error occurred while retrieving groups of the role: %s."), + ERROR_CODE_GET_ROLES_BY_USER_ID("65009", "Error occurred while retrieving the roles of the " + + "application by userID", "Error occurred while retrieving the roles of application by " + + "userID: %s."), + ERROR_CODE_GET_ROLES_BY_GROUP_ID("65010", "Error occurred while retrieving the roles of the " + + "application by groupID", "Error occurred while retrieving the roles of application by " + + "groupID: %s."), + ERROR_CODE_GROUP_ID_NO_FOUND("65011", "Given group not found", "Given group not found " + + "for groupID: %s."), // Client Errors. ERROR_CODE_DUPLICATE_ROLE("60001", "Role already exists.", diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java index a4ea631f4313..953c7791da80 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java @@ -75,6 +75,14 @@ public class SQLConstants { "FROM GROUP_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + "; AND IDP_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_IDP_ID + ";"; + public static final String GET_APPLICATION_ROLES_BY_USER_ID = "SELECT ar.ROLE_ID, ar.ROLE_NAME, ar.TENANT_ID, " + + "ar.APP_ID FROM APP_ROLE as ar INNER JOIN USER_ROLE as ur ON ar.ROLE_ID = ur.ROLE_ID WHERE ur.USER_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID + ";"; + + public static final String GET_APPLICATION_ROLES_BY_GROUP_ID = "SELECT ar.ROLE_ID, ar.ROLE_NAME, ar.TENANT_ID, " + + "ar.APP_ID FROM APP_ROLE as ar INNER JOIN GROUP_ROLE as gr ON ar.ROLE_ID = gr.ROLE_ID WHERE gr.GROUP_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID + ";"; + /** * SQL Placeholders. */ diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java index 9e6e368bfdef..1b8d2c31f934 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java @@ -48,15 +48,19 @@ boolean isExistingRole(String applicationId, String roleName, String tenantDomai void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, String tenantDomain) throws - ApplicationRoleManagementServerException; + ApplicationRoleManagementException; ApplicationRole getApplicationRoleAssignedUsers(String roleId, String tenantDomain) throws ApplicationRoleManagementException; - void updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, + void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, List addedGroups, List removedGroups, String tenantDomain) throws ApplicationRoleManagementException; ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, String tenantDomain) throws ApplicationRoleManagementException; + + List getApplicationRolesByUserId(String userId) throws ApplicationRoleManagementException; + + List getApplicationRolesByGroupId(String groupId) throws ApplicationRoleManagementException; } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java index 4e55878000b2..eceb02555129 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -27,6 +27,7 @@ import org.wso2.carbon.identity.application.common.model.IdentityProvider; import org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants; import org.wso2.carbon.identity.application.role.mgt.dao.ApplicationRoleMgtDAO; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementClientException; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; @@ -44,6 +45,8 @@ import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_CHECKING_ROLE_EXISTENCE; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_DELETE_ROLE; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLES_BY_APPLICATION; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLES_BY_GROUP_ID; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLES_BY_USER_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_ASSIGNED_USERS; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_BY_ID; @@ -206,7 +209,7 @@ public boolean isExistingRole(String applicationId, String roleName, String tena @Override public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, String tenantDomain) - throws ApplicationRoleManagementServerException { + throws ApplicationRoleManagementException { int tenantID; if (tenantDomain != null) { @@ -214,6 +217,7 @@ public void updateApplicationRoleAssignedUsers(String roleId, List added } else { tenantID = MultitenantConstants.INVALID_TENANT_ID; } + validateUserIds(addedUsers, tenantDomain); NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { @@ -265,8 +269,9 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String ten } @Override - public void updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, - List removedGroups, String tenantDomain) + public void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, + List addedGroups, List removedGroups, + String tenantDomain) throws ApplicationRoleManagementException { int tenantID; @@ -275,7 +280,8 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis } else { tenantID = MultitenantConstants.INVALID_TENANT_ID; } - + validateGroupIds(identityProvider, addedGroups, tenantDomain); + validateGroupIds(identityProvider, removedGroups, tenantDomain); NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { namedJdbcTemplate.withTransaction(template -> { @@ -285,7 +291,8 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis for (String groupId : addedGroups) { preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); - preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_IDP_ID, idpId); + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_IDP_ID, + identityProvider.getResourceId()); preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); preparedStatement.addBatch(); } @@ -349,6 +356,81 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityP } } + @Override + public List getApplicationRolesByUserId(String userId) throws ApplicationRoleManagementException { + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + return namedJdbcTemplate.executeQuery(SQLConstants.GET_APPLICATION_ROLES_BY_USER_ID, + (resultSet, rowNumber) -> + new ApplicationRole(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_ID), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_APP_ID)), + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_USER_ID, userId); + }); + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_GET_ROLES_BY_USER_ID, e, userId); + } + } + + @Override + public List getApplicationRolesByGroupId(String groupId) + throws ApplicationRoleManagementException { + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + return namedJdbcTemplate.executeQuery(SQLConstants.GET_APPLICATION_ROLES_BY_GROUP_ID, + (resultSet, rowNumber) -> + new ApplicationRole(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_ID), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_APP_ID)), + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); + }); + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_GET_ROLES_BY_GROUP_ID, e, groupId); + } + } + + public void validateGroupIds(IdentityProvider identityProvider, List groups, String tenantDomain) + throws ApplicationRoleManagementException { + + if (LOCAL_IDP.equals(identityProvider.getIdentityProviderName())) { + for (String groupId : groups) { + boolean isExists = groupIDResolver.isExists(groupId, tenantDomain); + if (!isExists) { + throw new ApplicationRoleManagementClientException("Given groupId is not found", + "Given groupId is not found", ""); + } + } + } else { + IdPGroup[] idpGroups = identityProvider.getIdPGroupConfig(); + Map idToNameMap = new HashMap<>(); + for (IdPGroup idpGroup : idpGroups) { + idToNameMap.put(idpGroup.getIdpGroupId(), idpGroup.getIdpGroupName()); + } + for (String groupId : groups) { + if (!idToNameMap.containsKey(groupId)) { + throw new ApplicationRoleManagementClientException("Given groupId is not found", + "Given groupId is not found", ""); + } + } + } + } + + public void validateUserIds(List users, String tenantDomain) + throws ApplicationRoleManagementException { + + for (String userId : users) { + boolean isExists = userIDResolver.isExists(userId, tenantDomain); + if (!isExists) { + throw new ApplicationRoleManagementClientException("Given user Id is not found", + "Given user Id is not found", ""); + } + } + } + private String getUserNamesByID(String userID, String tenantDomain) throws ApplicationRoleManagementException { @@ -360,4 +442,5 @@ private String getGroupNamesByID(String groupID, String tenantDomain) return groupIDResolver.getNameByID(groupID, tenantDomain); } + } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java index 6b9e8164b6b7..1e3ea42a9c83 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java @@ -103,7 +103,7 @@ public boolean isExistingRole(String applicationId, String roleName, String tena @Override public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, String tenantDomain) - throws ApplicationRoleManagementServerException { + throws ApplicationRoleManagementException { applicationRoleMgtDAO.updateApplicationRoleAssignedUsers(roleId, addedUsers, removedUsers, tenantDomain); } @@ -112,15 +112,23 @@ public void updateApplicationRoleAssignedUsers(String roleId, List added public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String tenantDomain) throws ApplicationRoleManagementException { + ApplicationRole applicationRole = getApplicationRoleFromCache(roleId, tenantDomain); + if (applicationRole == null) { + applicationRole = applicationRoleMgtDAO.getApplicationRoleById(roleId, tenantDomain); + if (applicationRole != null) { + addToCache(applicationRole, tenantDomain); + } + } return applicationRoleMgtDAO.getApplicationRoleAssignedUsers(roleId, tenantDomain); } @Override - public void updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, - List removedGroups, String tenantDomain) + public void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, + List addedGroups, List removedGroups, + String tenantDomain) throws ApplicationRoleManagementException { - applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, idpId, addedGroups, removedGroups, + applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, identityProvider, addedGroups, removedGroups, tenantDomain); } @@ -132,6 +140,20 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityP return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, identityProvider, tenantDomain); } + @Override + public List getApplicationRolesByUserId(String userId) + throws ApplicationRoleManagementException { + + return applicationRoleMgtDAO.getApplicationRolesByUserId(userId); + } + + @Override + public List getApplicationRolesByGroupId(String groupId) + throws ApplicationRoleManagementException { + + return applicationRoleMgtDAO.getApplicationRolesByGroupId(groupId); + } + private ApplicationRole getApplicationRoleFromCache(String applicationRoleId, String tenantDomain) { ApplicationRole applicationRole = null; diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java index fe5cc862cb7b..36876d9a7fd0 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java @@ -26,7 +26,26 @@ public String getNameByID(String id, String tenantDomain) throws ApplicationRole return groupName; } - public String resolveGroupNameFromGroupID(String id) throws ApplicationRoleManagementException { + @Override + public boolean isExists(String id, String tenantDomain) throws ApplicationRoleManagementException { + + return isGroupExists(id); + } + + private boolean isGroupExists(String id) throws ApplicationRoleManagementException { + + AbstractUserStoreManager userStoreManager; + try { + userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + return userStoreManager.isGroupExist(id); + } catch (UserStoreException e) { + throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID", "Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID: " + id, e); + } + } + + private String resolveGroupNameFromGroupID(String id) throws ApplicationRoleManagementException { AbstractUserStoreManager userStoreManager; try { diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java index 893172d74e6f..1f4c6b303373 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java @@ -9,4 +9,6 @@ public interface IDResolver { String getNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException; + boolean isExists(String id, String tenantDomain) throws ApplicationRoleManagementException; + } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java index 6491199a9082..6efb1b77070e 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java @@ -1,25 +1,20 @@ package org.wso2.carbon.identity.application.role.mgt.util; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementClientException; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; +import org.wso2.carbon.identity.application.role.mgt.internal.ApplicationRoleMgtServiceComponentHolder; +import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.user.api.UserStoreManager; import org.wso2.carbon.user.core.common.AbstractUserStoreManager; - -import java.util.ArrayList; -import java.util.List; +import org.wso2.carbon.user.core.service.RealmService; /** * UserId Resolver. */ public class UserIDResolver implements IDResolver { - private Log log = LogFactory.getLog(UserIDResolver.class); - @Override public String getNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException { @@ -31,47 +26,44 @@ public String getNameByID(String id, String tenantDomain) throws ApplicationRole return userName; } - public List getNamesByIDs(List idList, String tenantDomain) - throws ApplicationRoleManagementException { + @Override + public boolean isExists(String id, String tenantDomain) throws ApplicationRoleManagementException { - List usersList = new ArrayList<>(); - for (String id : idList) { - usersList.add(getNameByID(id, tenantDomain)); + return isGroupExists(id); + } + + private boolean isGroupExists(String id) throws ApplicationRoleManagementException { + + AbstractUserStoreManager userStoreManager; + try { + userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + return userStoreManager.isExistingUserWithID(id); + } catch (UserStoreException e) { + throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID", "Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID: " + id, e); } - return usersList; } - /** - * Retrieves the username of the given userID. - * - * @param id userID. - * @return username of the user. - * @throws ApplicationRoleManagementException ApplicationRoleManagementException. - */ public String resolveUserNameFromUserID(String id) throws ApplicationRoleManagementException { + AbstractUserStoreManager userStoreManager; try { - UserStoreManager userStoreManager = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getUserStoreManager(); - try { - if (userStoreManager instanceof AbstractUserStoreManager) { - return ((AbstractUserStoreManager) userStoreManager).getUserNameFromUserID(id); - } - if (log.isDebugEnabled()) { - log.debug("Provided user store manager for the userID: " + id + ", is not an instance " + - "of the AbstractUserStore manager"); - } - throw new ApplicationRoleManagementClientException("Unable to get the username of the userID", - "Unable to get the username of the userID: " + id + ".", ""); - } catch (UserStoreException e) { - throw new ApplicationRoleManagementServerException("Error occurred while resolving username for " + - "the userID Error occurred while resolving username for the userID: " + id, ""); - } + userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + return userStoreManager.getUserNameFromUserID(id); } catch (UserStoreException e) { throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " - + "to resolve username for the userID", "Error occurred while retrieving the userstore manager to " - + "resolve username for the userID: " + id, e); + + "to resolve username for the groupID", "Error occurred while retrieving the userstore manager " + + "to resolve username for the groupID: " + id, e); } } + private AbstractUserStoreManager getUserStoreManager(int tenantId) throws UserStoreException { + + RealmService realmService = ApplicationRoleMgtServiceComponentHolder.getInstance().getRealmService(); + UserRealm tenantUserRealm = realmService.getTenantUserRealm(tenantId); + + return (AbstractUserStoreManager) tenantUserRealm.getUserStoreManager(); + } + } From be5dd89b41f28181209400a26a863e4cdf9da8e2 Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Mon, 28 Aug 2023 08:53:55 +0530 Subject: [PATCH 05/21] fix app role update --- .../role/mgt/ApplicationRoleManager.java | 16 +- .../role/mgt/ApplicationRoleManagerImpl.java | 29 ++- .../ApplicationRoleMgtConstants.java | 47 +++-- .../role/mgt/constants/SQLConstants.java | 31 +++- .../role/mgt/dao/ApplicationRoleMgtDAO.java | 11 +- .../dao/impl/ApplicationRoleMgtDAOImpl.java | 169 ++++++++++-------- .../CacheBackedApplicationRoleMgtDAOImpl.java | 19 +- 7 files changed, 206 insertions(+), 116 deletions(-) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java index 89d4438c8614..8fc47b2386bc 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java @@ -39,10 +39,14 @@ public interface ApplicationRoleManager { /** * Update application role. * - * @param applicationRole Application role. + * @param roleId role Id. + * @param newName new role name. + * @param addedScopes List of scopes to be added. + * @param removedScopes List of scopes to be removed. * @throws ApplicationRoleManagementException Error occurred while updating the application role. */ - void updateApplicationRole(ApplicationRole applicationRole) throws ApplicationRoleManagementException; + void updateApplicationRole(String applicationId, String roleId, String newName, List addedScopes, + List removedScopes) throws ApplicationRoleManagementException; /** * Get the application role by role id. @@ -114,17 +118,21 @@ ApplicationRole getApplicationRoleAssignedGroups(String roleId, String idpId) * Get the list of application roles of a user. * * @param userId user ID. + * @param tenantDomain tenant domain. * @throws ApplicationRoleManagementException Error occurred while updating the application role. */ - List getApplicationRolesByUserId(String userId) throws ApplicationRoleManagementException; + List getApplicationRolesByUserId(String userId, String tenantDomain) + throws ApplicationRoleManagementException; /** * Get the list of application roles of a group. * * @param groupId group ID. + * @param tenantDomain tenant domain. * @throws ApplicationRoleManagementException Error occurred while updating the application role. */ - List getApplicationRolesByGroupId(String groupId) throws ApplicationRoleManagementException; + List getApplicationRolesByGroupId(String groupId, String tenantDomain) + throws ApplicationRoleManagementException; } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java index 767cb6ad468c..85af5fb73506 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java @@ -33,6 +33,7 @@ import java.util.List; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_DUPLICATE_ROLE; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_ROLE_NOT_FOUND; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.LOCAL_IDP; import static org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils.handleClientException; @@ -70,9 +71,11 @@ public void addApplicationRole(ApplicationRole applicationRole) throws Applicati } @Override - public void updateApplicationRole(ApplicationRole applicationRole) throws ApplicationRoleManagementException { + public void updateApplicationRole(String applicationId, String roleId, String newName, List addedScopes, + List removedScopes) throws ApplicationRoleManagementException { - // TODO : + // TODO: Check authorized scopes for the app and filter out added permissions + applicationRoleMgtDAO.updateApplicationRole(roleId, newName, addedScopes, removedScopes, getTenantDomain()); } @Override @@ -97,6 +100,7 @@ public void deleteApplicationRole(String roleId) throws ApplicationRoleManagemen public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers) throws ApplicationRoleManagementException { + validateAppRoleId(roleId); removeCommonValues(addedUsers, removedUsers); applicationRoleMgtDAO.updateApplicationRoleAssignedUsers(roleId, addedUsers, removedUsers, getTenantDomain()); } @@ -105,6 +109,7 @@ public void updateApplicationRoleAssignedUsers(String roleId, List added public ApplicationRole getApplicationRoleAssignedUsers(String roleId) throws ApplicationRoleManagementException { + validateAppRoleId(roleId); return applicationRoleMgtDAO.getApplicationRoleAssignedUsers(roleId, getTenantDomain()); } @@ -113,6 +118,7 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis List removedGroups) throws ApplicationRoleManagementException { + validateAppRoleId(roleId); try { IdentityProvider identityProvider; if (LOCAL_IDP.equals(idpId)) { @@ -135,6 +141,8 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis @Override public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String idpId) throws ApplicationRoleManagementException { + + validateAppRoleId(roleId); try { IdentityProvider identityProvider; if (LOCAL_IDP.equalsIgnoreCase(idpId)) { @@ -152,17 +160,26 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String id } @Override - public List getApplicationRolesByUserId(String userId) + public List getApplicationRolesByUserId(String userId, String tenantDomain) throws ApplicationRoleManagementException { - return applicationRoleMgtDAO.getApplicationRolesByUserId(userId); + return applicationRoleMgtDAO.getApplicationRolesByUserId(userId, tenantDomain); } @Override - public List getApplicationRolesByGroupId(String groupId) + public List getApplicationRolesByGroupId(String groupId, String tenantDomain) throws ApplicationRoleManagementException { - return applicationRoleMgtDAO.getApplicationRolesByGroupId(groupId); + return applicationRoleMgtDAO.getApplicationRolesByGroupId(groupId, tenantDomain); + } + + private void validateAppRoleId(String roleId) throws ApplicationRoleManagementException { + + boolean isExists = applicationRoleMgtDAO.checkRoleExists(roleId, getTenantDomain()); + + if (!isExists) { + throw handleClientException(ERROR_CODE_ROLE_NOT_FOUND, roleId); + } } private static String getTenantDomain() { diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java index 41f414c475e2..a473a4ef6730 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java @@ -23,7 +23,7 @@ */ public class ApplicationRoleMgtConstants { - private static final String APP_ROLE_MGT_ERROR_CODE_PREFIX = "APM-"; + private static final String APP_ROLE_MGT_ERROR_CODE_PREFIX = "APP-ROLE-"; public static final String LOCAL_IDP = "LOCAL"; /** @@ -41,29 +41,38 @@ public enum ErrorMessages { ERROR_CODE_GET_ROLES_BY_APPLICATION("65004", "Error occurred while retrieving the roles of the application", "Error occurred while retrieving the roles of application: %s."), ERROR_CODE_UPDATE_ROLE("65005", "Error occurred while updating the role.", - "Error occurred while updating the role: %s of application: %s."), + "Error occurred while updating the role with id: %s."), ERROR_CODE_DELETE_ROLE("65006", "Error occurred while deleting the role.", "Error occurred while deleting the role: %s."), - ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS("65007", "Error occurred while assigning users to " + - "the role.", "Error occurred while assigning users to the role: %s."), - ERROR_CODE_GET_ROLE_ASSIGNED_USERS("65008", "Error occurred while retrieving users of the role.", - "Error occurred while retrieving users of the role: %s."), - ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS("65007", "Error occurred while assigning groups to " + - "the role.", "Error occurred while assigning groups to the role: %s."), - ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS("65008", "Error occurred while retrieving groups of " + - "the role.", "Error occurred while retrieving groups of the role: %s."), - ERROR_CODE_GET_ROLES_BY_USER_ID("65009", "Error occurred while retrieving the roles of the " + - "application by userID", "Error occurred while retrieving the roles of application by " + - "userID: %s."), - ERROR_CODE_GET_ROLES_BY_GROUP_ID("65010", "Error occurred while retrieving the roles of the " + - "application by groupID", "Error occurred while retrieving the roles of application by " + - "groupID: %s."), - ERROR_CODE_GROUP_ID_NO_FOUND("65011", "Given group not found", "Given group not found " + - "for groupID: %s."), + ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS("65007", "Error occurred while updating assigned users to " + + "the role.", "Error occurred while updating assigned users to the roleId: %s."), + ERROR_CODE_GET_ROLE_ASSIGNED_USERS("65008", "Error occurred while retrieving assigned users of " + + "the role.", "Error occurred while retrieving users of the roleId: %s."), + ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS("65007", "Error occurred while updating assigned groups " + + "to the role.", "Error occurred while updating assigned groups to the roleId: %s."), + ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS("65008", "Error occurred while retrieving assigned groups " + + "of the role.", "Error occurred while retrieving assigned groups of the roleId: %s."), + ERROR_CODE_GET_ROLES_BY_USER_ID("65009", "Error occurred while retrieving the app roles by " + + "userID", "Error occurred while retrieving the app roles by userID: %s."), + ERROR_CODE_GET_ROLES_BY_GROUP_ID("65010", "Error occurred while retrieving the app role by " + + "groupID", "Error occurred while retrieving the app role by groupID: %s."), + ERROR_CODE_CHECKING_ROLE_EXISTENCE_BY_ID("65011", "Error occurred while checking the role " + + "existence by id.", "Error occurred while checking whether the role with id : %s."), // Client Errors. ERROR_CODE_DUPLICATE_ROLE("60001", "Role already exists.", - "Role with name: %s already exists in application: %s."); + "Role with name: %s already exists in application: %s."), + ERROR_CODE_ROLE_NOT_FOUND("60002", "Role doesn't exist.", + "Role with id: %s doesn't exist."), + ERROR_CODE_USER_ALREADY_ASSIGNED("60003", "Unable to assign user to app role.", + "User already assign for the roleId: %s."), + ERROR_CODE_GROUP_ALREADY_ASSIGNED("60004", "Unable to assign group to app role.", + "Group already assign for the roleId: %s."), + ERROR_CODE_USER_NOT_FOUND("60005", "Unable to assign user to app role.", + "User not found for the userId: %s."), + ERROR_CODE_GROUP_NOT_FOUND("60006", "Unable to assign group to app role.", + "Group not found for the groupId: %s."), + ; private final String code; private final String message; diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java index 953c7791da80..994aaecfaa8b 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/SQLConstants.java @@ -43,9 +43,11 @@ public class SQLConstants { public static final String UPDATE_APPLICATION_ROLE_BY_ID = "UPDATE APP_ROLE SET ROLE_NAME = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_NAME + "; WHERE ROLE_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";"; + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + "; AND TENANT_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";"; public static final String DELETE_APPLICATION_ROLE_BY_ID = "DELETE FROM APP_ROLE WHERE ROLE_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";"; + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + "; AND TENANT_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";"; public static final String ADD_APPLICATION_ROLE_USER = "INSERT INTO USER_ROLE (ROLE_ID, USER_ID, TENANT_ID) " + "VALUES (:" + @@ -55,10 +57,12 @@ public class SQLConstants { public static final String DELETE_ASSIGNED_USER_APPLICATION_ROLE = "DELETE FROM USER_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + "; AND USER_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID + ";"; + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID + "; AND TENANT_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";"; public static final String GET_ASSIGNED_USERS_OF_APPLICATION_ROLE = "SELECT USER_ID " + - "FROM USER_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + ";"; + "FROM USER_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + + "; AND TENANT_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";"; public static final String ADD_APPLICATION_ROLE_GROUP = "INSERT INTO GROUP_ROLE (ROLE_ID, GROUP_ID, IDP_ID," + " TENANT_ID) VALUES (:" + @@ -69,19 +73,27 @@ public class SQLConstants { public static final String DELETE_ASSIGNED_GROUP_APPLICATION_ROLE = "DELETE FROM GROUP_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + "; AND GROUP_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID + ";"; + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID + "; AND TENANT_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";"; public static final String GET_ASSIGNED_GROUPS_OF_APPLICATION_ROLE = "SELECT GROUP_ID, IDP_ID " + "FROM GROUP_ROLE WHERE ROLE_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + - "; AND IDP_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_IDP_ID + ";"; + "; AND IDP_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_IDP_ID + + "; AND TENANT_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";"; public static final String GET_APPLICATION_ROLES_BY_USER_ID = "SELECT ar.ROLE_ID, ar.ROLE_NAME, ar.TENANT_ID, " + "ar.APP_ID FROM APP_ROLE as ar INNER JOIN USER_ROLE as ur ON ar.ROLE_ID = ur.ROLE_ID WHERE ur.USER_ID = :" - + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID + ";"; + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID + "; AND ar.TENANT_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";"; public static final String GET_APPLICATION_ROLES_BY_GROUP_ID = "SELECT ar.ROLE_ID, ar.ROLE_NAME, ar.TENANT_ID, " + "ar.APP_ID FROM APP_ROLE as ar INNER JOIN GROUP_ROLE as gr ON ar.ROLE_ID = gr.ROLE_ID WHERE gr.GROUP_ID = :" - + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID + ";"; + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID + "; AND ar.TENANT_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + ";"; + + public static final String IS_APPLICATION_ROLE_EXISTS_BY_ID = "SELECT COUNT(1) FROM APP_ROLE WHERE ROLE_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_ID + "; AND TENANT_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID + "; "; /** * SQL Placeholders. @@ -96,4 +108,7 @@ public static final class SQLPlaceholders { public static final String DB_SCHEMA_COLUMN_NAME_GROUP_ID = "GROUP_ID"; public static final String DB_SCHEMA_COLUMN_NAME_IDP_ID = "IDP_ID"; } + + public static final String USER_ROLE_UNIQUE_CONSTRAINT = "user_role_unique"; + public static final String GROUP_ROLE_UNIQUE_CONSTRAINT = "group_role_unique"; } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java index 1b8d2c31f934..580da77dc8c9 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java @@ -38,7 +38,8 @@ ApplicationRole getApplicationRoleById(String roleId, String tenantDomain) List getApplicationRoles(String applicationId) throws ApplicationRoleManagementServerException; - void updateApplicationRole(String applicationId, String roleId, String tenantDomain) + void updateApplicationRole(String roleId, String newName, List addedScopes, List removedScopes, + String tenantDomain) throws ApplicationRoleManagementServerException; void deleteApplicationRole(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException; @@ -46,6 +47,8 @@ void updateApplicationRole(String applicationId, String roleId, String tenantDom boolean isExistingRole(String applicationId, String roleName, String tenantDomain) throws ApplicationRoleManagementServerException; + boolean checkRoleExists(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException; + void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, String tenantDomain) throws ApplicationRoleManagementException; @@ -60,7 +63,9 @@ void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identit ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, String tenantDomain) throws ApplicationRoleManagementException; - List getApplicationRolesByUserId(String userId) throws ApplicationRoleManagementException; + List getApplicationRolesByUserId(String userId, String tenantDomain) + throws ApplicationRoleManagementException; - List getApplicationRolesByGroupId(String groupId) throws ApplicationRoleManagementException; + List getApplicationRolesByGroupId(String groupId, String tenantDomain) + throws ApplicationRoleManagementException; } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java index eceb02555129..0281eb8518e8 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -18,6 +18,7 @@ package org.wso2.carbon.identity.application.role.mgt.dao.impl; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; @@ -27,12 +28,12 @@ import org.wso2.carbon.identity.application.common.model.IdentityProvider; import org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants; import org.wso2.carbon.identity.application.role.mgt.dao.ApplicationRoleMgtDAO; -import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementClientException; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; import org.wso2.carbon.identity.application.role.mgt.model.Group; import org.wso2.carbon.identity.application.role.mgt.model.User; +import org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils; import org.wso2.carbon.identity.application.role.mgt.util.GroupIDResolver; import org.wso2.carbon.identity.application.role.mgt.util.UserIDResolver; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; @@ -43,6 +44,7 @@ import java.util.Map; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_CHECKING_ROLE_EXISTENCE; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_CHECKING_ROLE_EXISTENCE_BY_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_DELETE_ROLE; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLES_BY_APPLICATION; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLES_BY_GROUP_ID; @@ -50,10 +52,14 @@ import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_ASSIGNED_USERS; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GET_ROLE_BY_ID; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GROUP_ALREADY_ASSIGNED; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_GROUP_NOT_FOUND; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_INSERT_ROLE; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_ROLE; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_USER_ALREADY_ASSIGNED; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_USER_NOT_FOUND; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.LOCAL_IDP; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_APP_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID; @@ -71,20 +77,13 @@ public class ApplicationRoleMgtDAOImpl implements ApplicationRoleMgtDAO { private static final Log LOG = LogFactory.getLog(ApplicationRoleMgtDAOImpl.class); - private UserIDResolver userIDResolver = new UserIDResolver(); - private GroupIDResolver groupIDResolver = new GroupIDResolver(); + private final UserIDResolver userIDResolver = new UserIDResolver(); + private final GroupIDResolver groupIDResolver = new GroupIDResolver(); @Override public ApplicationRole addApplicationRole(ApplicationRole applicationRole, String tenantDomain) throws ApplicationRoleManagementServerException { - int tenantID; - if (tenantDomain != null) { - tenantID = IdentityTenantUtil.getTenantId(tenantDomain); - } else { - tenantID = MultitenantConstants.INVALID_TENANT_ID; - } - NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { namedJdbcTemplate.withTransaction(template -> { @@ -92,7 +91,7 @@ public ApplicationRole addApplicationRole(ApplicationRole applicationRole, Strin namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, applicationRole.getRoleId()); namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_APP_ID, applicationRole.getApplicationId()); namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME, applicationRole.getRoleName()); - namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); }, null, false); return null; }); @@ -107,13 +106,6 @@ public ApplicationRole addApplicationRole(ApplicationRole applicationRole, Strin public ApplicationRole getApplicationRoleById(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException { - int tenantID; - if (tenantDomain != null) { - tenantID = IdentityTenantUtil.getTenantId(tenantDomain); - } else { - tenantID = MultitenantConstants.INVALID_TENANT_ID; - } - NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { return namedJdbcTemplate.fetchSingleRecord(SQLConstants.GET_APPLICATION_ROLE_BY_ID, @@ -123,7 +115,7 @@ public ApplicationRole getApplicationRoleById(String roleId, String tenantDomain resultSet.getString(DB_SCHEMA_COLUMN_NAME_APP_ID)), namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); - namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); }); } catch (DataAccessException e) { throw handleServerException(ERROR_CODE_GET_ROLE_BY_ID, e, roleId); @@ -150,19 +142,26 @@ public List getApplicationRoles(String applicationId) } @Override - public void updateApplicationRole(String applicationId, String roleId, String tenantDomain) + public void updateApplicationRole(String roleId, String newName, List addedScopes, + List removedScopes, String tenantDomain) throws ApplicationRoleManagementServerException { NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { namedJdbcTemplate.withTransaction(template -> { - template.executeUpdate(SQLConstants.UPDATE_APPLICATION_ROLE_BY_ID, namedPreparedStatement -> { - namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); - }); + if (StringUtils.isNotBlank(newName)) { + template.executeUpdate(SQLConstants.UPDATE_APPLICATION_ROLE_BY_ID, namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME, newName); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); + }); + } + // TODO: Add scopes + // TODO: Remove scopes return null; }); } catch (TransactionException e) { - throw handleServerException(ERROR_CODE_UPDATE_ROLE, e, roleId, applicationId); + throw handleServerException(ERROR_CODE_UPDATE_ROLE, e, roleId); } } @@ -172,9 +171,10 @@ public void deleteApplicationRole(String roleId, String tenantDomain) NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { - namedJdbcTemplate.executeQuery(SQLConstants.DELETE_APPLICATION_ROLE_BY_ID, (resultSet, rowNumber) -> null, + namedJdbcTemplate.executeUpdate(SQLConstants.DELETE_APPLICATION_ROLE_BY_ID, namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); }); } catch (DataAccessException e) { throw handleServerException(ERROR_CODE_DELETE_ROLE, e, roleId); @@ -185,12 +185,6 @@ public void deleteApplicationRole(String roleId, String tenantDomain) public boolean isExistingRole(String applicationId, String roleName, String tenantDomain) throws ApplicationRoleManagementServerException { - int tenantID; - if (tenantDomain != null) { - tenantID = IdentityTenantUtil.getTenantId(tenantDomain); - } else { - tenantID = MultitenantConstants.INVALID_TENANT_ID; - } NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { @@ -199,35 +193,45 @@ public boolean isExistingRole(String applicationId, String roleName, String tena namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME, roleName); namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_APP_ID, applicationId); - namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); }); } catch (DataAccessException e) { throw handleServerException(ERROR_CODE_CHECKING_ROLE_EXISTENCE, e, roleName, applicationId); } } + @Override + public boolean checkRoleExists(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException { + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + return namedJdbcTemplate.fetchSingleRecord(SQLConstants.IS_APPLICATION_ROLE_EXISTS_BY_ID, + (resultSet, rowNumber) -> resultSet.getInt(1) > 0, + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); + }); + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_CHECKING_ROLE_EXISTENCE_BY_ID, e, roleId); + } + } + @Override public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, String tenantDomain) throws ApplicationRoleManagementException { - int tenantID; - if (tenantDomain != null) { - tenantID = IdentityTenantUtil.getTenantId(tenantDomain); - } else { - tenantID = MultitenantConstants.INVALID_TENANT_ID; - } + // Validate given userIds are exists. validateUserIds(addedUsers, tenantDomain); - + validateUserIds(removedUsers, tenantDomain); NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { - String sqlStmt = SQLConstants.ADD_APPLICATION_ROLE_USER; namedJdbcTemplate.withTransaction(template -> { - namedJdbcTemplate.executeBatchInsert(sqlStmt, (preparedStatement -> { + namedJdbcTemplate.executeBatchInsert(SQLConstants.ADD_APPLICATION_ROLE_USER, (preparedStatement -> { for (String userId : addedUsers) { preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_USER_ID, userId); - preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); preparedStatement.addBatch(); } }), roleId); @@ -236,11 +240,16 @@ public void updateApplicationRoleAssignedUsers(String roleId, List added namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_USER_ID, userId); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, + getTenantId(tenantDomain)); }); } return null; }); } catch (TransactionException e) { + if (checkUniqueKeyConstrainViolated(e)) { + throw ApplicationRoleMgtUtils.handleClientException(ERROR_CODE_USER_ALREADY_ASSIGNED, roleId); + } throw handleServerException(ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS, e, roleId); } } @@ -256,6 +265,7 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String ten (resultSet, rowNumber) -> new User(resultSet.getString(DB_SCHEMA_COLUMN_NAME_USER_ID)), namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); }); for (User user : users) { user.setUserName(getUserNamesByID(user.getId(), tenantDomain)); @@ -274,12 +284,6 @@ public void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider String tenantDomain) throws ApplicationRoleManagementException { - int tenantID; - if (tenantDomain != null) { - tenantID = IdentityTenantUtil.getTenantId(tenantDomain); - } else { - tenantID = MultitenantConstants.INVALID_TENANT_ID; - } validateGroupIds(identityProvider, addedGroups, tenantDomain); validateGroupIds(identityProvider, removedGroups, tenantDomain); NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); @@ -293,7 +297,8 @@ public void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_IDP_ID, identityProvider.getResourceId()); - preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantID); + preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, + getTenantId(tenantDomain)); preparedStatement.addBatch(); } }), roleId); @@ -304,12 +309,17 @@ public void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, + getTenantId(tenantDomain)); }); } } return null; }); } catch (TransactionException e) { + if (checkUniqueKeyConstrainViolated(e)) { + throw ApplicationRoleMgtUtils.handleClientException(ERROR_CODE_GROUP_ALREADY_ASSIGNED, roleId); + } throw handleServerException(ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS, e, roleId); } } @@ -329,6 +339,7 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityP namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_IDP_ID, identityProvider.getResourceId()); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); }); if (LOCAL_IDP.equals(identityProvider.getIdentityProviderName())) { for (Group group : groups) { @@ -357,7 +368,8 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityP } @Override - public List getApplicationRolesByUserId(String userId) throws ApplicationRoleManagementException { + public List getApplicationRolesByUserId(String userId, String tenantDomain) + throws ApplicationRoleManagementException { NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { @@ -368,6 +380,7 @@ public List getApplicationRolesByUserId(String userId) throws A resultSet.getString(DB_SCHEMA_COLUMN_NAME_APP_ID)), namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_USER_ID, userId); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); }); } catch (DataAccessException e) { throw handleServerException(ERROR_CODE_GET_ROLES_BY_USER_ID, e, userId); @@ -375,22 +388,23 @@ public List getApplicationRolesByUserId(String userId) throws A } @Override - public List getApplicationRolesByGroupId(String groupId) + public List getApplicationRolesByGroupId(String groupId, String tenantDomain) throws ApplicationRoleManagementException { - NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); - try { - return namedJdbcTemplate.executeQuery(SQLConstants.GET_APPLICATION_ROLES_BY_GROUP_ID, - (resultSet, rowNumber) -> - new ApplicationRole(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_ID), - resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME), - resultSet.getString(DB_SCHEMA_COLUMN_NAME_APP_ID)), - namedPreparedStatement -> { - namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); - }); - } catch (DataAccessException e) { - throw handleServerException(ERROR_CODE_GET_ROLES_BY_GROUP_ID, e, groupId); - } + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + return namedJdbcTemplate.executeQuery(SQLConstants.GET_APPLICATION_ROLES_BY_GROUP_ID, + (resultSet, rowNumber) -> + new ApplicationRole(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_ID), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_APP_ID)), + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_GROUP_ID, groupId); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); + }); + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_GET_ROLES_BY_GROUP_ID, e, groupId); + } } public void validateGroupIds(IdentityProvider identityProvider, List groups, String tenantDomain) @@ -400,8 +414,7 @@ public void validateGroupIds(IdentityProvider identityProvider, List gro for (String groupId : groups) { boolean isExists = groupIDResolver.isExists(groupId, tenantDomain); if (!isExists) { - throw new ApplicationRoleManagementClientException("Given groupId is not found", - "Given groupId is not found", ""); + throw ApplicationRoleMgtUtils.handleClientException(ERROR_CODE_GROUP_NOT_FOUND, groupId); } } } else { @@ -412,8 +425,7 @@ public void validateGroupIds(IdentityProvider identityProvider, List gro } for (String groupId : groups) { if (!idToNameMap.containsKey(groupId)) { - throw new ApplicationRoleManagementClientException("Given groupId is not found", - "Given groupId is not found", ""); + throw ApplicationRoleMgtUtils.handleClientException(ERROR_CODE_GROUP_NOT_FOUND, groupId); } } } @@ -425,8 +437,7 @@ public void validateUserIds(List users, String tenantDomain) for (String userId : users) { boolean isExists = userIDResolver.isExists(userId, tenantDomain); if (!isExists) { - throw new ApplicationRoleManagementClientException("Given user Id is not found", - "Given user Id is not found", ""); + throw ApplicationRoleMgtUtils.handleClientException(ERROR_CODE_USER_NOT_FOUND, userId); } } } @@ -443,4 +454,22 @@ private String getGroupNamesByID(String groupID, String tenantDomain) return groupIDResolver.getNameByID(groupID, tenantDomain); } + private boolean checkUniqueKeyConstrainViolated(TransactionException e) { + + return e.getCause().getCause().getMessage().toLowerCase(). + contains(SQLConstants.GROUP_ROLE_UNIQUE_CONSTRAINT.toLowerCase()); + } + + private int getTenantId(String tenantDomain) { + + int tenantID; + if (tenantDomain != null) { + tenantID = IdentityTenantUtil.getTenantId(tenantDomain); + } else { + tenantID = MultitenantConstants.INVALID_TENANT_ID; + } + return tenantID; + } + + } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java index 1e3ea42a9c83..276a260389e2 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java @@ -77,11 +77,12 @@ public List getApplicationRoles(String applicationId) } @Override - public void updateApplicationRole(String applicationId, String roleId, String tenantDomain) + public void updateApplicationRole(String roleId, String newName, List addedScopes, + List removedScopes, String tenantDomain) throws ApplicationRoleManagementServerException { clearFromCache(roleId, tenantDomain); - applicationRoleMgtDAO.updateApplicationRole(applicationId, roleId, tenantDomain); + applicationRoleMgtDAO.updateApplicationRole(roleId, newName, addedScopes, removedScopes, tenantDomain); } @Override @@ -100,6 +101,12 @@ public boolean isExistingRole(String applicationId, String roleName, String tena return applicationRoleMgtDAO.isExistingRole(applicationId, roleName, tenantDomain); } + @Override + public boolean checkRoleExists(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException { + + return applicationRoleMgtDAO.checkRoleExists(roleId, tenantDomain); + } + @Override public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, String tenantDomain) @@ -141,17 +148,17 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityP } @Override - public List getApplicationRolesByUserId(String userId) + public List getApplicationRolesByUserId(String userId, String tenantDomain) throws ApplicationRoleManagementException { - return applicationRoleMgtDAO.getApplicationRolesByUserId(userId); + return applicationRoleMgtDAO.getApplicationRolesByUserId(userId, tenantDomain); } @Override - public List getApplicationRolesByGroupId(String groupId) + public List getApplicationRolesByGroupId(String groupId, String tenantDomain) throws ApplicationRoleManagementException { - return applicationRoleMgtDAO.getApplicationRolesByGroupId(groupId); + return applicationRoleMgtDAO.getApplicationRolesByGroupId(groupId, tenantDomain); } private ApplicationRole getApplicationRoleFromCache(String applicationRoleId, String tenantDomain) { From 64093777907089baf9e71c5643e45e6424e4c8d4 Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Mon, 28 Aug 2023 09:01:03 +0530 Subject: [PATCH 06/21] add licenses --- .../application/role/mgt/model/Group.java | 18 ++++++++++++++++++ .../application/role/mgt/model/User.java | 18 ++++++++++++++++++ .../role/mgt/util/GroupIDResolver.java | 18 ++++++++++++++++++ .../application/role/mgt/util/IDResolver.java | 18 ++++++++++++++++++ .../role/mgt/util/UserIDResolver.java | 18 ++++++++++++++++++ 5 files changed, 90 insertions(+) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java index d116f282b734..13c3e5895266 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/Group.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.wso2.carbon.identity.application.role.mgt.model; /** diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/User.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/User.java index 8a43f73773cc..63d86bb6ef78 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/User.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/User.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.wso2.carbon.identity.application.role.mgt.model; /** diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java index 36876d9a7fd0..a6418b532849 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.wso2.carbon.identity.application.role.mgt.util; import org.wso2.carbon.context.PrivilegedCarbonContext; diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java index 1f4c6b303373..4fa78bd66d33 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.wso2.carbon.identity.application.role.mgt.util; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java index 6efb1b77070e..f33e645c290a 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.wso2.carbon.identity.application.role.mgt.util; import org.wso2.carbon.context.PrivilegedCarbonContext; From f8cccb8ea06c1ffddbc1d9307eb2c2f460070c34 Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Mon, 28 Aug 2023 13:29:00 +0530 Subject: [PATCH 07/21] fix idp null --- .../pom.xml | 7 ++++ .../role/mgt/ApplicationRoleManagerImpl.java | 7 ++++ .../ApplicationRoleMgtConstants.java | 2 + .../dao/impl/ApplicationRoleMgtDAOImpl.java | 12 +++--- .../resources/dbscripts/h2.sql | 37 +++++++++++++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml index 428cd3b93d28..6dd09e1c5741 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml @@ -110,6 +110,13 @@ + + org.codehaus.mojo + findbugs-maven-plugin + + High + + diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java index 85af5fb73506..4972f1ede653 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java @@ -33,6 +33,7 @@ import java.util.List; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_DUPLICATE_ROLE; +import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_IDP_NOT_FOUND; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_ROLE_NOT_FOUND; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.LOCAL_IDP; import static org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils.handleClientException; @@ -129,6 +130,9 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis identityProvider = ApplicationRoleMgtServiceComponentHolder.getInstance() .getIdentityProviderManager().getIdPByResourceId(idpId, getTenantDomain(), true); } + if (identityProvider == null) { + throw handleClientException(ERROR_CODE_IDP_NOT_FOUND, idpId); + } removeCommonValues(addedGroups, removedGroups); applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, identityProvider, addedGroups, removedGroups, getTenantDomain()); @@ -152,6 +156,9 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, String id identityProvider = ApplicationRoleMgtServiceComponentHolder.getInstance() .getIdentityProviderManager().getIdPByResourceId(idpId, getTenantDomain(), true); } + if (identityProvider == null) { + throw handleClientException(ERROR_CODE_IDP_NOT_FOUND, idpId); + } return applicationRoleMgtDAO.getApplicationRoleAssignedGroups(roleId, identityProvider, getTenantDomain()); } catch (IdentityProviderManagementException e) { throw new ApplicationRoleManagementException("Error while retrieving idp", "Error while retrieving idp " + diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java index a473a4ef6730..b326ca5369bb 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java @@ -72,6 +72,8 @@ public enum ErrorMessages { "User not found for the userId: %s."), ERROR_CODE_GROUP_NOT_FOUND("60006", "Unable to assign group to app role.", "Group not found for the groupId: %s."), + ERROR_CODE_IDP_NOT_FOUND("60002", "IDP doesn't exist.", + "IDP with id: %s doesn't exist."), ; private final String code; diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java index 0281eb8518e8..c795acc346a5 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -61,6 +61,7 @@ import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_USER_ALREADY_ASSIGNED; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.ErrorMessages.ERROR_CODE_USER_NOT_FOUND; import static org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants.LOCAL_IDP; +import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.GROUP_ROLE_UNIQUE_CONSTRAINT; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_APP_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_GROUP_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_IDP_ID; @@ -68,6 +69,7 @@ import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROLE_NAME; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TENANT_ID; import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_USER_ID; +import static org.wso2.carbon.identity.application.role.mgt.constants.SQLConstants.USER_ROLE_UNIQUE_CONSTRAINT; import static org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils.getNewTemplate; import static org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils.handleServerException; @@ -247,7 +249,7 @@ public void updateApplicationRoleAssignedUsers(String roleId, List added return null; }); } catch (TransactionException e) { - if (checkUniqueKeyConstrainViolated(e)) { + if (checkUniqueKeyConstrainViolated(e, USER_ROLE_UNIQUE_CONSTRAINT)) { throw ApplicationRoleMgtUtils.handleClientException(ERROR_CODE_USER_ALREADY_ASSIGNED, roleId); } throw handleServerException(ERROR_CODE_UPDATE_ROLE_ASSIGNED_USERS, e, roleId); @@ -317,7 +319,7 @@ public void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider return null; }); } catch (TransactionException e) { - if (checkUniqueKeyConstrainViolated(e)) { + if (checkUniqueKeyConstrainViolated(e, GROUP_ROLE_UNIQUE_CONSTRAINT)) { throw ApplicationRoleMgtUtils.handleClientException(ERROR_CODE_GROUP_ALREADY_ASSIGNED, roleId); } throw handleServerException(ERROR_CODE_UPDATE_ROLE_ASSIGNED_GROUPS, e, roleId); @@ -454,10 +456,10 @@ private String getGroupNamesByID(String groupID, String tenantDomain) return groupIDResolver.getNameByID(groupID, tenantDomain); } - private boolean checkUniqueKeyConstrainViolated(TransactionException e) { + private boolean checkUniqueKeyConstrainViolated(TransactionException e, String constraint) { - return e.getCause().getCause().getMessage().toLowerCase(). - contains(SQLConstants.GROUP_ROLE_UNIQUE_CONSTRAINT.toLowerCase()); + String errorMessage = e.getCause().getCause().getMessage(); + return errorMessage.toLowerCase().contains(constraint.toLowerCase()); } private int getTenantId(String tenantDomain) { diff --git a/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/dbscripts/h2.sql b/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/dbscripts/h2.sql index cf7c5532f0b7..6c876edf3df1 100644 --- a/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/dbscripts/h2.sql +++ b/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/dbscripts/h2.sql @@ -1261,6 +1261,43 @@ CREATE TABLE IF NOT EXISTS IDN_OAUTH_PAR ( PARAMETERS MEDIUMTEXT ); +CREATE TABLE IF NOT EXISTS APP_ROLE ( + ROLE_ID varchar(255) NOT NULL PRIMARY KEY, + ROLE_NAME varchar(255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + APP_ID varchar(36) NOT NULL, + CURSOR_KEY SERIAL, + UNIQUE (ROLE_NAME, TENANT_ID, APP_ID), + FOREIGN KEY (APP_ID) REFERENCES SP_APP(UUID) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS SHARED_ROLE ( + SHARED_ROLE_ID varchar(255) NOT NULL, + MAIN_ROLE_ID varchar(255) NOT NULL, + UNIQUE (SHARED_ROLE_ID, MAIN_ROLE_ID), + PRIMARY KEY (SHARED_ROLE_ID, MAIN_ROLE_ID), + FOREIGN KEY (SHARED_ROLE_ID) REFERENCES APP_ROLE(ROLE_ID) ON DELETE CASCADE, + FOREIGN KEY (MAIN_ROLE_ID) REFERENCES APP_ROLE(ROLE_ID) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS GROUP_ROLE ( + ROLE_ID varchar(255) NOT NULL, + IDP_ID varchar(255) NOT NULL, + TENANT_ID varchar(255) NOT NULL, + GROUP_ID varchar(255) NOT NULL, + CURSOR_KEY SERIAL, + FOREIGN KEY (ROLE_ID) REFERENCES APP_ROLE(ROLE_ID) ON DELETE CASCADE, + FOREIGN KEY (IDP_ID) REFERENCES IDP(UUID) ON DELETE CASCADE, + CONSTRAINT GROUP_ROLE_UNIQUE UNIQUE (ROLE_ID, IDP_ID, GROUP_ID) +); + +CREATE TABLE IF NOT EXISTS USER_ROLE ( + ROLE_ID varchar(255) NOT NULL, + TENANT_ID varchar(255) NOT NULL, + USER_ID varchar(255) NOT NULL, + CURSOR_KEY SERIAL, + FOREIGN KEY (ROLE_ID) REFERENCES APP_ROLE(ROLE_ID) ON DELETE CASCADE, + CONSTRAINT USER_ROLE_UNIQUE UNIQUE (ROLE_ID, TENANT_ID, USER_ID) +); + -- --------------------------- INDEX CREATION ----------------------------- -- IDN_OAUTH2_ACCESS_TOKEN -- CREATE INDEX IDX_TC ON IDN_OAUTH2_ACCESS_TOKEN(TIME_CREATED); From 24cef0ddab4f3a939bba19c631a624946129f2f3 Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Mon, 28 Aug 2023 14:27:22 +0530 Subject: [PATCH 08/21] imporve error msg --- .../role/mgt/constants/ApplicationRoleMgtConstants.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java index b326ca5369bb..0428f4a7253e 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/constants/ApplicationRoleMgtConstants.java @@ -69,10 +69,10 @@ public enum ErrorMessages { ERROR_CODE_GROUP_ALREADY_ASSIGNED("60004", "Unable to assign group to app role.", "Group already assign for the roleId: %s."), ERROR_CODE_USER_NOT_FOUND("60005", "Unable to assign user to app role.", - "User not found for the userId: %s."), + "Group with id: %s doesn't exist"), ERROR_CODE_GROUP_NOT_FOUND("60006", "Unable to assign group to app role.", - "Group not found for the groupId: %s."), - ERROR_CODE_IDP_NOT_FOUND("60002", "IDP doesn't exist.", + "Group with id: %s doesn't exist"), + ERROR_CODE_IDP_NOT_FOUND("60007", "IDP doesn't exist.", "IDP with id: %s doesn't exist."), ; From 5fc3a85d3ab062770b0eabeaa911da5b1d8dc9e5 Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Tue, 29 Aug 2023 13:52:19 +0530 Subject: [PATCH 09/21] add return for patch app role --- .../role/mgt/ApplicationRoleManager.java | 5 +++-- .../role/mgt/ApplicationRoleManagerImpl.java | 10 ++++++---- .../role/mgt/dao/ApplicationRoleMgtDAO.java | 9 +++++---- .../dao/impl/ApplicationRoleMgtDAOImpl.java | 18 +++++++++--------- .../CacheBackedApplicationRoleMgtDAOImpl.java | 12 ++++++------ .../role/mgt/model/ApplicationRole.java | 4 ++++ 6 files changed, 33 insertions(+), 25 deletions(-) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java index 8fc47b2386bc..1fd7f1605eaf 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java @@ -83,7 +83,8 @@ void updateApplicationRole(String applicationId, String roleId, String newName, * @param removedUsers List of user IDs to be unassigned. * @throws ApplicationRoleManagementException Error occurred while updating the application role. */ - void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers) + ApplicationRole updateApplicationRoleAssignedUsers(String roleId, List addedUsers, + List removedUsers) throws ApplicationRoleManagementException; /** @@ -102,7 +103,7 @@ void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, * @param removedGroups List of group IDs to be unassigned. * @throws ApplicationRoleManagementException Error occurred while updating the application role. */ - void updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, + ApplicationRole updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, List removedGroups) throws ApplicationRoleManagementException; /** diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java index 4972f1ede653..e2def326878e 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java @@ -98,12 +98,14 @@ public void deleteApplicationRole(String roleId) throws ApplicationRoleManagemen } @Override - public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers) + public ApplicationRole updateApplicationRoleAssignedUsers(String roleId, List addedUsers, + List removedUsers) throws ApplicationRoleManagementException { validateAppRoleId(roleId); removeCommonValues(addedUsers, removedUsers); - applicationRoleMgtDAO.updateApplicationRoleAssignedUsers(roleId, addedUsers, removedUsers, getTenantDomain()); + return applicationRoleMgtDAO.updateApplicationRoleAssignedUsers(roleId, addedUsers, removedUsers, + getTenantDomain()); } @Override @@ -115,7 +117,7 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId) } @Override - public void updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, + public ApplicationRole updateApplicationRoleAssignedGroups(String roleId, String idpId, List addedGroups, List removedGroups) throws ApplicationRoleManagementException { @@ -134,7 +136,7 @@ public void updateApplicationRoleAssignedGroups(String roleId, String idpId, Lis throw handleClientException(ERROR_CODE_IDP_NOT_FOUND, idpId); } removeCommonValues(addedGroups, removedGroups); - applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, identityProvider, + return applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, identityProvider, addedGroups, removedGroups, getTenantDomain()); } catch (IdentityProviderManagementException e) { throw new ApplicationRoleManagementException("Error while retrieving idp", diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java index 580da77dc8c9..f5d58bf46997 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java @@ -49,15 +49,16 @@ boolean isExistingRole(String applicationId, String roleName, String tenantDomai boolean checkRoleExists(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException; - void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, - String tenantDomain) throws + ApplicationRole updateApplicationRoleAssignedUsers(String roleId, List addedUsers, + List removedUsers, String tenantDomain) throws ApplicationRoleManagementException; ApplicationRole getApplicationRoleAssignedUsers(String roleId, String tenantDomain) throws ApplicationRoleManagementException; - void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, List addedGroups, - List removedGroups, String tenantDomain) + ApplicationRole updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, + List addedGroups, List removedGroups, + String tenantDomain) throws ApplicationRoleManagementException; ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java index c795acc346a5..9314caba7eb0 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -219,8 +219,8 @@ public boolean checkRoleExists(String roleId, String tenantDomain) throws Applic } @Override - public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, - String tenantDomain) + public ApplicationRole updateApplicationRoleAssignedUsers(String roleId, List addedUsers, + List removedUsers, String tenantDomain) throws ApplicationRoleManagementException { // Validate given userIds are exists. @@ -228,7 +228,7 @@ public void updateApplicationRoleAssignedUsers(String roleId, List added validateUserIds(removedUsers, tenantDomain); NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { - namedJdbcTemplate.withTransaction(template -> { + return namedJdbcTemplate.withTransaction(template -> { namedJdbcTemplate.executeBatchInsert(SQLConstants.ADD_APPLICATION_ROLE_USER, (preparedStatement -> { for (String userId : addedUsers) { preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); @@ -246,7 +246,7 @@ public void updateApplicationRoleAssignedUsers(String roleId, List added getTenantId(tenantDomain)); }); } - return null; + return getApplicationRoleAssignedUsers(roleId, tenantDomain); }); } catch (TransactionException e) { if (checkUniqueKeyConstrainViolated(e, USER_ROLE_UNIQUE_CONSTRAINT)) { @@ -272,7 +272,7 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String ten for (User user : users) { user.setUserName(getUserNamesByID(user.getId(), tenantDomain)); } - ApplicationRole applicationRole = new ApplicationRole(roleId); + ApplicationRole applicationRole = new ApplicationRole(); applicationRole.setAssignedUsers(users); return applicationRole; } catch (DataAccessException e) { @@ -281,7 +281,7 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String ten } @Override - public void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, + public ApplicationRole updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, List addedGroups, List removedGroups, String tenantDomain) throws ApplicationRoleManagementException { @@ -290,7 +290,7 @@ public void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider validateGroupIds(identityProvider, removedGroups, tenantDomain); NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { - namedJdbcTemplate.withTransaction(template -> { + return namedJdbcTemplate.withTransaction(template -> { if (addedGroups.size() > 0) { namedJdbcTemplate.executeBatchInsert(SQLConstants.ADD_APPLICATION_ROLE_GROUP, (preparedStatement -> { @@ -316,7 +316,7 @@ public void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider }); } } - return null; + return getApplicationRoleAssignedGroups(roleId, identityProvider, tenantDomain); }); } catch (TransactionException e) { if (checkUniqueKeyConstrainViolated(e, GROUP_ROLE_UNIQUE_CONSTRAINT)) { @@ -361,7 +361,7 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityP } } } - ApplicationRole applicationRole = new ApplicationRole(roleId); + ApplicationRole applicationRole = new ApplicationRole(); applicationRole.setAssignedGroups(groups); return applicationRole; } catch (DataAccessException e) { diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java index 276a260389e2..fc1f63914a5a 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java @@ -108,11 +108,11 @@ public boolean checkRoleExists(String roleId, String tenantDomain) throws Applic } @Override - public void updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, - String tenantDomain) + public ApplicationRole updateApplicationRoleAssignedUsers(String roleId, List addedUsers, + List removedUsers, String tenantDomain) throws ApplicationRoleManagementException { - applicationRoleMgtDAO.updateApplicationRoleAssignedUsers(roleId, addedUsers, removedUsers, tenantDomain); + return applicationRoleMgtDAO.updateApplicationRoleAssignedUsers(roleId, addedUsers, removedUsers, tenantDomain); } @Override @@ -130,13 +130,13 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String ten } @Override - public void updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, + public ApplicationRole updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, List addedGroups, List removedGroups, String tenantDomain) throws ApplicationRoleManagementException { - applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, identityProvider, addedGroups, removedGroups, - tenantDomain); + return applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, identityProvider, addedGroups, + removedGroups, tenantDomain); } @Override diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java index 650378f522af..424a566cbd59 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/model/ApplicationRole.java @@ -60,6 +60,10 @@ public ApplicationRole(String roleName, String applicationId) { this.applicationId = applicationId; } + public ApplicationRole() { + + } + public ApplicationRole(String roleId) { this.roleId = roleId; From 7cedd732f2851ac8f024887f3e3fa55a47e6a39a Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Thu, 31 Aug 2023 12:53:06 +0530 Subject: [PATCH 10/21] initial unit tests --- .../pom.xml | 35 +- .../dao/impl/ApplicationRoleMgtDAOImpl.java | 35 +- .../mgt/util/ApplicationRoleMgtUtils.java | 78 + .../role/mgt/util/GroupIDResolver.java | 86 - .../application/role/mgt/util/IDResolver.java | 32 - .../role/mgt/util/UserIDResolver.java | 87 - .../role/mgt/ApplicationRoleManagerTest.java | 7 + .../impl/ApplicationRoleMgtDAOImplTest.java | 217 +++ .../src/test/resources/dbscripts/h2.sql | 1402 +++++++++++++++++ .../test/resources/repository.conf/carbon.xml | 686 ++++++++ .../repository.conf/identity/identity.xml | 996 ++++++++++++ .../src/test/resources/testng.xml | 28 + 12 files changed, 3459 insertions(+), 230 deletions(-) delete mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java delete mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java delete mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerTest.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/dbscripts/h2.sql create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/carbon.xml create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/identity/identity.xml create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/testng.xml diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml index 6dd09e1c5741..80bd924b84df 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml @@ -54,21 +54,46 @@ org.wso2.carbon.identity.framework org.wso2.carbon.identity.core - - org.testng - testng - test + org.wso2.carbon.identity.framework + org.wso2.carbon.idp.mgt + org.jacoco org.jacoco.agent runtime test + + org.testng + testng + test + + + com.h2database + h2 + test + + + org.powermock + powermock-api-mockito2 + test + + + org.powermock + powermock-module-testng + test + + + org.powermock + powermock-module-testng-common + test + org.wso2.carbon.identity.framework - org.wso2.carbon.idp.mgt + org.wso2.carbon.identity.testutil + test diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java index 9314caba7eb0..9ae78b615100 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -34,8 +34,6 @@ import org.wso2.carbon.identity.application.role.mgt.model.Group; import org.wso2.carbon.identity.application.role.mgt.model.User; import org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils; -import org.wso2.carbon.identity.application.role.mgt.util.GroupIDResolver; -import org.wso2.carbon.identity.application.role.mgt.util.UserIDResolver; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; @@ -79,8 +77,6 @@ public class ApplicationRoleMgtDAOImpl implements ApplicationRoleMgtDAO { private static final Log LOG = LogFactory.getLog(ApplicationRoleMgtDAOImpl.class); - private final UserIDResolver userIDResolver = new UserIDResolver(); - private final GroupIDResolver groupIDResolver = new GroupIDResolver(); @Override public ApplicationRole addApplicationRole(ApplicationRole applicationRole, String tenantDomain) @@ -88,20 +84,18 @@ public ApplicationRole addApplicationRole(ApplicationRole applicationRole, Strin NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { - namedJdbcTemplate.withTransaction(template -> { - template.executeInsert(SQLConstants.ADD_APPLICATION_ROLE, namedPreparedStatement -> { - namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, applicationRole.getRoleId()); - namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_APP_ID, applicationRole.getApplicationId()); - namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME, applicationRole.getRoleName()); - namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); - }, null, false); - return null; - }); + namedJdbcTemplate.withTransaction(template -> template.executeInsert(SQLConstants.ADD_APPLICATION_ROLE, + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, applicationRole.getRoleId()); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_APP_ID, applicationRole.getApplicationId()); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_NAME, applicationRole.getRoleName()); + namedPreparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, getTenantId(tenantDomain)); + }, null, false)); + return getApplicationRoleById(applicationRole.getRoleId(), tenantDomain); } catch (TransactionException e) { throw handleServerException(ERROR_CODE_INSERT_ROLE, e, applicationRole.getRoleName(), applicationRole.getApplicationId()); } - return applicationRole; } @Override @@ -228,7 +222,7 @@ public ApplicationRole updateApplicationRoleAssignedUsers(String roleId, List { + namedJdbcTemplate.withTransaction(template -> { namedJdbcTemplate.executeBatchInsert(SQLConstants.ADD_APPLICATION_ROLE_USER, (preparedStatement -> { for (String userId : addedUsers) { preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROLE_ID, roleId); @@ -246,8 +240,9 @@ public ApplicationRole updateApplicationRoleAssignedUsers(String roleId, List gro if (LOCAL_IDP.equals(identityProvider.getIdentityProviderName())) { for (String groupId : groups) { - boolean isExists = groupIDResolver.isExists(groupId, tenantDomain); + boolean isExists = ApplicationRoleMgtUtils.isGroupExists(groupId); if (!isExists) { throw ApplicationRoleMgtUtils.handleClientException(ERROR_CODE_GROUP_NOT_FOUND, groupId); } @@ -437,7 +432,7 @@ public void validateUserIds(List users, String tenantDomain) throws ApplicationRoleManagementException { for (String userId : users) { - boolean isExists = userIDResolver.isExists(userId, tenantDomain); + boolean isExists = ApplicationRoleMgtUtils.isUserExists(userId); if (!isExists) { throw ApplicationRoleMgtUtils.handleClientException(ERROR_CODE_USER_NOT_FOUND, userId); } @@ -447,13 +442,13 @@ public void validateUserIds(List users, String tenantDomain) private String getUserNamesByID(String userID, String tenantDomain) throws ApplicationRoleManagementException { - return userIDResolver.getNameByID(userID, tenantDomain); + return ApplicationRoleMgtUtils.getUserNameByID(userID, tenantDomain); } private String getGroupNamesByID(String groupID, String tenantDomain) throws ApplicationRoleManagementException { - return groupIDResolver.getNameByID(groupID, tenantDomain); + return ApplicationRoleMgtUtils.getGroupNameByID(groupID, tenantDomain); } private boolean checkUniqueKeyConstrainViolated(TransactionException e, String constraint) { diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java index 98bfd2203113..f231e32b887c 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java @@ -19,11 +19,18 @@ package org.wso2.carbon.identity.application.role.mgt.util; import org.apache.commons.lang.ArrayUtils; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; import org.wso2.carbon.identity.application.role.mgt.constants.ApplicationRoleMgtConstants; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementClientException; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; +import org.wso2.carbon.identity.application.role.mgt.internal.ApplicationRoleMgtServiceComponentHolder; import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; +import org.wso2.carbon.user.api.UserRealm; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.user.core.common.AbstractUserStoreManager; +import org.wso2.carbon.user.core.service.RealmService; /** * Application role management util. @@ -74,4 +81,75 @@ public static ApplicationRoleManagementClientException handleClientException( } return new ApplicationRoleManagementClientException(error.getMessage(), description, error.getCode()); } + + public static boolean isGroupExists(String id) throws ApplicationRoleManagementException { + + AbstractUserStoreManager userStoreManager; + try { + userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + return userStoreManager.isGroupExist(id); + } catch (UserStoreException e) { + throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID", "Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID: " + id, e); + } + } + public static String getGroupNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException { + + String groupName; + AbstractUserStoreManager userStoreManager; + try { + userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + groupName = userStoreManager.getGroupNameByGroupId(id); + } catch (UserStoreException e) { + throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID", "Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID: " + id, e); + } + if (groupName == null) { + String errorMessage = "A group doesn't exist with id: " + id + " in the tenantDomain: " + tenantDomain; + throw new ApplicationRoleManagementClientException(errorMessage, errorMessage, ""); + } + return groupName; + } + + public static boolean isUserExists(String id) throws ApplicationRoleManagementException { + + AbstractUserStoreManager userStoreManager; + try { + userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + return userStoreManager.isExistingUserWithID(id); + } catch (UserStoreException e) { + throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID", "Error occurred while retrieving the userstore manager " + + "to resolve group name for the groupID: " + id, e); + } + } + + public static String getUserNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException { + + String userName; + AbstractUserStoreManager userStoreManager; + try { + userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + userName = userStoreManager.getUserNameFromUserID(id); + } catch (UserStoreException e) { + throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " + + "to resolve username for the groupID", "Error occurred while retrieving the userstore manager " + + "to resolve username for the groupID: " + id, e); + } + if (userName == null) { + String errorMessage = "A user doesn't exist with id: " + id + " in the tenantDomain: " + tenantDomain; + throw new ApplicationRoleManagementClientException(errorMessage, errorMessage, ""); + } + return userName; + } + + private static AbstractUserStoreManager getUserStoreManager(int tenantId) throws UserStoreException { + + RealmService realmService = ApplicationRoleMgtServiceComponentHolder.getInstance().getRealmService(); + UserRealm tenantUserRealm = realmService.getTenantUserRealm(tenantId); + + return (AbstractUserStoreManager) tenantUserRealm.getUserStoreManager(); + } } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java deleted file mode 100644 index a6418b532849..000000000000 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/GroupIDResolver.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.identity.application.role.mgt.util; - -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementClientException; -import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; -import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; -import org.wso2.carbon.identity.application.role.mgt.internal.ApplicationRoleMgtServiceComponentHolder; -import org.wso2.carbon.user.api.UserRealm; -import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.user.core.common.AbstractUserStoreManager; -import org.wso2.carbon.user.core.service.RealmService; - -/** - * GroupId Resolver. - */ -public class GroupIDResolver implements IDResolver { - - @Override - public String getNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException { - - String groupName = resolveGroupNameFromGroupID(id); - if (groupName == null) { - String errorMessage = "A group doesn't exist with id: " + id + " in the tenantDomain: " + tenantDomain; - throw new ApplicationRoleManagementClientException(errorMessage, errorMessage, ""); - } - return groupName; - } - - @Override - public boolean isExists(String id, String tenantDomain) throws ApplicationRoleManagementException { - - return isGroupExists(id); - } - - private boolean isGroupExists(String id) throws ApplicationRoleManagementException { - - AbstractUserStoreManager userStoreManager; - try { - userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); - return userStoreManager.isGroupExist(id); - } catch (UserStoreException e) { - throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " - + "to resolve group name for the groupID", "Error occurred while retrieving the userstore manager " - + "to resolve group name for the groupID: " + id, e); - } - } - - private String resolveGroupNameFromGroupID(String id) throws ApplicationRoleManagementException { - - AbstractUserStoreManager userStoreManager; - try { - userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); - return userStoreManager.getGroupNameByGroupId(id); - } catch (UserStoreException e) { - throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " - + "to resolve group name for the groupID", "Error occurred while retrieving the userstore manager " - + "to resolve group name for the groupID: " + id, e); - } - } - - private AbstractUserStoreManager getUserStoreManager(int tenantId) throws UserStoreException { - - RealmService realmService = ApplicationRoleMgtServiceComponentHolder.getInstance().getRealmService(); - UserRealm tenantUserRealm = realmService.getTenantUserRealm(tenantId); - - return (AbstractUserStoreManager) tenantUserRealm.getUserStoreManager(); - } -} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java deleted file mode 100644 index 4fa78bd66d33..000000000000 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/IDResolver.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.identity.application.role.mgt.util; - -import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; - -/** - * Id Resolver. - */ -public interface IDResolver { - - String getNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException; - - boolean isExists(String id, String tenantDomain) throws ApplicationRoleManagementException; - -} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java deleted file mode 100644 index f33e645c290a..000000000000 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/UserIDResolver.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.identity.application.role.mgt.util; - -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementClientException; -import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; -import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; -import org.wso2.carbon.identity.application.role.mgt.internal.ApplicationRoleMgtServiceComponentHolder; -import org.wso2.carbon.user.api.UserRealm; -import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.user.core.common.AbstractUserStoreManager; -import org.wso2.carbon.user.core.service.RealmService; - -/** - * UserId Resolver. - */ -public class UserIDResolver implements IDResolver { - - @Override - public String getNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException { - - String userName = resolveUserNameFromUserID(id); - if (userName == null) { - String errorMessage = "A user doesn't exist with id: " + id + " in the tenantDomain: " + tenantDomain; - throw new ApplicationRoleManagementClientException(errorMessage, errorMessage, ""); - } - return userName; - } - - @Override - public boolean isExists(String id, String tenantDomain) throws ApplicationRoleManagementException { - - return isGroupExists(id); - } - - private boolean isGroupExists(String id) throws ApplicationRoleManagementException { - - AbstractUserStoreManager userStoreManager; - try { - userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); - return userStoreManager.isExistingUserWithID(id); - } catch (UserStoreException e) { - throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " - + "to resolve group name for the groupID", "Error occurred while retrieving the userstore manager " - + "to resolve group name for the groupID: " + id, e); - } - } - - public String resolveUserNameFromUserID(String id) throws ApplicationRoleManagementException { - - AbstractUserStoreManager userStoreManager; - try { - userStoreManager = getUserStoreManager(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); - return userStoreManager.getUserNameFromUserID(id); - } catch (UserStoreException e) { - throw new ApplicationRoleManagementServerException("Error occurred while retrieving the userstore manager " - + "to resolve username for the groupID", "Error occurred while retrieving the userstore manager " - + "to resolve username for the groupID: " + id, e); - } - } - - private AbstractUserStoreManager getUserStoreManager(int tenantId) throws UserStoreException { - - RealmService realmService = ApplicationRoleMgtServiceComponentHolder.getInstance().getRealmService(); - UserRealm tenantUserRealm = realmService.getTenantUserRealm(tenantId); - - return (AbstractUserStoreManager) tenantUserRealm.getUserStoreManager(); - } - -} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerTest.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerTest.java new file mode 100644 index 000000000000..084365c5813a --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerTest.java @@ -0,0 +1,7 @@ +package org.wso2.carbon.identity.application.role.mgt; + +import org.powermock.modules.testng.PowerMockTestCase; + +public class ApplicationRoleManagerTest extends PowerMockTestCase { + +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java new file mode 100644 index 000000000000..e6ed6fba43e2 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java @@ -0,0 +1,217 @@ +package org.wso2.carbon.identity.application.role.mgt.dao.impl; + +import org.apache.commons.dbcp.BasicDataSource; +import org.apache.commons.lang.StringUtils; +import org.mockito.Mockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; +import org.wso2.carbon.identity.application.common.model.IdPGroup; +import org.wso2.carbon.identity.application.common.model.IdentityProvider; +import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; +import org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils; +import org.wso2.carbon.identity.core.persistence.JDBCPersistenceManager; +import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; +import org.wso2.carbon.identity.core.util.IdentityTenantUtil; + +import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.sql.DataSource; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + +@PrepareForTest({DataSource.class, JDBCPersistenceManager.class, IdentityTenantUtil.class, + ApplicationRoleMgtUtils.class, IdentityDatabaseUtil.class}) +public class ApplicationRoleMgtDAOImplTest extends PowerMockTestCase { + + private static final String DB_NAME = "application_role_mgt_dao_db"; + private static final String TENANT_DOMAIN = "TEST_TENANT_DOMAIN"; + private static final int TENANT_ID = 2; + private static final int APP_ID = 1; + private static final String APP_NAME = "TEST_APP_NAME"; + private static final String USER_STORE = "TEST_USER_STORE"; + private static final String USERNAME = "TEST_USERNAME"; + private static final String AUTH_TYPE = "TEST_AUTH_TYPE"; + private static final String ROLE_ID = "TEST_ROLE_ID"; + private static final String ROLE_NAME = "TEST_ROLE_NAME"; + private static final int IDP_ID = 1; + private static final String IDP_NAME = "TEST_IDP_NAME"; + + private static Map dataSourceMap = new HashMap<>(); + private ApplicationRoleMgtDAOImpl daoImpl; + Connection connection = null; + + @BeforeClass + public void setUp() throws Exception { + + daoImpl = new ApplicationRoleMgtDAOImpl(); + initiateH2Database(getFilePath()); + populateApplication(); + populateIdp(); + } + + @AfterClass + public void tearDown() throws Exception { + + closeH2Database(); + } + + @Test + public void testAddApplicationRole() throws Exception { + + mockStatic(IdentityTenantUtil.class); + when(IdentityTenantUtil.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID); + mockStatic(IdentityDatabaseUtil.class); + Mockito.when(IdentityDatabaseUtil.getDataSource()).thenReturn(dataSourceMap.get(DB_NAME)); + ApplicationRole applicationRole = new ApplicationRole(); + applicationRole.setApplicationId(String.valueOf(APP_ID)); + applicationRole.setRoleId(ROLE_ID); + applicationRole.setRoleName(ROLE_NAME); + ApplicationRole addedApplicationRole = daoImpl.addApplicationRole(applicationRole, TENANT_DOMAIN); + Assert.assertNotNull(addedApplicationRole); + } + + @DataProvider + public Object[][] updateApplicationRoleAssignedUsersData() { + return new Object[][]{ + {new ArrayList<>(Arrays.asList("USER_1", "USER_2", "USER_3")), + new ArrayList<>(Collections.emptyList()), + }, + + }; + } + + @Test(dataProvider = "updateApplicationRoleAssignedUsersData", priority = 2) + public void testUpdateApplicationRoleAssignedUsers(List addedUsers, List removedUsers) + throws Exception { + + mockStatic(ApplicationRoleMgtUtils.class); + mockStatic(IdentityTenantUtil.class); + when(IdentityTenantUtil.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID); + when(ApplicationRoleMgtUtils.getNewTemplate()).thenReturn(new NamedJdbcTemplate(dataSourceMap.get(DB_NAME))); + when(ApplicationRoleMgtUtils.isUserExists(anyString())).thenReturn(true); + ApplicationRole role = + daoImpl.updateApplicationRoleAssignedUsers(ROLE_ID, addedUsers, removedUsers, TENANT_DOMAIN); + Assert.assertEquals(role.getAssignedUsers().size(), addedUsers.size()); + } + + @DataProvider + public Object[][] updateApplicationRoleAssignedGroupsData() { + return new Object[][]{ + {new ArrayList<>(Arrays.asList("GROUP_1", "GROUP_2", "GROUP_3")), + new ArrayList<>(Collections.emptyList()), + }, + + }; + } + + @Test(dataProvider = "updateApplicationRoleAssignedGroupsData", priority = 2) + public void testUpdateApplicationRoleAssignedGroups(List addedGroups, List removedGroups) + throws Exception { + + mockStatic(ApplicationRoleMgtUtils.class); + mockStatic(IdentityTenantUtil.class); + when(IdentityTenantUtil.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID); + when(ApplicationRoleMgtUtils.getNewTemplate()).thenReturn(new NamedJdbcTemplate(dataSourceMap.get(DB_NAME))); + when(ApplicationRoleMgtUtils.isGroupExists(anyString())).thenReturn(true); + IdentityProvider identityProvider = new IdentityProvider(); + identityProvider.setResourceId(String.valueOf(IDP_ID)); + List idPGroups = new ArrayList<>(); + for (String group: addedGroups) { + IdPGroup idPGroup = new IdPGroup(); + idPGroup.setIdpGroupId(group); + idPGroup.setIdpGroupName(group); + idPGroups.add(idPGroup); + } + identityProvider.setIdPGroupConfig(idPGroups.toArray(new IdPGroup[0])); + ApplicationRole role = + daoImpl.updateApplicationRoleAssignedGroups(ROLE_ID, identityProvider, addedGroups, removedGroups, + TENANT_DOMAIN); + Assert.assertEquals(role.getAssignedGroups().size(), addedGroups.size()); + } + + private void populateApplication() throws Exception { + + String domainDataSQL = "INSERT INTO SP_APP (ID, TENANT_ID, APP_NAME, USER_STORE, USERNAME, AUTH_TYPE, UUID) " + + "VALUES " + "(" + APP_ID + "," + TENANT_ID + ",'" + APP_NAME + "','" + USER_STORE + "','" + USERNAME + + "','" + AUTH_TYPE + "','" + APP_ID + "')"; + + try { + connection.createStatement().executeUpdate(domainDataSQL); + } catch (SQLException e) { + String errorMessage = "Error while Adding test data for SP_APP table"; + throw new Exception(errorMessage, e); + } + } + + private void populateIdp() throws Exception { + + String domainDataSQL = "INSERT INTO IDP (ID, TENANT_ID, NAME, UUID) " + + "VALUES " + "(" + IDP_ID + "," + TENANT_ID + ",'" + IDP_NAME + "','" + IDP_ID + "')"; + + try { + connection.createStatement().executeUpdate(domainDataSQL); + } catch (SQLException e) { + String errorMessage = "Error while Adding test data for IDP table"; + throw new Exception(errorMessage, e); + } + } + + /** + * Initiate H2 database. + * + * @param scriptPath Path to the database script. + * @throws Exception Error when initiating H2 database. + */ + private void initiateH2Database(String scriptPath) throws Exception { + + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName("org.h2.Driver"); + dataSource.setUsername("username"); + dataSource.setPassword("password"); + dataSource.setUrl("jdbc:h2:mem:test" + DB_NAME); + dataSource.setTestOnBorrow(true); + dataSource.setValidationQuery("select 1"); + connection = dataSource.getConnection(); + connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + scriptPath + "'"); + dataSourceMap.put(DB_NAME, dataSource); + } + + + /** + * Close H2 database. + * + * @throws Exception Error when closing H2 database. + */ + public static void closeH2Database() throws Exception { + + BasicDataSource dataSource = dataSourceMap.get(DB_NAME); + if (dataSource != null) { + dataSource.close(); + } + } + + private static String getFilePath() { + + if (StringUtils.isNotBlank("h2.sql")) { + return Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "dbscripts", "h2.sql") + .toString(); + } + throw new IllegalArgumentException("DB Script file name cannot be empty."); + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/dbscripts/h2.sql b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/dbscripts/h2.sql new file mode 100644 index 000000000000..e8e1f919401b --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/dbscripts/h2.sql @@ -0,0 +1,1402 @@ +CREATE TABLE IF NOT EXISTS IDN_BASE_TABLE ( + PRODUCT_NAME VARCHAR (20), + PRIMARY KEY (PRODUCT_NAME) +); + +INSERT INTO IDN_BASE_TABLE values ('WSO2 Identity Server'); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH_CONSUMER_APPS ( + ID INTEGER NOT NULL AUTO_INCREMENT, + CONSUMER_KEY VARCHAR (255), + CONSUMER_SECRET VARCHAR (2048), + USERNAME VARCHAR (255), + TENANT_ID INTEGER DEFAULT 0, + USER_DOMAIN VARCHAR(50), + APP_NAME VARCHAR (255), + OAUTH_VERSION VARCHAR (128), + CALLBACK_URL VARCHAR (2048), + GRANT_TYPES VARCHAR (1024), + PKCE_MANDATORY CHAR(1) DEFAULT '0', + PKCE_SUPPORT_PLAIN CHAR(1) DEFAULT '0', + APP_STATE VARCHAR (25) DEFAULT 'ACTIVE', + USER_ACCESS_TOKEN_EXPIRE_TIME BIGINT DEFAULT 3600, + APP_ACCESS_TOKEN_EXPIRE_TIME BIGINT DEFAULT 3600, + REFRESH_TOKEN_EXPIRE_TIME BIGINT DEFAULT 84600, + ID_TOKEN_EXPIRE_TIME BIGINT DEFAULT 3600, + CONSTRAINT CONSUMER_KEY_CONSTRAINT UNIQUE (CONSUMER_KEY), + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_SCOPE_VALIDATORS ( + APP_ID INTEGER NOT NULL, + SCOPE_VALIDATOR VARCHAR (128) NOT NULL, + PRIMARY KEY (APP_ID,SCOPE_VALIDATOR), + FOREIGN KEY (APP_ID) REFERENCES IDN_OAUTH_CONSUMER_APPS(ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH1A_REQUEST_TOKEN ( + REQUEST_TOKEN VARCHAR (512), + REQUEST_TOKEN_SECRET VARCHAR (512), + CONSUMER_KEY_ID INTEGER, + CALLBACK_URL VARCHAR (2048), + SCOPE VARCHAR(2048), + AUTHORIZED VARCHAR (128), + OAUTH_VERIFIER VARCHAR (512), + AUTHZ_USER VARCHAR (512), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (REQUEST_TOKEN), + FOREIGN KEY (CONSUMER_KEY_ID) REFERENCES IDN_OAUTH_CONSUMER_APPS(ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH1A_ACCESS_TOKEN ( + ACCESS_TOKEN VARCHAR (512), + ACCESS_TOKEN_SECRET VARCHAR (512), + CONSUMER_KEY_ID INTEGER, + SCOPE VARCHAR(2048), + AUTHZ_USER VARCHAR (512), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (ACCESS_TOKEN), + FOREIGN KEY (CONSUMER_KEY_ID) REFERENCES IDN_OAUTH_CONSUMER_APPS(ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_ACCESS_TOKEN ( + TOKEN_ID VARCHAR (255), + ACCESS_TOKEN VARCHAR (2048), + REFRESH_TOKEN VARCHAR (2048), + CONSUMER_KEY_ID INTEGER, + AUTHZ_USER VARCHAR (100), + TENANT_ID INTEGER, + USER_DOMAIN VARCHAR(50), + USER_TYPE VARCHAR (25), + GRANT_TYPE VARCHAR (50), + TIME_CREATED TIMESTAMP DEFAULT 0, + REFRESH_TOKEN_TIME_CREATED TIMESTAMP DEFAULT 0, + VALIDITY_PERIOD BIGINT, + REFRESH_TOKEN_VALIDITY_PERIOD BIGINT, + TOKEN_SCOPE_HASH VARCHAR (32), + TOKEN_STATE VARCHAR (25) DEFAULT 'ACTIVE', + TOKEN_STATE_ID VARCHAR (128) DEFAULT 'NONE', + SUBJECT_IDENTIFIER VARCHAR(255), + ACCESS_TOKEN_HASH VARCHAR (512), + REFRESH_TOKEN_HASH VARCHAR (512), + IDP_ID INTEGER DEFAULT -1 NOT NULL, + TOKEN_BINDING_REF VARCHAR (32) DEFAULT 'NONE', + CONSENTED_TOKEN VARCHAR(6), + PRIMARY KEY (TOKEN_ID), + FOREIGN KEY (CONSUMER_KEY_ID) REFERENCES IDN_OAUTH_CONSUMER_APPS(ID) ON DELETE CASCADE, + CONSTRAINT CON_APP_KEY UNIQUE (CONSUMER_KEY_ID,AUTHZ_USER,TENANT_ID,USER_DOMAIN,USER_TYPE,TOKEN_SCOPE_HASH, + TOKEN_STATE,TOKEN_STATE_ID,IDP_ID,TOKEN_BINDING_REF) +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_TOKEN_BINDING ( + TOKEN_ID VARCHAR (255), + TOKEN_BINDING_TYPE VARCHAR (32), + TOKEN_BINDING_REF VARCHAR (32), + TOKEN_BINDING_VALUE VARCHAR (1024), + TENANT_ID INTEGER DEFAULT -1, + UNIQUE (TOKEN_ID,TOKEN_BINDING_TYPE,TOKEN_BINDING_VALUE), + FOREIGN KEY (TOKEN_ID) REFERENCES IDN_OAUTH2_ACCESS_TOKEN(TOKEN_ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_ACCESS_TOKEN_AUDIT ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TOKEN_ID VARCHAR (255), + ACCESS_TOKEN VARCHAR(2048), + REFRESH_TOKEN VARCHAR(2048), + CONSUMER_KEY_ID INTEGER, + AUTHZ_USER VARCHAR (100), + TENANT_ID INTEGER, + USER_DOMAIN VARCHAR(50), + USER_TYPE VARCHAR (25), + GRANT_TYPE VARCHAR (50), + TIME_CREATED TIMESTAMP NULL, + REFRESH_TOKEN_TIME_CREATED TIMESTAMP NULL, + VALIDITY_PERIOD BIGINT, + REFRESH_TOKEN_VALIDITY_PERIOD BIGINT, + TOKEN_SCOPE_HASH VARCHAR(32), + TOKEN_STATE VARCHAR(25), + TOKEN_STATE_ID VARCHAR (128) , + SUBJECT_IDENTIFIER VARCHAR(255), + ACCESS_TOKEN_HASH VARCHAR(512), + REFRESH_TOKEN_HASH VARCHAR(512), + INVALIDATED_TIME TIMESTAMP NULL, + IDP_ID INTEGER DEFAULT -1 NOT NULL, + PRIMARY KEY(ID) +); + + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_AUTHORIZATION_CODE ( + CODE_ID VARCHAR (255), + AUTHORIZATION_CODE VARCHAR (2048), + CONSUMER_KEY_ID INTEGER, + CALLBACK_URL VARCHAR (2048), + SCOPE VARCHAR(2048), + AUTHZ_USER VARCHAR (100), + TENANT_ID INTEGER, + USER_DOMAIN VARCHAR(50), + TIME_CREATED TIMESTAMP, + VALIDITY_PERIOD BIGINT, + STATE VARCHAR (25) DEFAULT 'ACTIVE', + TOKEN_ID VARCHAR(255), + SUBJECT_IDENTIFIER VARCHAR(255), + PKCE_CODE_CHALLENGE VARCHAR (255), + PKCE_CODE_CHALLENGE_METHOD VARCHAR(128), + AUTHORIZATION_CODE_HASH VARCHAR (512), + IDP_ID INTEGER DEFAULT -1 NOT NULL, + PRIMARY KEY (CODE_ID), + FOREIGN KEY (CONSUMER_KEY_ID) REFERENCES IDN_OAUTH_CONSUMER_APPS(ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_AUTHZ_CODE_SCOPE( + CODE_ID VARCHAR(255), + SCOPE VARCHAR(255), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (CODE_ID, SCOPE), + FOREIGN KEY (CODE_ID) REFERENCES IDN_OAUTH2_AUTHORIZATION_CODE (CODE_ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_DEVICE_FLOW ( + CODE_ID VARCHAR(255), + DEVICE_CODE VARCHAR(255), + USER_CODE VARCHAR(25), + QUANTIFIER INTEGER NOT NULL DEFAULT 0, + CONSUMER_KEY_ID INTEGER, + LAST_POLL_TIME TIMESTAMP NOT NULL, + EXPIRY_TIME TIMESTAMP NOT NULL, + TIME_CREATED TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + POLL_TIME BIGINT, + STATUS VARCHAR (25) DEFAULT 'PENDING', + AUTHZ_USER VARCHAR (100), + TENANT_ID INTEGER, + USER_DOMAIN VARCHAR(50), + IDP_ID INTEGER, + PRIMARY KEY (DEVICE_CODE), + UNIQUE (CODE_ID), + CONSTRAINT USRCDE_QNTFR_CONSTRAINT UNIQUE (USER_CODE, QUANTIFIER), + FOREIGN KEY (CONSUMER_KEY_ID) REFERENCES IDN_OAUTH_CONSUMER_APPS(ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_DEVICE_FLOW_SCOPES ( + ID INTEGER NOT NULL AUTO_INCREMENT, + SCOPE_ID VARCHAR(255), + SCOPE VARCHAR(255), + PRIMARY KEY (ID), + FOREIGN KEY (SCOPE_ID) REFERENCES IDN_OAUTH2_DEVICE_FLOW(CODE_ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_ACCESS_TOKEN_SCOPE ( + TOKEN_ID VARCHAR (255), + TOKEN_SCOPE VARCHAR (255), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (TOKEN_ID, TOKEN_SCOPE), + FOREIGN KEY (TOKEN_ID) REFERENCES IDN_OAUTH2_ACCESS_TOKEN(TOKEN_ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_SCOPE ( + SCOPE_ID INTEGER NOT NULL AUTO_INCREMENT, + NAME VARCHAR(255) NOT NULL, + DISPLAY_NAME VARCHAR(255) NOT NULL, + DESCRIPTION VARCHAR(512), + TENANT_ID INTEGER NOT NULL DEFAULT -1, + SCOPE_TYPE VARCHAR(255) NOT NULL, + PRIMARY KEY (SCOPE_ID), + UNIQUE (NAME, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_SCOPE_BINDING ( + ID INTEGER NOT NULL AUTO_INCREMENT, + SCOPE_ID INTEGER NOT NULL, + SCOPE_BINDING VARCHAR(255) NOT NULL, + BINDING_TYPE VARCHAR(255) NOT NULL, + FOREIGN KEY (SCOPE_ID) REFERENCES IDN_OAUTH2_SCOPE(SCOPE_ID) ON DELETE CASCADE, + UNIQUE (SCOPE_ID, SCOPE_BINDING, BINDING_TYPE), + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_RESOURCE_SCOPE ( + RESOURCE_PATH VARCHAR(255) NOT NULL, + SCOPE_ID INTEGER NOT NULL, + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (RESOURCE_PATH), + FOREIGN KEY (SCOPE_ID) REFERENCES IDN_OAUTH2_SCOPE (SCOPE_ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_SCIM_GROUP ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + ROLE_NAME VARCHAR(255) NOT NULL, + ATTR_NAME VARCHAR(1024) NOT NULL, + ATTR_VALUE VARCHAR(1024), + UNIQUE(TENANT_ID, ROLE_NAME, ATTR_NAME), + PRIMARY KEY (ID) +); + + + +CREATE TABLE IF NOT EXISTS IDN_OPENID_REMEMBER_ME ( + USER_NAME VARCHAR(255) NOT NULL, + TENANT_ID INTEGER DEFAULT 0, + COOKIE_VALUE VARCHAR(1024), + CREATED_TIME TIMESTAMP, + PRIMARY KEY (USER_NAME, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_OPENID_USER_RPS ( + USER_NAME VARCHAR(255) NOT NULL, + TENANT_ID INTEGER DEFAULT 0, + RP_URL VARCHAR(255) NOT NULL, + TRUSTED_ALWAYS VARCHAR(128) DEFAULT 'FALSE', + LAST_VISIT DATE NOT NULL, + VISIT_COUNT INTEGER DEFAULT 0, + DEFAULT_PROFILE_NAME VARCHAR(255) DEFAULT 'DEFAULT', + PRIMARY KEY (USER_NAME, TENANT_ID, RP_URL) +); + +CREATE TABLE IF NOT EXISTS IDN_OPENID_ASSOCIATIONS ( + HANDLE VARCHAR(255) NOT NULL, + ASSOC_TYPE VARCHAR(255) NOT NULL, + EXPIRE_IN TIMESTAMP NOT NULL, + MAC_KEY VARCHAR(255) NOT NULL, + ASSOC_STORE VARCHAR(128) DEFAULT 'SHARED', + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (HANDLE) +); + +CREATE TABLE IDN_STS_STORE ( + ID INTEGER AUTO_INCREMENT, + TOKEN_ID VARCHAR(255) NOT NULL, + TOKEN_CONTENT BLOB(1024) NOT NULL, + CREATE_DATE TIMESTAMP NOT NULL, + EXPIRE_DATE TIMESTAMP NOT NULL, + STATE INTEGER DEFAULT 0, + PRIMARY KEY (ID) +); + +CREATE TABLE IDN_IDENTITY_USER_DATA ( + TENANT_ID INTEGER DEFAULT -1234, + USER_NAME VARCHAR(255) NOT NULL, + DATA_KEY VARCHAR(255) NOT NULL, + DATA_VALUE VARCHAR(2048), + PRIMARY KEY (TENANT_ID, USER_NAME, DATA_KEY) +); + +CREATE TABLE IDN_IDENTITY_META_DATA ( + USER_NAME VARCHAR(255) NOT NULL, + TENANT_ID INTEGER DEFAULT -1234, + METADATA_TYPE VARCHAR(255) NOT NULL, + METADATA VARCHAR(255) NOT NULL, + VALID VARCHAR(255) NOT NULL, + PRIMARY KEY (TENANT_ID, USER_NAME, METADATA_TYPE,METADATA) +); + +CREATE TABLE IF NOT EXISTS IDN_THRIFT_SESSION ( + SESSION_ID VARCHAR(255) NOT NULL, + USER_NAME VARCHAR(255) NOT NULL, + CREATED_TIME VARCHAR(255) NOT NULL, + LAST_MODIFIED_TIME VARCHAR(255) NOT NULL, + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (SESSION_ID) +); + +CREATE TABLE IDN_AUTH_SESSION_STORE ( + SESSION_ID VARCHAR (100) NOT NULL, + SESSION_TYPE VARCHAR(100) NOT NULL, + OPERATION VARCHAR(10) NOT NULL, + SESSION_OBJECT BLOB, + TIME_CREATED BIGINT, + TENANT_ID INTEGER DEFAULT -1, + EXPIRY_TIME BIGINT, + PRIMARY KEY (SESSION_ID, SESSION_TYPE, TIME_CREATED, OPERATION) +); + + +CREATE TABLE IDN_AUTH_TEMP_SESSION_STORE ( + SESSION_ID VARCHAR (100) NOT NULL, + SESSION_TYPE VARCHAR(100) NOT NULL, + OPERATION VARCHAR(10) NOT NULL, + SESSION_OBJECT BLOB, + TIME_CREATED BIGINT, + TENANT_ID INTEGER DEFAULT -1, + EXPIRY_TIME BIGINT, + PRIMARY KEY (SESSION_ID, SESSION_TYPE, TIME_CREATED, OPERATION) +); + +CREATE TABLE IF NOT EXISTS IDN_AUTH_USER ( + USER_ID VARCHAR(255) NOT NULL, + USER_NAME VARCHAR(255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + DOMAIN_NAME VARCHAR(255) NOT NULL, + IDP_ID INTEGER NOT NULL, + PRIMARY KEY (USER_ID), + CONSTRAINT USER_STORE_CONSTRAINT UNIQUE (USER_NAME, TENANT_ID, DOMAIN_NAME, IDP_ID)); + +CREATE TABLE IF NOT EXISTS IDN_AUTH_USER_SESSION_MAPPING ( + ID INTEGER NOT NULL AUTO_INCREMENT, + USER_ID VARCHAR(255) NOT NULL, + SESSION_ID VARCHAR(255) NOT NULL, + CONSTRAINT USER_SESSION_STORE_CONSTRAINT UNIQUE (USER_ID, SESSION_ID), + PRIMARY KEY (ID)); + +CREATE TABLE IF NOT EXISTS IDN_AUTH_SESSION_APP_INFO ( + SESSION_ID VARCHAR (100) NOT NULL, + SUBJECT VARCHAR (100) NOT NULL, + APP_ID INTEGER NOT NULL, + INBOUND_AUTH_TYPE VARCHAR (255) NOT NULL, + PRIMARY KEY (SESSION_ID, SUBJECT, APP_ID, INBOUND_AUTH_TYPE)); + +CREATE TABLE IF NOT EXISTS IDN_AUTH_SESSION_META_DATA ( + SESSION_ID VARCHAR (100) NOT NULL, + PROPERTY_TYPE VARCHAR (100) NOT NULL, + `VALUE` VARCHAR (255) NOT NULL, + PRIMARY KEY (SESSION_ID, PROPERTY_TYPE, `VALUE`) + ); + +CREATE TABLE IF NOT EXISTS SP_APP ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + APP_NAME VARCHAR (255) NOT NULL , + USER_STORE VARCHAR (255) NOT NULL, + USERNAME VARCHAR (255) NOT NULL , + DESCRIPTION VARCHAR (1024), + ROLE_CLAIM VARCHAR (512), + AUTH_TYPE VARCHAR (255) NOT NULL, + PROVISIONING_USERSTORE_DOMAIN VARCHAR (512), + IS_LOCAL_CLAIM_DIALECT CHAR(1) DEFAULT '1', + IS_SEND_LOCAL_SUBJECT_ID CHAR(1) DEFAULT '0', + IS_SEND_AUTH_LIST_OF_IDPS CHAR(1) DEFAULT '0', + IS_USE_TENANT_DOMAIN_SUBJECT CHAR(1) DEFAULT '1', + IS_USE_USER_DOMAIN_SUBJECT CHAR(1) DEFAULT '1', + ENABLE_AUTHORIZATION CHAR(1) DEFAULT '0', + SUBJECT_CLAIM_URI VARCHAR (512), + IS_SAAS_APP CHAR(1) DEFAULT '0', + IS_DUMB_MODE CHAR(1) DEFAULT '0', + UUID CHAR(36), + IMAGE_URL VARCHAR(1024), + ACCESS_URL VARCHAR(1024), + IS_DISCOVERABLE CHAR(1) DEFAULT '0', + + PRIMARY KEY (ID)); + +ALTER TABLE SP_APP ADD CONSTRAINT APPLICATION_NAME_CONSTRAINT UNIQUE(APP_NAME, TENANT_ID); +ALTER TABLE SP_APP ADD CONSTRAINT APPLICATION_UUID_CONSTRAINT UNIQUE(UUID); + +CREATE TABLE IF NOT EXISTS SP_METADATA ( + ID INTEGER AUTO_INCREMENT, + SP_ID INTEGER, + NAME VARCHAR(255) NOT NULL, + `VALUE` VARCHAR(255) NOT NULL, + DISPLAY_NAME VARCHAR(255), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (ID), + CONSTRAINT SP_METADATA_CONSTRAINT UNIQUE (SP_ID, NAME), + FOREIGN KEY (SP_ID) REFERENCES SP_APP(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS SP_INBOUND_AUTH ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + INBOUND_AUTH_KEY VARCHAR (255), + INBOUND_AUTH_TYPE VARCHAR (255) NOT NULL, + INBOUND_CONFIG_TYPE VARCHAR (255) NOT NULL, + PROP_NAME VARCHAR (255), + PROP_VALUE VARCHAR (1024) , + APP_ID INTEGER NOT NULL, + PRIMARY KEY (ID)); + +ALTER TABLE SP_INBOUND_AUTH ADD CONSTRAINT APPLICATION_ID_CONSTRAINT FOREIGN KEY (APP_ID) REFERENCES SP_APP (ID) ON DELETE CASCADE; + +CREATE TABLE IF NOT EXISTS SP_AUTH_STEP ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + STEP_ORDER INTEGER DEFAULT 1, + APP_ID INTEGER NOT NULL , + IS_SUBJECT_STEP CHAR(1) DEFAULT '0', + IS_ATTRIBUTE_STEP CHAR(1) DEFAULT '0', + PRIMARY KEY (ID)); + +ALTER TABLE SP_AUTH_STEP ADD CONSTRAINT APPLICATION_ID_CONSTRAINT_STEP FOREIGN KEY (APP_ID) REFERENCES SP_APP (ID) ON DELETE CASCADE; + +CREATE TABLE IF NOT EXISTS SP_FEDERATED_IDP ( + ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, + AUTHENTICATOR_ID INTEGER NOT NULL, + PRIMARY KEY (ID, AUTHENTICATOR_ID)); + +ALTER TABLE SP_FEDERATED_IDP ADD CONSTRAINT STEP_ID_CONSTRAINT FOREIGN KEY (ID) REFERENCES SP_AUTH_STEP (ID) ON DELETE CASCADE; + +CREATE TABLE IF NOT EXISTS SP_CLAIM_DIALECT ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + SP_DIALECT VARCHAR (512) NOT NULL, + APP_ID INTEGER NOT NULL, + PRIMARY KEY (ID)); + +ALTER TABLE SP_CLAIM_DIALECT ADD CONSTRAINT DIALECTID_APPID_CONSTRAINT FOREIGN KEY (APP_ID) REFERENCES SP_APP (ID) ON DELETE CASCADE; + +CREATE TABLE IF NOT EXISTS SP_CLAIM_MAPPING ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + IDP_CLAIM VARCHAR (512) NOT NULL , + SP_CLAIM VARCHAR (512) NOT NULL , + APP_ID INTEGER NOT NULL, + IS_REQUESTED VARCHAR(128) DEFAULT '0', + IS_MANDATORY VARCHAR(128) DEFAULT '0', + DEFAULT_VALUE VARCHAR(255), + PRIMARY KEY (ID)); + +ALTER TABLE SP_CLAIM_MAPPING ADD CONSTRAINT CLAIMID_APPID_CONSTRAINT FOREIGN KEY (APP_ID) REFERENCES SP_APP (ID) ON DELETE CASCADE; + +CREATE TABLE IF NOT EXISTS SP_ROLE_MAPPING ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + IDP_ROLE VARCHAR (255) NOT NULL , + SP_ROLE VARCHAR (255) NOT NULL , + APP_ID INTEGER NOT NULL, + PRIMARY KEY (ID)); + +ALTER TABLE SP_ROLE_MAPPING ADD CONSTRAINT ROLEID_APPID_CONSTRAINT FOREIGN KEY (APP_ID) REFERENCES SP_APP (ID) ON DELETE CASCADE; + +CREATE TABLE IF NOT EXISTS SP_REQ_PATH_AUTHENTICATOR ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + AUTHENTICATOR_NAME VARCHAR (255) NOT NULL , + APP_ID INTEGER NOT NULL, + PRIMARY KEY (ID)); + +ALTER TABLE SP_REQ_PATH_AUTHENTICATOR ADD CONSTRAINT REQ_AUTH_APPID_CONSTRAINT FOREIGN KEY (APP_ID) REFERENCES SP_APP (ID) ON DELETE CASCADE; + +CREATE TABLE IF NOT EXISTS SP_PROVISIONING_CONNECTOR ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + IDP_NAME VARCHAR (255) NOT NULL , + CONNECTOR_NAME VARCHAR (255) NOT NULL , + APP_ID INTEGER NOT NULL, + IS_JIT_ENABLED CHAR(1) NOT NULL DEFAULT '0', + BLOCKING CHAR(1) NOT NULL DEFAULT '0', + RULE_ENABLED CHAR(1) NOT NULL DEFAULT '0', + PRIMARY KEY (ID)); + +ALTER TABLE SP_PROVISIONING_CONNECTOR ADD CONSTRAINT PRO_CONNECTOR_APPID_CONSTRAINT FOREIGN KEY (APP_ID) REFERENCES SP_APP (ID) ON DELETE CASCADE; + +CREATE TABLE IF NOT EXISTS SP_AUTH_SCRIPT ( + ID INTEGER AUTO_INCREMENT NOT NULL, + TENANT_ID INTEGER NOT NULL, + APP_ID INTEGER NOT NULL, + TYPE VARCHAR(255) NOT NULL, + CONTENT BLOB DEFAULT NULL, + IS_ENABLED CHAR(1) NOT NULL DEFAULT '0', + PRIMARY KEY (ID)); + +CREATE TABLE SP_TEMPLATE ( + ID INTEGER AUTO_INCREMENT NOT NULL, + TENANT_ID INTEGER NOT NULL, + NAME VARCHAR(255) NOT NULL, + DESCRIPTION VARCHAR(1023), + CONTENT BLOB DEFAULT NULL, + PRIMARY KEY (ID), + CONSTRAINT SP_TEMPLATE_CONSTRAINT UNIQUE (TENANT_ID, NAME)); + +CREATE TABLE IF NOT EXISTS IDN_AUTH_WAIT_STATUS ( + ID INTEGER AUTO_INCREMENT NOT NULL, + TENANT_ID INTEGER NOT NULL, + LONG_WAIT_KEY VARCHAR(255) NOT NULL, + WAIT_STATUS CHAR(1) NOT NULL DEFAULT '1', + TIME_CREATED TIMESTAMP DEFAULT 0, + EXPIRE_TIME TIMESTAMP DEFAULT 0, + PRIMARY KEY (ID), + CONSTRAINT IDN_AUTH_WAIT_STATUS_KEY UNIQUE (LONG_WAIT_KEY)); + +CREATE TABLE IF NOT EXISTS IDP ( + ID INTEGER AUTO_INCREMENT, + TENANT_ID INTEGER, + NAME VARCHAR(254) NOT NULL, + IS_ENABLED CHAR(1) NOT NULL DEFAULT '1', + IS_PRIMARY CHAR(1) NOT NULL DEFAULT '0', + HOME_REALM_ID VARCHAR(254), + IMAGE MEDIUMBLOB, + CERTIFICATE BLOB, + ALIAS VARCHAR(254), + INBOUND_PROV_ENABLED CHAR(1) NOT NULL DEFAULT '0', + INBOUND_PROV_USER_STORE_ID VARCHAR(254), + USER_CLAIM_URI VARCHAR(254), + ROLE_CLAIM_URI VARCHAR(254), + DESCRIPTION VARCHAR(1024), + DEFAULT_AUTHENTICATOR_NAME VARCHAR(254), + DEFAULT_PRO_CONNECTOR_NAME VARCHAR(254), + PROVISIONING_ROLE VARCHAR(128), + IS_FEDERATION_HUB CHAR(1) NOT NULL DEFAULT '0', + IS_LOCAL_CLAIM_DIALECT CHAR(1) NOT NULL DEFAULT '0', + DISPLAY_NAME VARCHAR(255), + IMAGE_URL VARCHAR(1024), + UUID CHAR(36) NOT NULL, + PRIMARY KEY (ID), + UNIQUE (TENANT_ID, NAME), + UNIQUE (UUID) +); + +CREATE TABLE IF NOT EXISTS IDP_ROLE ( + ID INTEGER AUTO_INCREMENT, + IDP_ID INTEGER, + TENANT_ID INTEGER, + ROLE VARCHAR(254), + PRIMARY KEY (ID), + UNIQUE (IDP_ID, ROLE), + FOREIGN KEY (IDP_ID) REFERENCES IDP(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_GROUP ( + ID INTEGER AUTO_INCREMENT NOT NULL, + IDP_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, + GROUP_NAME VARCHAR(255) NOT NULL, + UUID CHAR(36) NOT NULL, + PRIMARY KEY (ID), + UNIQUE (IDP_ID, GROUP_NAME), + UNIQUE (UUID), + FOREIGN KEY (IDP_ID) REFERENCES IDP(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_ROLE_MAPPING ( + ID INTEGER AUTO_INCREMENT, + IDP_ROLE_ID INTEGER, + TENANT_ID INTEGER, + USER_STORE_ID VARCHAR (253), + LOCAL_ROLE VARCHAR(253), + PRIMARY KEY (ID), + UNIQUE (IDP_ROLE_ID, TENANT_ID, USER_STORE_ID, LOCAL_ROLE), + FOREIGN KEY (IDP_ROLE_ID) REFERENCES IDP_ROLE(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_CLAIM ( + ID INTEGER AUTO_INCREMENT, + IDP_ID INTEGER, + TENANT_ID INTEGER, + CLAIM VARCHAR(254), + PRIMARY KEY (ID), + UNIQUE (IDP_ID, CLAIM), + FOREIGN KEY (IDP_ID) REFERENCES IDP(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_CLAIM_MAPPING ( + ID INTEGER AUTO_INCREMENT, + IDP_CLAIM_ID INTEGER, + TENANT_ID INTEGER, + LOCAL_CLAIM VARCHAR(253), + DEFAULT_VALUE VARCHAR(255), + IS_REQUESTED VARCHAR(128) DEFAULT '0', + PRIMARY KEY (ID), + UNIQUE (IDP_CLAIM_ID, TENANT_ID, LOCAL_CLAIM), + FOREIGN KEY (IDP_CLAIM_ID) REFERENCES IDP_CLAIM(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_AUTHENTICATOR ( + ID INTEGER AUTO_INCREMENT, + TENANT_ID INTEGER, + IDP_ID INTEGER, + NAME VARCHAR(255) NOT NULL, + IS_ENABLED CHAR (1) DEFAULT '1', + DISPLAY_NAME VARCHAR(255), + PRIMARY KEY (ID), + UNIQUE (TENANT_ID, IDP_ID, NAME), + FOREIGN KEY (IDP_ID) REFERENCES IDP(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_METADATA ( + ID INTEGER AUTO_INCREMENT, + IDP_ID INTEGER, + NAME VARCHAR(255) NOT NULL, + `VALUE` VARCHAR(255) NOT NULL, + DISPLAY_NAME VARCHAR(255), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (ID), + CONSTRAINT IDP_METADATA_CONSTRAINT UNIQUE (IDP_ID, NAME), + FOREIGN KEY (IDP_ID) REFERENCES IDP(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_AUTHENTICATOR_PROPERTY ( + ID INTEGER AUTO_INCREMENT, + TENANT_ID INTEGER, + AUTHENTICATOR_ID INTEGER, + PROPERTY_KEY VARCHAR(255) NOT NULL, + PROPERTY_VALUE VARCHAR(2047), + IS_SECRET CHAR (1) DEFAULT '0', + PRIMARY KEY (ID), + UNIQUE (TENANT_ID, AUTHENTICATOR_ID, PROPERTY_KEY), + FOREIGN KEY (AUTHENTICATOR_ID) REFERENCES IDP_AUTHENTICATOR(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_PROVISIONING_CONFIG ( + ID INTEGER AUTO_INCREMENT, + TENANT_ID INTEGER, + IDP_ID INTEGER, + PROVISIONING_CONNECTOR_TYPE VARCHAR(255) NOT NULL, + IS_ENABLED CHAR (1) DEFAULT '0', + IS_BLOCKING CHAR (1) DEFAULT '0', + IS_RULES_ENABLED CHAR (1) DEFAULT '0', + PRIMARY KEY (ID), + UNIQUE (TENANT_ID, IDP_ID, PROVISIONING_CONNECTOR_TYPE), + FOREIGN KEY (IDP_ID) REFERENCES IDP(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_PROV_CONFIG_PROPERTY ( + ID INTEGER AUTO_INCREMENT, + TENANT_ID INTEGER, + PROVISIONING_CONFIG_ID INTEGER, + PROPERTY_KEY VARCHAR(255) NOT NULL, + PROPERTY_VALUE VARCHAR(2048), + PROPERTY_BLOB_VALUE BLOB, + PROPERTY_TYPE VARCHAR(32) NOT NULL, + IS_SECRET CHAR (1) DEFAULT '0', + PRIMARY KEY (ID), + UNIQUE (TENANT_ID, PROVISIONING_CONFIG_ID, PROPERTY_KEY), + FOREIGN KEY (PROVISIONING_CONFIG_ID) REFERENCES IDP_PROVISIONING_CONFIG(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_PROVISIONING_ENTITY ( + ID INTEGER AUTO_INCREMENT, + PROVISIONING_CONFIG_ID INTEGER, + ENTITY_TYPE VARCHAR(255) NOT NULL, + ENTITY_LOCAL_USERSTORE VARCHAR(255) NOT NULL, + ENTITY_NAME VARCHAR(255) NOT NULL, + ENTITY_VALUE VARCHAR(255), + TENANT_ID INTEGER, + ENTITY_LOCAL_ID VARCHAR(255), + PRIMARY KEY (ID), + UNIQUE (ENTITY_TYPE, TENANT_ID, ENTITY_LOCAL_USERSTORE, ENTITY_NAME, PROVISIONING_CONFIG_ID), + UNIQUE (PROVISIONING_CONFIG_ID, ENTITY_TYPE, ENTITY_VALUE), + FOREIGN KEY (PROVISIONING_CONFIG_ID) REFERENCES IDP_PROVISIONING_CONFIG(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDP_LOCAL_CLAIM ( + ID INTEGER AUTO_INCREMENT, + TENANT_ID INTEGER, + IDP_ID INTEGER, + CLAIM_URI VARCHAR(255) NOT NULL, + DEFAULT_VALUE VARCHAR(255), + IS_REQUESTED VARCHAR(128) DEFAULT '0', + PRIMARY KEY (ID), + UNIQUE (TENANT_ID, IDP_ID, CLAIM_URI), + FOREIGN KEY (IDP_ID) REFERENCES IDP(ID) ON DELETE CASCADE); + +CREATE TABLE IF NOT EXISTS IDN_ASSOCIATED_ID ( + ID INTEGER AUTO_INCREMENT, + IDP_USER_ID VARCHAR(255) NOT NULL, + TENANT_ID INTEGER DEFAULT -1234, + IDP_ID INTEGER NOT NULL, + DOMAIN_NAME VARCHAR(255) NOT NULL, + USER_NAME VARCHAR(255) NOT NULL, + ASSOCIATION_ID CHAR(36) NOT NULL, + PRIMARY KEY (ID), + UNIQUE(IDP_USER_ID, TENANT_ID, IDP_ID), + FOREIGN KEY (IDP_ID) REFERENCES IDP(ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_USER_ACCOUNT_ASSOCIATION ( + ASSOCIATION_KEY VARCHAR(255) NOT NULL, + TENANT_ID INTEGER, + DOMAIN_NAME VARCHAR(255) NOT NULL, + USER_NAME VARCHAR(255) NOT NULL, + PRIMARY KEY (TENANT_ID, DOMAIN_NAME, USER_NAME)); + +CREATE TABLE IF NOT EXISTS FIDO_DEVICE_STORE ( + TENANT_ID INTEGER, + DOMAIN_NAME VARCHAR(255) NOT NULL, + USER_NAME VARCHAR(45) NOT NULL, + TIME_REGISTERED TIMESTAMP, + KEY_HANDLE VARCHAR(200) NOT NULL, + DEVICE_DATA VARCHAR(2048) NOT NULL, + PRIMARY KEY (TENANT_ID, DOMAIN_NAME, USER_NAME, KEY_HANDLE)); + +CREATE TABLE IF NOT EXISTS FIDO2_DEVICE_STORE ( + TENANT_ID INTEGER, + DOMAIN_NAME VARCHAR(255) NOT NULL, + USER_NAME VARCHAR(45) NOT NULL, + TIME_REGISTERED TIMESTAMP, + USER_HANDLE VARCHAR(200) NOT NULL, + CREDENTIAL_ID VARCHAR(200) NOT NULL, + PUBLIC_KEY_COSE VARCHAR(2048) NOT NULL, + SIGNATURE_COUNT BIGINT, + USER_IDENTITY VARCHAR(200) NOT NULL, + DISPLAY_NAME VARCHAR(255), + IS_USERNAMELESS_SUPPORTED CHAR(1) DEFAULT '0', + PRIMARY KEY (TENANT_ID, DOMAIN_NAME, USER_NAME, USER_HANDLE)); + +CREATE TABLE IF NOT EXISTS WF_REQUEST ( + UUID VARCHAR (45), + CREATED_BY VARCHAR (255), + TENANT_ID INTEGER DEFAULT -1, + OPERATION_TYPE VARCHAR (50), + CREATED_AT TIMESTAMP, + UPDATED_AT TIMESTAMP, + STATUS VARCHAR (30), + REQUEST BLOB, + PRIMARY KEY (UUID) +); + +CREATE TABLE IF NOT EXISTS WF_BPS_PROFILE ( + PROFILE_NAME VARCHAR(45), + HOST_URL_MANAGER VARCHAR(255), + HOST_URL_WORKER VARCHAR(255), + USERNAME VARCHAR(100), + PASSWORD VARCHAR(1023), + CALLBACK_HOST VARCHAR (45), + CALLBACK_USERNAME VARCHAR(100), + CALLBACK_PASSWORD VARCHAR(255), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (PROFILE_NAME, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS WF_WORKFLOW( + ID VARCHAR (45), + WF_NAME VARCHAR (45), + DESCRIPTION VARCHAR (255), + TEMPLATE_ID VARCHAR (45), + IMPL_ID VARCHAR (45), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS WF_WORKFLOW_ASSOCIATION( + ID INTEGER NOT NULL AUTO_INCREMENT, + ASSOC_NAME VARCHAR (45), + EVENT_ID VARCHAR(45), + ASSOC_CONDITION VARCHAR (2000), + WORKFLOW_ID VARCHAR (45), + IS_ENABLED CHAR (1) DEFAULT '1', + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY(ID), + FOREIGN KEY (WORKFLOW_ID) REFERENCES WF_WORKFLOW(ID)ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS WF_WORKFLOW_CONFIG_PARAM( + WORKFLOW_ID VARCHAR (45), + PARAM_NAME VARCHAR (45), + PARAM_VALUE VARCHAR (1000), + PARAM_QNAME VARCHAR (45), + PARAM_HOLDER VARCHAR (45), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (WORKFLOW_ID, PARAM_NAME, PARAM_QNAME, PARAM_HOLDER), + FOREIGN KEY (WORKFLOW_ID) REFERENCES WF_WORKFLOW(ID)ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS WF_REQUEST_ENTITY_RELATIONSHIP( + REQUEST_ID VARCHAR (45), + ENTITY_NAME VARCHAR (255), + ENTITY_TYPE VARCHAR (50), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY(REQUEST_ID, ENTITY_NAME, ENTITY_TYPE, TENANT_ID), + FOREIGN KEY (REQUEST_ID) REFERENCES WF_REQUEST(UUID)ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS WF_WORKFLOW_REQUEST_RELATION( + RELATIONSHIP_ID VARCHAR (45), + WORKFLOW_ID VARCHAR (45), + REQUEST_ID VARCHAR (45), + UPDATED_AT TIMESTAMP, + STATUS VARCHAR (30), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (RELATIONSHIP_ID), + FOREIGN KEY (WORKFLOW_ID) REFERENCES WF_WORKFLOW(ID)ON DELETE CASCADE, + FOREIGN KEY (REQUEST_ID) REFERENCES WF_REQUEST(UUID)ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_RECOVERY_DATA ( + USER_NAME VARCHAR(255) NOT NULL, + USER_DOMAIN VARCHAR(127) NOT NULL, + TENANT_ID INTEGER DEFAULT -1, + CODE VARCHAR(255) NOT NULL, + SCENARIO VARCHAR(255) NOT NULL, + STEP VARCHAR(127) NOT NULL, + TIME_CREATED TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + REMAINING_SETS VARCHAR(2500) DEFAULT NULL, + PRIMARY KEY(USER_NAME, USER_DOMAIN, TENANT_ID, SCENARIO,STEP), + UNIQUE(CODE) +); + +CREATE TABLE IF NOT EXISTS IDN_PASSWORD_HISTORY_DATA ( + ID INTEGER NOT NULL AUTO_INCREMENT, + USER_NAME VARCHAR(255) NOT NULL, + USER_DOMAIN VARCHAR(127) NOT NULL, + TENANT_ID INTEGER DEFAULT -1, + SALT_VALUE VARCHAR(255), + HASH VARCHAR(255) NOT NULL, + TIME_CREATED TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (ID), + UNIQUE (USER_NAME,USER_DOMAIN,TENANT_ID,SALT_VALUE,HASH) +); + +CREATE TABLE IF NOT EXISTS IDN_CLAIM_DIALECT ( + ID INTEGER NOT NULL AUTO_INCREMENT, + DIALECT_URI VARCHAR (255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT DIALECT_URI_CONSTRAINT UNIQUE (DIALECT_URI, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_CLAIM ( + ID INTEGER NOT NULL AUTO_INCREMENT, + DIALECT_ID INTEGER NOT NULL, + CLAIM_URI VARCHAR (255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (DIALECT_ID) REFERENCES IDN_CLAIM_DIALECT(ID) ON DELETE CASCADE, + CONSTRAINT CLAIM_URI_CONSTRAINT UNIQUE (DIALECT_ID, CLAIM_URI, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_CLAIM_MAPPED_ATTRIBUTE ( + ID INTEGER NOT NULL AUTO_INCREMENT, + LOCAL_CLAIM_ID INTEGER, + USER_STORE_DOMAIN_NAME VARCHAR (255) NOT NULL, + ATTRIBUTE_NAME VARCHAR (255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (LOCAL_CLAIM_ID) REFERENCES IDN_CLAIM(ID) ON DELETE CASCADE, + CONSTRAINT USER_STORE_DOMAIN_CONSTRAINT UNIQUE (LOCAL_CLAIM_ID, USER_STORE_DOMAIN_NAME, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_CLAIM_PROPERTY ( + ID INTEGER NOT NULL AUTO_INCREMENT, + LOCAL_CLAIM_ID INTEGER, + PROPERTY_NAME VARCHAR (255) NOT NULL, + PROPERTY_VALUE VARCHAR (255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (LOCAL_CLAIM_ID) REFERENCES IDN_CLAIM(ID) ON DELETE CASCADE, + CONSTRAINT PROPERTY_NAME_CONSTRAINT UNIQUE (LOCAL_CLAIM_ID, PROPERTY_NAME, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_CLAIM_MAPPING ( + ID INTEGER NOT NULL AUTO_INCREMENT, + EXT_CLAIM_ID INTEGER NOT NULL, + MAPPED_LOCAL_CLAIM_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (EXT_CLAIM_ID) REFERENCES IDN_CLAIM(ID) ON DELETE CASCADE, + FOREIGN KEY (MAPPED_LOCAL_CLAIM_ID) REFERENCES IDN_CLAIM(ID) ON DELETE CASCADE, + CONSTRAINT EXT_TO_LOC_MAPPING_CONSTRN UNIQUE (EXT_CLAIM_ID, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_SAML2_ASSERTION_STORE ( + ID INTEGER NOT NULL AUTO_INCREMENT, + SAML2_ID VARCHAR(255) , + SAML2_ISSUER VARCHAR(255) , + SAML2_SUBJECT VARCHAR(255) , + SAML2_SESSION_INDEX VARCHAR(255) , + SAML2_AUTHN_CONTEXT_CLASS_REF VARCHAR(255) , + SAML2_ASSERTION VARCHAR(4096) , + ASSERTION BLOB , + PRIMARY KEY (ID) +); + +CREATE TABLE IDN_SAML2_ARTIFACT_STORE ( + ID INT NOT NULL AUTO_INCREMENT, + SOURCE_ID VARCHAR(255) NOT NULL, + MESSAGE_HANDLER VARCHAR(255) NOT NULL, + AUTHN_REQ_DTO BLOB NOT NULL, + SESSION_ID VARCHAR(255) NOT NULL, + INIT_TIMESTAMP TIMESTAMP NOT NULL, + EXP_TIMESTAMP TIMESTAMP NOT NULL, + ASSERTION_ID VARCHAR(255), + PRIMARY KEY (`ID`) +); + +CREATE TABLE IF NOT EXISTS IDN_OIDC_JTI ( + JWT_ID VARCHAR(255), + TENANT_ID INTEGER NOT NULL, + EXP_TIME TIMESTAMP NOT NULL , + TIME_CREATED TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , + PRIMARY KEY (JWT_ID, TENANT_ID) +); + + +CREATE TABLE IF NOT EXISTS IDN_OIDC_PROPERTY ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER, + CONSUMER_KEY VARCHAR(255) , + PROPERTY_KEY VARCHAR(255) NOT NULL, + PROPERTY_VALUE VARCHAR(2047) , + PRIMARY KEY (ID), + FOREIGN KEY (CONSUMER_KEY) REFERENCES IDN_OAUTH_CONSUMER_APPS(CONSUMER_KEY) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS IDN_OIDC_REQ_OBJECT_REFERENCE ( + ID INTEGER NOT NULL AUTO_INCREMENT, + CONSUMER_KEY_ID INTEGER , + CODE_ID VARCHAR(255) , + TOKEN_ID VARCHAR(255) , + SESSION_DATA_KEY VARCHAR(255), + PRIMARY KEY (ID), + FOREIGN KEY (CONSUMER_KEY_ID) REFERENCES IDN_OAUTH_CONSUMER_APPS(ID) ON DELETE CASCADE, + FOREIGN KEY (TOKEN_ID) REFERENCES IDN_OAUTH2_ACCESS_TOKEN(TOKEN_ID) ON DELETE CASCADE, + FOREIGN KEY (CODE_ID) REFERENCES IDN_OAUTH2_AUTHORIZATION_CODE(CODE_ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OIDC_REQ_OBJECT_CLAIMS ( + ID INTEGER NOT NULL AUTO_INCREMENT, + REQ_OBJECT_ID INTEGER, + CLAIM_ATTRIBUTE VARCHAR(255) , + ESSENTIAL CHAR(1) NOT NULL DEFAULT '0', + `VALUE` VARCHAR(255) , + IS_USERINFO CHAR(1) NOT NULL DEFAULT '0', + PRIMARY KEY (ID), + FOREIGN KEY (REQ_OBJECT_ID) REFERENCES IDN_OIDC_REQ_OBJECT_REFERENCE (ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OIDC_REQ_OBJ_CLAIM_VALUES ( + ID INTEGER NOT NULL AUTO_INCREMENT, + REQ_OBJECT_CLAIMS_ID INTEGER , + CLAIM_VALUES VARCHAR(255) , + PRIMARY KEY (ID), + FOREIGN KEY (REQ_OBJECT_CLAIMS_ID) REFERENCES IDN_OIDC_REQ_OBJECT_CLAIMS(ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_CERTIFICATE ( + ID INTEGER NOT NULL AUTO_INCREMENT, + NAME VARCHAR(100), + CERTIFICATE_IN_PEM BLOB, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY(ID), + CONSTRAINT CERTIFICATE_UNIQUE_KEY UNIQUE (NAME, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_OIDC_SCOPE_CLAIM_MAPPING ( + ID INTEGER NOT NULL AUTO_INCREMENT, + SCOPE_ID INTEGER NOT NULL, + EXTERNAL_CLAIM_ID INTEGER NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (SCOPE_ID) REFERENCES IDN_OAUTH2_SCOPE(SCOPE_ID) ON DELETE CASCADE, + FOREIGN KEY (EXTERNAL_CLAIM_ID) REFERENCES IDN_CLAIM(ID) ON DELETE CASCADE, + UNIQUE (SCOPE_ID, EXTERNAL_CLAIM_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_FUNCTION_LIBRARY ( + NAME VARCHAR(255) NOT NULL, + DESCRIPTION VARCHAR(1023), + TYPE VARCHAR(255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + DATA BLOB NOT NULL, + PRIMARY KEY (TENANT_ID,NAME) +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_CIBA_AUTH_CODE ( + AUTH_CODE_KEY CHAR (36), + AUTH_REQ_ID CHAR (36), + ISSUED_TIME TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSUMER_KEY VARCHAR(255), + LAST_POLLED_TIME TIMESTAMP NOT NULL, + POLLING_INTERVAL INTEGER, + EXPIRES_IN INTEGER, + AUTHENTICATED_USER_NAME VARCHAR(255), + USER_STORE_DOMAIN VARCHAR(100), + TENANT_ID INTEGER, + AUTH_REQ_STATUS VARCHAR (100) DEFAULT 'REQUESTED', + IDP_ID INTEGER, + UNIQUE(AUTH_REQ_ID), + PRIMARY KEY (AUTH_CODE_KEY), + FOREIGN KEY (CONSUMER_KEY) REFERENCES IDN_OAUTH_CONSUMER_APPS(CONSUMER_KEY) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_CIBA_REQUEST_SCOPES ( + ID INTEGER NOT NULL AUTO_INCREMENT, + AUTH_CODE_KEY CHAR (36), + SCOPE VARCHAR (255), + FOREIGN KEY (AUTH_CODE_KEY) REFERENCES IDN_OAUTH2_CIBA_AUTH_CODE(AUTH_CODE_KEY) ON DELETE CASCADE, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS IDN_FED_AUTH_SESSION_MAPPING ( + ID INTEGER NOT NULL AUTO_INCREMENT, + IDP_SESSION_ID VARCHAR(255) NOT NULL, + SESSION_ID VARCHAR(255) NOT NULL, + IDP_NAME VARCHAR(255) NOT NULL, + AUTHENTICATOR_ID VARCHAR(255), + PROTOCOL_TYPE VARCHAR(255), + TIME_CREATED TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + TENANT_ID INTEGER NOT NULL DEFAULT 0, + IDP_ID INTEGER NOT NULL DEFAULT 0, + FOREIGN KEY (IDP_ID) REFERENCES IDP(ID) ON DELETE CASCADE, + PRIMARY KEY (ID), + UNIQUE (IDP_SESSION_ID, TENANT_ID, IDP_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_CONFIG_TYPE ( + ID VARCHAR(255) NOT NULL, + NAME VARCHAR(255) NOT NULL, + DESCRIPTION VARCHAR(1023) NULL, + PRIMARY KEY (ID), + CONSTRAINT TYPE_NAME_CONSTRAINT UNIQUE (NAME) +); + +INSERT INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES +('9ab0ef95-13e9-4ed5-afaf-d29bed62f7bd', 'IDP_TEMPLATE', 'Template type to uniquely identify IDP templates'), +('3c4ac3d0-5903-4e3d-aaca-38df65b33bfd', 'APPLICATION_TEMPLATE', 'Template type to uniquely identify Application templates'), +('8ec6dbf1-218a-49bf-bc34-0d2db52d151c', 'CORS_CONFIGURATION', 'A resource type to keep the tenant CORS configurations'), +('669b99ca-cdb0-44a6-8cae-babed3b585df', 'Publisher', 'A resource type to keep the event publisher configurations'), +('73f6d9ca-62f4-4566-bab9-2a930ae51ba8', 'BRANDING_PREFERENCES', 'A resource type to keep the tenant branding preferences'), +('899c69b2-8bf7-46b5-9666-f7f99f90d6cc', 'fido-config', 'A resource type to store FIDO authenticator related preferences'), +('7f24050f-3e3d-4a00-b10f-fd5450d6523e', 'input-validation-configurations', 'A resource type to store input validation related configurations'), +('f4e83b8a-d1c4-a0d6-03a7-d48e268c60c5', 'PK_JWT_CONFIGURATION', 'A resource type to keep the tenant private key jwt configuration.'), +('9ec61e9d-f0e6-4952-9a09-ab842aeb2db2', 'ATTRIBUTE_CONFIGURATION', 'A resource type to store attribute related configurations.'); + +CREATE TABLE IF NOT EXISTS IDN_CONFIG_RESOURCE ( + ID VARCHAR(255) NOT NULL, + TENANT_ID INT NOT NULL, + NAME VARCHAR(255) NOT NULL, + CREATED_TIME TIMESTAMP NOT NULL, + LAST_MODIFIED TIMESTAMP NOT NULL, + HAS_FILE BOOLEAN NOT NULL, + HAS_ATTRIBUTE BOOLEAN NOT NULL, + TYPE_ID VARCHAR(255) NOT NULL, + UNIQUE (NAME, TENANT_ID, TYPE_ID), + PRIMARY KEY (ID) +); +ALTER TABLE IDN_CONFIG_RESOURCE +ADD CONSTRAINT TYPE_ID_FOREIGN_CONSTRAINT FOREIGN KEY (TYPE_ID) REFERENCES IDN_CONFIG_TYPE (ID) +ON DELETE CASCADE ON UPDATE CASCADE; + +CREATE TABLE IF NOT EXISTS IDN_CONFIG_ATTRIBUTE ( + ID VARCHAR(255) NOT NULL, + RESOURCE_ID VARCHAR(255) NOT NULL, + ATTR_KEY VARCHAR(255) NOT NULL, + ATTR_VALUE VARCHAR(1023) NULL, + PRIMARY KEY (ID), + UNIQUE (RESOURCE_ID, ATTR_KEY) +); +ALTER TABLE IDN_CONFIG_ATTRIBUTE +ADD CONSTRAINT RESOURCE_ID_ATTRIBUTE_FOREIGN_CONSTRAINT FOREIGN KEY (RESOURCE_ID) REFERENCES +IDN_CONFIG_RESOURCE (ID) ON DELETE CASCADE ON UPDATE CASCADE; + +CREATE TABLE IF NOT EXISTS IDN_CONFIG_FILE ( + ID VARCHAR(255) NOT NULL, + `VALUE` BLOB NULL, + RESOURCE_ID VARCHAR(255) NOT NULL, + NAME VARCHAR(255) NULL, + PRIMARY KEY (ID) +); +ALTER TABLE IDN_CONFIG_FILE +ADD CONSTRAINT RESOURCE_ID_FILE_FOREIGN_CONSTRAINT FOREIGN KEY (RESOURCE_ID) REFERENCES +IDN_CONFIG_RESOURCE (ID) ON DELETE CASCADE ON UPDATE CASCADE; + +CREATE TABLE IF NOT EXISTS IDN_REMOTE_FETCH_CONFIG ( + ID VARCHAR(255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + IS_ENABLED CHAR(1) NOT NULL, + REPO_MANAGER_TYPE VARCHAR(255) NOT NULL, + ACTION_LISTENER_TYPE VARCHAR(255) NOT NULL, + CONFIG_DEPLOYER_TYPE VARCHAR(255) NOT NULL, + REMOTE_FETCH_NAME VARCHAR(255), + REMOTE_RESOURCE_URI VARCHAR(255) NOT NULL, + ATTRIBUTES_JSON MEDIUMTEXT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT UC_REMOTE_RESOURCE_TYPE UNIQUE (TENANT_ID, CONFIG_DEPLOYER_TYPE) +); + +CREATE TABLE IF NOT EXISTS IDN_REMOTE_FETCH_REVISIONS ( + ID VARCHAR(255) NOT NULL, + CONFIG_ID VARCHAR(255) NOT NULL, + FILE_PATH VARCHAR(255) NOT NULL, + FILE_HASH VARCHAR(255), + DEPLOYED_DATE TIMESTAMP, + LAST_SYNC_TIME TIMESTAMP, + DEPLOYMENT_STATUS VARCHAR(255), + ITEM_NAME VARCHAR(255), + DEPLOY_ERR_LOG MEDIUMTEXT, + PRIMARY KEY (ID), + FOREIGN KEY (CONFIG_ID) REFERENCES IDN_REMOTE_FETCH_CONFIG(ID) ON DELETE CASCADE, + CONSTRAINT UC_REVISIONS UNIQUE (CONFIG_ID, ITEM_NAME) +); + + +CREATE TABLE IF NOT EXISTS IDN_USER_FUNCTIONALITY_MAPPING ( + ID VARCHAR(255) NOT NULL, + USER_ID VARCHAR(255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + FUNCTIONALITY_ID VARCHAR(255) NOT NULL, + IS_FUNCTIONALITY_LOCKED BOOLEAN NOT NULL, + FUNCTIONALITY_UNLOCK_TIME BIGINT NOT NULL, + FUNCTIONALITY_LOCK_REASON VARCHAR(1023), + FUNCTIONALITY_LOCK_REASON_CODE VARCHAR(255), + PRIMARY KEY (ID), + CONSTRAINT IDN_USER_FUNCTIONALITY_MAPPING_CONSTRAINT UNIQUE (USER_ID, TENANT_ID, FUNCTIONALITY_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_USER_FUNCTIONALITY_PROPERTY ( + ID VARCHAR(255) NOT NULL, + USER_ID VARCHAR(255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + FUNCTIONALITY_ID VARCHAR(255) NOT NULL, + PROPERTY_NAME VARCHAR(255), + PROPERTY_VALUE VARCHAR(255), + PRIMARY KEY (ID), + CONSTRAINT IDN_USER_FUNCTIONALITY_PROPERTY_CONSTRAINT UNIQUE (USER_ID, TENANT_ID, FUNCTIONALITY_ID, PROPERTY_NAME) +); + +CREATE TABLE IF NOT EXISTS IDN_CORS_ORIGIN ( + ID INT NOT NULL AUTO_INCREMENT, + TENANT_ID INT NOT NULL, + ORIGIN VARCHAR(2048) NOT NULL, + UUID CHAR(36) NOT NULL, + + PRIMARY KEY (ID), + UNIQUE (TENANT_ID, ORIGIN), + UNIQUE (UUID) +); + +CREATE TABLE IF NOT EXISTS IDN_CORS_ASSOCIATION ( + IDN_CORS_ORIGIN_ID INT NOT NULL, + SP_APP_ID INT NOT NULL, + + PRIMARY KEY (IDN_CORS_ORIGIN_ID, SP_APP_ID), + FOREIGN KEY (IDN_CORS_ORIGIN_ID) REFERENCES IDN_CORS_ORIGIN (ID) ON DELETE CASCADE, + FOREIGN KEY (SP_APP_ID) REFERENCES SP_APP (ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_USER_CONSENT ( + ID INTEGER NOT NULL AUTO_INCREMENT, + USER_ID VARCHAR(255) NOT NULL, + APP_ID CHAR(36) NOT NULL, + TENANT_ID INTEGER NOT NULL DEFAULT -1, + CONSENT_ID VARCHAR(255) NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (APP_ID) REFERENCES SP_APP(UUID) ON DELETE CASCADE, + UNIQUE (USER_ID, APP_ID, TENANT_ID), + UNIQUE (CONSENT_ID) +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH2_USER_CONSENTED_SCOPES ( + ID INTEGER NOT NULL AUTO_INCREMENT, + CONSENT_ID VARCHAR(255) NOT NULL, + TENANT_ID INTEGER NOT NULL DEFAULT -1, + SCOPE VARCHAR(255) NOT NULL, + CONSENT BOOLEAN NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (CONSENT_ID) REFERENCES IDN_OAUTH2_USER_CONSENT(CONSENT_ID) ON DELETE CASCADE, + UNIQUE (CONSENT_ID, SCOPE) +); + +CREATE TABLE IF NOT EXISTS IDN_SECRET_TYPE ( + ID VARCHAR(255) NOT NULL, + NAME VARCHAR(255) NOT NULL, + DESCRIPTION VARCHAR(1023) NULL, + PRIMARY KEY (ID), + CONSTRAINT SECRET_TYPE_NAME_CONSTRAINT UNIQUE (NAME) +); + +INSERT INTO IDN_SECRET_TYPE (ID, NAME, DESCRIPTION) VALUES +('1358bdbf-e0cc-4268-a42c-c3e0960e13f0', 'ADAPTIVE_AUTH_CALL_CHOREO', 'Secret type to uniquely identify secrets relevant to callChoreo adaptive auth function'), +('c508ca28-60c0-4493-a758-77e4173ffdb9', 'IDP_SECRET_PROPERTIES', 'Secret type to uniquely identify secrets relevant to identity providers'), +('433df096-62b7-4a36-b3eb-1bed9150ed35', 'IDVP_SECRET_PROPERTIES', 'Secret type to uniquely identify secrets relevant to identity verification providers'); + +CREATE TABLE IF NOT EXISTS IDN_SECRET ( + ID VARCHAR(255) NOT NULL, + TENANT_ID INT NOT NULL, + SECRET_NAME VARCHAR(1023) NOT NULL, + SECRET_VALUE VARCHAR(8000) NOT NULL, + CREATED_TIME TIMESTAMP NOT NULL, + LAST_MODIFIED TIMESTAMP NOT NULL, + TYPE_ID VARCHAR(255) NOT NULL, + DESCRIPTION VARCHAR(1023) NULL, + KEY_ID VARCHAR(255) NULL, + PRIMARY KEY (ID), + FOREIGN KEY (TYPE_ID) REFERENCES IDN_SECRET_TYPE(ID) ON DELETE CASCADE, + UNIQUE (SECRET_NAME, TENANT_ID, TYPE_ID) +); + +CREATE TABLE IF NOT EXISTS SP_SHARED_APP ( + ID INTEGER NOT NULL AUTO_INCREMENT, + MAIN_APP_ID CHAR(36) NOT NULL, + OWNER_ORG_ID CHAR(36) NOT NULL, + SHARED_APP_ID CHAR(36) NOT NULL, + SHARED_ORG_ID CHAR(36) NOT NULL, + SHARE_WITH_ALL_CHILDREN BOOLEAN DEFAULT FALSE, + PRIMARY KEY (ID), + FOREIGN KEY (MAIN_APP_ID) REFERENCES SP_APP(UUID) ON DELETE CASCADE, + FOREIGN KEY (SHARED_APP_ID) REFERENCES SP_APP(UUID) ON DELETE CASCADE, + UNIQUE (MAIN_APP_ID, OWNER_ORG_ID, SHARED_ORG_ID), + UNIQUE (SHARED_APP_ID) +); + +CREATE TABLE IF NOT EXISTS IDVP ( + ID INTEGER NOT NULL AUTO_INCREMENT, + UUID CHAR(36) NOT NULL, + TENANT_ID INTEGER NOT NULL, + IDVP_TYPE VARCHAR(254), + NAME VARCHAR(254), + DESCRIPTION VARCHAR(1024), + IS_ENABLED CHAR(1) NOT NULL DEFAULT '0', + PRIMARY KEY (ID), + UNIQUE (TENANT_ID, NAME), + UNIQUE (UUID) +); + +CREATE TABLE IF NOT EXISTS IDVP_CLAIM_MAPPING ( + ID INTEGER NOT NULL AUTO_INCREMENT, + IDVP_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, + CLAIM VARCHAR(254), + LOCAL_CLAIM VARCHAR(254), + PRIMARY KEY (ID), + UNIQUE (IDVP_ID, CLAIM, TENANT_ID), + FOREIGN KEY (IDVP_ID) REFERENCES IDVP(ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDVP_CONFIG ( + ID INTEGER NOT NULL AUTO_INCREMENT, + IDVP_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, + PROPERTY_KEY VARCHAR(254) NOT NULL, + PROPERTY_VALUE VARCHAR(1024), + IS_SECRET CHAR (1) DEFAULT '0', + PRIMARY KEY (ID), + UNIQUE (IDVP_ID, PROPERTY_KEY, TENANT_ID), + FOREIGN KEY (IDVP_ID) REFERENCES IDVP(ID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDV_CLAIM ( + ID INTEGER NOT NULL AUTO_INCREMENT, + UUID CHAR(36) NOT NULL, + USER_ID VARCHAR(254) NOT NULL, + CLAIM_URI VARCHAR(254), + IDVP_ID CHAR(36) NOT NULL, + TENANT_ID INTEGER NOT NULL, + IS_VERIFIED CHAR(1) NOT NULL DEFAULT '0', + METADATA BLOB, + PRIMARY KEY (ID), + UNIQUE (CLAIM_URI, TENANT_ID, USER_ID, IDVP_ID), + UNIQUE (UUID), + FOREIGN KEY (IDVP_ID) REFERENCES IDVP(UUID) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IDN_OAUTH_PAR ( + REQ_URI_REF VARCHAR(255) PRIMARY KEY, + CLIENT_ID VARCHAR(255), + SCHEDULED_EXPIRY BIGINT, + PARAMETERS MEDIUMTEXT +); + +CREATE TABLE IF NOT EXISTS APP_ROLE ( + ROLE_ID varchar(255) NOT NULL PRIMARY KEY, + ROLE_NAME varchar(255) NOT NULL, + TENANT_ID INTEGER NOT NULL, + APP_ID varchar(36) NOT NULL, + CURSOR_KEY SERIAL, + UNIQUE (ROLE_NAME, TENANT_ID, APP_ID), + FOREIGN KEY (APP_ID) REFERENCES SP_APP(UUID) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS SHARED_ROLE ( + SHARED_ROLE_ID varchar(255) NOT NULL, + MAIN_ROLE_ID varchar(255) NOT NULL, + UNIQUE (SHARED_ROLE_ID, MAIN_ROLE_ID), + PRIMARY KEY (SHARED_ROLE_ID, MAIN_ROLE_ID), + FOREIGN KEY (SHARED_ROLE_ID) REFERENCES APP_ROLE(ROLE_ID) ON DELETE CASCADE, + FOREIGN KEY (MAIN_ROLE_ID) REFERENCES APP_ROLE(ROLE_ID) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS GROUP_ROLE ( + ROLE_ID varchar(255) NOT NULL, + IDP_ID varchar(255) NOT NULL, + TENANT_ID varchar(255) NOT NULL, + GROUP_ID varchar(255) NOT NULL, + CURSOR_KEY SERIAL, + FOREIGN KEY (ROLE_ID) REFERENCES APP_ROLE(ROLE_ID) ON DELETE CASCADE, + FOREIGN KEY (IDP_ID) REFERENCES IDP(UUID) ON DELETE CASCADE, + CONSTRAINT GROUP_ROLE_UNIQUE UNIQUE (ROLE_ID, IDP_ID, GROUP_ID) +); + +CREATE TABLE IF NOT EXISTS USER_ROLE ( + ROLE_ID varchar(255) NOT NULL, + TENANT_ID varchar(255) NOT NULL, + USER_ID varchar(255) NOT NULL, + CURSOR_KEY SERIAL, + FOREIGN KEY (ROLE_ID) REFERENCES APP_ROLE(ROLE_ID) ON DELETE CASCADE, + CONSTRAINT USER_ROLE_UNIQUE UNIQUE (ROLE_ID, TENANT_ID, USER_ID) +); + +-- --------------------------- INDEX CREATION ----------------------------- +-- IDN_OAUTH2_ACCESS_TOKEN -- +CREATE INDEX IDX_TC ON IDN_OAUTH2_ACCESS_TOKEN(TIME_CREATED); +CREATE INDEX IDX_ATH ON IDN_OAUTH2_ACCESS_TOKEN(ACCESS_TOKEN_HASH); +CREATE INDEX IDX_AT_TI_UD ON IDN_OAUTH2_ACCESS_TOKEN(AUTHZ_USER, TENANT_ID, TOKEN_STATE, USER_DOMAIN); +CREATE INDEX IDX_AT_AT ON IDN_OAUTH2_ACCESS_TOKEN(ACCESS_TOKEN); +CREATE INDEX IDX_AT_RTH ON IDN_OAUTH2_ACCESS_TOKEN(REFRESH_TOKEN_HASH); +CREATE INDEX IDX_AT_RT ON IDN_OAUTH2_ACCESS_TOKEN(REFRESH_TOKEN); +CREATE INDEX IDX_TBR_TS ON IDN_OAUTH2_ACCESS_TOKEN(TOKEN_BINDING_REF, TOKEN_STATE); + +-- IDN_OAUTH2_AUTHORIZATION_CODE -- +CREATE INDEX IDX_AUTHORIZATION_CODE_HASH ON IDN_OAUTH2_AUTHORIZATION_CODE (AUTHORIZATION_CODE_HASH, CONSUMER_KEY_ID); +CREATE INDEX IDX_AUTHORIZATION_CODE_AU_TI ON IDN_OAUTH2_AUTHORIZATION_CODE (AUTHZ_USER, TENANT_ID, USER_DOMAIN, STATE); +CREATE INDEX IDX_AC_CKID ON IDN_OAUTH2_AUTHORIZATION_CODE(CONSUMER_KEY_ID); +CREATE INDEX IDX_AC_TID ON IDN_OAUTH2_AUTHORIZATION_CODE(TOKEN_ID); +CREATE INDEX IDX_AC_AC_CKID ON IDN_OAUTH2_AUTHORIZATION_CODE(AUTHORIZATION_CODE, CONSUMER_KEY_ID); +CREATE INDEX IDX_AT_CKID_AU_TID_UD_TSH_TS ON IDN_OAUTH2_ACCESS_TOKEN(CONSUMER_KEY_ID, AUTHZ_USER, TENANT_ID, USER_DOMAIN, TOKEN_SCOPE_HASH, TOKEN_STATE); + +-- IDN_SCIM_GROUP -- +CREATE INDEX IDX_IDN_SCIM_GROUP_TI_RN ON IDN_SCIM_GROUP (TENANT_ID, ROLE_NAME); +CREATE INDEX IDX_IDN_SCIM_GROUP_TI_RN_AN ON IDN_SCIM_GROUP (TENANT_ID, ROLE_NAME, ATTR_NAME); + +-- IDN_AUTH_SESSION_STORE -- +CREATE INDEX IDX_IDN_AUTH_SESSION_TIME ON IDN_AUTH_SESSION_STORE (TIME_CREATED); +CREATE INDEX IDX_IDN_AUTH_SSTR_ST_OP_ID_TM ON IDN_AUTH_SESSION_STORE (OPERATION, SESSION_TYPE, SESSION_ID, TIME_CREATED); +CREATE INDEX IDX_IDN_AUTH_SSTR_ET_ID ON IDN_AUTH_SESSION_STORE (EXPIRY_TIME, SESSION_ID); + +-- IDN_AUTH_TEMP_SESSION_STORE -- +CREATE INDEX IDX_IDN_AUTH_TMP_SESSION_TIME ON IDN_AUTH_TEMP_SESSION_STORE (TIME_CREATED); + +-- IDN_OIDC_SCOPE_CLAIM_MAPPING -- +CREATE INDEX IDX_AT_SI_ECI ON IDN_OIDC_SCOPE_CLAIM_MAPPING(SCOPE_ID, EXTERNAL_CLAIM_ID); + +-- IDN_OAUTH2_SCOPE -- +CREATE INDEX IDX_SC_TID ON IDN_OAUTH2_SCOPE(TENANT_ID); + +-- IDN_OAUTH2_SCOPE_BINDING -- +CREATE INDEX IDX_SB_SCPID ON IDN_OAUTH2_SCOPE_BINDING(SCOPE_ID); + +-- IDN_OIDC_REQ_OBJECT_REFERENCE -- +CREATE INDEX IDX_OROR_TID ON IDN_OIDC_REQ_OBJECT_REFERENCE(TOKEN_ID); + +-- IDN_OAUTH2_ACCESS_TOKEN_SCOPE -- +CREATE INDEX IDX_ATS_TID ON IDN_OAUTH2_ACCESS_TOKEN_SCOPE(TOKEN_ID); + +-- SP_TEMPLATE -- +CREATE INDEX IDX_SP_TEMPLATE ON SP_TEMPLATE (TENANT_ID, NAME); + +-- IDN_AUTH_USER -- +CREATE INDEX IDX_AUTH_USER_UN_TID_DN ON IDN_AUTH_USER (USER_NAME, TENANT_ID, DOMAIN_NAME); +CREATE INDEX IDX_AUTH_USER_DN_TOD ON IDN_AUTH_USER (DOMAIN_NAME, TENANT_ID); + +-- IDN_AUTH_USER_SESSION_MAPPING -- +CREATE INDEX IDX_USER_ID ON IDN_AUTH_USER_SESSION_MAPPING (USER_ID); +CREATE INDEX IDX_SESSION_ID ON IDN_AUTH_USER_SESSION_MAPPING (SESSION_ID); + +-- IDN_AUTH_SESSION_APP_INFO -- +CREATE INDEX IDX_AUTH_SAI_UN_AID_SID ON IDN_AUTH_SESSION_APP_INFO (APP_ID, SUBJECT, SESSION_ID); + +-- IDN_OAUTH_CONSUMER_APPS -- +CREATE INDEX IDX_OCA_UM_TID_UD_APN ON IDN_OAUTH_CONSUMER_APPS(USERNAME,TENANT_ID,USER_DOMAIN, APP_NAME); + +-- IDX_SPI_APP -- +CREATE INDEX IDX_SPI_APP ON SP_INBOUND_AUTH(APP_ID); + +-- IDN_OIDC_PROPERTY -- +CREATE INDEX IDX_IOP_CK ON IDN_OIDC_PROPERTY(CONSUMER_KEY); + +-- IDN_FIDO2_PROPERTY -- +CREATE INDEX IDX_FIDO2_STR ON FIDO2_DEVICE_STORE(USER_NAME, TENANT_ID, DOMAIN_NAME, CREDENTIAL_ID, USER_HANDLE); + +-- IDN_ASSOCIATED_ID -- +CREATE INDEX IDX_AI_DN_UN_AI ON IDN_ASSOCIATED_ID(DOMAIN_NAME, USER_NAME, ASSOCIATION_ID); + +-- IDN_OAUTH2_TOKEN_BINDING -- +CREATE INDEX IDX_IDN_AUTH_BIND ON IDN_OAUTH2_TOKEN_BINDING (TOKEN_BINDING_REF); +CREATE INDEX IDX_TK_VALUE_TYPE ON IDN_OAUTH2_TOKEN_BINDING (TOKEN_BINDING_VALUE, TOKEN_BINDING_TYPE); + +-- IDN_FED_AUTH_SESSION_MAPPING -- +CREATE INDEX IDX_FEDERATED_AUTH_SESSION_ID ON IDN_FED_AUTH_SESSION_MAPPING (SESSION_ID); + +-- IDN_REMOTE_FETCH_REVISIONS -- +CREATE INDEX IDX_REMOTE_FETCH_REVISION_CONFIG_ID ON IDN_REMOTE_FETCH_REVISIONS (CONFIG_ID); + +-- IDN_CORS_ASSOCIATION -- +CREATE INDEX IDX_CORS_SP_APP_ID ON IDN_CORS_ASSOCIATION (SP_APP_ID); + +-- IDN_CORS_ASSOCIATION -- +CREATE INDEX IDX_CORS_ORIGIN_ID ON IDN_CORS_ASSOCIATION (IDN_CORS_ORIGIN_ID); + +-- IDN_SECRET -- +CREATE INDEX IDN_SECRET_TYPE_ID ON IDN_SECRET (TYPE_ID); + +-- IDN_CLAIM -- +CREATE INDEX IDX_CLAIM_TI_CU ON IDN_CLAIM (TENANT_ID, CLAIM_URI); + +-- IDP_AUTHENTICATOR_PROPERTY -- +CREATE INDEX IDX_AUTH_PROP_AUTH_ID ON IDP_AUTHENTICATOR_PROPERTY (AUTHENTICATOR_ID); + +-- IDN_CONFIG_FILE -- +CREATE INDEX IDX_CON_FILE_RES_ID ON IDN_CONFIG_FILE (RESOURCE_ID); + diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/carbon.xml b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/carbon.xml new file mode 100644 index 000000000000..b46fba1be7df --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/carbon.xml @@ -0,0 +1,686 @@ + + + + + + + + WSO2 Identity Server + + + IS + + + 5.3.0 + + + localhost + + + localhost + + + local:/${carbon.context}/services/ + + + + + + + IdentityServer + + + + + + + org.wso2.carbon + + + / + + + + + + + + + 15 + + + + + + + + + 0 + + + + + 9999 + + 11111 + + + + + + 10389 + + 8000 + + + + + + 10500 + + + + + + + + + org.wso2.carbon.tomcat.jndi.CarbonJavaURLContextFactory + + + + + + + + + java + + + + + + + + + + false + + + false + + + 600 + + + + false + + + + + + + + 30 + + + + + + + + + 15 + + + + + + ${carbon.home}/repository/deployment/server/ + + + 15 + + + ${carbon.home}/repository/conf/axis2/axis2.xml + + + 30000 + + + ${carbon.home}/repository/deployment/client/ + + ${carbon.home}/repository/conf/axis2/axis2_client.xml + + true + + + + + + + + + + admin + Default Administrator Role + + + user + Default User Role + + + + + + + + + + + + ${carbon.home}/repository/resources/security/wso2carbon.jks + + JKS + + wso2carbon + + wso2carbon + + wso2carbon + + + + + + ${carbon.home}/repository/resources/security/client-truststore.jks + + JKS + + wso2carbon + + + + + + + + + + + + + + + + + + + UserManager + + + false + + org.wso2.carbon.identity.provider.AttributeCallbackHandler + + + org.wso2.carbon.identity.sts.store.DBTokenStore + + + true + allow + + + + + + + claim_mgt_menu + identity_mgt_emailtemplate_menu + identity_security_questions_menu + + + + ${carbon.home}/tmp/work + + + + + + true + + + 10 + + + 30 + + + + + + 100 + + + + keystore + certificate + * + + org.wso2.carbon.ui.transports.fileupload.AnyFileUploadExecutor + + + + + jarZip + + org.wso2.carbon.ui.transports.fileupload.JarZipUploadExecutor + + + + dbs + + org.wso2.carbon.ui.transports.fileupload.DBSFileUploadExecutor + + + + tools + + org.wso2.carbon.ui.transports.fileupload.ToolsFileUploadExecutor + + + + toolsAny + + org.wso2.carbon.ui.transports.fileupload.ToolsAnyFileUploadExecutor + + + + + + + + + + info + org.wso2.carbon.core.transports.util.InfoProcessor + + + wsdl + org.wso2.carbon.core.transports.util.Wsdl11Processor + + + wsdl2 + org.wso2.carbon.core.transports.util.Wsdl20Processor + + + xsd + org.wso2.carbon.core.transports.util.XsdProcessor + + + + + + false + false + true + svn + http://svnrepo.example.com/repos/ + username + password + true + + + + + + + + + + + + + + + ${require.carbon.servlet} + + + + + true + + + + + + + default repository + http://product-dist.wso2.com/p2/carbon/releases/wilkes/ + + + + + + + + true + + + + + + true + + diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/identity/identity.xml b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/identity/identity.xml new file mode 100644 index 000000000000..58c05f68e7ee --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/identity/identity.xml @@ -0,0 +1,996 @@ + + + + + + + + + jdbc/WSO2IdentityDB + + + + + true + true + 0 + + true + 20160 + 1140 + + + 50000 + + + true + + + + true + + 20 + + 40 + + + + + + + 15 + 20160 + + + + + + ${carbon.home}/conf/keystores + SunX509 + SunX509 + + + + localhost + + + SelfAndManaged + CertValidate + + + + + + + + + + + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/openidserver + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/openid + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/authenticationendpoint/openid_login.do + + false + + 7200 + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth/request-token + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth/authorize-url + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth/access-token + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth2/authorize + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth2/token + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth2/revoke + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth2/introspect + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth2/userinfo + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oidc/checksession + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oidc/logout + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/authenticationendpoint/oauth2_authz.do + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/authenticationendpoint/oauth2_error.do + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/authenticationendpoint/oauth2_consent.do + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/authenticationendpoint/oauth2_logout_consent.do + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/authenticationendpoint/oauth2_logout.do + + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/.well-known/webfinger + + + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/api/identity/oauth2/dcr/v1.1/register + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth2/jwks + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth2/oidcdiscovery + + + 300 + + 3600 + + 3600 + + 84600 + + 0 + + true + + org.wso2.carbon.identity.oauth.tokenprocessor.PlainTextPersistenceProcessor + + false + + false + + + + token + org.wso2.carbon.identity.oauth2.authz.handlers.AccessTokenResponseTypeHandler + + + code + org.wso2.carbon.identity.oauth2.authz.handlers.CodeResponseTypeHandler + + + id_token + org.wso2.carbon.identity.oauth2.authz.handlers.IDTokenResponseTypeHandler + + + id_token token + org.wso2.carbon.identity.oauth2.authz.handlers.IDTokenTokenResponseTypeHandler + + + + + + authorization_code + org.wso2.carbon.identity.oauth2.token.handlers.grant.AuthorizationCodeGrantHandler + + + password + org.wso2.carbon.identity.oauth2.token.handlers.grant.PasswordGrantHandler + + + refresh_token + org.wso2.carbon.identity.oauth2.token.handlers.grant.RefreshGrantHandler + + + client_credentials + org.wso2.carbon.identity.oauth2.token.handlers.grant.ClientCredentialsGrantHandler + false + false + + + urn:ietf:params:oauth:grant-type:saml2-bearer + org.wso2.carbon.identity.oauth2.token.handlers.grant.saml.SAML2BearerGrantHandler + + + iwa:ntlm + org.wso2.carbon.identity.oauth2.token.handlers.grant.iwa.ntlm.NTLMAuthenticationGrantHandler + + + urn:ietf:params:oauth:grant-type:jwt-bearer + org.wso2.carbon.identity.oauth2.grant.jwt.JWTBearerGrantHandler + org.wso2.carbon.identity.oauth2.grant.jwt.JWTGrantValidator + + + + + + + authorization_code + + + implicit + + + + + + + + + + + + + + + + + + + + + + + + + false + + + + + + + + false + + + + false + org.wso2.carbon.identity.oauth2.authcontext.JWTTokenGenerator + org.wso2.carbon.identity.oauth2.authcontext.DefaultClaimsRetriever + http://wso2.org/claims + SHA256withRSA + 15 + + + + + + + FEDERATED + + + + + org.wso2.carbon.identity.openidconnect.DefaultIDTokenBuilder + SHA256withRSA + + + RSA-OAEP + + A128GCM + + + + RSA1_5 + RSA-OAEP + + + A128GCM + A192GCM + A256GCM + A128CBC-HS256 + A128CBC+HS256 + + + true + + + + + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/oauth2/token + org.wso2.carbon.identity.openidconnect.SAMLAssertionClaimsCallback + 3600 + SHA256withRSA + org.wso2.carbon.identity.oauth.endpoint.user.impl.UserInfoUserStoreClaimRetriever + org.wso2.carbon.identity.oauth.endpoint.user.impl.UserInforRequestDefaultValidator + org.wso2.carbon.identity.oauth.endpoint.user.impl.UserInfoISAccessTokenValidator + org.wso2.carbon.identity.oauth.endpoint.user.impl.UserInfoJSONResponseBuilder + false + + false + + 120 + + + + + request_param_value_builder + org.wso2.carbon.identity.openidconnect.RequestParamRequestObjectBuilder + + + + + org.wso2.carbon.identity.openidconnect.RequestObjectValidatorImpl + + + + + true + 0 + 5 + + + + + + + + gtalk + talk.google.com + 5222 + gmail.com + multifactor1@gmail.com + wso2carbon + + + + + + ${carbon.host} + + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/samlsso + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/authenticationendpoint/samlsso_logout.do + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/authenticationendpoint/samlsso_notification.do + 5 + 60000 + + false + http://wso2.org/claims + + org.wso2.carbon.identity.sso.saml.builders.assertion.DefaultSAMLAssertionBuilder + org.wso2.carbon.identity.sso.saml.builders.encryption.DefaultSSOEncrypter + org.wso2.carbon.identity.sso.saml.builders.signature.DefaultSSOSigner + org.wso2.carbon.identity.sso.saml.validators.SAML2HTTPRedirectDeflateSignatureValidator + + + + 5 + false + http://www.w3.org/2000/09/xmldsig#rsa-sha1 + http://www.w3.org/2000/09/xmldsig#sha1 + http://www.w3.org/2001/04/xmlenc#aes256-cbc + http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p + true + + + + + true + + + + + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/services/wso2carbon-sts + + + + + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/passivests + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/authenticationendpoint/retry.do + org.wso2.carbon.identity.sts.passive.utils.NoPersistenceTokenStore + true + + + + + false + ${Ports.ThriftEntitlementReceivePort} + 10000 + + ${carbon.home}/repository/resources/security/wso2carbon.jks + wso2carbon + + + ${carbon.host} + + + + + + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/wso2/scim/Users + ${carbon.protocol}://${carbon.host}:${carbon.management.port}/wso2/scim/Groups + + + 5 + + + 10 + local://services + + + + + + + + + + + + + + + + + false + + true + true + + + true + + + + + + + + + + + org.wso2.carbon.identity.governance.store.JDBCIdentityDataStore + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + /permission/admin/manage/identity/identitymgt + + + + + + /permission/admin/manage/identity/usermgt/view + + + /permission/admin/manage/identity/usermgt/view + + + + /permission/admin/manage/identity/configmgt/list + + + + /permission/admin/manage/identity/configmgt/add + + + /permission/admin/manage/identity/configmgt/update + + + + /permission/admin/manage/identity/configmgt/delete + + + + /permission/admin/manage/identity/configmgt/add + + + /permission/admin/manage/identity/configmgt/update + + + + /permission/admin/manage/identity/configmgt/delete + + + + /permission/admin/manage/identity/configmgt/add + + + /permission/admin/manage/identity/configmgt/update + + + + /permission/admin/manage/identity/configmgt/delete + + + + + + + /permission/admin/manage/identity/consentmgt/add + + + + /permission/admin/manage/identity/consentmgt/delete + + + + /permission/admin/manage/identity/consentmgt/add + + + + /permission/admin/manage/identity/consentmgt/delete + + + + /permission/admin/manage/identity/consentmgt/add + + + + /permission/admin/manage/identity/consentmgt/delete + + + + /permission/admin/manage/identity/identitymgt + + + + /permission/admin/manage/identity/applicationmgt/create + + + /permission/admin/manage/identity/applicationmgt/delete + + + /permission/admin/manage/identity/applicationmgt/update + + + /permission/admin/manage/identity/applicationmgt/view + + + /permission/admin/manage/identity/applicationmgt/delete + + + /permission/admin/manage/identity/applicationmgt/create + + + /permission/admin/manage/identity/applicationmgt/view + + + /permission/admin/manage/identity/pep + + + /permission/admin/manage/identity/usermgt/create + + + /permission/admin/manage/identity/usermgt/list + + + /permission/admin/manage/identity/rolemgt/create + + + /permission/admin/manage/identity/rolemgt/view + + + /permission/admin/manage/identity/usermgt/view + + + /permission/admin/manage/identity/usermgt/update + + + /permission/admin/manage/identity/usermgt/update + + + /permission/admin/manage/identity/usermgt/delete + + + /permission/admin/manage/identity/rolemgt/view + + + /permission/admin/manage/identity/rolemgt/update + + + /permission/admin/manage/identity/rolemgt/update + + + /permission/admin/manage/identity/rolemgt/delete + + + /permission/admin/login + + + /permission/admin/manage/identity/usermgt/delete + + + /permission/admin/login + + + /permission/admin/login + + + /permission/admin/manage/identity/usermgt/create + + + + + + + + + /permission/admin/manage/identity/usermgt + + + /permission/admin/manage/identity/applicationmgt + + + + + + + /permission/admin/manage/identity/usermgt/update + + + + + + /permission/admin/manage/humantask/viewtasks + + + /permission/admin/login + + + /permission/admin/manage/identity/usermgt + + + /permission/admin/manage/identity/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + /api/identity/user/v1.0/ + /api/identity/consent-mgt/v1.0/ + /api/identity/recovery/v0.9/ + /oauth2/ + /scim2/ + /api/identity/entitlement/ + /api/identity/oauth2/dcr/v1.1/ + + + /identity/(.*) + + + + + 300 + + + + true + + 1000 + 1000 + 51200 + + + + + + http://localhost:8280/ + + 5 + + + + + + + + + + diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/testng.xml b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/testng.xml new file mode 100644 index 000000000000..c8ba227b3449 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/testng.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + \ No newline at end of file From 70d31f91cd23ab7c63303f39b405a433bd86efc6 Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Thu, 31 Aug 2023 16:24:22 +0530 Subject: [PATCH 11/21] change update return --- .../role/mgt/ApplicationRoleManager.java | 2 +- .../role/mgt/ApplicationRoleManagerImpl.java | 16 +-- .../role/mgt/dao/ApplicationRoleMgtDAO.java | 4 +- .../dao/impl/ApplicationRoleMgtDAOImpl.java | 5 +- .../CacheBackedApplicationRoleMgtDAOImpl.java | 4 +- .../impl/ApplicationRoleMgtDAOImplTest.java | 99 ++++++++++++++++--- 6 files changed, 104 insertions(+), 26 deletions(-) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java index 1fd7f1605eaf..406e54d444f0 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java @@ -45,7 +45,7 @@ public interface ApplicationRoleManager { * @param removedScopes List of scopes to be removed. * @throws ApplicationRoleManagementException Error occurred while updating the application role. */ - void updateApplicationRole(String applicationId, String roleId, String newName, List addedScopes, + ApplicationRole updateApplicationRole(String applicationId, String roleId, String newName, List addedScopes, List removedScopes) throws ApplicationRoleManagementException; /** diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java index e2def326878e..564a0d91a43e 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java @@ -72,11 +72,13 @@ public void addApplicationRole(ApplicationRole applicationRole) throws Applicati } @Override - public void updateApplicationRole(String applicationId, String roleId, String newName, List addedScopes, - List removedScopes) throws ApplicationRoleManagementException { + public ApplicationRole updateApplicationRole(String applicationId, String roleId, String newName, + List addedScopes, List removedScopes) + throws ApplicationRoleManagementException { // TODO: Check authorized scopes for the app and filter out added permissions - applicationRoleMgtDAO.updateApplicationRole(roleId, newName, addedScopes, removedScopes, getTenantDomain()); + return applicationRoleMgtDAO.updateApplicationRole(roleId, newName, addedScopes, removedScopes, + getTenantDomain()); } @Override @@ -138,10 +140,10 @@ public ApplicationRole updateApplicationRoleAssignedGroups(String roleId, String removeCommonValues(addedGroups, removedGroups); return applicationRoleMgtDAO.updateApplicationRoleAssignedGroups(roleId, identityProvider, addedGroups, removedGroups, getTenantDomain()); - } catch (IdentityProviderManagementException e) { - throw new ApplicationRoleManagementException("Error while retrieving idp", - "Error while retrieving idp for idpId: " + idpId, e); - } + } catch (IdentityProviderManagementException e) { + throw new ApplicationRoleManagementException("Error while retrieving idp", + "Error while retrieving idp for idpId: " + idpId, e); + } } @Override diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java index f5d58bf46997..aa78112630f9 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java @@ -38,8 +38,8 @@ ApplicationRole getApplicationRoleById(String roleId, String tenantDomain) List getApplicationRoles(String applicationId) throws ApplicationRoleManagementServerException; - void updateApplicationRole(String roleId, String newName, List addedScopes, List removedScopes, - String tenantDomain) + ApplicationRole updateApplicationRole(String roleId, String newName, List addedScopes, + List removedScopes, String tenantDomain) throws ApplicationRoleManagementServerException; void deleteApplicationRole(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException; diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java index 9ae78b615100..c011efbb76de 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -138,7 +138,7 @@ public List getApplicationRoles(String applicationId) } @Override - public void updateApplicationRole(String roleId, String newName, List addedScopes, + public ApplicationRole updateApplicationRole(String roleId, String newName, List addedScopes, List removedScopes, String tenantDomain) throws ApplicationRoleManagementServerException { @@ -156,6 +156,7 @@ public void updateApplicationRole(String roleId, String newName, List ad // TODO: Remove scopes return null; }); + return getApplicationRoleById(roleId, tenantDomain); } catch (TransactionException e) { throw handleServerException(ERROR_CODE_UPDATE_ROLE, e, roleId); } @@ -219,7 +220,6 @@ public ApplicationRole updateApplicationRoleAssignedUsers(String roleId, List { @@ -282,7 +282,6 @@ public ApplicationRole updateApplicationRoleAssignedGroups(String roleId, Identi throws ApplicationRoleManagementException { validateGroupIds(identityProvider, addedGroups, tenantDomain); - validateGroupIds(identityProvider, removedGroups, tenantDomain); NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { return namedJdbcTemplate.withTransaction(template -> { diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java index fc1f63914a5a..4812851ecb70 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java @@ -77,12 +77,12 @@ public List getApplicationRoles(String applicationId) } @Override - public void updateApplicationRole(String roleId, String newName, List addedScopes, + public ApplicationRole updateApplicationRole(String roleId, String newName, List addedScopes, List removedScopes, String tenantDomain) throws ApplicationRoleManagementServerException { clearFromCache(roleId, tenantDomain); - applicationRoleMgtDAO.updateApplicationRole(roleId, newName, addedScopes, removedScopes, tenantDomain); + return applicationRoleMgtDAO.updateApplicationRole(roleId, newName, addedScopes, removedScopes, tenantDomain); } @Override diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java index e6ed6fba43e2..41226bc1aed1 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java @@ -71,8 +71,19 @@ public void tearDown() throws Exception { closeH2Database(); } - @Test - public void testAddApplicationRole() throws Exception { + @DataProvider + public Object[][] addApplicationRoleData() { + return new Object[][]{ + {ROLE_ID, ROLE_NAME}, + {"TEST_ROLE_ID-1", "TEST_ROLE_NAME-1"}, + {"TEST_ROLE_ID-2", "TEST_ROLE_NAME-2"}, + {"TEST_ROLE_ID-3", "TEST_ROLE_NAME-3"}, + + }; + } + + @Test(dataProvider = "addApplicationRoleData", priority = 2) + public void testAddApplicationRole(String roleId, String roleName) throws Exception { mockStatic(IdentityTenantUtil.class); when(IdentityTenantUtil.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID); @@ -80,24 +91,84 @@ public void testAddApplicationRole() throws Exception { Mockito.when(IdentityDatabaseUtil.getDataSource()).thenReturn(dataSourceMap.get(DB_NAME)); ApplicationRole applicationRole = new ApplicationRole(); applicationRole.setApplicationId(String.valueOf(APP_ID)); - applicationRole.setRoleId(ROLE_ID); - applicationRole.setRoleName(ROLE_NAME); + applicationRole.setRoleId(roleId); + applicationRole.setRoleName(roleName); ApplicationRole addedApplicationRole = daoImpl.addApplicationRole(applicationRole, TENANT_DOMAIN); Assert.assertNotNull(addedApplicationRole); } + @Test(priority = 2) + public void testGetApplicationRoles() throws Exception { + + mockStatic(IdentityTenantUtil.class); + when(IdentityTenantUtil.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID); + mockStatic(IdentityDatabaseUtil.class); + Mockito.when(IdentityDatabaseUtil.getDataSource()).thenReturn(dataSourceMap.get(DB_NAME)); + List applicationRoles = daoImpl.getApplicationRoles(String.valueOf(APP_ID)); + Assert.assertNotNull(applicationRoles); + } + + @DataProvider + public Object[][] getApplicationRoleByIdData() { + return new Object[][]{ + {ROLE_ID}, + {"TEST_ROLE_ID-1"}, + {"TEST_ROLE_ID-2"}, + {"TEST_ROLE_ID-3"}, + + }; + } + @Test(dataProvider = "getApplicationRoleByIdData", priority = 2) + public void testGetApplicationRoleById(String roleId) throws Exception { + + mockStatic(IdentityTenantUtil.class); + when(IdentityTenantUtil.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID); + mockStatic(IdentityDatabaseUtil.class); + Mockito.when(IdentityDatabaseUtil.getDataSource()).thenReturn(dataSourceMap.get(DB_NAME)); + ApplicationRole applicationRole = daoImpl.getApplicationRoleById(roleId, TENANT_DOMAIN); + Assert.assertNotNull(applicationRole); + } + + @DataProvider + public Object[][] updateApplicationRoleData() { + return new Object[][]{ + {"TEST_ROLE_ID-1", "TEST_ROLE_NEW_NAME-1"}, + {"TEST_ROLE_ID-2", "TEST_ROLE_NEW_NAME-2"}, + {"TEST_ROLE_ID-3", "TEST_ROLE_NEW_NAME-3"}, + + }; + } + @Test(dataProvider = "updateApplicationRoleData", priority = 2) + public void testUpdateApplicationRole(String roleId, String newName) throws Exception { + + mockStatic(IdentityTenantUtil.class); + when(IdentityTenantUtil.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID); + mockStatic(IdentityDatabaseUtil.class); + Mockito.when(IdentityDatabaseUtil.getDataSource()).thenReturn(dataSourceMap.get(DB_NAME)); + ApplicationRole applicationRole = daoImpl.updateApplicationRole(roleId, newName, new ArrayList<>(), + new ArrayList<>(), TENANT_DOMAIN); + Assert.assertEquals(applicationRole.getRoleName(), newName); + } + @DataProvider public Object[][] updateApplicationRoleAssignedUsersData() { return new Object[][]{ {new ArrayList<>(Arrays.asList("USER_1", "USER_2", "USER_3")), - new ArrayList<>(Collections.emptyList()), + new ArrayList<>(Collections.emptyList()), 3 + }, + {new ArrayList<>(Arrays.asList("USER_4", "USER_5", "USER_6")), + new ArrayList<>(Arrays.asList("USER_1", "USER_2", "USER_3")), 3 + }, + {new ArrayList<>(Collections.emptyList()), + new ArrayList<>(Arrays.asList("USER_4", "USER_5", "USER_6")), 0 }, }; } @Test(dataProvider = "updateApplicationRoleAssignedUsersData", priority = 2) - public void testUpdateApplicationRoleAssignedUsers(List addedUsers, List removedUsers) + public void testUpdateApplicationRoleAssignedUsers(List addedUsers, List removedUsers, + int resultCount) throws Exception { mockStatic(ApplicationRoleMgtUtils.class); @@ -107,21 +178,27 @@ public void testUpdateApplicationRoleAssignedUsers(List addedUsers, List when(ApplicationRoleMgtUtils.isUserExists(anyString())).thenReturn(true); ApplicationRole role = daoImpl.updateApplicationRoleAssignedUsers(ROLE_ID, addedUsers, removedUsers, TENANT_DOMAIN); - Assert.assertEquals(role.getAssignedUsers().size(), addedUsers.size()); + Assert.assertEquals(role.getAssignedUsers().size(), resultCount); } @DataProvider public Object[][] updateApplicationRoleAssignedGroupsData() { return new Object[][]{ {new ArrayList<>(Arrays.asList("GROUP_1", "GROUP_2", "GROUP_3")), - new ArrayList<>(Collections.emptyList()), + new ArrayList<>(Collections.emptyList()), 3 + }, + {new ArrayList<>(Arrays.asList("GROUP_4", "GROUP_5", "GROUP_6")), + new ArrayList<>(Arrays.asList("GROUP_1", "GROUP_2", "GROUP_3")), 3 + }, + {new ArrayList<>(Collections.emptyList()), + new ArrayList<>(Arrays.asList("GROUP_4", "GROUP_5", "GROUP_6")), 0 }, - }; } @Test(dataProvider = "updateApplicationRoleAssignedGroupsData", priority = 2) - public void testUpdateApplicationRoleAssignedGroups(List addedGroups, List removedGroups) + public void testUpdateApplicationRoleAssignedGroups(List addedGroups, List removedGroups, + int resultCount) throws Exception { mockStatic(ApplicationRoleMgtUtils.class); @@ -142,7 +219,7 @@ public void testUpdateApplicationRoleAssignedGroups(List addedGroups, Li ApplicationRole role = daoImpl.updateApplicationRoleAssignedGroups(ROLE_ID, identityProvider, addedGroups, removedGroups, TENANT_DOMAIN); - Assert.assertEquals(role.getAssignedGroups().size(), addedGroups.size()); + Assert.assertEquals(role.getAssignedGroups().size(), resultCount); } private void populateApplication() throws Exception { From d640fff7cff8f85c0f7ff0d66d352de1ca98c5bb Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Thu, 31 Aug 2023 16:55:47 +0530 Subject: [PATCH 12/21] add unit tests --- .../impl/ApplicationRoleMgtDAOImplTest.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java index 41226bc1aed1..7f764223f206 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java @@ -13,6 +13,7 @@ import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; import org.wso2.carbon.identity.application.common.model.IdPGroup; import org.wso2.carbon.identity.application.common.model.IdentityProvider; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementServerException; import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; import org.wso2.carbon.identity.application.role.mgt.util.ApplicationRoleMgtUtils; import org.wso2.carbon.identity.core.persistence.JDBCPersistenceManager; @@ -150,6 +151,50 @@ public void testUpdateApplicationRole(String roleId, String newName) throws Exce Assert.assertEquals(applicationRole.getRoleName(), newName); } + @DataProvider + public Object[][] checkRoleExistsData() { + return new Object[][]{ + {"TEST_ROLE_ID-1", true}, + {"TEST_ROLE_ID-2", true}, + {"TEST_ROLE_ID-3", true}, + {"FAKE_ROLE_ID-3", false}, + + }; + } + @Test(dataProvider = "checkRoleExistsData", priority = 2) + public void testCheckRoleExists(String roleId, boolean isExists) throws Exception { + + mockStatic(IdentityTenantUtil.class); + when(IdentityTenantUtil.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID); + mockStatic(IdentityDatabaseUtil.class); + Mockito.when(IdentityDatabaseUtil.getDataSource()).thenReturn(dataSourceMap.get(DB_NAME)); + boolean isRoleExists = daoImpl.checkRoleExists(roleId, TENANT_DOMAIN); + Assert.assertEquals(isRoleExists, isExists); + } + + @DataProvider + public Object[][] deleteApplicationRoleData() { + return new Object[][]{ + {"TEST_ROLE_ID-1"}, + {"TEST_ROLE_ID-2"}, + {"TEST_ROLE_ID-3"}, + + }; + } + @Test(dataProvider = "deleteApplicationRoleData", priority = 3) + public void testDeleteApplicationRole(String roleId) { + + mockStatic(IdentityTenantUtil.class); + when(IdentityTenantUtil.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID); + mockStatic(IdentityDatabaseUtil.class); + Mockito.when(IdentityDatabaseUtil.getDataSource()).thenReturn(dataSourceMap.get(DB_NAME)); + try { + daoImpl.deleteApplicationRole(roleId, TENANT_DOMAIN); + } catch (ApplicationRoleManagementServerException e) { + Assert.fail(); + } + } + @DataProvider public Object[][] updateApplicationRoleAssignedUsersData() { return new Object[][]{ From c0ad0dfc7d15e484a241a8055f81d2114934aaf9 Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Thu, 31 Aug 2023 17:24:04 +0530 Subject: [PATCH 13/21] add method comments --- .../role/mgt/dao/ApplicationRoleMgtDAO.java | 100 ++++++++++++++++++ .../dao/impl/ApplicationRoleMgtDAOImpl.java | 4 +- .../mgt/util/ApplicationRoleMgtUtils.java | 27 +++++ .../role/mgt/ApplicationRoleManagerTest.java | 7 -- .../src/test/resources/testng.xml | 1 - 5 files changed, 129 insertions(+), 10 deletions(-) delete mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerTest.java diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java index aa78112630f9..71eaa00bdeac 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/ApplicationRoleMgtDAO.java @@ -30,43 +30,143 @@ */ public interface ApplicationRoleMgtDAO { + /** + * Add application role. + * + * @param applicationRole Application role. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementServerException Error occurred while adding application role. + */ ApplicationRole addApplicationRole(ApplicationRole applicationRole, String tenantDomain) throws ApplicationRoleManagementServerException; + /** + * Get application role by id. + * + * @param roleId Application roleId. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementServerException Error occurred while retrieving application role. + */ ApplicationRole getApplicationRoleById(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException; + /** + * Get all application role by application id. + * + * @param applicationId Application roleId. + * @throws ApplicationRoleManagementServerException Error occurred while retrieving all application role by app id. + */ List getApplicationRoles(String applicationId) throws ApplicationRoleManagementServerException; + /** + * Update application role. + * + * @param roleId Application roleId. + * @param newName New Application role name. + * @param addedScopes Scope to be added. + * @param removedScopes Scope to be removed. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementServerException Error occurred while updating application role. + */ ApplicationRole updateApplicationRole(String roleId, String newName, List addedScopes, List removedScopes, String tenantDomain) throws ApplicationRoleManagementServerException; + /** + * Delete application role. + * + * @param roleId Application roleId. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementServerException Error occurred while deleting application role. + */ void deleteApplicationRole(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException; + /** + * Check application role exists by name. + * + * @param applicationId Application id. + * @param roleName Role name. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementServerException Error occurred while checking application role exists by name. + */ boolean isExistingRole(String applicationId, String roleName, String tenantDomain) throws ApplicationRoleManagementServerException; + /** + * Check application role exists by id. + * + * @param roleId Application role id. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementServerException Error occurred while checking application role exists by id. + */ boolean checkRoleExists(String roleId, String tenantDomain) throws ApplicationRoleManagementServerException; + /** + * Update application role assigned users. + * + * @param roleId Application roleId. + * @param addedUsers Assigned users to be added. + * @param removedUsers Assigned users to be removed. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementException Error occurred while updating application role assigned users. + */ ApplicationRole updateApplicationRoleAssignedUsers(String roleId, List addedUsers, List removedUsers, String tenantDomain) throws ApplicationRoleManagementException; + /** + * Get application role assigned users. + * + * @param roleId Application roleId. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementException Error occurred while getting application role assigned users. + */ ApplicationRole getApplicationRoleAssignedUsers(String roleId, String tenantDomain) throws ApplicationRoleManagementException; + /** + * Update application role assigned groups. + * + * @param roleId Application roleId. + * @param identityProvider Identity provider. + * @param addedGroups Assigned groups to be added. + * @param removedGroups Assigned groups to be removed. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementException Error occurred while updating application role assigned groups. + */ ApplicationRole updateApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, List addedGroups, List removedGroups, String tenantDomain) throws ApplicationRoleManagementException; + /** + * Get application role assigned groups. + * + * @param roleId Application roleId. + * @param identityProvider Identity provider. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementException Error occurred while getting application role assigned groups. + */ ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityProvider identityProvider, String tenantDomain) throws ApplicationRoleManagementException; + /** + * Get application roles by userId. + * + * @param userId User Id. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementException Error occurred while getting application roles by userId. + */ List getApplicationRolesByUserId(String userId, String tenantDomain) throws ApplicationRoleManagementException; + /** + * Get application roles by groupId. + * + * @param groupId Group Id. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementException Error occurred while getting application roles by groupId. + */ List getApplicationRolesByGroupId(String groupId, String tenantDomain) throws ApplicationRoleManagementException; } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java index c011efbb76de..47bf2cdce24e 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -269,7 +269,7 @@ public ApplicationRole getApplicationRoleAssignedUsers(String roleId, String ten } ApplicationRole applicationRole = new ApplicationRole(); applicationRole.setAssignedUsers(users); - return applicationRole; + return applicationRole; } catch (DataAccessException e) { throw handleServerException(ERROR_CODE_GET_ROLE_ASSIGNED_USERS, e, roleId); } @@ -357,7 +357,7 @@ public ApplicationRole getApplicationRoleAssignedGroups(String roleId, IdentityP } ApplicationRole applicationRole = new ApplicationRole(); applicationRole.setAssignedGroups(groups); - return applicationRole; + return applicationRole; } catch (DataAccessException e) { throw handleServerException(ERROR_CODE_GET_ROLE_ASSIGNED_GROUPS, e, roleId); } diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java index f231e32b887c..005863c019dc 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java @@ -82,6 +82,12 @@ public static ApplicationRoleManagementClientException handleClientException( return new ApplicationRoleManagementClientException(error.getMessage(), description, error.getCode()); } + /** + * Check group exists by id. + * + * @param id Group ID. + * @throws ApplicationRoleManagementException Error occurred while checking group exists. + */ public static boolean isGroupExists(String id) throws ApplicationRoleManagementException { AbstractUserStoreManager userStoreManager; @@ -94,6 +100,14 @@ public static boolean isGroupExists(String id) throws ApplicationRoleManagementE + "to resolve group name for the groupID: " + id, e); } } + + /** + * Get group name by id. + * + * @param id Group ID. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementException Error occurred while getting group name by id. + */ public static String getGroupNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException { String groupName; @@ -113,6 +127,12 @@ public static String getGroupNameByID(String id, String tenantDomain) throws App return groupName; } + /** + * Check user exists by id. + * + * @param id User ID. + * @throws ApplicationRoleManagementException Error occurred while checking user exists. + */ public static boolean isUserExists(String id) throws ApplicationRoleManagementException { AbstractUserStoreManager userStoreManager; @@ -126,6 +146,13 @@ public static boolean isUserExists(String id) throws ApplicationRoleManagementEx } } + /** + * Get username by id. + * + * @param id User ID. + * @param tenantDomain Tenant domain. + * @throws ApplicationRoleManagementException Error occurred while getting username by id. + */ public static String getUserNameByID(String id, String tenantDomain) throws ApplicationRoleManagementException { String userName; diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerTest.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerTest.java deleted file mode 100644 index 084365c5813a..000000000000 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerTest.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.wso2.carbon.identity.application.role.mgt; - -import org.powermock.modules.testng.PowerMockTestCase; - -public class ApplicationRoleManagerTest extends PowerMockTestCase { - -} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/testng.xml b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/testng.xml index c8ba227b3449..c8b8370bffbd 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/testng.xml +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/testng.xml @@ -22,7 +22,6 @@ - \ No newline at end of file From 63fae606f655ac2ca2690e716a6c2e09ccd87fa4 Mon Sep 17 00:00:00 2001 From: Thilina Shashimal Senarath Date: Mon, 4 Sep 2023 12:13:12 +0530 Subject: [PATCH 14/21] add manager tests --- .../pom.xml | 77 +++++-- .../role/mgt/ApplicationRoleManager.java | 2 +- .../role/mgt/ApplicationRoleManagerImpl.java | 24 ++- .../dao/impl/ApplicationRoleMgtDAOImpl.java | 46 ++++- .../CacheBackedApplicationRoleMgtDAOImpl.java | 12 ++ .../mgt/util/ApplicationRoleMgtUtils.java | 6 + .../mgt/ApplicationRoleManagerImplTest.java | 192 ++++++++++++++++++ .../impl/ApplicationRoleMgtDAOImplTest.java | 34 ---- .../src/test/resources/dbscripts/h2.sql | 6 + .../conf}/carbon.xml | 30 ++- .../conf}/identity/identity.xml | 4 +- .../src/test/resources/testng.xml | 1 + .../resources/identity.xml.j2 | 16 ++ 13 files changed, 379 insertions(+), 71 deletions(-) create mode 100644 components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImplTest.java rename components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/{repository.conf => repository/conf}/carbon.xml (97%) rename components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/{repository.conf => repository/conf}/identity/identity.xml (99%) diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml index 80bd924b84df..52784ca145ab 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/pom.xml @@ -38,18 +38,6 @@ org.apache.felix.scr.ds-annotations provided - - org.wso2.eclipse.osgi - org.eclipse.osgi.services - - - org.eclipse.osgi - org.eclipse.osgi - - - commons-logging - commons-logging - org.wso2.carbon.identity.framework org.wso2.carbon.identity.core @@ -135,6 +123,71 @@ + + org.apache.maven.plugins + maven-surefire-plugin + ${maven.surefire.plugin.version} + + + --add-opens java.xml/jdk.xml.internal=ALL-UNNAMED + --add-exports java.base/jdk.internal.loader=ALL-UNNAMED + + + src/test/resources/testng.xml + + + + + org.jacoco + jacoco-maven-plugin + ${jacoco.version} + + + default-prepare-agent + + prepare-agent + + + + default-prepare-agent-integration + + prepare-agent-integration + + + + default-report + + report + + + + default-report-integration + + report-integration + + + + default-check + + check + + + + + BUNDLE + + + COMPLEXITY + COVEREDRATIO + 0.90 + + + + + + + + org.codehaus.mojo findbugs-maven-plugin diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java index 406e54d444f0..22f436a3f163 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManager.java @@ -34,7 +34,7 @@ public interface ApplicationRoleManager { * @param applicationRole Application role. * @throws ApplicationRoleManagementException Error occurred while adding application role. */ - void addApplicationRole(ApplicationRole applicationRole) throws ApplicationRoleManagementException; + ApplicationRole addApplicationRole(ApplicationRole applicationRole) throws ApplicationRoleManagementException; /** * Update application role. diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java index 564a0d91a43e..d9d591cd63e9 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImpl.java @@ -58,7 +58,8 @@ public static ApplicationRoleManager getInstance() { new CacheBackedApplicationRoleMgtDAOImpl(new ApplicationRoleMgtDAOImpl()); @Override - public void addApplicationRole(ApplicationRole applicationRole) throws ApplicationRoleManagementException { + public ApplicationRole addApplicationRole(ApplicationRole applicationRole) + throws ApplicationRoleManagementException { String tenantDomain = getTenantDomain(); boolean existingRole = @@ -68,7 +69,7 @@ public void addApplicationRole(ApplicationRole applicationRole) throws Applicati throw handleClientException(ERROR_CODE_DUPLICATE_ROLE, applicationRole.getRoleName(), applicationRole.getApplicationId()); } - applicationRoleMgtDAO.addApplicationRole(applicationRole, tenantDomain); + return applicationRoleMgtDAO.addApplicationRole(applicationRole, tenantDomain); } @Override @@ -76,6 +77,7 @@ public ApplicationRole updateApplicationRole(String applicationId, String roleId List addedScopes, List removedScopes) throws ApplicationRoleManagementException { + validateAppRoleId(roleId); // TODO: Check authorized scopes for the app and filter out added permissions return applicationRoleMgtDAO.updateApplicationRole(roleId, newName, addedScopes, removedScopes, getTenantDomain()); @@ -84,6 +86,7 @@ public ApplicationRole updateApplicationRole(String applicationId, String roleId @Override public ApplicationRole getApplicationRoleById(String roleId) throws ApplicationRoleManagementException { + validateAppRoleId(roleId); return applicationRoleMgtDAO.getApplicationRoleById(roleId, getTenantDomain()); } @@ -96,6 +99,7 @@ public List getApplicationRoles(String applicationId) throws Ap @Override public void deleteApplicationRole(String roleId) throws ApplicationRoleManagementException { + validateAppRoleId(roleId); applicationRoleMgtDAO.deleteApplicationRole(roleId, getTenantDomain()); } @@ -184,6 +188,12 @@ public List getApplicationRolesByGroupId(String groupId, String return applicationRoleMgtDAO.getApplicationRolesByGroupId(groupId, tenantDomain); } + /** + * Validate application role id. + * + * @param roleId Role ID. + * @throws ApplicationRoleManagementException Error occurred while validating roleId. + */ private void validateAppRoleId(String roleId) throws ApplicationRoleManagementException { boolean isExists = applicationRoleMgtDAO.checkRoleExists(roleId, getTenantDomain()); @@ -193,11 +203,21 @@ private void validateAppRoleId(String roleId) throws ApplicationRoleManagementEx } } + /** + * Get tenant domain. + * + */ private static String getTenantDomain() { return PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); } + /** + * Remove common values in given two lists. + * + * @param list1 List 1. + * @param list2 List 2. + */ private void removeCommonValues(List list1, List list2) { HashSet set = new HashSet<>(list1); diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java index 47bf2cdce24e..edd741469c59 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImpl.java @@ -219,7 +219,7 @@ public ApplicationRole updateApplicationRoleAssignedUsers(String roleId, List { @@ -281,7 +281,7 @@ public ApplicationRole updateApplicationRoleAssignedGroups(String roleId, Identi String tenantDomain) throws ApplicationRoleManagementException { - validateGroupIds(identityProvider, addedGroups, tenantDomain); + validateGroupIds(identityProvider, addedGroups); NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { return namedJdbcTemplate.withTransaction(template -> { @@ -403,7 +403,14 @@ public List getApplicationRolesByGroupId(String groupId, String } } - public void validateGroupIds(IdentityProvider identityProvider, List groups, String tenantDomain) + /** + * Validate groups. + * + * @param identityProvider Identity Provider. + * @param groups Group IDs. + * @throws ApplicationRoleManagementException Error occurred while validating groups. + */ + private void validateGroupIds(IdentityProvider identityProvider, List groups) throws ApplicationRoleManagementException { if (LOCAL_IDP.equals(identityProvider.getIdentityProviderName())) { @@ -427,7 +434,13 @@ public void validateGroupIds(IdentityProvider identityProvider, List gro } } - public void validateUserIds(List users, String tenantDomain) + /** + * Validate users. + * + * @param users User IDs. + * @throws ApplicationRoleManagementException Error occurred while validating users. + */ + private void validateUserIds(List users) throws ApplicationRoleManagementException { for (String userId : users) { @@ -438,24 +451,49 @@ public void validateUserIds(List users, String tenantDomain) } } + /** + * Get username by user id. + * + * @param userID User IDs. + * @param tenantDomain Tenant Domain. + * @throws ApplicationRoleManagementException Error occurred while getting username by id. + */ private String getUserNamesByID(String userID, String tenantDomain) throws ApplicationRoleManagementException { return ApplicationRoleMgtUtils.getUserNameByID(userID, tenantDomain); } + /** + * Get group name by user id. + * + * @param groupID Group IDs. + * @param tenantDomain Tenant Domain. + * @throws ApplicationRoleManagementException Error occurred while getting group name by id. + */ private String getGroupNamesByID(String groupID, String tenantDomain) throws ApplicationRoleManagementException { return ApplicationRoleMgtUtils.getGroupNameByID(groupID, tenantDomain); } + /** + * Check SQL Unique Key Constrain Violated. + * + * @param e Transaction Exception. + * @param constraint SQL constraint. + */ private boolean checkUniqueKeyConstrainViolated(TransactionException e, String constraint) { String errorMessage = e.getCause().getCause().getMessage(); return errorMessage.toLowerCase().contains(constraint.toLowerCase()); } + /** + * Get tenant id by name. + * + * @param tenantDomain Tenant Domain. + */ private int getTenantId(String tenantDomain) { int tenantID; diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java index 4812851ecb70..3a1582f40968 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/CacheBackedApplicationRoleMgtDAOImpl.java @@ -174,6 +174,12 @@ private ApplicationRole getApplicationRoleFromCache(String applicationRoleId, St return applicationRole; } + /** + * Add to cache. + * + * @param applicationRole Application role. + * @param tenantDomain Tenant domain. + */ private void addToCache(ApplicationRole applicationRole, String tenantDomain) { if (LOG.isDebugEnabled()) { @@ -186,6 +192,12 @@ private void addToCache(ApplicationRole applicationRole, String tenantDomain) { applicationRoleCache.addToCache(cacheKey, cacheEntry, tenantDomain); } + /** + * Clear from cache. + * + * @param applicationRoleId Application role id. + * @param tenantDomain Tenant domain. + */ private void clearFromCache(String applicationRoleId, String tenantDomain) { if (LOG.isDebugEnabled()) { diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java index 005863c019dc..c023f2932312 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/main/java/org/wso2/carbon/identity/application/role/mgt/util/ApplicationRoleMgtUtils.java @@ -172,6 +172,12 @@ public static String getUserNameByID(String id, String tenantDomain) throws Appl return userName; } + /** + * Get username by id. + * + * @param tenantId Tenant ID. + * @throws UserStoreException Error occurred while getting user store manager. + */ private static AbstractUserStoreManager getUserStoreManager(int tenantId) throws UserStoreException { RealmService realmService = ApplicationRoleMgtServiceComponentHolder.getInstance().getRealmService(); diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImplTest.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImplTest.java new file mode 100644 index 000000000000..988801ee91b8 --- /dev/null +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/ApplicationRoleManagerImplTest.java @@ -0,0 +1,192 @@ +package org.wso2.carbon.identity.application.role.mgt; + +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.wso2.carbon.identity.application.role.mgt.exceptions.ApplicationRoleManagementException; +import org.wso2.carbon.identity.application.role.mgt.model.ApplicationRole; +import org.wso2.carbon.identity.common.testng.WithAxisConfiguration; +import org.wso2.carbon.identity.common.testng.WithCarbonHome; +import org.wso2.carbon.identity.common.testng.WithH2Database; +import org.wso2.carbon.identity.common.testng.WithRealmService; +import org.wso2.carbon.identity.common.testng.WithRegistry; + +import java.util.ArrayList; + +@WithAxisConfiguration +@WithCarbonHome +@WithRegistry +@WithRealmService +@WithH2Database(jndiName = "jdbc/WSO2IdentityDB", files = {"dbScripts/h2.sql"}) +public class ApplicationRoleManagerImplTest extends PowerMockTestCase { + + private ApplicationRoleManager applicationRoleManager; + + @BeforeClass + public void setUp() { + + applicationRoleManager = ApplicationRoleManagerImpl.getInstance(); + } + + @AfterClass + public void tearDown() { + + } + + @DataProvider(name = "addApplicationRoleDataProvider") + public Object[][] addApplicationRoleData() { + + ApplicationRole applicationRole1 = createApplicationRole("1"); + ApplicationRole applicationRole2 = createApplicationRole("2"); + return new Object[][]{ + {applicationRole1}, + {applicationRole2} + }; + } + + @Test(dataProvider = "addApplicationRoleDataProvider", priority = 1) + public void testAddApplicationRole(ApplicationRole applicationRole) throws ApplicationRoleManagementException { + + ApplicationRole role = applicationRoleManager.addApplicationRole(applicationRole); + Assert.assertNotNull(role); + } + + @Test(priority = 1) + public void testAddApplicationRoleException() throws Exception { + + ApplicationRole applicationRole = createApplicationRole("1"); + applicationRoleManager.addApplicationRole(applicationRole); + ApplicationRoleManagementException exception = null; + try { + applicationRoleManager.addApplicationRole(applicationRole); + } catch (ApplicationRoleManagementException e) { + exception = e; + } + Assert.assertNotNull(exception); + } + + @DataProvider(name = "getApplicationRoleByIdData") + public Object[][] getApplicationRoleByIdData() throws ApplicationRoleManagementException { + + ApplicationRole applicationRole1 = createApplicationRole("1"); + ApplicationRole applicationRole2 = createApplicationRole("2"); + applicationRoleManager.addApplicationRole(applicationRole1); + applicationRoleManager.addApplicationRole(applicationRole2); + return new Object[][]{ + {applicationRole1}, + {applicationRole2} + }; + } + + @Test(dataProvider = "getApplicationRoleByIdData", priority = 2) + public void testGetApplicationRoleById(ApplicationRole applicationRole) throws Exception { + + ApplicationRole role = applicationRoleManager.getApplicationRoleById(applicationRole.getRoleId()); + Assert.assertNotNull(role); + } + + @Test(priority = 2) + public void testGetApplicationRoleByIdException() { + + + Exception exception = null; + try { + applicationRoleManager.getApplicationRoleById("fake-id"); + } catch (Exception e) { + exception = e; + } + Assert.assertNotNull(exception); + } + + @DataProvider(name = "updateApplicationRoleData") + public Object[][] updateApplicationRoleData() throws ApplicationRoleManagementException { + + ApplicationRole applicationRole1 = createApplicationRole("1"); + ApplicationRole applicationRole2 = createApplicationRole("2"); + applicationRoleManager.addApplicationRole(applicationRole1); + applicationRoleManager.addApplicationRole(applicationRole2); + return new Object[][]{ + {applicationRole1, "NEW_NAME-1"}, + {applicationRole2, "NEW_NAME-2"} + }; + } + + @Test(dataProvider = "updateApplicationRoleData", priority = 2) + public void testUpdateApplicationRole(ApplicationRole applicationRole, String newName) throws Exception { + + ApplicationRole role = applicationRoleManager.updateApplicationRole(applicationRole.getApplicationId(), + applicationRole.getRoleId(), newName, new ArrayList<>(), new ArrayList<>()); + Assert.assertNotNull(role); + } + + @Test(priority = 2) + public void testUpdateApplicationRoleException() { + + + Exception exception = null; + try { + applicationRoleManager.updateApplicationRole("fake-app-id", + "fake-roke-id", "newName", new ArrayList<>(), new ArrayList<>()); + } catch (Exception e) { + exception = e; + } + Assert.assertNotNull(exception); + } + + @DataProvider(name = "deleteApplicationRoleData") + public Object[][] deleteApplicationRoleData() throws ApplicationRoleManagementException { + + ApplicationRole applicationRole1 = createApplicationRole("1"); + ApplicationRole applicationRole2 = createApplicationRole("2"); + applicationRoleManager.addApplicationRole(applicationRole1); + applicationRoleManager.addApplicationRole(applicationRole2); + return new Object[][]{ + {applicationRole1}, + {applicationRole2} + }; + } + + @Test(dataProvider = "deleteApplicationRoleData", priority = 2) + public void testDeleteApplicationRole(ApplicationRole applicationRole) throws Exception { + + Exception exception = null; + try { + applicationRoleManager.deleteApplicationRole(applicationRole.getRoleId()); + } catch (Exception e) { + exception = e; + } + Assert.assertNull(exception); + } + + @Test(priority = 2) + public void testDeleteApplicationRoleException() { + + + Exception exception = null; + try { + applicationRoleManager.deleteApplicationRole("fake-id"); + } catch (Exception e) { + exception = e; + } + Assert.assertNotNull(exception); + } + + /** + * Create application role with the given postfix. + * + * @param postFix Postfix to be appended to each API resource and scope information. + * @return Application Role. + */ + private static ApplicationRole createApplicationRole(String postFix) { + + ApplicationRole applicationRole = new ApplicationRole(); + applicationRole.setRoleId("testAppRoleId-" + postFix); + applicationRole.setRoleName("testAppRoleName-" + postFix); + applicationRole.setApplicationId("1"); + applicationRole.setPermissions(new String[0]); + return applicationRole; + } +} diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java index 7f764223f206..f7b0ff93aa7c 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/java/org/wso2/carbon/identity/application/role/mgt/dao/impl/ApplicationRoleMgtDAOImplTest.java @@ -22,7 +22,6 @@ import java.nio.file.Paths; import java.sql.Connection; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -44,14 +43,9 @@ public class ApplicationRoleMgtDAOImplTest extends PowerMockTestCase { private static final String TENANT_DOMAIN = "TEST_TENANT_DOMAIN"; private static final int TENANT_ID = 2; private static final int APP_ID = 1; - private static final String APP_NAME = "TEST_APP_NAME"; - private static final String USER_STORE = "TEST_USER_STORE"; - private static final String USERNAME = "TEST_USERNAME"; - private static final String AUTH_TYPE = "TEST_AUTH_TYPE"; private static final String ROLE_ID = "TEST_ROLE_ID"; private static final String ROLE_NAME = "TEST_ROLE_NAME"; private static final int IDP_ID = 1; - private static final String IDP_NAME = "TEST_IDP_NAME"; private static Map dataSourceMap = new HashMap<>(); private ApplicationRoleMgtDAOImpl daoImpl; @@ -62,8 +56,6 @@ public void setUp() throws Exception { daoImpl = new ApplicationRoleMgtDAOImpl(); initiateH2Database(getFilePath()); - populateApplication(); - populateIdp(); } @AfterClass @@ -267,32 +259,6 @@ public void testUpdateApplicationRoleAssignedGroups(List addedGroups, Li Assert.assertEquals(role.getAssignedGroups().size(), resultCount); } - private void populateApplication() throws Exception { - - String domainDataSQL = "INSERT INTO SP_APP (ID, TENANT_ID, APP_NAME, USER_STORE, USERNAME, AUTH_TYPE, UUID) " + - "VALUES " + "(" + APP_ID + "," + TENANT_ID + ",'" + APP_NAME + "','" + USER_STORE + "','" + USERNAME - + "','" + AUTH_TYPE + "','" + APP_ID + "')"; - - try { - connection.createStatement().executeUpdate(domainDataSQL); - } catch (SQLException e) { - String errorMessage = "Error while Adding test data for SP_APP table"; - throw new Exception(errorMessage, e); - } - } - - private void populateIdp() throws Exception { - - String domainDataSQL = "INSERT INTO IDP (ID, TENANT_ID, NAME, UUID) " + - "VALUES " + "(" + IDP_ID + "," + TENANT_ID + ",'" + IDP_NAME + "','" + IDP_ID + "')"; - - try { - connection.createStatement().executeUpdate(domainDataSQL); - } catch (SQLException e) { - String errorMessage = "Error while Adding test data for IDP table"; - throw new Exception(errorMessage, e); - } - } /** * Initiate H2 database. diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/dbscripts/h2.sql b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/dbscripts/h2.sql index e8e1f919401b..54d8c6e9ac04 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/dbscripts/h2.sql +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/dbscripts/h2.sql @@ -1400,3 +1400,9 @@ CREATE INDEX IDX_AUTH_PROP_AUTH_ID ON IDP_AUTHENTICATOR_PROPERTY (AUTHENTICATOR_ -- IDN_CONFIG_FILE -- CREATE INDEX IDX_CON_FILE_RES_ID ON IDN_CONFIG_FILE (RESOURCE_ID); +-- POPULATE SP_APP FOR UNIT TESTS -- +INSERT INTO SP_APP (ID, TENANT_ID, APP_NAME, USER_STORE, USERNAME, AUTH_TYPE, UUID) VALUES (1, 2, 'TEST_APP_NAME', + 'TEST_USER_STORE', 'TEST_USERNAME', 'TEST_AUTH_TYPE', '1'); + +-- POPULATE IDP FOR UNIT TESTS -- + INSERT INTO IDP (ID, TENANT_ID, NAME, UUID) VALUES (1, 2, 'TEST_IDP_NAME', '1'); \ No newline at end of file diff --git a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/carbon.xml b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository/conf/carbon.xml similarity index 97% rename from components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/carbon.xml rename to components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository/conf/carbon.xml index b46fba1be7df..7374caf39e37 100644 --- a/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository.conf/carbon.xml +++ b/components/application-role-mgt/org.wso2.carbon.identity.application.role.mgt/src/test/resources/repository/conf/carbon.xml @@ -1,21 +1,19 @@ + Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +-->