Search in sources :

Example 6 with Permission

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

the class SecurityApi method listPermissions.

@ResponseStatus(HttpStatus.OK)
@GetMapping({ "/private/{group}/permissions" })
@ApiOperation(httpMethod = "GET", value = "Get permissions by group", notes = "", produces = MediaType.APPLICATION_JSON_VALUE, response = List.class)
public List<ReadablePermission> listPermissions(@PathVariable String group) {
    Group g = null;
    try {
        g = groupService.findByName(group);
        if (g == null) {
            throw new ResourceNotFoundException("Group [" + group + "] does not exist");
        }
    } catch (Exception e) {
        LOGGER.error("An error occured while getting group [" + group + "]", e);
        throw new ServiceRuntimeException("An error occured while getting group [" + group + "]");
    }
    Set<Permission> permissions = g.getPermissions();
    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) Group(com.salesmanager.core.model.user.Group) ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) ReadablePermission(com.salesmanager.shop.model.security.ReadablePermission) Permission(com.salesmanager.core.model.user.Permission) ArrayList(java.util.ArrayList) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation)

Example 7 with Permission

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

the class InitializationDatabaseImpl method createSecurityGroups.

private void createSecurityGroups() throws ServiceException {
    // create permissions
    // Map name object
    Map<String, Permission> permissionKeys = new HashMap<String, Permission>();
    Permission AUTH = new Permission("AUTH");
    permissionService.create(AUTH);
    permissionKeys.put(AUTH.getPermissionName(), AUTH);
    Permission SUPERADMIN = new Permission("SUPERADMIN");
    permissionService.create(SUPERADMIN);
    permissionKeys.put(SUPERADMIN.getPermissionName(), SUPERADMIN);
    Permission ADMIN = new Permission("ADMIN");
    permissionService.create(ADMIN);
    permissionKeys.put(ADMIN.getPermissionName(), ADMIN);
    Permission PRODUCTS = new Permission("PRODUCTS");
    permissionService.create(PRODUCTS);
    permissionKeys.put(PRODUCTS.getPermissionName(), PRODUCTS);
    Permission ORDER = new Permission("ORDER");
    permissionService.create(ORDER);
    permissionKeys.put(ORDER.getPermissionName(), ORDER);
    Permission CONTENT = new Permission("CONTENT");
    permissionService.create(CONTENT);
    permissionKeys.put(CONTENT.getPermissionName(), CONTENT);
    Permission STORE = new Permission("STORE");
    permissionService.create(STORE);
    permissionKeys.put(STORE.getPermissionName(), STORE);
    Permission TAX = new Permission("TAX");
    permissionService.create(TAX);
    permissionKeys.put(TAX.getPermissionName(), TAX);
    Permission PAYMENT = new Permission("PAYMENT");
    permissionService.create(PAYMENT);
    permissionKeys.put(PAYMENT.getPermissionName(), PAYMENT);
    Permission CUSTOMER = new Permission("CUSTOMER");
    permissionService.create(CUSTOMER);
    permissionKeys.put(CUSTOMER.getPermissionName(), CUSTOMER);
    Permission SHIPPING = new Permission("SHIPPING");
    permissionService.create(SHIPPING);
    permissionKeys.put(SHIPPING.getPermissionName(), SHIPPING);
    Permission AUTH_CUSTOMER = new Permission("AUTH_CUSTOMER");
    permissionService.create(AUTH_CUSTOMER);
    permissionKeys.put(AUTH_CUSTOMER.getPermissionName(), AUTH_CUSTOMER);
    SecurityGroupsBuilder groupBuilder = new SecurityGroupsBuilder();
    groupBuilder.addGroup("SUPERADMIN", GroupType.ADMIN).addPermission(permissionKeys.get("AUTH")).addPermission(permissionKeys.get("SUPERADMIN")).addPermission(permissionKeys.get("ADMIN")).addPermission(permissionKeys.get("PRODUCTS")).addPermission(permissionKeys.get("ORDER")).addPermission(permissionKeys.get("CONTENT")).addPermission(permissionKeys.get("STORE")).addPermission(permissionKeys.get("TAX")).addPermission(permissionKeys.get("PAYMENT")).addPermission(permissionKeys.get("CUSTOMER")).addPermission(permissionKeys.get("SHIPPING")).addGroup("ADMIN", GroupType.ADMIN).addPermission(permissionKeys.get("AUTH")).addPermission(permissionKeys.get("ADMIN")).addPermission(permissionKeys.get("PRODUCTS")).addPermission(permissionKeys.get("ORDER")).addPermission(permissionKeys.get("CONTENT")).addPermission(permissionKeys.get("STORE")).addPermission(permissionKeys.get("TAX")).addPermission(permissionKeys.get("PAYMENT")).addPermission(permissionKeys.get("CUSTOMER")).addPermission(permissionKeys.get("SHIPPING")).addGroup("ADMIN_RETAILER", GroupType.ADMIN).addPermission(permissionKeys.get("AUTH")).addPermission(permissionKeys.get("ADMIN")).addPermission(permissionKeys.get("PRODUCTS")).addPermission(permissionKeys.get("ORDER")).addPermission(permissionKeys.get("CONTENT")).addPermission(permissionKeys.get("STORE")).addPermission(permissionKeys.get("TAX")).addPermission(permissionKeys.get("PAYMENT")).addPermission(permissionKeys.get("CUSTOMER")).addPermission(permissionKeys.get("SHIPPING")).addGroup("ADMIN_STORE", GroupType.ADMIN).addPermission(permissionKeys.get("AUTH")).addPermission(permissionKeys.get("CONTENT")).addPermission(permissionKeys.get("STORE")).addPermission(permissionKeys.get("TAX")).addPermission(permissionKeys.get("PAYMENT")).addPermission(permissionKeys.get("CUSTOMER")).addPermission(permissionKeys.get("SHIPPING")).addGroup("ADMIN_CATALOGUE", GroupType.ADMIN).addPermission(permissionKeys.get("AUTH")).addPermission(permissionKeys.get("PRODUCTS")).addGroup("ADMIN_ORDER", GroupType.ADMIN).addPermission(permissionKeys.get("AUTH")).addPermission(permissionKeys.get("ORDER")).addGroup("ADMIN_CONTENT", GroupType.ADMIN).addPermission(permissionKeys.get("AUTH")).addPermission(permissionKeys.get("CONTENT")).addGroup("CUSTOMER", GroupType.CUSTOMER).addPermission(permissionKeys.get("AUTH")).addPermission(permissionKeys.get("AUTH_CUSTOMER"));
    for (Group g : groupBuilder.build()) {
        groupService.create(g);
    }
}
Also used : Group(com.salesmanager.core.model.user.Group) HashMap(java.util.HashMap) Permission(com.salesmanager.core.model.user.Permission) SecurityGroupsBuilder(com.salesmanager.core.business.utils.SecurityGroupsBuilder)

