Search in sources :

Example 1 with Group

use of com.salesmanager.core.model.user.Group in project shopizer by shopizer-ecommerce.

the class SecurityGroupsBuilder method addPermission.

public SecurityGroupsBuilder addPermission(String name) {
    if (this.lastGroup == null) {
        Group g = this.groups.get(0);
        if (g == null) {
            g = new Group();
            g.setGroupName("UNDEFINED");
            g.setGroupType(GroupType.ADMIN);
            groups.add(g);
            this.lastGroup = g;
        }
    }
    Permission permission = new Permission();
    permission.setPermissionName(name);
    lastGroup.getPermissions().add(permission);
    return this;
}
Also used : Group(com.salesmanager.core.model.user.Group) Permission(com.salesmanager.core.model.user.Permission)

Example 2 with Group

use of com.salesmanager.core.model.user.Group in project shopizer by shopizer-ecommerce.

the class UserServicesImpl method loadUserByUsername.

public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
    com.salesmanager.core.model.user.User user = null;
    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    try {
        user = userService.getByUserName(userName);
        if (user == null) {
            return null;
        }
        // required to login
        GrantedAuthority role = new SimpleGrantedAuthority(ROLE_PREFIX + Constants.PERMISSION_AUTHENTICATED);
        authorities.add(role);
        List<Integer> groupsId = new ArrayList<Integer>();
        List<Group> groups = user.getGroups();
        for (Group group : groups) {
            groupsId.add(group.getId());
        }
        List<Permission> permissions = permissionService.getPermissions(groupsId);
        for (Permission permission : permissions) {
            GrantedAuthority auth = new SimpleGrantedAuthority(ROLE_PREFIX + permission.getPermissionName());
            authorities.add(auth);
        }
    } catch (Exception e) {
        LOGGER.error("Exception while querrying user", e);
        throw new SecurityDataAccessException("Exception while querrying user", e);
    }
    User secUser = new User(userName, user.getAdminPassword(), user.isActive(), true, true, true, authorities);
    return secUser;
}
Also used : Group(com.salesmanager.core.model.user.Group) User(org.springframework.security.core.userdetails.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) DataAccessException(org.springframework.dao.DataAccessException) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Permission(com.salesmanager.core.model.user.Permission)

Example 3 with Group

use of com.salesmanager.core.model.user.Group in project shopizer by shopizer-ecommerce.

the class UserServicesImpl method createDefaultAdmin.

public void createDefaultAdmin() throws Exception {
    MerchantStore store = merchantStoreService.getByCode(MerchantStore.DEFAULT_STORE);
    String password = passwordEncoder.encode(DEFAULT_INITIAL_PASSWORD);
    List<Group> groups = groupService.listGroup(GroupType.ADMIN);
    // creation of the super admin admin:password)
    com.salesmanager.core.model.user.User user = new com.salesmanager.core.model.user.User("admin@shopizer.com", password, "admin@shopizer.com");
    user.setFirstName("Administrator");
    user.setLastName("User");
    for (Group group : groups) {
        if (group.getGroupName().equals(Constants.GROUP_SUPERADMIN) || group.getGroupName().equals(Constants.GROUP_ADMIN)) {
            user.getGroups().add(group);
        }
    }
    user.setMerchantStore(store);
    userService.create(user);
}
Also used : Group(com.salesmanager.core.model.user.Group) User(org.springframework.security.core.userdetails.User) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore)

Example 4 with Group

use of com.salesmanager.core.model.user.Group 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 5 with Group

use of com.salesmanager.core.model.user.Group in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method authorizedStore.

