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;
}
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);
}
}
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;
}
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);
}
Aggregations