Java Code Examples for org.wso2.carbon.user.api.UserStoreManager#getUserClaimValues()

The following examples show how to use org.wso2.carbon.user.api.UserStoreManager#getUserClaimValues() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtendedPasswordGrantHandler.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
private Claim[] getUserClaimValues(String authorizedUser, UserStoreManager userStoreManager)
        throws
        UserStoreException {
    Claim[] userClaims = userClaimsCache.getValueFromCache(authorizedUser);
    if(userClaims != null){
        return userClaims;
    }else{
        if(log.isDebugEnabled()){
            log.debug("Cache miss for user claims. Username :" + authorizedUser);
        }
        userClaims = userStoreManager.getUserClaimValues(
                authorizedUser, null);
        userClaimsCache.addToCache(authorizedUser,userClaims);
        return userClaims;
    }
}
 
Example 2
Source File: UserIdentityManagementUtil.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all user claims
 *
 * @param userName
 * @return
 * @throws IdentityMgtServiceException
 */
public static UserIdentityClaimDTO[] getAllUserIdentityClaims(String userName)
        throws IdentityMgtServiceException {
    int tenantId = 0;
    try {
        tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
        UserStoreManager userStoreManager =
                IdentityMgtServiceComponent.getRealmService()
                        .getTenantUserRealm(tenantId)
                        .getUserStoreManager();
        // read all claims and convert them to UserIdentityClaimDTO
        Claim[] claims = userStoreManager.getUserClaimValues(userName, null);
        List<UserIdentityClaimDTO> allDefaultClaims = new ArrayList<UserIdentityClaimDTO>();
        for (Claim claim : claims) {
            if (claim.getClaimUri().contains(UserCoreConstants.DEFAULT_CARBON_DIALECT)) {
                UserIdentityClaimDTO claimDTO = new UserIdentityClaimDTO();
                claimDTO.setClaimUri(claim.getClaimUri());
                claimDTO.setClaimValue(claim.getValue());
                allDefaultClaims.add(claimDTO);
            }
        }
        UserIdentityClaimDTO[] claimDTOs = new UserIdentityClaimDTO[allDefaultClaims.size()];
        return allDefaultClaims.toArray(claimDTOs);
    } catch (UserStoreException e) {
        throw new IdentityMgtServiceException("Error while getting user identity claims", e);
    }
}
 
Example 3
Source File: DefaultClaimHandler.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
private Map<String, String> retrieveAllNunNullUserClaimValues(AuthenticatedUser authenticatedUser,
                                                              String tenantDomain,
                                                              String tenantAwareUserName, ClaimManager claimManager,
                                                              UserStoreManager userStore)
        throws FrameworkException {

    Map<String, String> allLocalClaims = new HashMap<>();
    try {

        org.wso2.carbon.user.api.ClaimMapping[] claimMappings = claimManager
                .getAllClaimMappings(ApplicationConstants.LOCAL_IDP_DEFAULT_CLAIM_DIALECT);
        List<String> localClaimURIs = new ArrayList<>();
        for (org.wso2.carbon.user.api.ClaimMapping mapping : claimMappings) {
            String claimURI = mapping.getClaim().getClaimUri();
            localClaimURIs.add(claimURI);
        }
        allLocalClaims = userStore.getUserClaimValues(tenantAwareUserName,
                localClaimURIs.toArray(new String[localClaimURIs.size()]), null);
    } catch (UserStoreException e) {
        if (e.getMessage().contains("UserNotFound")) {
            if (log.isDebugEnabled()) {
                log.debug("User " + tenantAwareUserName + " not found in user store");
            }
        } else {
            throw new FrameworkException("Error occurred while getting all user claims for " +
                    authenticatedUser + " in " + tenantDomain, e);
        }
    }
    if (allLocalClaims == null) {
        allLocalClaims = new HashMap<>();
    }
    return allLocalClaims;
}
 
Example 4
Source File: UserIdentityManagementUtil.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all user claims
 *
 * @param userName
 * @return
 * @throws IdentityMgtServiceException
 */
