KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > person > PersonServiceImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.security.person;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.LinkedHashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
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 JavaDoc SYSTEM_FOLDER = "/sys:system";
48
49     public static final String JavaDoc PEOPLE_FOLDER = SYSTEM_FOLDER + "/sys:people";
50
51     // IOC
52

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 JavaDoc companyHomePath;
70
71     private NodeRef companyHomeNodeRef;
72
73     private static Set JavaDoc<QName> mutableProperties;
74
75     static
76     {
77         Set JavaDoc<QName> props = new HashSet JavaDoc<QName>();
78         props.add(ContentModel.PROP_HOMEFOLDER);
79         props.add(ContentModel.PROP_FIRSTNAME);
80         // Middle Name
81
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 JavaDoc caseSensitiveUserName)
103     {
104         String JavaDoc 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 JavaDoc caseSensitiveUserName)
125     {
126         return getPersonOrNull(caseSensitiveUserName) != null;
127     }
128
129     public NodeRef getPersonOrNull(String JavaDoc caseSensitiveUserName)
130     {
131         String JavaDoc 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 JavaDoc realUserName = DefaultTypeConverter.INSTANCE.convert(
151                             String JavaDoc.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 JavaDoc<QName> getMutableProperties()
178     {
179         return mutableProperties;
180     }
181
182     public void setPersonProperties(String JavaDoc caseSensitiveUserName, Map JavaDoc<QName, Serializable JavaDoc> properties)
183     {
184         String JavaDoc 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 JavaDoc userName)
210     {
211         HashMap JavaDoc<QName, Serializable JavaDoc> properties = getDefaultProperties(userName);
212         return createPerson(properties);
213     }
214
215     private HashMap JavaDoc<QName, Serializable JavaDoc> getDefaultProperties(String JavaDoc userName)
216     {
217         HashMap JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
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 JavaDoc<QName, Serializable JavaDoc> properties)
233     {
234         String JavaDoc caseSensitiveUserName = DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, properties
235                 .get(ContentModel.PROP_USERNAME));
236         String JavaDoc 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 JavaDoc<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 JavaDoc userName)
266     {
267         NodeRef personNodeRef = getPersonOrNull(userName);
268         
269         // delete the person
270
if (personNodeRef != null)
271         {
272             nodeService.deleteNode(personNodeRef);
273         }
274
275         // translate username based on user name case sensitivity
276
String JavaDoc authorityName = userNamesAreCaseSensitive ? userName : userName.toLowerCase();
277         
278         // remove user from any containing authorities
279
Set JavaDoc<String JavaDoc> containerAuthorities = authorityService.getContainingAuthorities(null, userName, true);
280         for (String JavaDoc containerAuthority : containerAuthorities)
281         {
282             authorityService.removeAuthority(containerAuthority, authorityName);
283         }
284         
285         // remove any user permissions
286
permissionServiceSPI.deletePermissions(authorityName);
287     }
288
289     public Set JavaDoc<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 JavaDoc<NodeRef> nodes = new LinkedHashSet JavaDoc<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 JavaDoc storeUrl)
356     {
357         this.storeRef = new StoreRef(storeUrl);
358     }
359
360     public void setCompanyHomePath(String JavaDoc companyHomePath)
361     {
362         this.companyHomePath = companyHomePath;
363     }
364
365     public synchronized NodeRef getCompanyHome()
366     {
367         if (companyHomeNodeRef == null)
368         {
369             List JavaDoc<NodeRef> refs = searchService.selectNodes(nodeService.getRootNode(storeRef), companyHomePath, null,
370                     namespacePrefixResolver, false);
371             if (refs.size() != 1)
372             {
373                 throw new IllegalStateException JavaDoc("Invalid company home path: found : " + refs.size());
374             }
375             companyHomeNodeRef = refs.get(0);
376         }
377         return companyHomeNodeRef;
378     }
379
380     // IOC Setters
381

382 }
383
Popular Tags