Search in sources :

Example 1 with PersistableUser

use of com.salesmanager.shop.model.user.PersistableUser in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method update.

@Override
public ReadableUser update(Long id, String authenticatedUser, MerchantStore store, PersistableUser user) {
    Validate.notNull(user, "User cannot be null");
    Validate.notNull(store, "store cannot be null");
    try {
        User userModel = userService.getById(id);
        if (userModel == null) {
            throw new ServiceRuntimeException("Cannot find user [" + user.getUserName() + "]");
        }
        if (userModel.getId().longValue() != id.longValue()) {
            throw new ServiceRuntimeException("Cannot find user [" + user.getUserName() + "] id or name does not match");
        }
        User auth = userService.getByUserName(authenticatedUser);
        if (auth == null) {
            throw new ServiceRuntimeException("Cannot find user [" + authenticatedUser + "]");
        }
        User adminName = getByUserName(user.getUserName());
        if (adminName != null) {
            if (adminName.getId().longValue() != userModel.getId().longValue()) {
                throw new ServiceRuntimeException("User id [" + userModel.getId() + "] does not match [" + user.getUserName() + "]");
            }
        }
        boolean isActive = userModel.isActive();
        List<Group> originalGroups = userModel.getGroups();
        Group superadmin = originalGroups.stream().filter(group -> Constants.GROUP_SUPERADMIN.equals(group.getGroupName())).findAny().orElse(null);
        // i'm i editing my own profile ?
        if (authenticatedUser.equals(adminName)) {
            if (!userModel.getMerchantStore().getCode().equals(store.getCode())) {
                throw new OperationNotAllowedException("User [" + adminName + "] cannot change owning store");
            }
        } else {
            // i am an admin or super admin
            Group adminOrSuperadmin = originalGroups.stream().filter(group -> (Constants.GROUP_SUPERADMIN.equals(group.getGroupName()) || Constants.ADMIN_USER.equals(group.getGroupName()) || Constants.ADMIN_STORE.equals(group.getGroupName()))).findAny().orElse(null);
            if (!userModel.getMerchantStore().getCode().equals(store.getCode()) && adminOrSuperadmin == null) {
                throw new OperationNotAllowedException("User [" + adminName + "] cannot change owning store");
            }
        }
        userModel = converPersistabletUserToUser(store, languageService.defaultLanguage(), userModel, user);
        // admin
        if (superadmin != null) {
            userModel.setGroups(originalGroups);
        }
        Group adminGroup = auth.getGroups().stream().filter((group) -> Constants.GROUP_SUPERADMIN.equals(group.getGroupName()) || Constants.GROUP_SUPERADMIN.equals(group.getGroupName())).findAny().orElse(null);
        if (adminGroup == null) {
            userModel.setGroups(originalGroups);
            userModel.setActive(isActive);
        }
        user.setPassword(userModel.getAdminPassword());
        userService.update(userModel);
        return this.convertUserToReadableUser(languageService.defaultLanguage(), userModel);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Cannot update user [" + user.getUserName() + "]", e);
    }
}
Also used : ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) Group(com.salesmanager.core.model.user.Group) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 2 with PersistableUser

use of com.salesmanager.shop.model.user.PersistableUser in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method create.

@Override
public ReadableUser create(PersistableUser user, MerchantStore store) {
    Validate.notNull(store, "MerchantStore must not be null");
    Validate.notNull(user, "User must not be null");
    Validate.notNull(user.getUserName(), "Username must not be null");
    try {
        // check if user exists
        User tempUser = userService.getByUserName(user.getUserName(), store.getCode());
        if (tempUser != null) {
            throw new ServiceRuntimeException("User [" + user.getUserName() + "] already exists for store [" + store.getCode() + "]");
        }
        /**
         * validate password
         */
        if (!securityFacade.matchRawPasswords(user.getPassword(), user.getRepeatPassword())) {
            throw new ServiceRuntimeException("Passwords dos not match, make sure password and repeat password are equals");
        }
        /**
         * Validate new password
         */
        if (!securityFacade.validateUserPassword(user.getPassword())) {
            throw new ServiceRuntimeException("New password does not apply to format policy");
        }
        String newPasswordEncoded = securityFacade.encodePassword(user.getPassword());
        User userModel = new User();
        userModel = converPersistabletUserToUser(store, languageService.defaultLanguage(), userModel, user);
        if (CollectionUtils.isEmpty(userModel.getGroups())) {
            throw new ServiceRuntimeException("No valid group groups associated with user " + user.getUserName());
        }
        userModel.setAdminPassword(newPasswordEncoded);
        userService.saveOrUpdate(userModel);
        // now build returned object
        User createdUser = userService.getById(userModel.getId());
        return convertUserToReadableUser(createdUser.getDefaultLanguage(), createdUser);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Cannot create user " + user.getUserName() + " for store " + store.getCode(), e);
    }
}
Also used : ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 3 with PersistableUser