@Override
public boolean authorizedStore(String userName, String merchantStoreCode) {
    try {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Set<String> roles = authentication.getAuthorities().stream().map(r -> r.getAuthority()).collect(Collectors.toSet());
        ReadableUser readableUser = findByUserName(userName, languageService.defaultLanguage());
        // unless superadmin
        for (ReadableGroup group : readableUser.getGroups()) {
            if (Constants.GROUP_SUPERADMIN.equals(group.getName())) {
                return true;
            }
        }
        boolean authorized = false;
        User user = userService.findByStore(readableUser.getId(), merchantStoreCode);
        if (user != null) {
            authorized = true;
        } else {
            user = userService.getByUserName(userName);
        }
        if (user != null && !authorized) {
            // get parent
            MerchantStore store = merchantStoreService.getParent(merchantStoreCode);
            // user can be in parent
            MerchantStore st = user.getMerchantStore();
            if (store != null && st.getCode().equals(store.getCode())) {
                authorized = true;
            }
        }
        return authorized;
    } catch (Exception e) {
        throw new ServiceRuntimeException("Cannot authorize user " + userName + " for store " + merchantStoreCode, e.getMessage());
    }
}
Also used : PermissionService(com.salesmanager.core.business.services.user.PermissionService) Date(java.util.Date) EmailConstants(com.salesmanager.shop.constants.EmailConstants) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) LanguageService(com.salesmanager.core.business.services.reference.language.LanguageService) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) Locale(java.util.Locale) Map(java.util.Map) GenericEntityList(com.salesmanager.core.model.common.GenericEntityList) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) MerchantStoreService(com.salesmanager.core.business.services.merchant.MerchantStoreService) ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) Set(java.util.Set) ReadableUserList(com.salesmanager.shop.model.user.ReadableUserList) ReadableUserPopulator(com.salesmanager.shop.populator.user.ReadableUserPopulator) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) List(java.util.List) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) CollectionUtils(org.springframework.util.CollectionUtils) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) Optional(java.util.Optional) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Authentication(org.springframework.security.core.Authentication) EmailService(com.salesmanager.core.business.services.system.EmailService) Async(org.springframework.scheduling.annotation.Async) Email(com.salesmanager.core.business.modules.email.Email) Group(com.salesmanager.core.model.user.Group) Constants(com.salesmanager.shop.constants.Constants) DateUtil(com.salesmanager.shop.utils.DateUtil) CredentialsReset(com.salesmanager.core.model.common.CredentialsReset) ReadablePermission(com.salesmanager.shop.model.security.ReadablePermission) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) Permission(com.salesmanager.core.model.user.Permission) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) LabelUtils(com.salesmanager.shop.utils.LabelUtils) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) EmailUtils(com.salesmanager.shop.utils.EmailUtils) UserPassword(com.salesmanager.shop.model.user.UserPassword) User(com.salesmanager.core.model.user.User) Criteria(com.salesmanager.core.model.common.Criteria) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) Validate(org.jsoup.helper.Validate) Logger(org.slf4j.Logger) UserFacade(com.salesmanager.shop.store.controller.user.facade.UserFacade) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) UserService(com.salesmanager.core.business.services.user.UserService) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) PersistableUserPopulator(com.salesmanager.shop.populator.user.PersistableUserPopulator) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) FilePathUtils(com.salesmanager.shop.utils.FilePathUtils) UserCriteria(com.salesmanager.core.model.user.UserCriteria) SecurityFacade(com.salesmanager.shop.store.controller.security.facade.SecurityFacade) ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) Authentication(org.springframework.security.core.Authentication) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Aggregations

Group (com.salesmanager.core.model.user.Group)22 ArrayList (java.util.ArrayList)10 Permission (com.salesmanager.core.model.user.Permission)9 ReadableGroup (com.salesmanager.shop.model.security.ReadableGroup)9 ServiceException (com.salesmanager.core.business.exception.ServiceException)8 User (com.salesmanager.core.model.user.User)6 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)6 ConversionException (com.salesmanager.core.business.exception.ConversionException)5 PersistableGroup (com.salesmanager.shop.model.security.PersistableGroup)5 PersistableUser (com.salesmanager.shop.model.user.PersistableUser)5 ReadableUser (com.salesmanager.shop.model.user.ReadableUser)5 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)4 Language (com.salesmanager.core.model.reference.language.Language)3 GrantedAuthority (org.springframework.security.core.GrantedAuthority)3 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)3 Email (com.salesmanager.core.business.modules.email.Email)2 MerchantStoreService (com.salesmanager.core.business.services.merchant.MerchantStoreService)2 LanguageService (com.salesmanager.core.business.services.reference.language.LanguageService)2 EmailService (com.salesmanager.core.business.services.system.EmailService)2 PermissionService (com.salesmanager.core.business.services.user.PermissionService)2