use of org.craftercms.studio.api.v2.service.security.UserService in project studio by craftercms.
the class SecurityServiceImpl method getUserRoles.
@Override
@ValidateParams
public Set<String> getUserRoles(@ValidateStringParam(name = "site") final String site, @ValidateStringParam(name = "user") String user, boolean includeGlobal) {
try {
// TODO: We should replace this with userService.getUserSiteRoles, but that one is protected by permissions.
// TODO: When the UserService is refactored to use UserServiceInternal, we could use that method and
// TODO: remove this one
List<Group> groups = userServiceInternal.getUserGroups(-1, user);
if (groups != null && groups.size() > 0) {
logger.debug("Groups for " + user + " in " + site + ": " + groups);
PermissionsConfigTO rolesConfig = loadConfiguration(site, getRoleMappingsFileName());
Set<String> userRoles = new HashSet<String>();
if (rolesConfig != null) {
Map<String, List<String>> rolesMap = rolesConfig.getRoles();
for (Group group : groups) {
String groupName = group.getGroupName();
if (StringUtils.equals(groupName, SYSTEM_ADMIN_GROUP)) {
Collection<List<String>> mapValues = rolesMap.values();
mapValues.forEach(valueList -> {
userRoles.addAll(valueList);
});
break;
} else {
List<String> roles = rolesMap.get(groupName);
if (roles != null) {
userRoles.addAll(roles);
}
}
}
}
if (includeGlobal) {
PermissionsConfigTO globalRolesConfig = loadGlobalRolesConfiguration();
addGlobalUserRoles(user, userRoles, globalRolesConfig);
List<String> groupNames = groups.stream().map(x -> x.getGroupName()).collect(Collectors.toList());
addGlobalGroupRoles(userRoles, groupNames, globalRolesConfig);
}
return userRoles;
} else {
logger.debug("No groups found for " + user + " in " + site);
}
} catch (ServiceLayerException | UserNotFoundException e) {
logger.error("Error while getting groups for user {0}", e);
}
return new HashSet<>(0);
}
Aggregations