Search in sources :

Example 1 with Group

use of org.craftercms.studio.api.v2.dal.Group in project studio by craftercms.

the class LdapAuthenticationProvider method addGroupToUser.

private void addGroupToUser(User user, String groupName) {
    Group group = new Group();
    group.setGroupName(groupName);
    group.setGroupDescription("Externally managed group");
    group.setOrganization(null);
    UserGroup userGroup = new UserGroup();
    userGroup.setGroup(group);
    if (user.getGroups() == null) {
        user.setGroups(new ArrayList<UserGroup>());
    }
    user.getGroups().add(userGroup);
}
Also used : UserGroup(org.craftercms.studio.api.v2.dal.UserGroup) Group(org.craftercms.studio.api.v2.dal.Group) UserGroup(org.craftercms.studio.api.v2.dal.UserGroup)

Example 2 with Group

use of org.craftercms.studio.api.v2.dal.Group in project studio by craftercms.

the class UserServiceImpl method deleteUsers.

@Override
@HasPermission(type = DefaultPermission.class, action = "delete_users")
public void deleteUsers(List<Long> userIds, List<String> usernames) throws ServiceLayerException, AuthenticationException, UserNotFoundException {
    User currentUser = getCurrentUser();
    if (CollectionUtils.containsAny(userIds, Arrays.asList(currentUser.getId())) || CollectionUtils.containsAny(usernames, Arrays.asList(currentUser.getUsername()))) {
        throw new ServiceLayerException("Cannot delete self.");
    }
    generalLockService.lock(REMOVE_SYSTEM_ADMIN_MEMBER_LOCK);
    try {
        try {
            Group g = groupServiceInternal.getGroupByName(SYSTEM_ADMIN_GROUP);
            List<User> members = groupServiceInternal.getGroupMembers(g.getId(), 0, Integer.MAX_VALUE, StringUtils.EMPTY);
            if (CollectionUtils.isNotEmpty(members)) {
                List<User> membersAfterRemove = new ArrayList<User>();
                membersAfterRemove.addAll(members);
                members.forEach(m -> {
                    if (CollectionUtils.isNotEmpty(userIds)) {
                        if (userIds.contains(m.getId())) {
                            membersAfterRemove.remove(m);
                        }
                    }
                    if (CollectionUtils.isNotEmpty(usernames)) {
                        if (usernames.contains(m.getUsername())) {
                            membersAfterRemove.remove(m);
                        }
                    }
                });
                if (CollectionUtils.isEmpty(membersAfterRemove)) {
                    throw new ServiceLayerException("Removing all members of the System Admin group is not allowed." + " We must have at least one system administrator.");
                }
            }
        } catch (GroupNotFoundException e) {
            throw new ServiceLayerException("The System Admin group is not found.", e);
        }
        List<User> toDelete = userServiceInternal.getUsersByIdOrUsername(userIds, usernames);
        userServiceInternal.deleteUsers(userIds, usernames);
        SiteFeed siteFeed = siteService.getSite(studioConfiguration.getProperty(CONFIGURATION_GLOBAL_SYSTEM_SITE));
        AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
        auditLog.setOperation(OPERATION_DELETE);
        auditLog.setActorId(getCurrentUser().getUsername());
        auditLog.setPrimaryTargetId(siteFeed.getSiteId());
        auditLog.setPrimaryTargetType(TARGET_TYPE_USER);
        auditLog.setPrimaryTargetValue(siteFeed.getName());
        List<AuditLogParameter> paramters = new ArrayList<AuditLogParameter>();
        for (User deletedUser : toDelete) {
            AuditLogParameter paramter = new AuditLogParameter();
            paramter.setTargetId(Long.toString(deletedUser.getId()));
            paramter.setTargetType(TARGET_TYPE_USER);
            paramter.setTargetValue(deletedUser.getUsername());
            paramters.add(paramter);
        }
        auditLog.setParameters(paramters);
        auditServiceInternal.insertAuditLog(auditLog);
    } finally {
        generalLockService.unlock(REMOVE_SYSTEM_ADMIN_MEMBER_LOCK);
    }
}
Also used : Group(org.craftercms.studio.api.v2.dal.Group) User(org.craftercms.studio.api.v2.dal.User) AuthenticatedUser(org.craftercms.studio.model.AuthenticatedUser) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) ArrayList(java.util.ArrayList) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) GroupNotFoundException(org.craftercms.studio.api.v1.exception.security.GroupNotFoundException) AuditLogParameter(org.craftercms.studio.api.v2.dal.AuditLogParameter) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Example 3 with Group

use of org.craftercms.studio.api.v2.dal.Group in project studio by craftercms.

the class UserServiceImpl method getUserSiteRoles.

