Search in sources :

Example 11 with UserFieldDTO

use of org.wso2.carbon.identity.user.profile.stub.types.UserFieldDTO in project carbon-identity-framework by wso2.

the class UserProfileAdmin method getUserProfiles.

public UserProfileDTO[] getUserProfiles(String username) throws UserProfileException {
    UserProfileDTO[] profiles;
    String[] availableProfileConfigurations = new String[0];
    String profileConfig = null;
    try {
        if (!this.isAuthorized(username, USER_PROFILE_VIEW_PERMISSION)) {
            throw new UserProfileException(authorizationFailureMessage);
        }
        // Any other attempt is unauthorized. So attempts will be logged and denied.
        if (isAdminProfileSpoof(username)) {
            log.warn("Unauthorized attempt. User " + CarbonContext.getThreadLocalCarbonContext().getUsername() + " is trying to view the profile of the admin user.");
            throw new UserProfileException(authorizationFailureMessage);
        }
        UserRealm realm = getUserRealm();
        UserStoreManager userStoreManager = realm.getUserStoreManager();
        boolean isReadOnly = userStoreManager.isReadOnly();
        int index;
        index = username.indexOf("/");
        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 = username.substring(0, index);
            if (userStoreManager instanceof AbstractUserStoreManager) {
                secUserStoreManager = ((AbstractUserStoreManager) userStoreManager).getSecondaryUserStoreManager(domain);
                if (secUserStoreManager != null) {
                    isReadOnly = secUserStoreManager.isReadOnly();
                }
            }
        }
        ProfileConfigurationManager profileAdmin = realm.getProfileConfigurationManager();
        if (profileAdmin != null) {
            availableProfileConfigurations = getAvailableProfileConfiguration(profileAdmin);
        }
        String[] profileNames = null;
        if (secUserStoreManager != null) {
            profileNames = secUserStoreManager.getProfileNames(username);
        } else {
            profileNames = userStoreManager.getProfileNames(username);
        }
        profiles = new UserProfileDTO[profileNames.length];
        Claim[] claims = getAllSupportedClaims(realm, UserCoreConstants.DEFAULT_CARBON_DIALECT);
        String[] claimUris = new String[claims.length + 1];
        for (int i = 0; i < claims.length; i++) {
            claimUris[i] = claims[i].getClaimUri();
        }
        claimUris[claims.length] = UserCoreConstants.PROFILE_CONFIGURATION;
        for (int i = 0; i < profileNames.length; i++) {
            String profile = profileNames[i];
            Map<String, String> valueMap = userStoreManager.getUserClaimValues(username, claimUris, profile);
            List<UserFieldDTO> userFields = new ArrayList<UserFieldDTO>();
            for (int j = 0; j < claims.length; j++) {
                UserFieldDTO data = new UserFieldDTO();
                Claim claim = claims[j];
                String claimUri = claim.getClaimUri();
                if (!UserCoreConstants.PROFILE_CONFIGURATION.equals(claimUri)) {
                    data.setClaimUri(claimUri);
                    data.setFieldValue(valueMap.get(claimUri));
                    data.setDisplayName(claim.getDisplayTag());
                    data.setRegEx(claim.getRegEx());
                    data.setRequired(claim.isRequired());
                    data.setDisplayOrder(claim.getDisplayOrder());
                    data.setCheckedAttribute(claim.isCheckedAttribute());
                    data.setReadOnly(claim.isReadOnly());
                    userFields.add(data);
                }
            }
            UserProfileDTO temp = new UserProfileDTO();
            temp.setProfileName(profile);
            temp.setFieldValues(userFields.toArray(new UserFieldDTO[userFields.size()]));
            temp.setProfileConfigurations(availableProfileConfigurations);
            profileConfig = valueMap.get(UserCoreConstants.PROFILE_CONFIGURATION);
            if (profileConfig == null) {
                profileConfig = UserCoreConstants.DEFAULT_PROFILE_CONFIGURATION;
            }
            if (isReadOnly) {
                profileConfig = "readonly";
            }
            temp.setProfileConifuration(profileConfig);
            profiles[i] = temp;
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        // Not logging. Already logged.
        throw new UserProfileException(e.getMessage(), e);
    }
    return profiles;
}
Also used : ArrayList(java.util.ArrayList) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) UserStoreException(org.wso2.carbon.user.api.UserStoreException) CarbonException(org.wso2.carbon.CarbonException) FederatedAssociationManagerException(org.wso2.carbon.identity.user.profile.mgt.association.federation.exception.FederatedAssociationManagerException) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException) ProfileConfigurationManager(org.wso2.carbon.user.core.profile.ProfileConfigurationManager) UserRealm(org.wso2.carbon.user.core.UserRealm) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) Claim(org.wso2.carbon.user.api.Claim)

