Skip to content

Commit

Permalink
Merge pull request #270 from sadilchamishka/resolve-user-id
Browse files Browse the repository at this point in the history
Add util method to get the user ID of an associated user
  • Loading branch information
sadilchamishka authored Oct 25, 2023
2 parents 78be659 + 894cb2c commit d851f97
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2022, WSO2 Inc. (http://www.wso2.com).
~ Copyright (c) 2022-2023, WSO2 LLC. (http://www.wso2.com).
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ 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.
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Expand Down Expand Up @@ -59,6 +61,10 @@
<groupId>org.wso2.carbon.identity.organization.management.core</groupId>
<artifactId>org.wso2.carbon.identity.organization.management.service</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.organization.management</groupId>
<artifactId>org.wso2.carbon.identity.organization.management.organization.user.sharing</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -101,6 +107,8 @@
org.wso2.carbon.identity.organization.management.authz.service.handler;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.authz;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.exception;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.organization.user.sharing.util;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.organization.user.sharing.models;version="${org.wso2.identity.organization.mgt.imp.pkg.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.user.core.common;version="${carbon.kernel.package.import.version.range}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@
import org.wso2.carbon.identity.organization.management.authz.service.OrganizationManagementAuthorizationContext;
import org.wso2.carbon.identity.organization.management.authz.service.exception.OrganizationManagementAuthzServiceServerException;
import org.wso2.carbon.identity.organization.management.authz.service.internal.OrganizationManagementAuthzServiceHolder;
import org.wso2.carbon.identity.organization.management.organization.user.sharing.util.OrganizationSharedUserUtil;
import org.wso2.carbon.identity.organization.management.service.authz.OrganizationManagementAuthorizationManager;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementServerException;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

import java.util.Optional;

import static org.wso2.carbon.identity.auth.service.util.Constants.OAUTH2_ALLOWED_SCOPES;
import static org.wso2.carbon.identity.auth.service.util.Constants.OAUTH2_VALIDATE_SCOPE;
import static org.wso2.carbon.identity.organization.management.authz.service.constant.AuthorizationConstants.RESOURCE_PERMISSION_NONE;
Expand Down Expand Up @@ -143,7 +147,7 @@ private void validatePermissions(String orgId, String permissionString, User use

try {
boolean isUserAuthorized = OrganizationManagementAuthorizationManager.getInstance().isUserAuthorized
(getUserId(user), permissionString, orgId);
(getUserId(user, orgId), permissionString, orgId);
if (isUserAuthorized) {
authorizationResult.setAuthorizationStatus(AuthorizationStatus.GRANT);
}
Expand All @@ -152,12 +156,15 @@ private void validatePermissions(String orgId, String permissionString, User use
}
}

private String getUserId(User user) throws OrganizationManagementAuthzServiceServerException {
private String getUserId(User user, String orgId) throws OrganizationManagementAuthzServiceServerException {

try {
AbstractUserStoreManager userStoreManager = (AbstractUserStoreManager) getUserStoreManager(user);
return userStoreManager.getUser(null, user.getUserName()).getUserID();
} catch (UserStoreException e) {
String userId = userStoreManager.getUser(null, user.getUserName()).getUserID();
Optional<String> optionalUserId =
OrganizationSharedUserUtil.getUserIdOfAssociatedUserByOrgId(userId, orgId);
return optionalUserId.orElse(userId);
} catch (UserStoreException | OrganizationManagementException e) {
throw new OrganizationManagementAuthzServiceServerException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.carbon.identity.organization.management.organization.user.sharing.internal;

import org.wso2.carbon.identity.organization.management.organization.user.sharing.OrganizationUserSharingService;
import org.wso2.carbon.identity.organization.management.role.management.service.RoleManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.user.core.service.RealmService;
Expand All @@ -31,6 +32,7 @@ public class OrganizationUserSharingDataHolder {
private RealmService realmService;
private OrganizationManager organizationManager;
private RoleManager roleManager;
private OrganizationUserSharingService organizationUserSharingService;

public static OrganizationUserSharingDataHolder getInstance() {

Expand Down Expand Up @@ -96,4 +98,24 @@ public void setRoleManager(RoleManager roleManager) {

this.roleManager = roleManager;
}

/**
* Get the organization user sharing service.
*
* @return OrganizationUserSharingService organization user sharing service.
*/
public OrganizationUserSharingService getOrganizationUserSharingService() {

return organizationUserSharingService;
}

/**
* Set the organization user sharing service.
*
* @param organizationUserSharingService Organization user sharing service.
*/
public void setOrganizationUserSharingService(OrganizationUserSharingService organizationUserSharingService) {

this.organizationUserSharingService = organizationUserSharingService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ public class OrganizationUserSharingServiceComponent {
protected void activate(ComponentContext componentContext) {

BundleContext bundleContext = componentContext.getBundleContext();
bundleContext.registerService(OrganizationUserSharingService.class.getName(),
new OrganizationUserSharingServiceImpl(), null);
OrganizationUserSharingService organizationUserSharingService = new OrganizationUserSharingServiceImpl();
OrganizationUserSharingDataHolder.getInstance()
.setOrganizationUserSharingService(organizationUserSharingService);
bundleContext.registerService(OrganizationUserSharingService.class.getName(), organizationUserSharingService,
null);
bundleContext.registerService(UserOperationEventListener.class.getName(),
new SharedUserOperationEventListener(), null);
bundleContext.registerService(AbstractEventHandler.class.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

package org.wso2.carbon.identity.organization.management.organization.user.sharing.util;

import org.wso2.carbon.identity.organization.management.organization.user.sharing.internal.OrganizationUserSharingDataHolder;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
import org.wso2.carbon.user.core.UserStoreException;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;

import java.util.Map;
import java.util.Optional;

import static org.wso2.carbon.identity.organization.management.organization.user.sharing.constant.UserSharingConstants.CLAIM_MANAGED_ORGANIZATION;

Expand All @@ -39,4 +42,14 @@ public static String getUserManagedOrganizationClaim(AbstractUserStoreManager us
return claimsMap.get(CLAIM_MANAGED_ORGANIZATION);
}


/**
* Get the user ID of the associated user by the organization ID.
*/
public static Optional<String> getUserIdOfAssociatedUserByOrgId(String associatedUserId, String orgId)
throws OrganizationManagementException {

return Optional.ofNullable(OrganizationUserSharingDataHolder.getInstance().getOrganizationUserSharingService()
.getUserAssociationOfAssociatedUserByOrgId(associatedUserId, orgId).getUserId());
}
}

0 comments on commit d851f97

Please sign in to comment.