@Override
@HasPermission(type = DefaultPermission.class, action = "read_users")
public List<String> getUserSiteRoles(long userId, String username, String site) throws ServiceLayerException, UserNotFoundException {
    List<Group> groups = userServiceInternal.getUserGroups(userId, username);
    if (CollectionUtils.isNotEmpty(groups)) {
        Map<String, List<String>> roleMappings = configurationService.geRoleMappings(site);
        Set<String> userRoles = new LinkedHashSet<>();
        if (MapUtils.isNotEmpty(roleMappings)) {
            for (Group group : groups) {
                String groupName = group.getGroupName();
                if (groupName.equals(SYSTEM_ADMIN_GROUP)) {
                    // If sysadmin, return all roles
                    Collection<List<String>> roleSets = roleMappings.values();
                    for (List<String> roleSet : roleSets) {
                        userRoles.addAll(roleSet);
                    }
                    break;
                } else {
                    List<String> roles = roleMappings.get(groupName);
                    if (CollectionUtils.isNotEmpty(roles)) {
                        userRoles.addAll(roles);
                    }
                }
            }
        }
        return new ArrayList<>(userRoles);
    } else {
        return Collections.emptyList();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Group(org.craftercms.studio.api.v2.dal.Group) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Example 4 with Group

use of org.craftercms.studio.api.v2.dal.Group in project studio by craftercms.

the class GroupServiceInternalImpl method deleteGroup.

@RetryingOperation
@Override
public void deleteGroup(List<Long> groupIds) throws GroupNotFoundException, ServiceLayerException {
    for (Long groupId : groupIds) {
        if (!groupExists(groupId, StringUtils.EMPTY)) {
            throw new GroupNotFoundException("No group found for id '" + groupId + "'");
        }
    }
    Map<String, Object> params = new HashMap<>();
    params.put(GROUP_IDS, groupIds);
    try {
        groupDao.deleteGroups(params);
    } catch (Exception e) {
        throw new ServiceLayerException("Unknown database error", e);
    }
}
Also used : HashMap(java.util.HashMap) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) GroupNotFoundException(org.craftercms.studio.api.v1.exception.security.GroupNotFoundException) GroupAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.GroupAlreadyExistsException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) GroupNotFoundException(org.craftercms.studio.api.v1.exception.security.GroupNotFoundException) ConfigurationException(org.craftercms.studio.api.v2.exception.configuration.ConfigurationException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation)

Example 5 with Group

use of org.craftercms.studio.api.v2.dal.Group in project studio by craftercms.

the class GroupServiceInternalImpl method createGroup.

@Override
public Group createGroup(long orgId, String groupName, String groupDescription) throws GroupAlreadyExistsException, ServiceLayerException {
    if (groupExists(-1, groupName)) {
        throw new GroupAlreadyExistsException("Group '" + groupName + "' already exists");
    }
    Map<String, Object> params = new HashMap<>();
    params.put(ORG_ID, orgId);
    params.put(GROUP_NAME, groupName);
    params.put(GROUP_DESCRIPTION, groupDescription);
    try {
        groupDao.createGroup(params);
        Group group = new Group();
        group.setId((Long) params.get(ID));
        group.setGroupName(groupName);
        group.setGroupDescription(groupDescription);
        return group;
    } catch (Exception e) {
        throw new ServiceLayerException("Unknown database error", e);
    }
}
Also used : Group(org.craftercms.studio.api.v2.dal.Group) HashMap(java.util.HashMap) GroupAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.GroupAlreadyExistsException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) GroupAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.GroupAlreadyExistsException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) GroupNotFoundException(org.craftercms.studio.api.v1.exception.security.GroupNotFoundException) ConfigurationException(org.craftercms.studio.api.v2.exception.configuration.ConfigurationException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Aggregations

Group (org.craftercms.studio.api.v2.dal.Group)26 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)23 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)20 HashMap (java.util.HashMap)17 GroupNotFoundException (org.craftercms.studio.api.v1.exception.security.GroupNotFoundException)13 ArrayList (java.util.ArrayList)12 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)12 User (org.craftercms.studio.api.v2.dal.User)12 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)11 GroupAlreadyExistsException (org.craftercms.studio.api.v1.exception.security.GroupAlreadyExistsException)10 ConfigurationException (org.craftercms.studio.api.v2.exception.configuration.ConfigurationException)10 HasPermission (org.craftercms.commons.security.permissions.annotations.HasPermission)8 ResponseBody (org.craftercms.studio.model.rest.ResponseBody)6 List (java.util.List)5 AuditLogParameter (org.craftercms.studio.api.v2.dal.AuditLogParameter)5 UserAlreadyExistsException (org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException)4 SiteService (org.craftercms.studio.api.v1.service.site.SiteService)4 AuditServiceInternal (org.craftercms.studio.api.v2.service.audit.internal.AuditServiceInternal)4 StudioConfiguration (org.craftercms.studio.api.v2.utils.StudioConfiguration)4 ZonedDateTime (java.time.ZonedDateTime)3