1 package org.tigris.scarab.tools; 2 3 48 49 import java.util.ArrayList ; 50 import java.util.List ; 51 import java.util.LinkedList ; 52 import java.util.Iterator ; 53 import java.io.Serializable ; 54 55 import org.apache.fulcrum.security.TurbineSecurity; 56 import org.apache.fulcrum.security.entity.Group; 57 import org.apache.fulcrum.security.entity.Permission; 58 import org.apache.fulcrum.security.util.RoleSet; 59 import org.apache.fulcrum.security.entity.Role; 60 import org.apache.fulcrum.security.util.AccessControlList; 61 import org.apache.fulcrum.security.util.DataBackendException; 62 import org.apache.fulcrum.security.util.UnknownEntityException; 63 64 import org.apache.torque.util.Criteria; 65 import org.apache.torque.TorqueException; 66 67 import org.apache.turbine.services.pull.ApplicationTool; 68 69 import org.tigris.scarab.om.ScarabUser; 70 import org.tigris.scarab.om.PendingGroupUserRolePeer; 71 import org.tigris.scarab.om.PendingGroupUserRole; 72 import org.tigris.scarab.om.Module; 73 import org.tigris.scarab.services.cache.ScarabCache; 74 75 90 public class SecurityAdminTool 91 implements ApplicationTool, Serializable 92 { 93 private static final String HAS_REQUESTED_ROLE = "hasRequestedRole"; 94 95 private static final String GET_PENDING = "getPendingGroupUserRoles"; 96 97 public void init(Object data) 98 { 99 } 100 101 public void refresh() 102 { 103 } 104 105 110 public ScarabUser getUserByUsername(String username) throws Exception 111 { 112 ScarabUser user = null; 113 114 try 115 { 116 user = (ScarabUser)TurbineSecurity.getUser(username); 117 } 118 catch (UnknownEntityException uee) 119 { 120 } 123 catch (DataBackendException dbe) 124 { 125 } 126 127 return user; 128 } 129 130 135 public Permission getPermissionByName(String name) throws Exception 136 { 137 Permission permission = null; 138 permission = TurbineSecurity.getPermission(name); 139 140 return permission; 141 } 142 143 148 public Role getRoleByName(String name) throws Exception 149 { 150 Role role = null; 151 role = TurbineSecurity.getRole(name); 152 153 return role; 154 } 155 156 159 public Group[] getGroups() throws Exception 160 { 161 return TurbineSecurity.getAllGroups().getGroupsArray(); 162 } 163 164 168 public List getNonMemberGroups(ScarabUser user) throws Exception 169 { 170 AccessControlList acl = getACL(user); 171 Group[] groups = TurbineSecurity.getAllGroups().getGroupsArray(); 172 List nonmemberGroups = new LinkedList (); 173 for (int i=0; i<groups.length; i++) 174 { 175 Module module = (Module)groups[i]; 176 if (!module.isGlobalModule() && !module.getDeleted()) 177 { 178 RoleSet roleSet = acl.getRoles(groups[i]); 179 if (roleSet == null || roleSet.size() == 0) 180 { 181 boolean hasRole = false; 182 Role[] roles = 184 TurbineSecurity.getAllRoles().getRolesArray(); 185 for (int j=0; j<roles.length; j++) 186 { 187 if (hasRequestedRole(user, roles[j], groups[i])) 188 { 189 hasRole = true; 190 break; 191 } 192 } 193 if (!hasRole) 194 { 195 nonmemberGroups.add(groups[i]); 196 } 197 } 198 } 199 } 200 return nonmemberGroups; 201 } 202 203 public boolean hasRequestedRole(ScarabUser user, Role role, Group group) 204 throws TorqueException 205 { 206 List result = null; 207 Object obj = ScarabCache.get(this, HAS_REQUESTED_ROLE, user); 208 if (obj == null) 209 { 210 Criteria crit = new Criteria(); 211 crit.add(PendingGroupUserRolePeer.USER_ID, user.getUserId()); 212 result = PendingGroupUserRolePeer.doSelect(crit); 213 ScarabCache.put(result, this, HAS_REQUESTED_ROLE); 214 } 215 else 216 { 217 result = (List )obj; 218 } 219 boolean b = false; 220 Iterator iter = result.iterator(); 221 while (iter.hasNext()) 222 { 223 PendingGroupUserRole pmur = (PendingGroupUserRole)iter.next(); 224 if (pmur.getRoleName().equals(role.getName()) 225 && ((Module)group).getModuleId().equals(pmur.getGroupId())) 226 { 227 b = true; 228 break; 229 } 230 } 231 return b; 232 } 233 234 237 public Permission[] getPermissions() throws Exception 238 { 239 return (TurbineSecurity.getAllPermissions().getPermissionsArray()); 240 } 241 242 245 public List getPermissionsAsStrings() throws Exception 246 { 247 Permission[] allPerms = this.getPermissions(); 248 List list = new ArrayList (allPerms.length); 249 for (int i=0; i<allPerms.length;i++) 250 { 251 list.add(allPerms[i].getName()); 252 } 253 return list; 254 } 255 256 259 public Role[] getRoles() throws Exception 260 { 261 return TurbineSecurity.getAllRoles().getRolesArray(); 262 } 263 264 267 public List getNonRootRoles() throws Exception 268 { 269 List nonRootRoles = new LinkedList (); 270 Role[] roles = TurbineSecurity.getAllRoles().getRolesArray(); 271 for (int i=0; i<roles.length; i++) 272 { 273 Role role = roles[i]; 274 if (!role.getName().equals("Root")) 275 { 276 nonRootRoles.add(role); 277 } 278 } 279 return nonRootRoles; 280 } 281 282 public List getPendingGroupUserRoles(Module module) 283 throws TorqueException 284 { 285 List result = null; 286 Object obj = ScarabCache.get(this, GET_PENDING, module); 287 if (obj == null) 288 { 289 Criteria crit = new Criteria(); 290 crit.add(PendingGroupUserRolePeer.GROUP_ID, module.getModuleId()); 291 result = PendingGroupUserRolePeer.doSelect(crit); 292 ScarabCache.put(result, this, GET_PENDING); 293 } 294 else 295 { 296 result = (List )obj; 297 } 298 return result; 299 } 300 301 304 public AccessControlList getACL(ScarabUser user) throws Exception 305 { 306 return TurbineSecurity.getACL(user); 307 } 308 } 309 | Popular Tags |