@RequestMapping("role-def-sync") public String sync(@RequestParam("id") Long id) throws Exception { RoleDef roleDef = roleDefManager.get(id); List<Role> roles = roleManager.findBy("roleDef.id", id); ScopeDTO currentScope = ScopeHolder.getScopeDto(); List<ScopeDTO> scopeDtos; if (currentScope.isShared()) { scopeDtos = scopeConnector.findAll(); } else { scopeDtos = new ArrayList<ScopeDTO>(); scopeDtos.add(currentScope); } for (ScopeDTO scopeDto : scopeDtos) { Role existedRole = null; for (Role role : roles) { if (role.getScopeId().equals(scopeDto.getId())) { existedRole = role; break; } } if (existedRole == null) { Role role = new Role(); role.setName(roleDef.getName()); role.setRoleDef(roleDef); role.setScopeId(scopeDto.getId()); roleManager.save(role); } } for (Role role : roles) { boolean existed = false; for (ScopeDTO scopeDto : scopeDtos) { if (role.getScopeId().equals(scopeDto.getId())) { existed = true; break; } } if (!existed) { roleManager.remove(role); } } return "redirect:/auth/role-def-manage.do?id=" + id; }
public RoleDTO convertRoleDto(Role role, boolean useScope) { RoleDTO roleDto = new RoleDTO(); roleDto.setId(role.getId()); if (useScope) { roleDto.setName( role.getName() + "(" + scopeConnector.findById(role.getScopeId()).getName() + ")"); } else { roleDto.setName(role.getName()); } roleDto.setScopeId(role.getScopeId()); return roleDto; }
public String input() { if (userText != null) { for (String str : userText.split("\n")) { str = str.trim(); if (str.length() == 0) { continue; } String username = str; UserDTO userDto = userConnector.findByUsername(username, ScopeHolder.getUserRepoRef()); if (userDto == null) { addActionMessage(str + " is not exists."); } else { UserStatus userStatus = authService.createOrGetUserStatus( username, userDto.getId(), ScopeHolder.getUserRepoRef(), ScopeHolder.getScopeId()); try { userStatusChecker.check(userStatus); userStatuses.add(userStatus); } catch (CheckUserStatusException ex) { addActionMessage(ex.getMessage()); } } } } roles = roleManager.find("from Role where scopeId=?", ScopeHolder.getScopeId()); roleDtos.addAll(convertRoleDtos(roles, false)); List<ScopeInfo> sharedScopeInfos = scopeConnector.findSharedScopes(); logger.info("{}", sharedScopeInfos); for (ScopeInfo scopeInfo : sharedScopeInfos) { List<Role> sharedRoles = authService.findRoles(scopeInfo.getId()); roleDtos.addAll(convertRoleDtos(sharedRoles, true)); } return INPUT; }
/** http://localhost:8080/ctx/model/service.do x-scope-code: default */ public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String scopeCode = request.getHeader(scopeHeaderName); if (scopeCode == null) { scopeCode = defaultScopeCode; } try { ScopeHolder.setScopeDto(scopeConnector.findByCode(scopeCode)); request.setAttribute("scopePrefix", request.getContextPath()); filterChain.doFilter(request, response); } finally { ScopeHolder.clear(); } }
@RequestMapping("role-def-manage") public String manage(@RequestParam("id") Long id, Model model) throws Exception { RoleDef roleDef = roleDefManager.get(id); List<Role> roles = roleManager.findBy("roleDef.id", id); ScopeDTO currentScope = ScopeHolder.getScopeDto(); List<ScopeDTO> scopeDtos; if (currentScope.isShared()) { scopeDtos = scopeConnector.findAll(); } else { scopeDtos = new ArrayList<ScopeDTO>(); scopeDtos.add(currentScope); } List<RoleDTO> roleDtos = new ArrayList<RoleDTO>(); for (ScopeDTO scopeDto : scopeDtos) { Role existedRole = null; for (Role role : roles) { if (role.getScopeId().equals(scopeDto.getId())) { existedRole = role; break; } } if (existedRole == null) { RoleDTO roleDto = new RoleDTO(); roleDto.setName(roleDef.getName()); roleDto.setScopeId(scopeDto.getId()); roleDto.setStatus("added"); roleDtos.add(roleDto); } else { RoleDTO roleDto = new RoleDTO(); roleDto.setName(roleDef.getName()); roleDto.setId(existedRole.getId()); roleDto.setScopeId(scopeDto.getId()); roleDto.setStatus("existed"); roleDtos.add(roleDto); } } for (Role role : roles) { boolean existed = false; for (ScopeDTO scopeDto : scopeDtos) { if (role.getScopeId().equals(scopeDto.getId())) { existed = true; break; } } if (!existed) { RoleDTO roleDto = new RoleDTO(); roleDto.setName(roleDef.getName()); roleDto.setId(role.getId()); roleDto.setScopeId(role.getScopeId()); roleDto.setStatus("removed"); roleDtos.add(roleDto); } } model.addAttribute("roleDts", roleDtos); return "auth/role-def-manage"; }