Search in sources :

Example 1 with Permission

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

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

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

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

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

the class SecurityApi method permissions.

/**
 * Permissions Requires service user authentication
 *
 * @return
 */
@GetMapping("/private/permissions")
public List<ReadablePermission> permissions() {
    List<Permission> permissions = permissionService.list();
    List<ReadablePermission> readablePermissions = new ArrayList<ReadablePermission>();
    for (Permission permission : permissions) {
        ReadablePermission readablePermission = new ReadablePermission();
        readablePermission.setName(permission.getPermissionName());
        readablePermission.setId(permission.getId());
        readablePermissions.add(readablePermission);
    }
    return readablePermissions;
}
Also used : ReadablePermission(com.salesmanager.shop.model.security.ReadablePermission) ReadablePermission(com.salesmanager.shop.model.security.ReadablePermission) Permission(com.salesmanager.core.model.user.Permission) ArrayList(java.util.ArrayList) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Permission (com.salesmanager.core.model.user.Permission)9 Group (com.salesmanager.core.model.user.Group)7 ArrayList (java.util.ArrayList)6 GrantedAuthority (org.springframework.security.core.GrantedAuthority)4 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)4 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)3 ServiceException (com.salesmanager.core.business.exception.ServiceException)2 SecurityDataAccessException (com.salesmanager.shop.admin.security.SecurityDataAccessException)2 ReadablePermission (com.salesmanager.shop.model.security.ReadablePermission)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 SecurityGroupsBuilder (com.salesmanager.core.business.utils.SecurityGroupsBuilder)1 Customer (com.salesmanager.core.model.customer.Customer)1 PermissionList (com.salesmanager.core.model.user.PermissionList)1 User (com.salesmanager.core.model.user.User)1 ReadableGroup (com.salesmanager.shop.model.security.ReadableGroup)1 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)1 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)1 JWTUser (com.salesmanager.shop.store.security.user.JWTUser)1 ApiOperation (io.swagger.annotations.ApiOperation)1 HashMap (java.util.HashMap)1