Search in sources :

Example 1 with SecurityDataAccessException

use of com.salesmanager.shop.admin.security.SecurityDataAccessException 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 2 with SecurityDataAccessException

use of com.salesmanager.shop.admin.security.SecurityDataAccessException 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

ServiceException (com.salesmanager.core.business.exception.ServiceException)2 Group (com.salesmanager.core.model.user.Group)2 Permission (com.salesmanager.core.model.user.Permission)2 SecurityDataAccessException (com.salesmanager.shop.admin.security.SecurityDataAccessException)2 ArrayList (java.util.ArrayList)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2 Customer (com.salesmanager.core.model.customer.Customer)1 User (com.salesmanager.core.model.user.User)1 JWTUser (com.salesmanager.shop.store.security.user.JWTUser)1