public static UserIdentityClaimDTO[] getAllUserIdentityClaims(String userName)
        throws IdentityMgtServiceException {
    int tenantId = 0;
    try {
        tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
        UserStoreManager userStoreManager =
                IdentityMgtServiceComponent.getRealmService()
                        .getTenantUserRealm(tenantId)
                        .getUserStoreManager();
        // read all claims and convert them to UserIdentityClaimDTO
        Claim[] claims = userStoreManager.getUserClaimValues(userName, null);
        List<UserIdentityClaimDTO> allDefaultClaims = new ArrayList<UserIdentityClaimDTO>();
        for (Claim claim : claims) {
            if (claim.getClaimUri().contains(UserCoreConstants.DEFAULT_CARBON_DIALECT)) {
                UserIdentityClaimDTO claimDTO = new UserIdentityClaimDTO();
                claimDTO.setClaimUri(claim.getClaimUri());
                claimDTO.setClaimValue(claim.getValue());
                allDefaultClaims.add(claimDTO);
            }
        }
        UserIdentityClaimDTO[] claimDTOs = new UserIdentityClaimDTO[allDefaultClaims.size()];
        return allDefaultClaims.toArray(claimDTOs);
    } catch (UserStoreException e) {
        throw new IdentityMgtServiceException("Error while getting user identity claims", e);
    }
}
 
Example 5
Source File: UserProfileAdmin.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieve a claim of the authorized user.
 *
 * @param claimUri    Claim URI in wso2 dialect.
 * @param profileName User profile name.
 * @return Claim value.
 * @throws UserProfileException
 */
public String getUserClaim(String claimUri, String profileName) throws UserProfileException {

    if (StringUtils.isBlank(claimUri)) {
        throw new UserProfileException("Invalid input parameter. Claim URI cannot be null.");
    }
    if (StringUtils.isBlank(profileName)) {
        throw new UserProfileException("Invalid input parameter. Profile name cannot be null.");
    }
    String loggedInUsername = CarbonContext.getThreadLocalCarbonContext().getUsername();
    if (StringUtils.isBlank(loggedInUsername)) {
        throw new UserProfileException("Could not find a logged in user in the current carbon context.");
    }

    String claimValue = null;
    try {
        UserStoreManager userStoreManager = getUserRealm().getUserStoreManager();
        int index = loggedInUsername.indexOf(UserCoreConstants.DOMAIN_SEPARATOR);

        if (index < 0) {
            if (log.isDebugEnabled()) {
                log.debug("Logged in username : '" + loggedInUsername + "' does not contain domain name.");
            }
            /* if domain is not provided, this can be the scenario where user from a secondary user store
            logs in without domain name and tries to view his own profile. */
            MessageContext messageContext = MessageContext.getCurrentMessageContext();
            HttpServletRequest request = (HttpServletRequest) messageContext
                    .getProperty(TRANSPORT_HTTP_SERVLET_REQUEST);
            String domainName = (String) request.getSession().getAttribute(LOGGED_IN_DOMAIN);
            if (StringUtils.isNotBlank(domainName)) {
                loggedInUsername = domainName + UserCoreConstants.DOMAIN_SEPARATOR + loggedInUsername;
            }
        }
        index = loggedInUsername.indexOf(UserCoreConstants.DOMAIN_SEPARATOR);
        UserStoreManager secUserStoreManager = null;

        // Check whether we have a secondary UserStoreManager setup.
        if (index > 0) {
            // Using the short-circuit. User name comes with the domain name.
            String domain = loggedInUsername.substring(0, index);
            if (log.isDebugEnabled()) {
                log.debug("Domain name found in the logged in username. Domain name: " + domain);
            }
            if (userStoreManager instanceof AbstractUserStoreManager) {
                secUserStoreManager = ((AbstractUserStoreManager) userStoreManager)
                        .getSecondaryUserStoreManager(domain);
            }
        }
        Map<String, String> claimValues;
        if (secUserStoreManager != null) {
            claimValues = secUserStoreManager.getUserClaimValues(loggedInUsername, new String[]{claimUri},
                    profileName);
        } else {
            claimValues = userStoreManager.getUserClaimValues(loggedInUsername, new String[]{claimUri},
                    profileName);
        }
        if (claimValues != null) {
            claimValue = claimValues.get(claimUri);
        }
    } catch (UserStoreException e) {
        String message = String.format("An error occurred while getting the user claim '%s' in '%s' profile of " +
                "the user '%s'", claimUri, profileName, loggedInUsername);
        log.error(message, e);
        throw new UserProfileException(message, e);
    }
    return claimValue;
}