Search in sources :

Example 1 with RealmConfiguration

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

the class UserRoleProsser method getRolesByUserName.

public List<String> getRolesByUserName(String userName) {
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    RealmService realmService = (RealmService) carbonContext.getOSGiService(RealmService.class, null);
    List<String> currentUserRoleList = null;
    try {
        RealmConfiguration realmConfiguration = new RealmConfiguration();
        String[] currentUserRoles = realmService.getUserRealm(realmConfiguration).getUserStoreManager().getRoleListOfUser(userName);
        currentUserRoleList = Arrays.asList(currentUserRoles);
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        log.error("unable to retrieve user roles for user " + userName + " : ", e);
    }
    if (currentUserRoleList != null && !currentUserRoleList.isEmpty()) {
        return currentUserRoleList;
    } else {
        return Collections.emptyList();
    }
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) RealmService(org.wso2.carbon.user.core.service.RealmService) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext)

Example 2 with RealmConfiguration

use of org.wso2.carbon.user.api.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)

Aggregations

PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)2 RealmConfiguration (org.wso2.carbon.user.api.RealmConfiguration)2 RealmService (org.wso2.carbon.user.core.service.RealmService)2