Example 8 with Permission

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

the class PermissionRepositoryImpl method listByCriteria.

@Override
public PermissionList listByCriteria(PermissionCriteria criteria) {
    PermissionList permissionList = new PermissionList();
    StringBuilder countBuilderSelect = new StringBuilder();
    countBuilderSelect.append("select count(p) from Permission as p");
    StringBuilder countBuilderWhere = new StringBuilder();
    if (criteria.getGroupIds() != null && criteria.getGroupIds().size() > 0) {
        countBuilderSelect.append(" INNER JOIN p.groups grous");
        countBuilderWhere.append(" where grous.id in (:cid)");
    }
    Query countQ = em.createQuery(countBuilderSelect.toString() + countBuilderWhere.toString());
    if (criteria.getGroupIds() != null && criteria.getGroupIds().size() > 0) {
        countQ.setParameter("cid", criteria.getGroupIds());
    }
    Number count = (Number) countQ.getSingleResult();
    permissionList.setTotalCount(count.intValue());
    if (count.intValue() == 0)
        return permissionList;
    StringBuilder qs = new StringBuilder();
    qs.append("select p from Permission as p ");
    qs.append("join fetch p.groups grous ");
    if (criteria.getGroupIds() != null && criteria.getGroupIds().size() > 0) {
        qs.append(" where grous.id in (:cid)");
    }
    qs.append(" order by p.id asc ");
    String hql = qs.toString();
    Query q = em.createQuery(hql);
    if (criteria.getGroupIds() != null && criteria.getGroupIds().size() > 0) {
        q.setParameter("cid", criteria.getGroupIds());
    }
    if (criteria.getMaxCount() > 0) {
        q.setFirstResult(criteria.getStartIndex());
        if (criteria.getMaxCount() < count.intValue()) {
            q.setMaxResults(criteria.getMaxCount());
            permissionList.setTotalCount(criteria.getMaxCount());
        } else {
            q.setMaxResults(count.intValue());
            permissionList.setTotalCount(count.intValue());
        }
    }
    @SuppressWarnings("unchecked") List<Permission> permissions = q.getResultList();
    permissionList.setPermissions(permissions);
    return permissionList;
}
Also used : Query(javax.persistence.Query) PermissionList(com.salesmanager.core.model.user.PermissionList) Permission(com.salesmanager.core.model.user.Permission)

Example 9 with Permission

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

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