/** * Creates a Guest principal with Username="guest" password="" and role ROLE_GUEST. * The guest principal should be used with unauthenticated users. * * @return the Principal instance */ public Principal createGuestPrincipal(){ List<GrantedAuthority> authorities = new ArrayList<>(); authorities.add(new GrantedAuthorityImpl("ROLE_GUEST")); try { User u = userService.get(UserReservedNames.GUEST.userName()); return new UsernamePasswordAuthenticationToken(u,"", authorities); } catch (NotFoundServiceEx e) { if(LOGGER.isDebugEnabled()){ LOGGER.debug("User GUEST is not configured, creating on-the-fly a default one"); } } User guest = new User(); guest.setName("guest"); guest.setRole(Role.GUEST); guest.setGroups(new HashSet<UserGroup>()); Principal principal = new UsernamePasswordAuthenticationToken(guest,"", authorities); return principal; }
@Override public boolean insertSpecialUsers() { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Persisting Reserved Users... "); } User u = new User(); u.setName(UserReservedNames.GUEST.userName()); u.setRole(Role.GUEST); Search search = new Search(); search.addFilterEqual("groupName", GroupReservedNames.EVERYONE.groupName()); List<UserGroup> userGroup = userGroupDAO.search(search); if(userGroup.size() != 1){ LOGGER.warn("More than EVERYONE group is found..."); } u.setGroups(new HashSet<UserGroup>(userGroup)); userDAO.persist(u); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Special User '" + u.getName() + "' persisted!"); } return true; }
@Override public void assignUserGroup(long userId, long groupId) throws NotFoundServiceEx{ UserGroup groupToAssign = userGroupDAO.find(groupId); // Check if the group user want to assign is an allowed one if(!GroupReservedNames.isAllowedName(groupToAssign.getGroupName())){ throw new NotFoundServiceEx("You can't re-assign the group EVERYONE or any other reserved groups..."); } User targetUser = userDAO.find(userId); if(groupToAssign == null || targetUser == null){ throw new NotFoundServiceEx("The userGroup or the user you provide doesn't exist"); } if(targetUser.getGroups() == null){ Set<UserGroup> groups = new HashSet<UserGroup>(); groups.add(groupToAssign); targetUser.setGroups(groups); userDAO.merge(targetUser); } else{ targetUser.getGroups().add(groupToAssign); userDAO.merge(targetUser); } }
@Override public User get(SecurityContext sc, String name, boolean includeAttributes) throws NotFoundWebEx { if (name == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("User Name is null !"); } throw new BadRequestWebEx("User name is null"); } User ret; try { ret = userService.get(name); if (includeAttributes) { ret.setAttribute(ret.getAttribute()); } else { ret.setAttribute(null); } ret.setGroups(removeReservedGroups(ret.getGroups())); } catch (NotFoundServiceEx e) { throw new NotFoundWebEx("User not found"); } return ret; }
Role role = extractUserRoleAndGroups(user.getRole(), authorities, groups); user.setRole(role); user.setGroups(checkReservedGroups(groups)); Role role = extractUserRoleAndGroups(null, authorities, groups); user.setRole(role); user.setGroups(checkReservedGroups(groups)); if(userMapper != null) { userMapper.mapUser(ldapUser, user);
/** * @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; }
existingGroups.add(ugEveryone.get(0)); u.setGroups(new HashSet<UserGroup>(existingGroups));
user.setGroups(Collections.EMPTY_SET); if(userMapper != null) { userMapper.mapUser(rawUser, user);
@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; }
old.setGroups(groups); userUpdated = true;
@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; }