/** * Helper method that creates an Authentication object for the given user, * populating GrantedAuthority instances. * * @param user * @return */ protected Authentication createAuthenticationForUser(User user) { if (user != null) { String role = user.getRole().toString(); List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(new GrantedAuthorityImpl("ROLE_" + role)); return new UsernamePasswordAuthenticationToken(user, user.getPassword(), authorities); } else { LOGGER.error(USER_NOT_FOUND_MSG); return null; } }
/** * @param id * @param groupName */ public RESTUserGroup(Long id, String groupName, Set<User> users, String description) { this.id = id; this.groupName = groupName; List<RESTUser> list = new ArrayList<RESTUser>(); for(User u : users){ list.add(new RESTUser(u.getId(), u.getName(), u.getRole(), u.getGroups(), true)); } this.restUsers = new UserList(list); this.description = description; }
@Override public UserList getUserList(SecurityContext sc, String nameLike, Integer page, Integer entries, boolean includeAttributes) throws BadRequestWebEx { nameLike = nameLike.replaceAll("[*]", "%"); try { List<User> userList = userService.getAll(page, entries, nameLike, includeAttributes); Iterator<User> iterator = userList.iterator(); List<RESTUser> restUSERList = new ArrayList<RESTUser>(); while (iterator.hasNext()) { User user = iterator.next(); RESTUser restUser = new RESTUser(user.getId(), user.getName(), user.getRole(), user.getGroups(), false); restUSERList.add(restUser); } return new UserList(restUSERList); } catch (BadRequestServiceEx ex) { throw new BadRequestWebEx(ex.getMessage()); } }
/** * @param role * @return boolean */ public boolean isUserInRoleAux(String role) { if (Role.GUEST.name().equals(role)) { if (principal.isGuest()) return true; } else { if (principal.isGuest()) return false; else return principal.getUser().getRole().name().equals(role); } return false; }
@Override public UserList getAll(SecurityContext sc, Integer page, Integer entries) throws BadRequestWebEx { try { List<User> userList = userService.getAll(page, entries); Iterator<User> iterator = userList.iterator(); List<RESTUser> restUSERList = new ArrayList<RESTUser>(); while (iterator.hasNext()) { User user = iterator.next(); RESTUser restUser = new RESTUser(user.getId(), user.getName(), user.getRole(), user.getGroups(), false); restUSERList.add(restUser); } return new UserList(restUSERList); } catch (BadRequestServiceEx ex) { throw new BadRequestWebEx(ex.getMessage()); } }
String role = user.getRole().toString();
String role = user.getRole().toString(); if (!roleAllowed(role)){ throw new BadCredentialsException(UNAUTHORIZED_MSG);
if (authUser.getRole().equals(Role.ADMIN)) { return true;
if (authUser.getRole().equals(Role.ADMIN)) { return true;
/** * Add security filtering in order to filter out resources the user has not read access to */ public void addReadSecurityConstraints(Search searchCriteria, User user) { // no further constraints for admin user if(user.getRole() == Role.ADMIN) { return; } Filter userFiltering = Filter.equal("user.name", user.getName()); if(! user.getGroups().isEmpty()) { List<Long> groupsId = new ArrayList<>(); for (UserGroup group : user.getGroups()) { groupsId.add(group.getId()); } userFiltering = Filter.or( userFiltering, Filter.in("group.id", groupsId)); } Filter securityFilter = Filter.some( "security", Filter.and( Filter.equal("canRead", true), userFiltering ) ); searchCriteria.addFilter(securityFilter); }
if((authUser.getRole() == Role.ADMIN) || (owner == null) || (owner.getValue().equals(authUser.getName()))) { try { return new SecurityRuleList(resourceService.getSecurityRules(id));
@Override public long getCount(User user, String nameLike, boolean all) throws BadRequestServiceEx { if (user == null) throw new BadRequestServiceEx("User must be defined."); Search searchCriteria = new Search(UserGroup.class); searchCriteria.addSortAsc("groupName"); Role userRole = user.getRole(); if (userRole.equals((Role)Role.USER)){ Set<UserGroup> userGrp = user.getGroups(); Collection<Long> grpIds = new Vector<Long>(); for(UserGroup grp :userGrp){ grpIds.add(grp.getId()); } searchCriteria.addFilterIn("id", grpIds); } if (nameLike != null) { searchCriteria.addFilterILike("groupName", nameLike); } if(!all) searchCriteria.addFilterNotEqual("groupName", GroupReservedNames.EVERYONE.groupName()); return userGroupDAO.count(searchCriteria); }
if((authUser.getRole() == Role.ADMIN) || (owner == null) || (owner.getValue().equals(authUser.getName()))) { try { resourceService.updateSecurityRules(id, Convert
/** * @param list * @param includeAttributes * @return List<User> */ private List<User> configUserList(List<User> list, boolean includeAttributes) { List<User> uList = new ArrayList<User>(list.size()); for (User user : list) { User u = new User(); u.setGroups(user.getGroups()); u.setId(user.getId()); u.setName(user.getName()); u.setEnabled(user.isEnabled()); u.setPassword(user.getPassword()); u.setRole(user.getRole()); if (includeAttributes) { u.setAttribute(user.getAttribute()); } uList.add(u); } return uList; }
public ResourceAuth getResourceAuth(User authUser, long resourceId) if (authUser.getRole().equals(Role.ADMIN)) { return new ResourceAuth(true, true);
@Override public List<UserGroup> getAllAllowed(User user, Integer page, Integer entries, String nameLike, boolean all) throws BadRequestServiceEx { if (user == null) throw new BadRequestServiceEx("User must be defined."); if (((page != null) && (entries == null)) || ((page == null) && (entries != null))) { throw new BadRequestServiceEx("Page and entries params should be declared together."); } Search searchCriteria = new Search(UserGroup.class); if (page != null) { searchCriteria.setMaxResults(entries); searchCriteria.setPage(page); } searchCriteria.addSortAsc("groupName"); Role userRole = user.getRole(); if (userRole.equals((Role)Role.USER)){ Set<UserGroup> userGrp = user.getGroups(); Collection<Long> grpIds = new Vector<Long>(); for(UserGroup grp :userGrp){ grpIds.add(grp.getId()); } searchCriteria.addFilterIn("id", grpIds); } if (nameLike != null) searchCriteria.addFilterILike("groupName", nameLike); if(!all) searchCriteria.addFilterNotEqual("groupName", GroupReservedNames.EVERYONE.groupName()); List<UserGroup> found = userGroupDAO.search(searchCriteria); return found; }
@Override public User getAuthUserDetails(SecurityContext sc, boolean includeAttributes) { User authUser = extractAuthUser(sc); User ret = null; try { authUser = userService.get(authUser.getName()); if (authUser != null) { if(authUser.getRole().equals(Role.GUEST)){ throw new NotFoundWebEx("User not found"); } ret = new User(); ret.setId(authUser.getId()); ret.setName(authUser.getName()); // ret.setPassword(authUser.getPassword()); // NO! password should not be sent out of the server! ret.setRole(authUser.getRole()); ret.setGroups(authUser.getGroups()); if (includeAttributes) { ret.setAttribute(authUser.getAttribute()); } } } catch (NotFoundServiceEx e) { throw new NotFoundWebEx("User not found"); } return ret; }
if (authUser.getRole().equals(Role.ADMIN)) { shortResource.setCanEdit(true); shortResource.setCanDelete(true);
@Override public User get(SecurityContext sc, long id, boolean includeAttributes) throws NotFoundWebEx { if (id == -1) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Retriving dummy data !"); } // // return test instance // User user = new User(); user.setName("dummy name"); return user; } User authUser = userService.get(id); if (authUser == null) { throw new NotFoundWebEx("User not found"); } User ret = new User(); ret.setId(authUser.getId()); ret.setName(authUser.getName()); // ret.setPassword(authUser.getPassword()); // NO! password should not be sent out of the server! ret.setRole(authUser.getRole()); ret.setEnabled(authUser.isEnabled()); ret.setGroups(removeReservedGroups(authUser.getGroups())); if (includeAttributes) { ret.setAttribute(authUser.getAttribute()); } return ret; }