Search in sources :

Example 6 with UserNotFoundException

use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.

the class UserServiceInternalImpl method setUserPassword.

@RetryingOperation
@Override
public boolean setUserPassword(String username, String newPassword) throws UserNotFoundException, UserExternallyManagedException, ServiceLayerException {
    if (!userExists(-1, username)) {
        throw new UserNotFoundException();
    } else {
        if (verifyPasswordRequirements(newPassword)) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put(USER_ID, -1);
            params.put(USERNAME, username);
            try {
                User user = userDao.getUserByIdOrUsername(params);
                if (user.isExternallyManaged()) {
                    throw new UserExternallyManagedException();
                } else {
                    String hashedPassword = CryptoUtils.hashPassword(newPassword);
                    params = new HashMap<String, Object>();
                    params.put(USERNAME, username);
                    params.put(PASSWORD, hashedPassword);
                    userDao.setUserPassword(params);
                    return true;
                }
            } catch (Exception e) {
                throw new ServiceLayerException("Unknown database error", e);
            }
        } else {
            throw new PasswordRequirementsFailedException("User password does not fulfill requirements");
        }
    }
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) UserExternallyManagedException(org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException) User(org.craftercms.studio.api.v2.dal.User) HashMap(java.util.HashMap) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PasswordDoesNotMatchException(org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException) UserAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) UserExternallyManagedException(org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException) PasswordRequirementsFailedException(org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PasswordRequirementsFailedException(org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation)

Example 7 with UserNotFoundException

use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.

the class UserServiceInternalImpl method updateUser.

@RetryingOperation
@Override
public void updateUser(User user) throws UserNotFoundException, ServiceLayerException {
    long userId = user.getId();
    String username = user.getUsername() != null ? user.getUsername() : StringUtils.EMPTY;
    User oldUser = getUserByIdOrUsername(userId, username);
    Map<String, Object> params = new HashMap<>();
    params.put(USER_ID, oldUser.getId());
    params.put(FIRST_NAME, user.getFirstName());
    params.put(LAST_NAME, user.getLastName());
    params.put(EMAIL, user.getEmail());
    params.put(TIMEZONE, StringUtils.EMPTY);
    params.put(LOCALE, StringUtils.EMPTY);
    try {
        userDao.updateUser(params);
    } catch (Exception e) {
        throw new ServiceLayerException("Unknown database error", e);
    }
}
Also used : User(org.craftercms.studio.api.v2.dal.User) HashMap(java.util.HashMap) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PasswordDoesNotMatchException(org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException) UserAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) UserExternallyManagedException(org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException) PasswordRequirementsFailedException(org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation)

Example 8 with UserNotFoundException

use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.

the class UserServiceInternalImpl method getUserGroups.

@Override
public List<Group> getUserGroups(long userId, String username) throws UserNotFoundException, ServiceLayerException {
    if (!userExists(userId, username)) {
        throw new UserNotFoundException("No user found for username '" + username + "' or id '" + userId + "'");
    }
    Map<String, Object> params = new HashMap<>();
    params.put(USER_ID, userId);
    params.put(USERNAME, username);
    try {
        return userDao.getUserGroups(params);
    } catch (Exception e) {
        throw new ServiceLayerException("Unknown database error", e);
    }
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) HashMap(java.util.HashMap) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PasswordDoesNotMatchException(org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException) UserAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) UserExternallyManagedException(org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException) PasswordRequirementsFailedException(org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Example 9 with UserNotFoundException

use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.

the class UserServiceInternalImpl method getUserByIdOrUsername.

@Override
public User getUserByIdOrUsername(long userId, String username) throws ServiceLayerException, UserNotFoundException {
    Map<String, Object> params = new HashMap<>();
    params.put(USER_ID, userId);
    params.put(USERNAME, username);
    User user;
    try {
        user = userDao.getUserByIdOrUsername(params);
    } catch (Exception e) {
        throw new ServiceLayerException("Unknown database error", e);
    }
    if (user == null) {
        throw new UserNotFoundException("No user found for username '" + username + "' or id '" + userId + "'");
    }
    return user;
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) User(org.craftercms.studio.api.v2.dal.User) HashMap(java.util.HashMap) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PasswordDoesNotMatchException(org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException) UserAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) UserExternallyManagedException(org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException) PasswordRequirementsFailedException(org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Example 10 with UserNotFoundException

use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.

the class GroupServiceImpl method removeGroupMembers.

@Override
@HasPermission(type = DefaultPermission.class, action = "update_groups")
public void removeGroupMembers(long groupId, List<Long> userIds, List<String> usernames) throws ServiceLayerException, UserNotFoundException, GroupNotFoundException, AuthenticationException {
    Group group = getGroup(groupId);
    generalLockService.lock(REMOVE_SYSTEM_ADMIN_MEMBER_LOCK);
    try {
        if (group.getGroupName().equals(SYSTEM_ADMIN_GROUP)) {
            List<User> members = getGroupMembers(groupId, 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.");
                }
            }
        }
        List<User> users = userServiceInternal.getUsersByIdOrUsername(userIds, usernames);
        groupServiceInternal.removeGroupMembers(groupId, userIds, usernames);
        SiteFeed siteFeed = siteService.getSite(studioConfiguration.getProperty(CONFIGURATION_GLOBAL_SYSTEM_SITE));
        AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
        auditLog.setOperation(OPERATION_REMOVE_MEMBERS);
        auditLog.setActorId(userService.getCurrentUser().getUsername());
        auditLog.setSiteId(siteFeed.getId());
        auditLog.setPrimaryTargetId(Long.toString(group.getId()));
        auditLog.setPrimaryTargetType(TARGET_TYPE_USER);
        auditLog.setPrimaryTargetValue(group.getGroupName());
        List<AuditLogParameter> paramters = new ArrayList<AuditLogParameter>();
        for (User user : users) {
            AuditLogParameter paramter = new AuditLogParameter();
            paramter.setTargetId(Long.toString(user.getId()));
            paramter.setTargetType(TARGET_TYPE_USER);
            paramter.setTargetValue(user.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) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) ArrayList(java.util.ArrayList) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) AuditLogParameter(org.craftercms.studio.api.v2.dal.AuditLogParameter) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Aggregations

UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)43 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)40 User (org.craftercms.studio.api.v2.dal.User)32 IOException (java.io.IOException)15 HashMap (java.util.HashMap)15 ArrayList (java.util.ArrayList)11 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)11 UserExternallyManagedException (org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException)11 Group (org.craftercms.studio.api.v2.dal.Group)11 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)11 Repository (org.eclipse.jgit.lib.Repository)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 UserAlreadyExistsException (org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException)10 Git (org.eclipse.jgit.api.Git)10 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)10 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)9 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)9 CryptoException (org.craftercms.commons.crypto.CryptoException)8 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)8 PasswordDoesNotMatchException (org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException)7