1 43 package net.jforum.security; 44 45 import java.util.ArrayList ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import java.util.Map ; 50 51 55 public class UserSecurityHelper 56 { 57 public static void mergeUserGroupRoles(RoleCollection userRoles, List groupsRolesList) 58 { 59 Map newRolesMap = new HashMap (); 60 61 for (Iterator iter = groupsRolesList.iterator(); iter.hasNext(); ) { 63 RoleCollection rc = (RoleCollection)iter.next(); 64 65 for (Iterator rcIter = rc.values().iterator(); rcIter.hasNext(); ) { 66 final Role role = (Role)rcIter.next(); 67 Role userRole = userRoles.get(role.getName()); 68 69 if (userRole == null) { 70 if (newRolesMap.containsKey(role.getName())) { 71 ((List )newRolesMap.get(role.getName())).add(role); 72 } 73 else { 74 newRolesMap.put(role.getName(), new ArrayList () {{ add(role); }}); 75 } 76 } 77 else { 78 for (Iterator vIter = role.getValues().iterator(); vIter.hasNext(); ) { 80 RoleValue gRv = (RoleValue)vIter.next(); 81 RoleValue uRv = userRole.getValues().get(gRv.getValue()); 82 83 if (uRv == null) { 86 userRole.getValues().add(gRv); 87 } 88 } 89 } 90 } 91 } 92 93 for (Iterator iter = newRolesMap.entrySet().iterator(); iter.hasNext(); ) { 94 Map.Entry entry = (Map.Entry )iter.next(); 95 96 Role newRole = new Role(); 97 newRole.setName((String )entry.getKey()); 98 newRole.setType(PermissionControl.ROLE_DENY); 99 100 List roles = (List )entry.getValue(); 101 for (Iterator rolesIter = roles.iterator(); rolesIter.hasNext(); ) { 102 Role role = (Role)rolesIter.next(); 103 newRole.setId(role.getId()); 104 105 if (role.getValues().size() == 0) { 109 if (role.getType() == PermissionControl.ROLE_ALLOW) { 110 newRole.setType(PermissionControl.ROLE_ALLOW); 111 break; 112 } 113 } 114 else { 115 for (Iterator valuesIter = role.getValues().iterator(); valuesIter.hasNext(); ) { 119 RoleValue rv = (RoleValue)valuesIter.next(); 120 RoleValue currentValue = newRole.getValues().get(rv.getValue()); 121 122 if (currentValue == null) { 123 newRole.getValues().add(rv); 124 } 125 else { 126 if (rv.getType() != currentValue.getType() 129 && rv.getType() == PermissionControl.ROLE_ALLOW) { 130 newRole.getValues().remove(currentValue); 131 currentValue.setType(PermissionControl.ROLE_ALLOW); 132 newRole.getValues().add(currentValue); 133 } 134 } 135 } 136 } 137 } 138 139 userRoles.add(newRole); 140 } 141 } 142 } 143 | Popular Tags |