Search in sources :

Example 16 with User

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

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

the class UserFacadeImpl method requestPasswordReset.

@Override
public void requestPasswordReset(String userName, String userContextPath, MerchantStore store, Language language) {
    Validate.notNull(userName, "Username cannot be empty");
    Validate.notNull(userContextPath, "Return url cannot be empty");
    try {
        // get user by user name
        User user = userService.getByUserName(userName, store.getCode());
        if (user == null) {
            throw new ResourceNotFoundException("User [" + userName + "] not found for store [" + store.getCode() + "]");
        }
        // generates unique token
        String token = UUID.randomUUID().toString();
        Date expiry = DateUtil.addDaysToCurrentDate(2);
        CredentialsReset credsRequest = new CredentialsReset();
        credsRequest.setCredentialsRequest(token);
        credsRequest.setCredentialsRequestExpiry(expiry);
        user.setCredentialsResetRequest(credsRequest);
        userService.saveOrUpdate(user);
        // reset password link
        // this will build http | https ://domain/contextPath
        String baseUrl = userContextPath;
        if (!filePathUtils.isValidURL(baseUrl)) {
            throw new ServiceRuntimeException("Request url [" + baseUrl + "] is invalid");
        }
        // need to add link to controller receiving user reset password
        // request
        String customerResetLink = new StringBuilder().append(baseUrl).append(Constants.SLASH).append(String.format(resetUserLink, store.getCode(), token)).toString();
        resetPasswordRequest(user, customerResetLink, store, lamguageService.toLocale(language, store));
    } catch (Exception e) {
        throw new ServiceRuntimeException("Error while executing resetPassword request", e);
    }
}
Also used : ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) CredentialsReset(com.salesmanager.core.model.common.CredentialsReset) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Date(java.util.Date) 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)

Example 18 with User

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

the class UserFacadeImpl method getByCriteria.

@Override
public ReadableUserList getByCriteria(Language language, String drawParam, Criteria criteria) {
    try {
        ReadableUserList readableUserList = new ReadableUserList();
        GenericEntityList<User> userList = userService.listByCriteria(criteria);
        for (User user : userList.getList()) {
            ReadableUser readableUser = this.convertUserToReadableUser(language, user);
            readableUserList.getData().add(readableUser);
        }
        readableUserList.setRecordsTotal(userList.getTotalCount());
        readableUserList.setNumber(userList.getList().size());
        readableUserList.setTotalPages(userList.getTotalPages());
        // readableUserList.setTotalPages(readableUserList.getData().size());
        readableUserList.setRecordsFiltered(Math.toIntExact(userList.getTotalCount()));
        return readableUserList;
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Cannot get users by criteria user", e);
    }
}
Also used : ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) ReadableUserList(com.salesmanager.shop.model.user.ReadableUserList) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 19 with User

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

the class AbstractAuthenticatinSuccessHandler method onAuthenticationSuccess.

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
    // last access timestamp
    String userName = authentication.getName();
    /**
     * Spring Security 4 does not seem to add security context in the session
     * creating the authentication to be lost during the login
     */
    SecurityContext securityContext = SecurityContextHolder.getContext();
    HttpSession session = request.getSession(true);
    session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
    try {
        User user = userService.getByUserName(userName);
        Date lastAccess = user.getLoginTime();
        if (lastAccess == null) {
            lastAccess = new Date();
        }
        user.setLastAccess(lastAccess);
        user.setLoginTime(new Date());
        userService.saveOrUpdate(user);
        redirectAfterSuccess(request, response);
    } catch (Exception e) {
        LOGGER.error("User authenticationSuccess", e);
    }
}
Also used : User(com.salesmanager.core.model.user.User) HttpSession(javax.servlet.http.HttpSession) SecurityContext(org.springframework.security.core.context.SecurityContext) Date(java.util.Date)

Example 20 with User

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

the class UserServiceImpl method listByCriteria.

@Override
public Page<User> listByCriteria(UserCriteria criteria, int page, int count) throws ServiceException {
    Pageable pageRequest = PageRequest.of(page, count);
    Page<User> users = null;
    if (criteria.getStoreIds() != null) {
        // search within a predefined list
        // of stores
        users = pageableUserRepository.listByStoreIds(criteria.getStoreIds(), criteria.getAdminEmail(), pageRequest);
    } else if (StringUtils.isBlank(criteria.getStoreCode())) {
        // search for
        // a
        // specific
        // store
        users = pageableUserRepository.listAll(criteria.getAdminEmail(), pageRequest);
    } else if (criteria.getStoreCode() != null) {
        // store code
        users = pageableUserRepository.listByStore(criteria.getStoreCode(), criteria.getAdminEmail(), pageRequest);
    }
    return users;
}
Also used : Pageable(org.springframework.data.domain.Pageable) User(com.salesmanager.core.model.user.User)

Aggregations

User (com.salesmanager.core.model.user.User)21 ServiceException (com.salesmanager.core.business.exception.ServiceException)16 PersistableUser (com.salesmanager.shop.model.user.PersistableUser)15 ReadableUser (com.salesmanager.shop.model.user.ReadableUser)14 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)13 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)7 Group (com.salesmanager.core.model.user.Group)6 ConversionException (com.salesmanager.core.business.exception.ConversionException)5 PersistableGroup (com.salesmanager.shop.model.security.PersistableGroup)5 OperationNotAllowedException (com.salesmanager.shop.store.api.exception.OperationNotAllowedException)5 UnauthorizedException (com.salesmanager.shop.store.api.exception.UnauthorizedException)5 ReadableGroup (com.salesmanager.shop.model.security.ReadableGroup)4 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)4 GenericRuntimeException (com.salesmanager.shop.store.api.exception.GenericRuntimeException)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 CredentialsReset (com.salesmanager.core.model.common.CredentialsReset)3 GenericEntityList (com.salesmanager.core.model.common.GenericEntityList)3 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)3 Language (com.salesmanager.core.model.reference.language.Language)3