KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > admin > patch > impl > GuestUserPatch


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.admin.patch.impl;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.alfresco.i18n.I18NUtil;
25 import org.alfresco.model.ContentModel;
26 import org.alfresco.repo.admin.patch.AbstractPatch;
27 import org.alfresco.repo.importer.ImporterBootstrap;
28 import org.alfresco.service.cmr.admin.PatchException;
29 import org.alfresco.service.cmr.repository.ChildAssociationRef;
30 import org.alfresco.service.cmr.repository.NodeRef;
31 import org.alfresco.service.cmr.repository.NodeService;
32 import org.alfresco.service.cmr.repository.StoreRef;
33 import org.alfresco.service.cmr.search.SearchService;
34 import org.alfresco.service.cmr.security.PermissionService;
35 import org.alfresco.service.cmr.security.PersonService;
36 import org.alfresco.service.namespace.NamespaceService;
37 import org.alfresco.service.namespace.QName;
38 import org.springframework.context.MessageSource;
39
40 /**
41  * Ensures that the <b>guest</b> user homespace exists.<br/> A guest user homespace is now created during bootstrap. It is required for guest user access, but in older databases
42  * will not exist.
43  *
44  * @author Andy Hind
45  */

46 public class GuestUserPatch extends AbstractPatch
47 {
48     private static final String JavaDoc MSG_SUCCESS = "patch.guestUser.result";
49
50     private static final String JavaDoc COMPANY_HOME_CHILD_NAME = "spaces.company_home.childname";
51
52     private static final String JavaDoc GUEST_HOME_CHILD_NAME = "spaces.guest_home.childname";
53
54     private static final String JavaDoc GUEST_HOME_NAME = "spaces.guest_home.name";
55
56     private static final String JavaDoc GUEST_HOME_DESCRIPTION = "spaces.guest_home.description";
57
58     private PersonService personService;
59
60     private NodeService nodeService;
61
62     private SearchService searchService;
63
64     private PermissionService permissionService;
65
66     private ImporterBootstrap importerBootstrap;
67
68     private NamespaceService namespaceService;
69
70     private String JavaDoc guestId = "guest";
71
72     private MessageSource messageSource;
73
74     public GuestUserPatch()
75     {
76         super();
77     }
78
79     public void setGuestId(String JavaDoc guestId)
80     {
81         this.guestId = guestId;
82     }
83
84     public void setImporterBootstrap(ImporterBootstrap importerBootstrap)
85     {
86         this.importerBootstrap = importerBootstrap;
87     }
88
89     public void setNamespaceService(NamespaceService namespaceService)
90     {
91         this.namespaceService = namespaceService;
92     }
93
94     public void setNodeService(NodeService nodeService)
95     {
96         this.nodeService = nodeService;
97     }
98
99     public void setPermissionService(PermissionService permissionService)
100     {
101         this.permissionService = permissionService;
102     }
103
104     public void setPersonService(PersonService personService)
105     {
106         this.personService = personService;
107     }
108
109     public void setSearchService(SearchService searchService)
110     {
111         this.searchService = searchService;
112     }
113
114     public void setMessageSource(MessageSource messageSource)
115     {
116         this.messageSource = messageSource;
117     }
118
119     @Override JavaDoc
120     protected String JavaDoc applyInternal() throws Exception JavaDoc
121     {
122         // Config
123

124         StoreRef storeRef = importerBootstrap.getStoreRef();
125         if (storeRef == null)
126         {
127             throw new PatchException("Bootstrap store has not been set");
128         }
129
130         NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);
131         Properties JavaDoc configuration = importerBootstrap.getConfiguration();
132
133         String JavaDoc companyHomeChildName = configuration.getProperty(COMPANY_HOME_CHILD_NAME);
134         if (companyHomeChildName == null || companyHomeChildName.length() == 0)
135         {
136             throw new PatchException("Bootstrap property '" + COMPANY_HOME_CHILD_NAME + "' is not present");
137         }
138
139         String JavaDoc guestHomeChildName = configuration.getProperty(GUEST_HOME_CHILD_NAME);
140         if (guestHomeChildName == null || guestHomeChildName.length() == 0)
141         {
142             throw new PatchException("Bootstrap property '" + GUEST_HOME_CHILD_NAME + "' is not present");
143         }
144
145         // Set company home space permissions
146

147         NodeRef companyHomeRef = setCompanyHomeSpacePermissions(storeRootNodeRef, companyHomeChildName);
148
149         // Add the guest home space
150

151         NodeRef guestHomeRef = addGuestHomeSpace(storeRootNodeRef, configuration, companyHomeChildName,
152                 guestHomeChildName, companyHomeRef);
153
154         // Add the guest user and fix read access to the created person
155

156         addGuestUser(guestHomeRef);
157
158         return I18NUtil.getMessage(MSG_SUCCESS);
159     }
160
161     private void addGuestUser(NodeRef guestHomeRef)
162     {
163         if (!personService.personExists(guestId))
164         {
165             HashMap JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
166             properties.put(ContentModel.PROP_USERNAME, guestId);
167             properties.put(ContentModel.PROP_HOMEFOLDER, guestHomeRef);
168             properties.put(ContentModel.PROP_FIRSTNAME, "Guest");
169             properties.put(ContentModel.PROP_LASTNAME, "");
170             properties.put(ContentModel.PROP_EMAIL, "");
171             properties.put(ContentModel.PROP_ORGID, "");
172
173             personService.createPerson(properties);
174
175         }
176
177         NodeRef personRef = personService.getPerson(guestId);
178
179         permissionService.setInheritParentPermissions(personRef, false);
180         permissionService.setPermission(personRef, guestId, PermissionService.CONSUMER, true);
181
182     }
183
184     private NodeRef addGuestHomeSpace(NodeRef storeRootNodeRef, Properties JavaDoc configuration, String JavaDoc companyHomeChildName,
185             String JavaDoc guestHomeChildName, NodeRef companyHomeRef)
186     {
187         List JavaDoc<NodeRef> nodeRefs = searchService.selectNodes(companyHomeRef, guestHomeChildName, null, namespaceService,
188                 false);
189         if (nodeRefs.size() == 0)
190         {
191             // create
192

193             String JavaDoc guestHomeName = messageSource.getMessage(GUEST_HOME_NAME, null, I18NUtil.getLocale());
194             if (guestHomeName == null || guestHomeName.length() == 0)
195             {
196                 throw new PatchException("Bootstrap property '" + GUEST_HOME_NAME + "' is not present");
197             }
198
199             String JavaDoc guestHomeDescription = messageSource.getMessage(GUEST_HOME_DESCRIPTION, null, I18NUtil.getLocale());
200             if (guestHomeDescription == null || guestHomeDescription.length() == 0)
201             {
202                 throw new PatchException("Bootstrap property '" + GUEST_HOME_DESCRIPTION + "' is not present");
203             }
204
205             HashMap JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
206             properties.put(ContentModel.PROP_NAME, guestHomeName);
207             properties.put(ContentModel.PROP_TITLE, guestHomeName);
208             properties.put(ContentModel.PROP_DESCRIPTION, guestHomeDescription);
209             properties.put(ContentModel.PROP_ICON, "space-icon-default");
210
211             ChildAssociationRef childAssocRef = nodeService.createNode(companyHomeRef, ContentModel.ASSOC_CONTAINS,
212                     QName.createQName(guestHomeChildName, namespaceService), ContentModel.TYPE_FOLDER, properties);
213
214             NodeRef nodeRef = childAssocRef.getChildRef();
215             // add the required aspects
216
nodeService.addAspect(nodeRef, ContentModel.ASPECT_UIFACETS, null);
217
218             setGuestHomePermissions(nodeRef);
219
220             return nodeRef;
221
222         }
223         else if (nodeRefs.size() == 1)
224         {
225             NodeRef nodeRef = nodeRefs.get(0);
226             setGuestHomePermissions(nodeRef);
227             return nodeRef;
228         }
229         else
230         {
231             throw new PatchException("XPath returned too many results: \n"
232                     + " root: " + storeRootNodeRef + "\n" + " xpath: " + companyHomeChildName + "\n"
233                     + " results: " + nodeRefs);
234         }
235     }
236
237     private void setGuestHomePermissions(NodeRef nodeRef)
238     {
239         permissionService.setInheritParentPermissions(nodeRef, false);
240         permissionService.setPermission(nodeRef, PermissionService.ALL_AUTHORITIES, PermissionService.CONSUMER, true);
241         permissionService.setPermission(nodeRef, guestId, PermissionService.CONSUMER, true);
242     }
243
244     private NodeRef setCompanyHomeSpacePermissions(NodeRef storeRootNodeRef, String JavaDoc companyHomeChildName)
245     {
246         List JavaDoc<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, companyHomeChildName, null,
247                 namespaceService, false);
248         if (nodeRefs.size() == 0)
249         {
250             throw new PatchException("XPath didn't return any results: \n"
251                     + " root: " + storeRootNodeRef + "\n" + " xpath: " + companyHomeChildName);
252         }
253         else if (nodeRefs.size() > 1)
254         {
255             throw new PatchException("XPath returned too many results: \n"
256                     + " root: " + storeRootNodeRef + "\n" + " xpath: " + companyHomeChildName + "\n"
257                     + " results: " + nodeRefs);
258         }
259         NodeRef companyHomeRef = nodeRefs.get(0);
260
261         permissionService.setInheritParentPermissions(companyHomeRef, false);
262         permissionService.setPermission(companyHomeRef, PermissionService.ALL_AUTHORITIES, PermissionService.CONSUMER,
263                 true);
264         return companyHomeRef;
265     }
266
267 }
268
Popular Tags