use of org.craftercms.studio.api.v2.dal.QueryParameterNames.USERNAME 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.v2.dal.QueryParameterNames.USERNAME 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();
}
}
use of org.craftercms.studio.api.v2.dal.QueryParameterNames.USERNAME 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.v2.dal.QueryParameterNames.USERNAME 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.v2.dal.QueryParameterNames.USERNAME 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");
}
}
}
Aggregations