1 17 package org.alfresco.repo.security.person; 18 19 import java.io.Serializable ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.LinkedHashSet ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import org.alfresco.error.AlfrescoRuntimeException; 29 import org.alfresco.model.ContentModel; 30 import org.alfresco.repo.security.permissions.PermissionServiceSPI; 31 import org.alfresco.service.cmr.repository.NodeRef; 32 import org.alfresco.service.cmr.repository.NodeService; 33 import org.alfresco.service.cmr.repository.StoreRef; 34 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; 35 import org.alfresco.service.cmr.search.ResultSet; 36 import org.alfresco.service.cmr.search.ResultSetRow; 37 import org.alfresco.service.cmr.search.SearchParameters; 38 import org.alfresco.service.cmr.search.SearchService; 39 import org.alfresco.service.cmr.security.AuthorityService; 40 import org.alfresco.service.cmr.security.NoSuchPersonException; 41 import org.alfresco.service.cmr.security.PersonService; 42 import org.alfresco.service.namespace.NamespacePrefixResolver; 43 import org.alfresco.service.namespace.QName; 44 45 public class PersonServiceImpl implements PersonService 46 { 47 public static final String SYSTEM_FOLDER = "/sys:system"; 48 49 public static final String PEOPLE_FOLDER = SYSTEM_FOLDER + "/sys:people"; 50 51 53 private StoreRef storeRef; 54 55 private NodeService nodeService; 56 57 private SearchService searchService; 58 59 private AuthorityService authorityService; 60 61 private PermissionServiceSPI permissionServiceSPI; 62 63 private NamespacePrefixResolver namespacePrefixResolver; 64 65 private boolean createMissingPeople; 66 67 private boolean userNamesAreCaseSensitive; 68 69 private String companyHomePath; 70 71 private NodeRef companyHomeNodeRef; 72 73 private static Set <QName> mutableProperties; 74 75 static 76 { 77 Set <QName> props = new HashSet <QName>(); 78 props.add(ContentModel.PROP_HOMEFOLDER); 79 props.add(ContentModel.PROP_FIRSTNAME); 80 props.add(ContentModel.PROP_LASTNAME); 82 props.add(ContentModel.PROP_EMAIL); 83 props.add(ContentModel.PROP_ORGID); 84 mutableProperties = Collections.unmodifiableSet(props); 85 } 86 87 public PersonServiceImpl() 88 { 89 super(); 90 } 91 92 public boolean getUserNamesAreCaseSensitive() 93 { 94 return userNamesAreCaseSensitive; 95 } 96 97 public void setUserNamesAreCaseSensitive(boolean userNamesAreCaseSensitive) 98 { 99 this.userNamesAreCaseSensitive = userNamesAreCaseSensitive; 100 } 101 102 public NodeRef getPerson(String caseSensitiveUserName) 103 { 104 String userName = userNamesAreCaseSensitive ? caseSensitiveUserName : caseSensitiveUserName.toLowerCase(); 105 NodeRef personNode = getPersonOrNull(userName); 106 if (personNode == null) 107 { 108 if (createMissingPeople()) 109 { 110 return createMissingPerson(userName); 111 } 112 else 113 { 114 throw new NoSuchPersonException(userName); 115 } 116 117 } 118 else 119 { 120 return personNode; 121 } 122 } 123 124 public boolean personExists(String caseSensitiveUserName) 125 { 126 return getPersonOrNull(caseSensitiveUserName) != null; 127 } 128 129 public NodeRef getPersonOrNull(String caseSensitiveUserName) 130 { 131 String userName = userNamesAreCaseSensitive ? caseSensitiveUserName : caseSensitiveUserName.toLowerCase(); 132 SearchParameters sp = new SearchParameters(); 133 sp.setLanguage(SearchService.LANGUAGE_LUCENE); 134 sp.setQuery("TYPE:\\{http\\://www.alfresco.org/model/content/1.0\\}person +@cm\\:userName:\"" + userName + "\""); 135 sp.addStore(storeRef); 136 sp.excludeDataInTheCurrentTransaction(false); 137 138 ResultSet rs = null; 139 140 try 141 { 142 rs = searchService.query(sp); 143 144 for (ResultSetRow row : rs) 145 { 146 147 NodeRef nodeRef = row.getNodeRef(); 148 if (nodeService.exists(nodeRef)) 149 { 150 String realUserName = DefaultTypeConverter.INSTANCE.convert( 151 String .class, 152 nodeService.getProperty(nodeRef, ContentModel.PROP_USERNAME)); 153 realUserName = userNamesAreCaseSensitive ? realUserName : realUserName.toLowerCase(); 154 if (realUserName.equals(userName)) 155 { 156 return nodeRef; 157 } 158 } 159 } 160 } 161 finally 162 { 163 if (rs != null) 164 { 165 rs.close(); 166 } 167 } 168 169 return null; 170 } 171 172 public boolean createMissingPeople() 173 { 174 return createMissingPeople; 175 } 176 177 public Set <QName> getMutableProperties() 178 { 179 return mutableProperties; 180 } 181 182 public void setPersonProperties(String caseSensitiveUserName, Map <QName, Serializable > properties) 183 { 184 String userName = userNamesAreCaseSensitive ? caseSensitiveUserName : caseSensitiveUserName.toLowerCase(); 185 NodeRef personNode = getPersonOrNull(userName); 186 if (personNode == null) 187 { 188 if (createMissingPeople()) 189 { 190 personNode = createMissingPerson(userName); 191 } 192 else 193 { 194 throw new PersonException("No person found for user name " + userName); 195 } 196 197 } 198 199 properties.put(ContentModel.PROP_USERNAME, userName); 200 201 nodeService.setProperties(personNode, properties); 202 } 203 204 public boolean isMutable() 205 { 206 return true; 207 } 208 209 private NodeRef createMissingPerson(String userName) 210 { 211 HashMap <QName, Serializable > properties = getDefaultProperties(userName); 212 return createPerson(properties); 213 } 214 215 private HashMap <QName, Serializable > getDefaultProperties(String userName) 216 { 217 HashMap <QName, Serializable > properties = new HashMap <QName, Serializable >(); 218 properties.put(ContentModel.PROP_USERNAME, userName); 219 properties.put(ContentModel.PROP_HOMEFOLDER, getHomeFolder()); 220 properties.put(ContentModel.PROP_FIRSTNAME, userName); 221 properties.put(ContentModel.PROP_LASTNAME, ""); 222 properties.put(ContentModel.PROP_EMAIL, ""); 223 properties.put(ContentModel.PROP_ORGID, ""); 224 return properties; 225 } 226 227 private NodeRef getHomeFolder() 228 { 229 return getCompanyHome(); 230 } 231 232 public NodeRef createPerson(Map <QName, Serializable > properties) 233 { 234 String caseSensitiveUserName = DefaultTypeConverter.INSTANCE.convert(String .class, properties 235 .get(ContentModel.PROP_USERNAME)); 236 String userName = userNamesAreCaseSensitive ? caseSensitiveUserName : caseSensitiveUserName.toLowerCase(); 237 properties.put(ContentModel.PROP_USERNAME, userName); 238 return nodeService.createNode( 239 getPeopleContainer(), 240 ContentModel.ASSOC_CHILDREN, 241 ContentModel.TYPE_PERSON, 242 ContentModel.TYPE_PERSON, 243 properties).getChildRef(); 244 } 245 246 public NodeRef getPeopleContainer() 247 { 248 NodeRef rootNodeRef = nodeService.getRootNode(storeRef); 249 List <NodeRef> results = searchService.selectNodes( 250 rootNodeRef, 251 PEOPLE_FOLDER, 252 null, 253 namespacePrefixResolver, 254 false); 255 if (results.size() == 0) 256 { 257 throw new AlfrescoRuntimeException("Required people system path not found: " + PEOPLE_FOLDER); 258 } 259 else 260 { 261 return results.get(0); 262 } 263 } 264 265 public void deletePerson(String userName) 266 { 267 NodeRef personNodeRef = getPersonOrNull(userName); 268 269 if (personNodeRef != null) 271 { 272 nodeService.deleteNode(personNodeRef); 273 } 274 275 String authorityName = userNamesAreCaseSensitive ? userName : userName.toLowerCase(); 277 278 Set <String > containerAuthorities = authorityService.getContainingAuthorities(null, userName, true); 280 for (String containerAuthority : containerAuthorities) 281 { 282 authorityService.removeAuthority(containerAuthority, authorityName); 283 } 284 285 permissionServiceSPI.deletePermissions(authorityName); 287 } 288 289 public Set <NodeRef> getAllPeople() 290 { 291 SearchParameters sp = new SearchParameters(); 292 sp.setLanguage(SearchService.LANGUAGE_LUCENE); 293 sp.setQuery("TYPE:\"" + ContentModel.TYPE_PERSON + "\""); 294 sp.addStore(storeRef); 295 sp.excludeDataInTheCurrentTransaction(false); 296 297 LinkedHashSet <NodeRef> nodes = new LinkedHashSet <NodeRef>(); 298 ResultSet rs = null; 299 300 try 301 { 302 rs = searchService.query(sp); 303 304 305 for (ResultSetRow row : rs) 306 { 307 308 NodeRef nodeRef = row.getNodeRef(); 309 if (nodeService.exists(nodeRef)) 310 { 311 nodes.add(nodeRef); 312 } 313 } 314 } 315 finally 316 { 317 if (rs != null) 318 { 319 rs.close(); 320 } 321 } 322 return nodes; 323 } 324 325 public void setCreateMissingPeople(boolean createMissingPeople) 326 { 327 this.createMissingPeople = createMissingPeople; 328 } 329 330 public void setNamespacePrefixResolver(NamespacePrefixResolver namespacePrefixResolver) 331 { 332 this.namespacePrefixResolver = namespacePrefixResolver; 333 } 334 335 public void setAuthorityService(AuthorityService authorityService) 336 { 337 this.authorityService = authorityService; 338 } 339 340 public void setPermissionServiceSPI(PermissionServiceSPI permissionServiceSPI) 341 { 342 this.permissionServiceSPI = permissionServiceSPI; 343 } 344 345 public void setNodeService(NodeService nodeService) 346 { 347 this.nodeService = nodeService; 348 } 349 350 public void setSearchService(SearchService searchService) 351 { 352 this.searchService = searchService; 353 } 354 355 public void setStoreUrl(String storeUrl) 356 { 357 this.storeRef = new StoreRef(storeUrl); 358 } 359 360 public void setCompanyHomePath(String companyHomePath) 361 { 362 this.companyHomePath = companyHomePath; 363 } 364 365 public synchronized NodeRef getCompanyHome() 366 { 367 if (companyHomeNodeRef == null) 368 { 369 List <NodeRef> refs = searchService.selectNodes(nodeService.getRootNode(storeRef), companyHomePath, null, 370 namespacePrefixResolver, false); 371 if (refs.size() != 1) 372 { 373 throw new IllegalStateException ("Invalid company home path: found : " + refs.size()); 374 } 375 companyHomeNodeRef = refs.get(0); 376 } 377 return companyHomeNodeRef; 378 } 379 380 382 } 383 | Popular Tags |