Search in sources :

Example 16 with Group

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

Example 17 with Group

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

the class UserFacadeImpl method listByCriteria.

@Override
public ReadableUserList listByCriteria(UserCriteria criteria, int page, int count, Language language) {
    try {
        ReadableUserList readableUserList = new ReadableUserList();
        // filtering by userName is not in this implementation
        Page<User> userList = null;
        Optional<String> storeCode = Optional.ofNullable(criteria.getStoreCode());
        if (storeCode.isPresent()) {
            // get store
            MerchantStore store = merchantStoreService.getByCode(storeCode.get());
            if (store != null && (store.isRetailer() != null)) {
                if (store.isRetailer().booleanValue()) {
                    // get group stores
                    List<MerchantStore> stores = merchantStoreService.findAllStoreNames(store.getCode());
                    List<Integer> intList = stores.stream().map(s -> s.getId()).collect(Collectors.toList());
                    criteria.setStoreIds(intList);
                    // search over store list
                    criteria.setStoreCode(null);
                }
            }
        }
        userList = userService.listByCriteria(criteria, page, count);
        List<ReadableUser> readableUsers = new ArrayList<ReadableUser>();
        if (userList != null) {
            readableUsers = userList.getContent().stream().map(user -> convertUserToReadableUser(language, user)).collect(Collectors.toList());
            readableUserList.setRecordsTotal(userList.getTotalElements());
            readableUserList.setTotalPages(userList.getTotalPages());
            readableUserList.setNumber(userList.getSize());
            readableUserList.setRecordsFiltered(userList.getSize());
        }
        readableUserList.setData(readableUsers);
        return readableUserList;
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Cannot get users by criteria user", e);
    }
}
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) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ArrayList(java.util.ArrayList) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) ReadableUserList(com.salesmanager.shop.model.user.ReadableUserList) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore)

Example 18 with Group

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

the class SecurityGroupsBuilder method addGroup.

public SecurityGroupsBuilder addGroup(String name, GroupType type) {
    Group g = new Group();
    g.setGroupName(name);
    g.setGroupType(type);
    groups.add(g);
    this.lastGroup = g;
    return this;
}
Also used : Group(com.salesmanager.core.model.user.Group)

Example 19 with Group

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

the class SecurityGroupsBuilder method addPermission.

public SecurityGroupsBuilder addPermission(Permission permission) {
    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;
        }
    }
    lastGroup.getPermissions().add(permission);
    return this;
}
Also used : Group(com.salesmanager.core.model.user.Group)

Example 20 with Group

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

the class JWTAdminServicesImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
    User user = null;
    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    try {
        LOGGER.debug("Loading user by user id: {}", userName);
        user = userService.getByUserName(userName);
        if (user == null) {
            // return null;
            throw new UsernameNotFoundException("User " + userName + " not found");
        }
        // 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());
        }
        if (CollectionUtils.isNotEmpty(groupsId)) {
            List<Permission> permissions = permissionService.getPermissions(groupsId);
            for (Permission permission : permissions) {
                GrantedAuthority auth = new SimpleGrantedAuthority(permission.getPermissionName());
                authorities.add(auth);
            }
        }
    } catch (ServiceException e) {
        LOGGER.error("Exception while querrying customer", e);
        throw new SecurityDataAccessException("Cannot authenticate customer", e);
    }
    return userDetails(userName, user, authorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Group(com.salesmanager.core.model.user.Group) User(com.salesmanager.core.model.user.User) JWTUser(com.salesmanager.shop.store.security.user.JWTUser) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SecurityDataAccessException(com.salesmanager.shop.admin.security.SecurityDataAccessException) ArrayList(java.util.ArrayList) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ServiceException(com.salesmanager.core.business.exception.ServiceException) Permission(com.salesmanager.core.model.user.Permission)

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