/** * @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; }
@Override public User merge(User entity) { String newpw = entity.getNewPassword(); if (newpw != null && !newpw.isEmpty()) { String enc = PwEncoder.encode(newpw); entity.setPassword(enc); } return super.merge(entity); }
/** * @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; }
throw new BadRequestServiceEx("User type must be specified !"); if(!UserReservedNames.isAllowedName(user.getName())){ throw new BadRequestServiceEx("User name '" + user.getName() + "' is not allowed..."); User u = new User(); u.setName(user.getName()); u.setNewPassword(user.getNewPassword()); u.setEnabled(user.isEnabled()); u.setRole(user.getRole()); Set<UserGroup> groups = user.getGroups(); List<String> groupNames = new ArrayList<String>(); List<UserGroup> existingGroups = new ArrayList<UserGroup>(); throw new NotFoundServiceEx("At least one User group not found; review the groups associated to the user you want to insert" + user.getId()); if(ugEveryone == null || ugEveryone.size() != 1){ LOGGER.error("No UserGroup EVERYONE found, or more than 1 results has been found... skip the EVERYONE group associations for user '" + user.getName() + "'"); u.setGroups(new HashSet<UserGroup>(existingGroups)); List<UserAttribute> attributes = user.getAttribute(); return u.getId();
if (authUser.getRole().equals(Role.ADMIN)) { String npw = user.getNewPassword(); if (npw != null && !npw.isEmpty()) { old.setNewPassword(user.getNewPassword()); userUpdated = true; } else { old.setNewPassword(null); Role nr = user.getRole(); if (nr != null) { old.setRole(nr); userUpdated = true; if(old.isEnabled() != user.isEnabled()){ old.setEnabled(user.isEnabled()); userUpdated = true; Set<UserGroup> groups = user.getGroups(); if (groups != null) { old.setGroups(groups); userUpdated = true; } else if (old.getName().equals(authUser.getName())) { // Check if the User is the same String npw = user.getNewPassword(); if (npw != null && !npw.isEmpty()) { old.setNewPassword(user.getNewPassword()); userUpdated = true; } else { old.setNewPassword(null);
/** * 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); }
LOGGER.info("US: " + us);// + " PW: " + PwEncoder.encode(pw) + " -- " + user.getPassword()); if (!user.isEnabled()) { throw new DisabledException(USER_NOT_FOUND_MSG); Role role = extractUserRoleAndGroups(user.getRole(), authorities, groups); user.setRole(role); user.setGroups(checkReservedGroups(groups)); user = new User(); user.setName(us); user.setNewPassword(null); user.setEnabled(true); user.setRole(role); user.setGroups(checkReservedGroups(groups)); if(userMapper != null) { userMapper.mapUser(ldapUser, user);
/** * 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; }
User user = new User(); user.setName(userName); user.setNewPassword(credentials); user.setEnabled(enableAutoCreatedUsers); user.setRole(role); user.setGroups(Collections.EMPTY_SET); if(userMapper != null) { userMapper.mapUser(rawUser, user);
@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; }
if (authUser.getRole().equals(Role.ADMIN)) { shortResource.setCanEdit(true); shortResource.setCanDelete(true); UserGroup userGroup = rule.getGroup(); if (owner != null) { if (owner.getId().equals(authUser.getId())) { if (rule.isCanWrite()) { shortResource.setCanEdit(true); List<String> groups = extratcGroupNames(authUser.getGroups()); if (groups.contains(userGroup.getGroupName())) { if (rule.isCanWrite()) {
@Override public long update(User user) throws NotFoundServiceEx, BadRequestServiceEx { User orig = userDAO.find(user.getId()); throw new NotFoundServiceEx("User not found " + user.getId()); Set<UserGroup> groups = user.getGroups(); List<String> groupNames = new ArrayList<String>(); List<UserGroup> existingGroups = new ArrayList<UserGroup>(); throw new NotFoundServiceEx("At least one User group not found; review the groups associated to the user you want to insert" + user.getId()); if(ugEveryone == null || ugEveryone.size() != 1){ LOGGER.error("No UserGroup EVERYONE found, or more than 1 results has been found... skip the EVERYONE group associations for user '" + user.getName() + "'"); user.getGroups().clear(); user.getGroups().addAll(existingGroups); return orig.getId();
@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); }
LOGGER.info("Creating now"); user = new User(); user.setName(username); user.setNewPassword(getNewUserPassword(message, username)); user.setRole(newUsersRole); try { user.setId(userService.insert(user));
user = userService.get(us); LOGGER.info("US: " + us );//+ " PW: " + PwEncoder.encode(pw) + " -- " + user.getPassword()); if (user.getPassword() == null || !PwEncoder.isPasswordValid(user.getPassword(),pw)) { throw new BadCredentialsException(UNAUTHORIZED_MSG); if(!user.isEnabled()){ throw new DisabledException(USER_NOT_FOUND_MSG); String role = user.getRole().toString();
throw new BadRequestWebEx("User is null"); if (user.getId() != null) { throw new BadRequestWebEx("Id should be null"); List<UserAttribute> usAttribute = user.getAttribute(); user.setAttribute(null);
/** * 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; } }
@Override public User find(Long id) { User user = super.find(id); if (user != null) { // // To load the LAZY list of the user attributes // if (Hibernate.isInitialized(user)) { List<UserAttribute> attributes = user.getAttribute(); Hibernate.initialize(attributes); Set<UserGroup> groups = user.getGroups(); Hibernate.initialize(groups); } } return user; }
@Override public String getName() { return user != null ? user.getName() : null; }