/** * 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"; }
/** * 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/"; } }
/** * Download file entry of given path. * * @param user current user * @param path user * @param response response */ @RequestMapping("/download/**") public void download(User user, @RemainedPath String path, HttpServletResponse response) { FileEntry fileEntry = fileEntryService.getFileEntry(user, path); if (fileEntry == null) { LOG.error("{} requested to download not existing file entity {}", user.getUserId(), path); return; } response.reset(); try { response.addHeader( "Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(FilenameUtils.getName(fileEntry.getPath()), "utf8")); } catch (UnsupportedEncodingException e1) { LOG.error(e1.getMessage(), e1); } response.setContentType("application/octet-stream; charset=UTF-8"); response.addHeader("Content-Length", "" + fileEntry.getFileSize()); byte[] buffer = new byte[4096]; ByteArrayInputStream fis = null; OutputStream toClient = null; try { fis = new ByteArrayInputStream(fileEntry.getContentBytes()); toClient = new BufferedOutputStream(response.getOutputStream()); int readLength; while (((readLength = fis.read(buffer)) != -1)) { toClient.write(buffer, 0, readLength); } } catch (IOException e) { throw new NGrinderRuntimeException("error while download file", e); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(toClient); } }
/** * get the available agent count map in all regions of the user, including the free agents and * user specified agents. * * @param user current user * @return user available agent count map */ @Override @Transactional public Map<String, MutableInt> getUserAvailableAgentCountMap(User user) { Set<String> regions = getRegions(); Map<String, MutableInt> availShareAgents = newHashMap(regions); Map<String, MutableInt> availUserOwnAgent = newHashMap(regions); for (String region : regions) { availShareAgents.put(region, new MutableInt(0)); availUserOwnAgent.put(region, new MutableInt(0)); } String myAgentSuffix = "_owned_" + user.getUserId(); for (AgentInfo agentInfo : getAllActiveAgentInfoFromDB()) { // Skip the all agents which doesn't approved, is inactive or // doesn't have region // prefix. if (!agentInfo.isApproved()) { continue; } String fullRegion = agentInfo.getRegion(); String region = extractRegionFromAgentRegion(fullRegion); if (StringUtils.isBlank(region) || !regions.contains(region)) { continue; } // It's my own agent if (fullRegion.endsWith(myAgentSuffix)) { incrementAgentCount(availUserOwnAgent, region, user.getUserId()); } else if (fullRegion.contains("_owned_")) { // If it's the others agent.. skip.. continue; } else { incrementAgentCount(availShareAgents, region, user.getUserId()); } } int maxAgentSizePerConsole = getMaxAgentSizePerConsole(); for (String region : regions) { MutableInt mutableInt = availShareAgents.get(region); int shareAgentCount = mutableInt.intValue(); mutableInt.setValue(Math.min(shareAgentCount, maxAgentSizePerConsole)); mutableInt.add(availUserOwnAgent.get(region)); } return availShareAgents; }
/** * 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); }
/** * 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); }
/** * Get the details of given path. * * @param user user * @param path user * @param revision revision. -1 if HEAD * @param model model * @return script/scriptEditor */ @RequestMapping("/detail/**") public String getDetail( User user, @RemainedPath String path, @RequestParam(value = "r", required = false) Long revision, ModelMap model) { FileEntry script = fileEntryService.getFileEntry(user, path, revision); if (script == null || !script.getFileType().isEditable()) { LOG.error( "Error while getting file detail on {}. the file does not exist or not editable", path); model.clear(); return "redirect:/script/list"; } model.addAttribute("file", script); model.addAttribute("ownerId", user.getUserId()); return "script/scriptEditor"; }