Search in sources :

Example 26 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class DefaultAttributeFinder method getSupportedAttributes.

/*
     * (non-Javadoc)
     *
     * @see org.wso2.carbon.identity.entitlement.pip.PIPAttributeFinder#getSupportedAttributes()
     */
public Set<String> getSupportedAttributes() {
    try {
        ClaimManager claimManager = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getClaimManager();
        ClaimMapping[] claims = claimManager.getAllClaimMappings(UserCoreConstants.DEFAULT_CARBON_DIALECT);
        for (ClaimMapping claim : claims) {
            supportedAttrs.add(claim.getClaim().getClaimUri());
        }
    } catch (Exception e) {
    // ignore
    }
    return supportedAttrs;
}
Also used : ClaimManager(org.wso2.carbon.user.api.ClaimManager) ClaimMapping(org.wso2.carbon.user.api.ClaimMapping) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Example 27 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class UserIdentityManagementUtil method getUserList.

private static String[] getUserList(int tenantId, String claim, String value, String profileName) throws IdentityMgtServiceException {
    org.wso2.carbon.user.core.UserStoreManager userStoreManager = null;
    String[] userList = null;
    RealmService realmService = IdentityMgtServiceComponent.getRealmService();
    try {
        if (realmService.getTenantUserRealm(tenantId) != null) {
            userStoreManager = (org.wso2.carbon.user.core.UserStoreManager) realmService.getTenantUserRealm(tenantId).getUserStoreManager();
        }
    } catch (Exception e) {
        String msg = "Error retrieving the user store manager for the tenant";
        throw new IdentityMgtServiceException(msg, e);
    }
    try {
        if (userStoreManager != null) {
            userList = userStoreManager.getUserList(claim, value, profileName);
        }
        return userList;
    } catch (Exception e) {
        String msg = "Unable to retrieve the claim for the given tenant";
        throw new IdentityMgtServiceException(msg, e);
    }
}
Also used : IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 28 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class UserIdentityManagementUtil method getAllUserIdentityClaims.

/**
 * 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);
    }
}
Also used : IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) ArrayList(java.util.ArrayList) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) UserIdentityClaimDTO(org.wso2.carbon.identity.mgt.dto.UserIdentityClaimDTO) Claim(org.wso2.carbon.user.api.Claim)

Example 29 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class UserIdentityManagementUtil method getUsernameByClaims.

/**
 * @param claims
 * @param tenantId
 * @return
 * @throws IdentityMgtServiceException - If user cannot be retrieved using the provided claims.
 */
public static String getUsernameByClaims(UserIdentityClaimDTO[] claims, int tenantId) throws IdentityMgtServiceException {
    if (claims == null || claims.length < 1) {
        throw new IdentityMgtServiceException("No fields found for user search");
    }
    String userName = null;
    String[] tempUserList = null;
    // passed array.
    for (int i = 0; i < claims.length; i++) {
        UserIdentityClaimDTO claim = claims[i];
        if (claim.getClaimUri() != null && claim.getClaimValue() != null) {
            String[] userList = getUserList(tenantId, claim.getClaimUri(), claim.getClaimValue(), null);
            if (userList != null && userList.length > 0) {
                if (userList.length == 1) {
                    return userList[0];
                } else {
                    // If more than one user find the first matching user. Hence need to define unique claims
                    if (tempUserList != null) {
                        for (int j = 0; j < tempUserList.length; j++) {
                            for (int x = 0; x < userList.length; x++) {
                                if (tempUserList[j].equals(userList[x])) {
                                    return userList[x];
                                }
                            }
                        }
                    }
                    tempUserList = userList;
                    continue;
                }
            } else {
                throw new IdentityMgtServiceException("No associated user is found for given claim values");
            }
        }
    }
    return userName;
}
Also used : IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) UserIdentityClaimDTO(org.wso2.carbon.identity.mgt.dto.UserIdentityClaimDTO)

Example 30 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class Utils method getClaimFromUserStoreManager.

/**
 * Get the claims from the user store manager
 *
 * @param userName user name
 * @param tenantId tenantId
 * @param claim    claim name
 * @return claim value
 * @throws IdentityException if fails
 */
public static String getClaimFromUserStoreManager(String userName, int tenantId, String claim) throws IdentityException {
    org.wso2.carbon.user.core.UserStoreManager userStoreManager = null;
    RealmService realmService = IdentityMgtServiceComponent.getRealmService();
    String claimValue = "";
    try {
        if (realmService.getTenantUserRealm(tenantId) != null) {
            userStoreManager = (org.wso2.carbon.user.core.UserStoreManager) realmService.getTenantUserRealm(tenantId).getUserStoreManager();
        }
    } catch (Exception e) {
        String msg = "Error retrieving the user store manager for tenant id : " + tenantId;
        log.error(msg, e);
        throw IdentityException.error(msg, e);
    }
    try {
        if (userStoreManager != null) {
            Map<String, String> claimsMap = userStoreManager.getUserClaimValues(userName, new String[] { claim }, UserCoreConstants.DEFAULT_PROFILE);
            if (claimsMap != null && !claimsMap.isEmpty()) {
                claimValue = claimsMap.get(claim);
            }
        }
        return claimValue;
    } catch (Exception e) {
        String msg = "Unable to retrieve the claim for user : " + userName;
        log.error(msg, e);
        throw IdentityException.error(msg, e);
    }
}
Also used : RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Aggregations

HashMap (java.util.HashMap)112 ArrayList (java.util.ArrayList)89 ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)66 UserStoreException (org.wso2.carbon.user.api.UserStoreException)65 Test (org.testng.annotations.Test)63 ClaimMetadataException (org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException)55 Map (java.util.Map)49 PreparedStatement (java.sql.PreparedStatement)48 SQLException (java.sql.SQLException)43 LocalClaim (org.wso2.carbon.identity.claim.metadata.mgt.model.LocalClaim)34 RealmService (org.wso2.carbon.user.core.service.RealmService)30 UserRealm (org.wso2.carbon.user.core.UserRealm)29 Claim (org.wso2.carbon.user.api.Claim)28 UserStoreException (org.wso2.carbon.user.core.UserStoreException)28 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)28 ResultSet (java.sql.ResultSet)27 Connection (java.sql.Connection)25 ClaimConfig (org.wso2.carbon.identity.application.common.model.ClaimConfig)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 Claim (org.wso2.carbon.identity.application.common.model.Claim)24