1 17 package org.alfresco.repo.admin.patch.impl; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Properties ; 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 46 public class GuestUserPatch extends AbstractPatch 47 { 48 private static final String MSG_SUCCESS = "patch.guestUser.result"; 49 50 private static final String COMPANY_HOME_CHILD_NAME = "spaces.company_home.childname"; 51 52 private static final String GUEST_HOME_CHILD_NAME = "spaces.guest_home.childname"; 53 54 private static final String GUEST_HOME_NAME = "spaces.guest_home.name"; 55 56 private static final String 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 guestId = "guest"; 71 72 private MessageSource messageSource; 73 74 public GuestUserPatch() 75 { 76 super(); 77 } 78 79 public void setGuestId(String 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 120 protected String applyInternal() throws Exception 121 { 122 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 configuration = importerBootstrap.getConfiguration(); 132 133 String 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 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 147 NodeRef companyHomeRef = setCompanyHomeSpacePermissions(storeRootNodeRef, companyHomeChildName); 148 149 151 NodeRef guestHomeRef = addGuestHomeSpace(storeRootNodeRef, configuration, companyHomeChildName, 152 guestHomeChildName, companyHomeRef); 153 154 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 <QName, Serializable > properties = new HashMap <QName, Serializable >(); 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 configuration, String companyHomeChildName, 185 String guestHomeChildName, NodeRef companyHomeRef) 186 { 187 List <NodeRef> nodeRefs = searchService.selectNodes(companyHomeRef, guestHomeChildName, null, namespaceService, 188 false); 189 if (nodeRefs.size() == 0) 190 { 191 193 String 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 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 <QName, Serializable > properties = new HashMap <QName, Serializable >(); 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 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 companyHomeChildName) 245 { 246 List <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 |