use of com.salesmanager.shop.model.user.PersistableUser in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method updateEnabled.

@Override
public void updateEnabled(MerchantStore store, PersistableUser user) {
    Validate.notNull(user, "User cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(user.getId(), "User.id cannot be null");
    try {
        User modelUser = userService.findByStore(user.getId(), store.getCode());
        if (modelUser == null) {
            throw new ResourceNotFoundException("User with id [" + user.getId() + "] not found for store [" + store.getCode() + "]");
        }
        modelUser.setActive(user.isActive());
        userService.saveOrUpdate(modelUser);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Error while updating user enable flag", e);
    }
}
Also used : ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 4 with PersistableUser

use of com.salesmanager.shop.model.user.PersistableUser in project shopizer by shopizer-ecommerce.

the class PersistableUserPopulator method populate.

@Override
public User populate(PersistableUser source, User target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(source, "PersistableUser cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    if (target == null) {
        target = new User();
    }
    target.setFirstName(source.getFirstName());
    target.setLastName(source.getLastName());
    target.setAdminEmail(source.getEmailAddress());
    target.setAdminName(source.getUserName());
    if (!StringUtils.isBlank(source.getPassword())) {
        target.setAdminPassword(passwordEncoder.encode(source.getPassword()));
    }
    if (!StringUtils.isBlank(source.getStore())) {
        try {
            MerchantStore userStore = merchantStoreService.getByCode(source.getStore());
            target.setMerchantStore(userStore);
        } catch (ServiceException e) {
            throw new ConversionException("Error while reading MerchantStore store [" + source.getStore() + "]", e);
        }
    } else {
        target.setMerchantStore(store);
    }
    target.setActive(source.isActive());
    Language lang = null;
    try {
        lang = languageService.getByCode(source.getDefaultLanguage());
    } catch (Exception e) {
        throw new ConversionException("Cannot get language [" + source.getDefaultLanguage() + "]", e);
    }
    // set default language
    target.setDefaultLanguage(lang);
    List<Group> userGroups = new ArrayList<Group>();
    List<String> names = new ArrayList<String>();
    for (PersistableGroup group : source.getGroups()) {
        names.add(group.getName());
    }
    try {
        List<Group> groups = groupService.listGroupByNames(names);
        for (Group g : groups) {
            userGroups.add(g);
        }
    } catch (Exception e1) {
        throw new ConversionException("Error while getting user groups", e1);
    }
    target.setGroups(userGroups);
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) Group(com.salesmanager.core.model.user.Group) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) User(com.salesmanager.core.model.user.User) ServiceException(com.salesmanager.core.business.exception.ServiceException) Language(com.salesmanager.core.model.reference.language.Language) ArrayList(java.util.ArrayList) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ServiceException(com.salesmanager.core.business.exception.ServiceException) ConversionException(com.salesmanager.core.business.exception.ConversionException)

Example 5 with PersistableUser

use of com.salesmanager.shop.model.user.PersistableUser in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method authorizedGroups.

@Override
public void authorizedGroups(String authenticatedUser, PersistableUser user) {
    Validate.notNull(authenticatedUser, "Required authenticated user");
    Validate.notNull(user, "Required persistable user");
    try {
        User currentUser = userService.getByUserName(authenticatedUser);
        boolean isSuperAdmin = false;
        for (Group g : currentUser.getGroups()) {
            if (g.getGroupName().equals("SUPERADMIN")) {
                isSuperAdmin = true;
                break;
            }
        }
        for (PersistableGroup g : user.getGroups()) {
            if (g.getName().equals("SUPERADMIN")) {
                if (!isSuperAdmin) {
                    throw new UnauthorizedException("Superadmin group not allowed");
                }
            }
        }
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Error while looking for authorization", e);
    }
}
Also used : ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) Group(com.salesmanager.core.model.user.Group) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Aggregations

PersistableUser (com.salesmanager.shop.model.user.PersistableUser)6 ServiceException (com.salesmanager.core.business.exception.ServiceException)5 User (com.salesmanager.core.model.user.User)5 ReadableUser (com.salesmanager.shop.model.user.ReadableUser)5 PersistableGroup (com.salesmanager.shop.model.security.PersistableGroup)4 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)4 Group (com.salesmanager.core.model.user.Group)3 ReadableGroup (com.salesmanager.shop.model.security.ReadableGroup)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)1 Language (com.salesmanager.core.model.reference.language.Language)1 UserPassword (com.salesmanager.shop.model.user.UserPassword)1 OperationNotAllowedException (com.salesmanager.shop.store.api.exception.OperationNotAllowedException)1 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)1 UnauthorizedException (com.salesmanager.shop.store.api.exception.UnauthorizedException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 HttpEntity (org.springframework.http.HttpEntity)1