Search in sources :

Example 11 with RealmConfiguration

use of org.wso2.carbon.user.core.config.RealmConfiguration in project core-util by WSO2Telco.

the class UserAuthorizationValidator method isAuthorizedRole.

public boolean isAuthorizedRole(String userName, Set<String> allowedRolesSet) {
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    RealmService realmService = (RealmService) carbonContext.getOSGiService(RealmService.class, null);
    try {
        RealmConfiguration realmConfiguration = new RealmConfiguration();
        String[] currentUserRoles = realmService.getUserRealm(realmConfiguration).getUserStoreManager().getRoleListOfUser(userName);
        List<String> currentUserRolesList = Arrays.asList(currentUserRoles);
        Iterator<String> iterator = allowedRolesSet.iterator();
        while (iterator.hasNext()) {
            String allowedRole = iterator.next();
            if (currentUserRolesList.contains(allowedRole)) {
                return true;
            }
        }
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        log.error("authorization failed for user : " + userName, e);
        return false;
    }
    log.error("authorization failed for user : " + userName);
    return false;
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) RealmService(org.wso2.carbon.user.core.service.RealmService) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext)

Example 12 with RealmConfiguration

use of org.wso2.carbon.user.core.config.RealmConfiguration in project carbon-apimgt by wso2.

the class AbstractJWTGenerator method getMultiAttributeSeparator.

protected String getMultiAttributeSeparator(int tenantId) {
    try {
        RealmConfiguration realmConfiguration = null;
        RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
        if (realmService != null && tenantId != MultitenantConstants.INVALID_TENANT_ID) {
            UserStoreManager userStoreManager = (UserStoreManager) realmService.getTenantUserRealm(tenantId).getUserStoreManager();
            realmConfiguration = userStoreManager.getRealmConfiguration();
        }
        if (realmConfiguration != null) {
            String claimSeparator = realmConfiguration.getUserStoreProperty(APIConstants.MULTI_ATTRIBUTE_SEPARATOR);
            if (claimSeparator != null && !claimSeparator.trim().isEmpty()) {
                return claimSeparator;
            }
        }
    } catch (UserStoreException e) {
        log.error("Error occurred while getting the realm configuration, User store properties might not be " + "returned", e);
    }
    return null;
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager)

Aggregations

RealmConfiguration (org.wso2.carbon.user.api.RealmConfiguration)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Test (org.junit.Test)5 UserRealm (org.wso2.carbon.user.core.UserRealm)5 RealmService (org.wso2.carbon.user.core.service.RealmService)5 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3 UserStoreException (org.wso2.carbon.user.core.UserStoreException)3 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)3 File (java.io.File)2 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)2 RealmConfigXMLProcessor (org.wso2.carbon.user.core.config.RealmConfigXMLProcessor)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 Environment (org.wso2.carbon.apimgt.api.model.Environment)1 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)1 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)1 APIMConfigService (org.wso2.carbon.apimgt.impl.config.APIMConfigService)1 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)1