Search in sources :

Example 6 with Group

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

the class AbstractCustomerServices method loadUserByUsername.

public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
    Customer user = null;
    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    try {
        LOGGER.debug("Loading user by user id: {}", userName);
        user = customerService.getByNick(userName);
        if (user == null) {
            // return null;
            throw new UsernameNotFoundException("User " + userName + " not found");
        }
        // required to login
        GrantedAuthority role = new SimpleGrantedAuthority(ROLE_PREFIX + Constants.PERMISSION_CUSTOMER_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) Customer(com.salesmanager.core.model.customer.Customer) 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)

Example 7 with Group

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

the class ReadableCustomerMapper method merge.

@Override
public ReadableCustomer merge(Customer source, ReadableCustomer target, MerchantStore store, Language language) {
    if (source.getId() != null && source.getId() > 0) {
        target.setId(source.getId());
    }
    target.setEmailAddress(source.getEmailAddress());
    if (StringUtils.isNotEmpty(source.getNick())) {
        target.setUserName(source.getNick());
    }
    if (source.getDefaultLanguage() != null) {
        target.setLanguage(source.getDefaultLanguage().getCode());
    }
    if (source.getGender() != null) {
        target.setGender(source.getGender().name());
    }
    if (StringUtils.isNotEmpty(source.getProvider())) {
        target.setProvider(source.getProvider());
    }
    if (source.getBilling() != null) {
        Address address = new Address();
        address.setAddress(source.getBilling().getAddress());
        address.setCity(source.getBilling().getCity());
        address.setCompany(source.getBilling().getCompany());
        address.setFirstName(source.getBilling().getFirstName());
        address.setLastName(source.getBilling().getLastName());
        address.setPostalCode(source.getBilling().getPostalCode());
        address.setPhone(source.getBilling().getTelephone());
        if (source.getBilling().getCountry() != null) {
            address.setCountry(source.getBilling().getCountry().getIsoCode());
        }
        if (source.getBilling().getZone() != null) {
            address.setZone(source.getBilling().getZone().getCode());
        }
        if (source.getBilling().getState() != null) {
            address.setStateProvince(source.getBilling().getState());
        }
        target.setFirstName(address.getFirstName());
        target.setLastName(address.getLastName());
        target.setBilling(address);
    }
    if (source.getCustomerReviewAvg() != null) {
        target.setRating(source.getCustomerReviewAvg().doubleValue());
    }
    if (source.getCustomerReviewCount() != null) {
        target.setRatingCount(source.getCustomerReviewCount().intValue());
    }
    if (source.getDelivery() != null) {
        Address address = new Address();
        address.setCity(source.getDelivery().getCity());
        address.setAddress(source.getDelivery().getAddress());
        address.setCompany(source.getDelivery().getCompany());
        address.setFirstName(source.getDelivery().getFirstName());
        address.setLastName(source.getDelivery().getLastName());
        address.setPostalCode(source.getDelivery().getPostalCode());
        address.setPhone(source.getDelivery().getTelephone());
        if (source.getDelivery().getCountry() != null) {
            address.setCountry(source.getDelivery().getCountry().getIsoCode());
        }
        if (source.getDelivery().getZone() != null) {
            address.setZone(source.getDelivery().getZone().getCode());
        }
        if (source.getDelivery().getState() != null) {
            address.setStateProvince(source.getDelivery().getState());
        }
        target.setDelivery(address);
    } else {
        target.setDelivery(target.getBilling());
    }
    if (source.getAttributes() != null) {
        for (CustomerAttribute attribute : source.getAttributes()) {
            ReadableCustomerAttribute readableAttribute = new ReadableCustomerAttribute();
            readableAttribute.setId(attribute.getId());
            readableAttribute.setTextValue(attribute.getTextValue());
            ReadableCustomerOption option = new ReadableCustomerOption();
            option.setId(attribute.getCustomerOption().getId());
            option.setCode(attribute.getCustomerOption().getCode());
            CustomerOptionDescription d = new CustomerOptionDescription();
            d.setDescription(attribute.getCustomerOption().getDescriptionsSettoList().get(0).getDescription());
            d.setName(attribute.getCustomerOption().getDescriptionsSettoList().get(0).getName());
            option.setDescription(d);
            readableAttribute.setCustomerOption(option);
            ReadableCustomerOptionValue optionValue = new ReadableCustomerOptionValue();
            optionValue.setId(attribute.getCustomerOptionValue().getId());
            CustomerOptionValueDescription vd = new CustomerOptionValueDescription();
            vd.setDescription(attribute.getCustomerOptionValue().getDescriptionsSettoList().get(0).getDescription());
            vd.setName(attribute.getCustomerOptionValue().getDescriptionsSettoList().get(0).getName());
            optionValue.setCode(attribute.getCustomerOptionValue().getCode());
            optionValue.setDescription(vd);
            readableAttribute.setCustomerOptionValue(optionValue);
            target.getAttributes().add(readableAttribute);
        }
        if (source.getGroups() != null) {
            for (Group group : source.getGroups()) {
                ReadableGroup readableGroup = new ReadableGroup();
                readableGroup.setId(group.getId().longValue());
                readableGroup.setName(group.getGroupName());
                readableGroup.setType(group.getGroupType().name());
                target.getGroups().add(readableGroup);
            }
        }
    }
    return target;
}
Also used : CustomerOptionDescription(com.salesmanager.shop.model.customer.attribute.CustomerOptionDescription) ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) Group(com.salesmanager.core.model.user.Group) ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) Address(com.salesmanager.shop.model.customer.address.Address) ReadableCustomerOptionValue(com.salesmanager.shop.model.customer.attribute.ReadableCustomerOptionValue) ReadableCustomerAttribute(com.salesmanager.shop.model.customer.attribute.ReadableCustomerAttribute) CustomerAttribute(com.salesmanager.core.model.customer.attribute.CustomerAttribute) ReadableCustomerOption(com.salesmanager.shop.model.customer.attribute.ReadableCustomerOption) CustomerOptionValueDescription(com.salesmanager.shop.model.customer.attribute.CustomerOptionValueDescription) ReadableCustomerAttribute(com.salesmanager.shop.model.customer.attribute.ReadableCustomerAttribute)

