/** * Save or Update user detail info. * * @param user current user * @param model model * @param updatedUser user to be updated. * @param followersStr user Id list that current will share his permission to. * @return "redirect:/user/list" if current user change his info, otheriwise return "redirect:/" */ @RequestMapping("/save") @PreAuthorize("hasAnyRole('A') or #user.id == #updatedUser.id") public String saveOrUpdateUserDetail( User user, ModelMap model, @ModelAttribute("user") User updatedUser, @RequestParam(required = false) String followersStr) { checkArgument(updatedUser.validate()); if (user.getRole() == Role.USER) { // General user can not change their role. User updatedUserInDb = userService.getUserById(updatedUser.getUserId()); checkNotNull(updatedUserInDb); updatedUser.setRole(updatedUserInDb.getRole()); // prevent user to modify with other user id checkArgument( updatedUserInDb.getId().equals(updatedUser.getId()), "Illegal request to update user:%s", updatedUser); } if (updatedUser.exist()) { userService.modifyUser(updatedUser, followersStr); } else { userService.saveUser(updatedUser); } model.clear(); if (user.getId().equals(updatedUser.getId())) { return "redirect:/"; } else { return "redirect:/user/"; } }
/** * Get the follower list. * * @param user current user * @param model model * @return "user/userOptionGroup" */ @RequestMapping("/switchUserList") public String switchUserList(User user, ModelMap model) { if (user.getRole().hasPermission(Permission.SWITCH_TO_ANYONE)) { List<User> allUserByRole = userService.getAllUserByRole(Role.USER.getFullName()); model.addAttribute("shareUserList", allUserByRole); } else { User currUser = userService.getUserByIdWithoutCache(user.getUserId()); model.addAttribute("shareUserList", currUser.getOwners()); } return "user/userOptionGroup"; }
/** * Add new user into local db. * * @param securedUser user */ @Transactional public void addNewUserIntoLocal(SecuredUser securedUser) { User user = securedUser.getUser(); user.setAuthProviderClass(securedUser.getUserInfoProviderClass()); user.setCreatedDate(new Date()); User newUser = userService.getUserById(user.getUserId()); if (newUser != null) { user = newUser.merge(user); } if (user.getRole() == null) { user.setRole(Role.USER); } User savedUser = userService.saveUser(user); securedUser.setUser(savedUser); }
/** * Check the user id existence. * * @param model model * @param userId userId to be checked * @return success json if true. */ @PreAuthorize("hasAnyRole('A')") @RequestMapping("/{userId}/checkUserId") @ResponseBody public String checkUserId(ModelMap model, @PathVariable String userId) { User user = userService.getUserById(userId); return (user == null) ? returnSuccess() : returnError(); }
/** * Delete users. * * @param model model * @param userIds comma separated user ids. * @return "redirect:/user/list" */ @PreAuthorize("hasAnyRole('A')") @RequestMapping("/delete") public String deleteUser(ModelMap model, @RequestParam String userIds) { String[] ids = userIds.split(","); ArrayList<String> aListNumbers = new ArrayList<String>(Arrays.asList(ids)); userService.deleteUsers(aListNumbers); model.clear(); return "redirect:/user/"; }
/** * Get user detail page. * * @param user current user * @param model mode * @param userId user to get * @return "user/userDetail" */ @RequestMapping("/{userId}") @PreAuthorize("hasAnyRole('A') or #user.userId == #userId") public String getUserDetail(User user, final ModelMap model, @PathVariable final String userId) { model.addAttribute("roleSet", EnumSet.allOf(Role.class)); User userFromDB = userService.getUserByIdWithoutCache(userId); model.addAttribute("user", userFromDB); getUserShareList(userFromDB, model); return "user/userDetail"; }
/** * Get the current user profile. * * @param user current user * @param model model * @return "user/userInfo" */ @RequestMapping("/profile") public String userProfile(User user, ModelMap model) { checkNotEmpty(user.getUserId(), "UserID should not be NULL!"); User currentUser = userService.getUserByIdWithoutCache(user.getUserId()); model.addAttribute("user", currentUser); getUserShareList(currentUser, model); model.addAttribute("action", "profile"); return "user/userInfo"; }
/** * Get user list on the given role. * * @param model model * @param roleName role * @param keywords search keyword. * @return user/userList */ @PreAuthorize("hasAnyRole('A')") @RequestMapping({"", "/"}) public String getUserList( ModelMap model, @RequestParam(required = false) String roleName, @RequestParam(required = false) String keywords) { List<User> userList = null; if (StringUtils.isEmpty(keywords)) { userList = userService.getAllUserByRole(roleName); } else { userList = userService.getUserListByKeyWord(keywords); model.put("keywords", keywords); } model.addAttribute("userList", userList); EnumSet<Role> roleSet = EnumSet.allOf(Role.class); model.addAttribute("roleSet", roleSet); model.addAttribute("roleName", roleName); return "user/userList"; }
/** * Get user list that current user will be shared, excluding current user. * * @param user current user * @param model model */ private void getUserShareList(User user, ModelMap model) { if (user == null) { model.addAttribute("followers", Lists.newArrayList()); model.addAttribute("shareUserList", Lists.newArrayList()); return; } List<User> users = Lists.newArrayList(); String userId = user.getUserId(); for (User u : userService.getAllUserByRole(Role.USER.getFullName())) { if (u.getUserId().equals(userId)) { continue; } users.add(u.getUserBaseInfo()); } model.addAttribute("followers", user.getFollowers()); model.addAttribute("shareUserList", users); }
@Override public User loadUser(String userId) { return userService.getUserById(userId); }