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");
}
}
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations