@Test public void saveRole() throws IOException { RoleForm form = new RoleForm("role", null, Sets.newHashSet( Permission.EDIT_BRANCH.name(), Permission.EDIT_PAGE.name())); BindingResult bindingResult = new BeanPropertyBindingResult(form, "roleForm"); String view = roleController.saveRole(form, bindingResult, authentication); assertEquals("/roles", removeViewPrefix(view)); assertRedirect(view); assertFalse(bindingResult.hasErrors()); verify(userStore).saveRole( argRole("role", EnumSet.of(Permission.EDIT_BRANCH, Permission.EDIT_PAGE)), same(USER)); }
@RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") public String saveRole(@ModelAttribute @Valid RoleForm form, BindingResult bindingResult, Authentication authentication) throws IOException { if (StringUtils.isNotBlank(form.getName()) && (StringUtils.isBlank(form.getOriginalName()) || !form.getName().equals(form.getOriginalName()))) { try { if (userStore.getRole(form.getName()) != null) { bindingResult.rejectValue("name", "role.name.exists"); } } catch (RoleNotFoundException e) { } } if (bindingResult.hasErrors()) { return "/user/role/edit"; } EnumSet<Permission> permissions = EnumSet.noneOf(Permission.class); for (String permission : form.getPermissions()) { permissions.add(Permission.valueOf(permission)); } String newRoleName = form.getOriginalName(); if (StringUtils.isBlank(newRoleName)) { newRoleName = form.getName(); } Role role = new Role(newRoleName, permissions); User user = userStore.getUser(authentication.getName()); userStore.saveRole(role, user); if (StringUtils.isNotBlank(form.getOriginalName()) && StringUtils.isNotBlank(form.getName()) && !StringUtils.equals(form.getName(), form.getOriginalName())) { userStore.renameRole(form.getOriginalName(), form.getName(), user); } return "redirect:/roles"; }
RoleController { @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") public String saveRole(@ModelAttribute @Valid RoleForm form, BindingResult bindingResult, Authentication authentication) throws IOException { if (StringUtils.isNotBlank(form.getName()) && (StringUtils.isBlank(form.getOriginalName()) || !form.getName().equals(form.getOriginalName()))) { try { if (userStore.getRole(form.getName()) != null) { bindingResult.rejectValue("name", "role.name.exists"); } } catch (RoleNotFoundException e) { } } if (bindingResult.hasErrors()) { return "/user/role/edit"; } EnumSet<Permission> permissions = EnumSet.noneOf(Permission.class); for (String permission : form.getPermissions()) { permissions.add(Permission.valueOf(permission)); } String newRoleName = form.getOriginalName(); if (StringUtils.isBlank(newRoleName)) { newRoleName = form.getName(); } Role role = new Role(newRoleName, permissions); User user = userStore.getUser(authentication.getName()); userStore.saveRole(role, user); if (StringUtils.isNotBlank(form.getOriginalName()) && StringUtils.isNotBlank(form.getName()) && !StringUtils.equals(form.getName(), form.getOriginalName())) { userStore.renameRole(form.getOriginalName(), form.getName(), user); } return "redirect:/roles"; } }
RoleController { @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") public String saveRole(@ModelAttribute @Valid RoleForm form, BindingResult bindingResult, Authentication authentication) throws IOException { if (StringUtils.isNotBlank(form.getName()) && (StringUtils.isBlank(form.getOriginalName()) || !form.getName().equals(form.getOriginalName()))) { try { if (userStore.getRole(form.getName()) != null) { bindingResult.rejectValue("name", "role.name.exists"); } } catch (RoleNotFoundException e) { } } if (bindingResult.hasErrors()) { return "/user/role/edit"; } EnumSet<Permission> permissions = EnumSet.noneOf(Permission.class); for (String permission : form.getPermissions()) { permissions.add(Permission.valueOf(permission)); } String newRoleName = form.getOriginalName(); if (StringUtils.isBlank(newRoleName)) { newRoleName = form.getName(); } Role role = new Role(newRoleName, permissions); User user = userStore.getUser(authentication.getName()); userStore.saveRole(role, user); if (StringUtils.isNotBlank(form.getOriginalName()) && StringUtils.isNotBlank(form.getName()) && !StringUtils.equals(form.getName(), form.getOriginalName())) { userStore.renameRole(form.getOriginalName(), form.getName(), user); } return "redirect:/roles"; }  }
RoleController { @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") public String saveRole(@ModelAttribute @Valid RoleForm form, BindingResult bindingResult, Authentication authentication) throws IOException { if (StringUtils.isNotBlank(form.getName()) && (StringUtils.isBlank(form.getOriginalName()) || !form.getName().equals(form.getOriginalName()))) { try { if (userStore.getRole(form.getName()) != null) { bindingResult.rejectValue("name", "role.name.exists"); } } catch (RoleNotFoundException e) { } } if (bindingResult.hasErrors()) { return "/user/role/edit"; } EnumSet<Permission> permissions = EnumSet.noneOf(Permission.class); for (String permission : form.getPermissions()) { permissions.add(Permission.valueOf(permission)); } String newRoleName = form.getOriginalName(); if (StringUtils.isBlank(newRoleName)) { newRoleName = form.getName(); } Role role = new Role(newRoleName, permissions); User user = userStore.getUser(authentication.getName()); userStore.saveRole(role, user); if (StringUtils.isNotBlank(form.getOriginalName()) && StringUtils.isNotBlank(form.getName()) && !StringUtils.equals(form.getName(), form.getOriginalName())) { userStore.renameRole(form.getOriginalName(), form.getName(), user); } return "redirect:/roles"; }  @RequestMapping(value="/add", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN)") String addRole(Model model); @RequestMapping(value="/edit/{roleName:" + DocumentrConstants.ROLE_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN)") String editRole(@PathVariable String roleName, Model model); @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") String saveRole(@ModelAttribute @Valid RoleForm form, BindingResult bindingResult,
			Authentication authentication); @RequestMapping(value="/delete/{roleName:" + DocumentrConstants.ROLE_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN) and !isLastAdminRole(#roleName)") String deleteRole(@PathVariable String roleName, Authentication authentication); @ModelAttribute RoleForm createRoleForm(@RequestParam(required=false) String name,
			@RequestParam(required=false) String originalName,
			@RequestParam(required=false) Set<String> permissions); }
RoleController { @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") public String saveRole(@ModelAttribute @Valid RoleForm form, BindingResult bindingResult, Authentication authentication) throws IOException { if (StringUtils.isNotBlank(form.getName()) && (StringUtils.isBlank(form.getOriginalName()) || !form.getName().equals(form.getOriginalName()))) { try { if (userStore.getRole(form.getName()) != null) { bindingResult.rejectValue("name", "role.name.exists"); } } catch (RoleNotFoundException e) { } } if (bindingResult.hasErrors()) { return "/user/role/edit"; } EnumSet<Permission> permissions = EnumSet.noneOf(Permission.class); for (String permission : form.getPermissions()) { permissions.add(Permission.valueOf(permission)); } String newRoleName = form.getOriginalName(); if (StringUtils.isBlank(newRoleName)) { newRoleName = form.getName(); } Role role = new Role(newRoleName, permissions); User user = userStore.getUser(authentication.getName()); userStore.saveRole(role, user); if (StringUtils.isNotBlank(form.getOriginalName()) && StringUtils.isNotBlank(form.getName()) && !StringUtils.equals(form.getName(), form.getOriginalName())) { userStore.renameRole(form.getOriginalName(), form.getName(), user); } return "redirect:/roles"; }  @RequestMapping(value="/add", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN)") String addRole(Model model); @RequestMapping(value="/edit/{roleName:" + DocumentrConstants.ROLE_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN)") String editRole(@PathVariable String roleName, Model model); @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") String saveRole(@ModelAttribute @Valid RoleForm form, BindingResult bindingResult,
			Authentication authentication); @RequestMapping(value="/delete/{roleName:" + DocumentrConstants.ROLE_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN) and !isLastAdminRole(#roleName)") String deleteRole(@PathVariable String roleName, Authentication authentication); @ModelAttribute RoleForm createRoleForm(@RequestParam(required=false) String name,
			@RequestParam(required=false) String originalName,
			@RequestParam(required=false) Set<String> permissions);  }