Example 12 with UserFieldDTO

use of org.wso2.carbon.identity.user.profile.stub.types.UserFieldDTO in project carbon-identity-framework by wso2.

the class UserProfileAdmin method setUserProfile.

public void setUserProfile(String username, UserProfileDTO profile) throws UserProfileException {
    UserRealm realm = null;
    try {
        if (!this.isAuthorized(username, USER_PROFILE_MANAGE_PERMISSION)) {
            throw new UserProfileException(authorizationFailureMessage);
        }
        // Any other attempt is unauthorized. So attempts will be logged and denied.
        if (isAdminProfileSpoof(username)) {
            log.warn("Unauthorized attempt. User " + CarbonContext.getThreadLocalCarbonContext().getUsername() + " is trying to modify the profile of the admin user.");
            throw new UserProfileException(authorizationFailureMessage);
        }
        int indexOne;
        indexOne = username.indexOf("/");
        if (indexOne < 0) {
            /*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.servletRequest");
            String domainName = (String) request.getSession().getAttribute("logged_in_domain");
            if (domainName != null) {
                username = domainName + "/" + username;
            }
        }
        realm = getUserRealm();
        UserFieldDTO[] udatas = profile.getFieldValues();
        Map<String, String> map = new HashMap<String, String>();
        for (UserFieldDTO data : udatas) {
            String claimURI = data.getClaimUri();
            String value = data.getFieldValue();
            if (!data.isReadOnly()) {
                // Quick fix for not to remove OTP checkbox when false
                if (value == "" && "http://wso2.org/claims/identity/otp".equals(claimURI)) {
                    value = "false";
                }
                map.put(claimURI, value);
            }
        }
        if (profile.getProfileConifuration() != null) {
            map.put(UserCoreConstants.PROFILE_CONFIGURATION, profile.getProfileConifuration());
        } else {
            map.put(UserCoreConstants.PROFILE_CONFIGURATION, UserCoreConstants.DEFAULT_PROFILE_CONFIGURATION);
        }
        UserStoreManager admin = realm.getUserStoreManager();
        // User store manager expects tenant aware username
        admin.setUserClaimValues(username, map, profile.getProfileName());
    } catch (UserStoreException e) {
        // Not logging. Already logged.
        throw new UserProfileException(e.getMessage(), e);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new UserProfileException(e.getMessage(), e);
    }
}
Also used : HashMap(java.util.HashMap) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) UserStoreException(org.wso2.carbon.user.api.UserStoreException) CarbonException(org.wso2.carbon.CarbonException) FederatedAssociationManagerException(org.wso2.carbon.identity.user.profile.mgt.association.federation.exception.FederatedAssociationManagerException) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException) HttpServletRequest(javax.servlet.http.HttpServletRequest) UserRealm(org.wso2.carbon.user.core.UserRealm) UserStoreException(org.wso2.carbon.user.api.UserStoreException) MessageContext(org.apache.axis2.context.MessageContext)

Example 13 with UserFieldDTO

use of org.wso2.carbon.identity.user.profile.stub.types.UserFieldDTO in project carbon-identity-framework by wso2.

the class UserRegistrationService method addUser.

public void addUser(UserDTO user) throws Exception {
    UserFieldDTO[] userFieldDTOs = null;
    Map<String, String> userClaims = null;
    userFieldDTOs = user.getUserFields();
    userClaims = new HashMap<String, String>();
    if (userFieldDTOs != null) {
        for (UserFieldDTO userFieldDTO : userFieldDTOs) {
            userClaims.put(userFieldDTO.getClaimUri(), userFieldDTO.getFieldValue());
        }
    }
    UserRealm realm = null;
    String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(user.getUserName());
    String tenantName = MultitenantUtils.getTenantDomain(user.getUserName());
    realm = IdentityTenantUtil.getRealm(tenantName, null);
    addUser(tenantAwareUserName, user.getPassword(), userClaims, null, realm);
}
Also used : UserRealm(org.wso2.carbon.user.core.UserRealm) UserFieldDTO(org.wso2.carbon.identity.user.registration.dto.UserFieldDTO)

Example 14 with UserFieldDTO

use of org.wso2.carbon.identity.user.profile.stub.types.UserFieldDTO in project carbon-identity-framework by wso2.

the class UserRegistrationService method getUserFieldDTO.

private UserFieldDTO getUserFieldDTO(String claimUri, String displayName, boolean isRequired, int displayOrder, String regex, boolean isSupportedByDefault) {
    UserFieldDTO fieldDTO = null;
    fieldDTO = new UserFieldDTO();
    fieldDTO.setClaimUri(claimUri);
    fieldDTO.setFieldName(displayName);
    fieldDTO.setRequired(isRequired);
    fieldDTO.setDisplayOrder(displayOrder);
    fieldDTO.setSupportedByDefault(isSupportedByDefault);
    fieldDTO.setRegEx(regex);
    return fieldDTO;
}
Also used : UserFieldDTO(org.wso2.carbon.identity.user.registration.dto.UserFieldDTO)

Example 15 with UserFieldDTO

use of org.wso2.carbon.identity.user.profile.stub.types.UserFieldDTO in project carbon-identity-framework by wso2.

the class UserRegistrationAdminServiceClient method addUser.

/**
 * Add new user.
 * @param username Username of the user.
 * @param password Password of the user.
 * @param userFields User fields to be updated.
 * @throws RemoteException
 * @throws UserRegistrationAdminServiceException
 */
public void addUser(String username, char[] password, List<UserFieldDTO> userFields) throws RemoteException, UserRegistrationAdminServiceException {
    UserDTO userDTO = new UserDTO();
    userDTO.setUserName(username);
    userDTO.setPassword(new String(password));
    userDTO.setUserFields(userFields.toArray(new UserFieldDTO[userFields.size()]));
    stub.addUser(userDTO);
}
Also used : UserFieldDTO(org.wso2.carbon.identity.user.registration.stub.dto.UserFieldDTO) UserDTO(org.wso2.carbon.identity.user.registration.stub.dto.UserDTO)

Aggregations

UserFieldDTO (org.wso2.carbon.identity.user.profile.stub.types.UserFieldDTO)9 UserProfileDTO (org.wso2.carbon.identity.user.profile.stub.types.UserProfileDTO)9 Test (org.testng.annotations.Test)7 ISIntegrationTest (org.wso2.identity.integration.common.utils.ISIntegrationTest)7 UserRealm (org.wso2.carbon.user.core.UserRealm)6 CarbonException (org.wso2.carbon.CarbonException)4 FederatedAssociationManagerException (org.wso2.carbon.identity.user.profile.mgt.association.federation.exception.FederatedAssociationManagerException)4 IdentityProviderManagementException (org.wso2.carbon.idp.mgt.IdentityProviderManagementException)4 UserStoreException (org.wso2.carbon.user.api.UserStoreException)4 UserProfileMgtServiceClient (org.wso2.identity.integration.common.clients.UserProfileMgtServiceClient)4 UserFieldDTO (org.wso2.carbon.identity.user.registration.dto.UserFieldDTO)3 Claim (org.wso2.carbon.user.api.Claim)3 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)3 AbstractUserStoreManager (org.wso2.carbon.user.core.common.AbstractUserStoreManager)3 ProfileConfigurationManager (org.wso2.carbon.user.core.profile.ProfileConfigurationManager)3 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MessageContext (org.apache.axis2.context.MessageContext)2 SetEnvironment (org.wso2.carbon.automation.engine.annotations.SetEnvironment)2 UserDTO (org.wso2.carbon.identity.user.registration.stub.dto.UserDTO)2