Example 8 with Group

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

the class CustomerFacadeImpl method authenticate.

public void authenticate(Customer customer, String userName, String password) throws Exception {
    Validate.notNull(customer, "Customer cannot be null");
    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    GrantedAuthority role = // required
    new SimpleGrantedAuthority(ROLE_PREFIX + Constants.PERMISSION_CUSTOMER_AUTHENTICATED);
    // to
    // login
    authorities.add(role);
    List<Integer> groupsId = new ArrayList<Integer>();
    List<Group> groups = customer.getGroups();
    if (groups != null) {
        for (Group group : groups) {
            groupsId.add(group.getId());
        }
        if (groupsId != null && groupsId.size() > 0) {
            List<Permission> permissions = permissionService.getPermissions(groupsId);
            for (Permission permission : permissions) {
                GrantedAuthority auth = new SimpleGrantedAuthority(permission.getPermissionName());
                authorities.add(auth);
            }
        }
    }
    Authentication authenticationToken = new UsernamePasswordAuthenticationToken(userName, password, authorities);
    Authentication authentication = customerAuthenticationManager.authenticate(authenticationToken);
    SecurityContextHolder.getContext().setAuthentication(authentication);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Group(com.salesmanager.core.model.user.Group) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ArrayList(java.util.ArrayList) Permission(com.salesmanager.core.model.user.Permission) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 9 with Group

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

the class SecurityApi method groups.

/**
 * Load groups Requires service user authentication
 *
 * @return
 */
@GetMapping("/private/groups")
public List<ReadableGroup> groups() {
    List<Group> groups = groupService.list();
    List<ReadableGroup> readableGroups = new ArrayList<ReadableGroup>();
    for (Group group : groups) {
        ReadableGroup readableGroup = new ReadableGroup();
        readableGroup.setName(group.getGroupName());
        readableGroup.setId(group.getId().longValue());
        readableGroup.setType(group.getGroupType().name());
        readableGroups.add(readableGroup);
    }
    return readableGroups;
}
Also used : ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) Group(com.salesmanager.core.model.user.Group) ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) ArrayList(java.util.ArrayList) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 10 with Group

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

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