Search in sources :

Example 1 with USERNAME

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;
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) UserExternallyManagedException(org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException) StringTokenizer(java.util.StringTokenizer) User(org.craftercms.studio.api.v2.dal.User) AuthenticatedUser(org.craftercms.studio.model.AuthenticatedUser) ZonedDateTime(java.time.ZonedDateTime)

Example 2 with USERNAME

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();
    }
}
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 3 with USERNAME

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;
}
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) AuthenticatedUser(org.craftercms.studio.model.AuthenticatedUser) ZonedDateTime(java.time.ZonedDateTime) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Example 4 with USERNAME

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);
    }
}
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 5 with USERNAME

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

Aggregations

ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)32 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)31 User (org.craftercms.studio.api.v2.dal.User)30 HashMap (java.util.HashMap)15 IOException (java.io.IOException)12 UserAlreadyExistsException (org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException)12 UserExternallyManagedException (org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 ArrayList (java.util.ArrayList)9 PasswordDoesNotMatchException (org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException)9 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)8 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)8 Group (org.craftercms.studio.api.v2.dal.Group)8 PasswordRequirementsFailedException (org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException)8 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)7 Git (org.eclipse.jgit.api.Git)7 Repository (org.eclipse.jgit.lib.Repository)7 InputStream (java.io.InputStream)6 AuthenticationSystemException (org.craftercms.studio.api.v1.exception.security.AuthenticationSystemException)6 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)6