1 17 package org.alfresco.repo.security.authority; 18 19 import java.util.Collections ; 20 import java.util.HashSet ; 21 import java.util.Set ; 22 23 import org.alfresco.model.ContentModel; 24 import org.alfresco.repo.security.authentication.AuthenticationComponent; 25 import org.alfresco.repo.security.permissions.PermissionServiceSPI; 26 import org.alfresco.service.cmr.repository.NodeRef; 27 import org.alfresco.service.cmr.repository.NodeService; 28 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; 29 import org.alfresco.service.cmr.security.AuthorityService; 30 import org.alfresco.service.cmr.security.AuthorityType; 31 import org.alfresco.service.cmr.security.PermissionService; 32 import org.alfresco.service.cmr.security.PersonService; 33 34 39 public class AuthorityServiceImpl implements AuthorityService 40 { 41 private PersonService personService; 42 43 private NodeService nodeService; 44 45 private AuthorityDAO authorityDAO; 46 47 private PermissionServiceSPI permissionServiceSPI; 48 49 private Set <String > adminSet = Collections.singleton(PermissionService.ADMINISTRATOR_AUTHORITY); 50 51 private Set <String > guestSet = Collections.singleton(PermissionService.GUEST_AUTHORITY); 52 53 private Set <String > allSet = Collections.singleton(PermissionService.ALL_AUTHORITIES); 54 55 private Set <String > adminUsers; 56 57 private AuthenticationComponent authenticationComponent; 58 59 public AuthorityServiceImpl() 60 { 61 super(); 62 } 63 64 public void setNodeService(NodeService nodeService) 65 { 66 this.nodeService = nodeService; 67 } 68 69 public void setPersonService(PersonService personService) 70 { 71 this.personService = personService; 72 } 73 74 public void setAuthorityDAO(AuthorityDAO authorityDAO) 75 { 76 this.authorityDAO = authorityDAO; 77 } 78 79 public void setPermissionServiceSPI(PermissionServiceSPI permissionServiceSPI) 80 { 81 this.permissionServiceSPI = permissionServiceSPI; 82 } 83 84 88 public boolean hasAdminAuthority() 89 { 90 String currentUserName = authenticationComponent.getCurrentUserName(); 91 return ((currentUserName != null) && adminUsers.contains(currentUserName)); 92 } 93 94 96 public void setAuthenticationComponent(AuthenticationComponent authenticationComponent) 97 { 98 this.authenticationComponent = authenticationComponent; 99 } 100 101 public void setAdminUsers(Set <String > adminUsers) 102 { 103 this.adminUsers = adminUsers; 104 } 105 106 public Set <String > getAuthorities() 107 { 108 Set <String > authorities = new HashSet <String >(); 109 String currentUserName = authenticationComponent.getCurrentUserName(); 110 if (adminUsers.contains(currentUserName)) 111 { 112 authorities.addAll(adminSet); 113 } 114 if(AuthorityType.getAuthorityType(currentUserName) != AuthorityType.GUEST) 115 { 116 authorities.addAll(allSet); 117 } 118 authorities.addAll(getContainingAuthorities(null, currentUserName, false)); 119 return authorities; 120 } 121 122 public Set <String > getAllAuthorities(AuthorityType type) 123 { 124 Set <String > authorities = new HashSet <String >(); 125 switch (type) 126 { 127 case ADMIN: 128 authorities.addAll(adminSet); 129 break; 130 case EVERYONE: 131 authorities.addAll(allSet); 132 break; 133 case GUEST: 134 authorities.addAll(guestSet); 135 break; 136 case GROUP: 137 authorities.addAll(authorityDAO.getAllAuthorities(type)); 138 break; 139 case OWNER: 140 break; 141 case ROLE: 142 authorities.addAll(authorityDAO.getAllAuthorities(type)); 143 break; 144 case USER: 145 for (NodeRef personRef : personService.getAllPeople()) 146 { 147 authorities.add(DefaultTypeConverter.INSTANCE.convert(String .class, nodeService.getProperty(personRef, 148 ContentModel.PROP_USERNAME))); 149 } 150 break; 151 default: 152 break; 153 } 154 return authorities; 155 } 156 157 public void addAuthority(String parentName, String childName) 158 { 159 authorityDAO.addAuthority(parentName, childName); 160 } 161 162 private void checkTypeIsMutable(AuthorityType type) 163 { 164 if((type == AuthorityType.GROUP) || (type == AuthorityType.ROLE)) 165 { 166 return; 167 } 168 else 169 { 170 throw new AuthorityException("Trying to modify a fixed authority"); 171 } 172 } 173 174 public String createAuthority(AuthorityType type, String parentName, String shortName) 175 { 176 checkTypeIsMutable(type); 177 String name = getName(type, shortName); 178 authorityDAO.createAuthority(parentName, name); 179 return name; 180 } 181 182 public void deleteAuthority(String name) 183 { 184 AuthorityType type = AuthorityType.getAuthorityType(name); 185 checkTypeIsMutable(type); 186 authorityDAO.deleteAuthority(name); 187 permissionServiceSPI.deletePermissions(name); 188 } 189 190 public Set <String > getAllRootAuthorities(AuthorityType type) 191 { 192 return authorityDAO.getAllRootAuthorities(type); 193 } 194 195 public Set <String > getContainedAuthorities(AuthorityType type, String name, boolean immediate) 196 { 197 return authorityDAO.getContainedAuthorities(type, name, immediate); 198 } 199 200 public Set <String > getContainingAuthorities(AuthorityType type, String name, boolean immediate) 201 { 202 return authorityDAO.getContainingAuthorities(type, name, immediate); 203 } 204 205 public String getName(AuthorityType type, String shortName) 206 { 207 if (type.isFixedString()) 208 { 209 return type.getFixedString(); 210 } 211 else if (type.isPrefixed()) 212 { 213 return type.getPrefixString() + shortName; 214 } 215 else 216 { 217 return shortName; 218 } 219 } 220 221 public String getShortName(String name) 222 { 223 AuthorityType type = AuthorityType.getAuthorityType(name); 224 if (type.isFixedString()) 225 { 226 return ""; 227 } 228 else if (type.isPrefixed()) 229 { 230 return name.substring(type.getPrefixString().length()); 231 } 232 else 233 { 234 return name; 235 } 236 237 } 238 239 public void removeAuthority(String parentName, String childName) 240 { 241 authorityDAO.removeAuthority(parentName, childName); 242 } 243 244 } 245 | Popular Tags |