use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class UserServiceImpl method validateToken.
@Override
public boolean validateToken(String token) throws UserNotFoundException, UserExternallyManagedException, ServiceLayerException {
boolean toRet = false;
String decryptedToken = decryptToken(token);
if (StringUtils.isNotEmpty(decryptedToken)) {
StringTokenizer tokenElements = new StringTokenizer(decryptedToken, "|");
if (tokenElements.countTokens() == 3) {
String username = tokenElements.nextToken();
User userProfile = userServiceInternal.getUserByIdOrUsername(-1, username);
if (userProfile == null) {
logger.info("User profile not found for " + username);
throw new UserNotFoundException();
} else {
if (userProfile.isExternallyManaged()) {
throw new UserExternallyManagedException();
} else {
String studioId = tokenElements.nextToken();
if (StringUtils.equals(studioId, instanceService.getInstanceId())) {
long tokenTimestamp = Long.parseLong(tokenElements.nextToken());
ZonedDateTime now = ZonedDateTime.now();
toRet = tokenTimestamp >= now.toInstant().toEpochMilli();
}
}
}
}
}
return toRet;
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException 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);
}
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class UserServiceImpl method forgotPassword.
@Override
public boolean forgotPassword(String username) throws ServiceLayerException, UserNotFoundException, UserExternallyManagedException {
logger.debug("Getting user profile for " + username);
User user = userServiceInternal.getUserByIdOrUsername(-1, username);
boolean success = false;
if (user == null) {
logger.info("User profile not found for " + username);
throw new UserNotFoundException();
} else {
if (user.isExternallyManaged()) {
throw new UserExternallyManagedException();
} else {
if (user.getEmail() != null) {
String email = user.getEmail();
logger.debug("Creating security token for forgot password");
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime ttl = now.plusMinutes(Long.parseLong(studioConfiguration.getProperty(SECURITY_FORGOT_PASSWORD_TOKEN_TIMEOUT)));
long timestamp = ttl.toInstant().toEpochMilli();
String studioId = instanceService.getInstanceId();
String token = username + "|" + studioId + "|" + timestamp;
String hashedToken = encryptToken(token);
logger.debug("Sending forgot password email to " + email);
sendForgotPasswordEmail(email, hashedToken);
success = true;
} else {
logger.info("User " + username + " does not have assigned email with account");
throw new ServiceLayerException("User " + username + " does not have assigned email with account");
}
}
}
return success;
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class UserServiceInternalImpl method isUserMemberOfGroup.
@Override
public boolean isUserMemberOfGroup(String username, String groupName) throws UserNotFoundException, ServiceLayerException {
if (!userExists(-1, username)) {
throw new UserNotFoundException("No user found for username '" + username + "'");
}
Map<String, Object> params = new HashMap<>();
params.put(GROUP_NAME, groupName);
params.put(USERNAME, username);
try {
int result = userDao.isUserMemberOfGroup(params);
return result > 0;
} 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 enableUsers.
@RetryingOperation
@Override
public List<User> enableUsers(List<Long> userIds, List<String> usernames, boolean enabled) throws ServiceLayerException, UserNotFoundException {
List<User> users = getUsersByIdOrUsername(userIds, usernames);
Map<String, Object> params = new HashMap<>();
params.put(USER_IDS, users.stream().map(User::getId).collect(Collectors.toList()));
params.put(ENABLED, enabled ? 1 : 0);
try {
userDao.enableUsers(params);
return getUsersByIdOrUsername(userIds, usernames);
} catch (Exception e) {
throw new ServiceLayerException("Unknown database error", e);
}
}
Aggregations