Example #1
0
 /** @param authorityNames the authorityNames to set */
 public void setAuthorityNames(List<String> authorityNames) {
   removeList = new ArrayList<UserAuthority>();
   addedList = new ArrayList<UserAuthority>();
   this.authorityNames = new ArrayList<String>();
   if (authorityNames == null) {
     for (UserAuthority auth : authorities) {
       removeList.add(auth);
     }
     for (UserAuthority auth : removeList) {
       this.removeAuthority(auth);
     }
     return;
   }
   this.authorityNames.addAll(authorityNames);
   for (UserAuthority role : authorities) {
     if (authorityNames.contains(role.getAuthority())) {
       // we keep the role, and remove the name
       authorityNames.remove(role.getAuthority());
     } else {
       // this role has been removed
       removeList.add(role);
     }
   }
   for (UserAuthority role : removeList) {
     this.removeAuthority(role);
   }
   // now we need to add what's left in rNames
   for (String name : authorityNames) {
     addedList.add(new UserAuthority(this, name));
     authorities.add(new UserAuthority(this, name));
   }
 }
Example #2
0
 public void mapAuthorityMatrix(Map<String, String> allRoles) {
   userAuthorityMap = new HashMap<String, String>();
   List<String> roleKeys = new ArrayList<String>();
   for (UserAuthority role : this.authorities) roleKeys.add(role.getAuthority());
   for (String key : allRoles.keySet()) {
     if (roleKeys.contains(key)) {
       userAuthorityMap.put(key, allRoles.get(key));
     }
   }
 }
Example #3
0
 /**
  * Remove role from group authority by name
  *
  * @param role
  * @return true if remove successful otherwise false
  */
 public boolean removeAuthority(String authorityName) {
   if (StringUtil.isEmpty(authorityName)) return false;
   for (UserAuthority role : getAuthorities()) {
     if (authorityName.equals(role.getAuthority())) {
       removeAuthority(role);
       return true;
     }
   }
   return false;
 }
Example #4
0
 /**
  * Add authority to group authorities
  *
  * @param authority
  * @return true if add successful otherwise false
  */
 public boolean addAuthority(UserAuthority authority) {
   if (authority == null) throw new IllegalArgumentException("Empty authority is not allowed.");
   if (authorities != null) {
     authorities.remove(authority);
   }
   authority.setUserGroup(this);
   return authorities.add(authority);
 }
 @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
 @Path("/delete")
 @POST
 public String delete(@FormParam("jsonString") String jsonString) {
   Authority authority = JsonMapper.buildNonDefaultMapper().fromJson(jsonString, Authority.class);
   String authorityName = authority.getName();
   int numDeleted = authorityPowerService.deleteByAuthorityName(authorityName);
   int result = authorityService.delete(authority);
   List<UserAuthority> userAuthorityList = userAuthorityService.findByAuthorityName(authorityName);
   if (userAuthorityList.size() > 0) {
     for (UserAuthority ua : userAuthorityList) {
       String userName = ua.getUserName();
       User user = userService.findByName(userName);
       String role = user.getRole();
       String[] roles = role.split(";");
       String[] newRoles = new String[roles.length - 1];
       int temp = 0;
       for (int i = 0; i < roles.length; i++) {
         if (!roles[i].equals(authorityName)) {
           newRoles[temp] = roles[i] + ";";
           temp++;
         }
       }
       String roles2 = "";
       String nr;
       for (int i = 0; i < newRoles.length; i++) {
         roles2 += newRoles[i];
       }
       if (roles2.equals("")) {
         nr = "";
       } else {
         nr = roles2.substring(0, roles2.length() - 1);
       }
       user.setRole(nr);
       userService.update(user);
       userAuthorityService.delete(ua);
     }
   }
   if ((result > 0) && (numDeleted >= 0)) {
     return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.SUCCESS);
   } else {
     return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.ERROR);
   }
 }
Example #6
0
 public void syncAuthorityNames() {
   authorityNames = new ArrayList<String>();
   for (UserAuthority auth : authorities) {
     authorityNames.add(auth.getAuthority());
   }
 }