@Override protected Authentication checkToken(String token) { UserAttribute attribute = new UserAttribute(); attribute.setName(attributeName); attribute.setValue(token); // looks for user(s) having the specified attribute with the given // token value Collection<User> users = userService.getByAttribute(attribute); // the token is considered valid if only 1 user matches if(users.size() == 1) { User user = users.iterator().next(); return createAuthenticationForUser(user); } else if(users.size() > 1) { LOGGER.error("Too many users matching the given token. Only one is allowed for a token to be valid!"); } else { LOGGER.error("No users matching the given token."); } return null; }
@Override public Collection<User> getByAttribute(UserAttribute attribute) { Search searchCriteria = new Search(UserAttribute.class); searchCriteria.addFilterEqual("name", attribute.getName()); searchCriteria.addFilterEqual("value", attribute.getValue()); searchCriteria.addFetch("user"); List<UserAttribute> attributes = userAttributeDAO.search(searchCriteria); Set<User> users = new HashSet<User>(); for(UserAttribute userAttr : attributes) { if(userAttr.getUser() != null) { users.add(userAttr.getUser()); } } return users; } }
UserAttribute aDto = iteratorDto.next(); UserAttribute a = new UserAttribute(); a.setValue(aDto.getValue()); a.setName(aDto.getName()); attributes.add(a);
@Override public void updateAttributes(long id, List<UserAttribute> attributes) throws NotFoundServiceEx { User user = userDAO.find(id); if (user == null) { throw new NotFoundServiceEx("User not found " + id); } // // Removing old attributes // List<UserAttribute> oldList = user.getAttribute(); // Iterator<UserAttribute> iterator; if (oldList != null) { for (UserAttribute a : oldList) { userAttributeDAO.removeById(a.getId()); } } // // Saving old attributes // for (UserAttribute a : attributes) { a.setUser(user); userAttributeDAO.persist(a); } }
a.setUser(u); userAttributeDAO.persist(a);
@Override /** * If details is a UserDetailsWithAttributes object, its attributes are mapped to User * attributes. * Each mapping is an SpEL expression using the UserDetailsWithAttributes as a source. * */ public void mapUser(Object details, User user) { List<UserAttribute> attributes = new ArrayList<UserAttribute>(); details = preProcessDetails(details); for(String attributeName : attributeMappings.keySet()) { Expression exp = parser.parseExpression(attributeMappings.get(attributeName)); UserAttribute userAttribute = new UserAttribute(); userAttribute.setName(attributeName); Object value = exp.getValue(evaluationContext, details); userAttribute.setValue(value == null ? null : value.toString()); attributes.add(userAttribute); } user.setAttribute(attributes); }