Skip to content

Commit e4e7379

Browse files
committed
Fix bug where the documentLibrary folder was not being created with the appropriate permissions for private and moderated sites. It was leaving ACL inheritance turned on, which is a problem for moderated sites because moderated site folders have the everyone group as a consumer. If you inherit that, then all users can browse the document library folder even if they haven't been granted access to the site. Closes #7. Also modified a test and turned on debug when testing.
1 parent 8436137 commit e4e7379

File tree

5 files changed

+60
-39
lines changed

5 files changed

+60
-39
lines changed

share-site-space-templates-repo/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.ecmarchitect</groupId>
66
<artifactId>share-site-space-templates-repo</artifactId>
7-
<version>1.0.0</version>
7+
<version>1.1.1</version>
88
<name>share-site-space-templates-repo AMP project</name>
99
<packaging>amp</packaging>
1010
<description>Manages the lifecycle of the share-site-space-templates-repo AMP (Alfresco Module Package)</description>

share-site-space-templates-repo/src/main/amp/config/alfresco/module/share-site-space-templates-repo/context/service-context.xml

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<property name="searchService">
3131
<ref bean="SearchService" />
3232
</property>
33+
<property name="siteService">
34+
<ref bean="SiteService" />
35+
</property>
3336
</bean>
3437

3538
</beans>

share-site-space-templates-repo/src/main/java/com/ecmarchitect/share/behavior/ShareDocumentLibraryFromTemplate.java

+49-32
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.ecmarchitect.share.behavior;
2-
import java.io.Serializable;
3-
import java.util.HashMap;
4-
import java.util.Map;
2+
import static org.alfresco.repo.site.SiteModel.PROP_SITE_PRESET;
3+
import static org.alfresco.repo.site.SiteModel.TYPE_SITE;
4+
5+
import java.util.List;
56

67
import org.alfresco.model.ContentModel;
78
import org.alfresco.repo.node.NodeServicePolicies;
89
import org.alfresco.repo.policy.Behaviour;
910
import org.alfresco.repo.policy.Behaviour.NotificationFrequency;
1011
import org.alfresco.repo.policy.JavaBehaviour;
1112
import org.alfresco.repo.policy.PolicyComponent;
12-
import org.alfresco.repo.site.SiteModel;
1313
import org.alfresco.service.cmr.model.FileExistsException;
1414
import org.alfresco.service.cmr.model.FileFolderService;
1515
import org.alfresco.service.cmr.model.FileNotFoundException;
@@ -19,21 +19,20 @@
1919
import org.alfresco.service.cmr.repository.StoreRef;
2020
import org.alfresco.service.cmr.search.ResultSet;
2121
import org.alfresco.service.cmr.search.SearchService;
22+
import org.alfresco.service.cmr.site.SiteService;
2223
import org.alfresco.service.namespace.NamespaceService;
2324
import org.alfresco.service.namespace.QName;
2425
import org.apache.log4j.Logger;
2526

26-
import static org.alfresco.repo.site.SiteModel.ASPECT_SITE_CONTAINER;
27-
import static org.alfresco.repo.site.SiteModel.PROP_SITE_PRESET;
28-
import static org.alfresco.repo.site.SiteModel.TYPE_SITE;
29-
3027
public class ShareDocumentLibraryFromTemplate implements NodeServicePolicies.OnCreateNodePolicy {
3128

29+
private static final String DOCUMENT_LIBRARY = "documentLibrary";
3230
// Dependencies
3331
private NodeService nodeService;
3432
private PolicyComponent policyComponent;
3533
private FileFolderService fileFolderService;
3634
private SearchService searchService;
35+
private SiteService siteService;
3736

3837
// Behaviors
3938
private Behaviour onCreateNode;
@@ -42,7 +41,7 @@ public class ShareDocumentLibraryFromTemplate implements NodeServicePolicies.OnC
4241

4342
public void init() {
4443
if (logger.isDebugEnabled()) logger.debug("Initializing rateable behaviors");
45-
44+
4645
// Create behaviors
4746
this.onCreateNode = new JavaBehaviour(this, "onCreateNode", NotificationFrequency.TRANSACTION_COMMIT);
4847

@@ -102,32 +101,42 @@ public void onCreateNode(ChildAssociationRef childAssocRef) {
102101
if (spaceTemplate == null) {
103102
logger.debug("Space template doesn't exist");
104103
return;
104+
} else {
105+
logger.debug("Found space template: " + nodeService.getProperty(spaceTemplate, ContentModel.PROP_NAME));
105106
}
106-
107-
//otherwise, create the documentLibrary folder as a child of this site folder
108-
//using the space template found above
109-
NodeRef documentLibrary;
110-
try {
111-
documentLibrary = fileFolderService.copy(spaceTemplate, siteFolder, "documentLibrary").getNodeRef();
112-
113-
logger.debug("Successfully created the document library node from a template");
114-
115-
//add the site container aspect, set the descriptions, set the component ID
116-
Map<QName, Serializable> props = new HashMap<QName, Serializable>();
117-
props.put(ContentModel.PROP_DESCRIPTION, "Document Library");
118-
props.put(SiteModel.PROP_COMPONENT_ID, "documentLibrary");
119-
nodeService.addAspect(documentLibrary, ASPECT_SITE_CONTAINER, props);
120-
121-
} catch (FileExistsException e) {
122-
logger.debug("The document library node already exists. Each child needs to be copied.");
123-
// TODO implement this piece
124-
//iterate over the children of the source space template and copy them into the target
125-
126-
} catch (FileNotFoundException e) {
127-
//can't find the space template, just bail
128-
logger.warn("Share site tried to use a space template, but the source space template could not be found.");
107+
108+
// otherwise, create the documentLibrary folder
109+
String siteId = (String) nodeService.getProperty(siteFolder, ContentModel.PROP_NAME);
110+
logger.debug("Site ID: " + siteId);
111+
112+
// use the site service to do this so that permissions get set correctly
113+
NodeRef documentLibrary = siteService.getContainer(siteId, DOCUMENT_LIBRARY);
114+
if (documentLibrary == null) {
115+
// create the document library container using the site service
116+
documentLibrary = siteService.createContainer(siteId, DOCUMENT_LIBRARY, null, null);
117+
if (documentLibrary == null) {
118+
logger.error("Document library could not be created for: " + siteId);
119+
}
129120
}
130121

122+
// now, for each child in the space template, do a copy to the documentLibrary
123+
List<ChildAssociationRef> children = nodeService.getChildAssocs(spaceTemplate);
124+
for (ChildAssociationRef childRef : children) {
125+
// we only want contains associations
126+
if (childRef.getQName().equals(ContentModel.ASSOC_CONTAINS)) {
127+
continue;
128+
}
129+
NodeRef child = childRef.getChildRef();
130+
try {
131+
fileFolderService.copy(child, documentLibrary, null);
132+
logger.debug("Successfully copied a child node from the template");
133+
} catch (FileExistsException e) {
134+
logger.debug("The child node already exists in the document library.");
135+
} catch (FileNotFoundException e) {
136+
//can't find the space template, just bail
137+
logger.warn("Share site tried to use a space template, but the source space template could not be found.");
138+
}
139+
}
131140
}
132141

133142
public NodeService getNodeService() {
@@ -165,5 +174,13 @@ public void setSearchService(SearchService searchService) {
165174
this.searchService = searchService;
166175
}
167176

177+
public SiteService getSiteService() {
178+
return siteService;
179+
}
180+
181+
public void setSiteService(SiteService siteService) {
182+
this.siteService = siteService;
183+
}
184+
168185
}
169186

share-site-space-templates-repo/src/test/java/com/ecmarchitect/test/ShareSiteSpaceTemplateTest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public void testCreateWithoutSpaceTemplate() {
5656
AuthenticationUtil.setFullyAuthenticatedUser(ADMIN_USER_NAME);
5757

5858
String siteShortName = "test-site-" + System.currentTimeMillis();
59-
59+
logger.debug("without space template: " + siteShortName);
6060
SiteInfo testSite = siteService.createSite("site-dashboard", siteShortName, "test site", "test site description", SiteVisibility.PUBLIC);
6161

6262
NodeRef documentLibrary = siteService.getContainer(testSite.getShortName(), SiteService.DOCUMENT_LIBRARY);
6363

64-
assertEquals(documentLibrary, null);
64+
assertEquals(null, documentLibrary);
6565

6666
siteService.deleteSite(siteShortName);
6767
}
@@ -93,16 +93,17 @@ public void testCreateWithSpaceTemplate() {
9393
contentProps).getChildRef();
9494

9595
String siteShortName = "test-site-" + System.currentTimeMillis();
96-
96+
logger.debug("with space template: " + siteShortName);
9797
SiteInfo testSite = siteService.createSite(presetName, siteShortName, "test site", "test site description", SiteVisibility.PUBLIC);
9898

9999
NodeRef documentLibrary = siteService.getContainer(testSite.getShortName(), SiteService.DOCUMENT_LIBRARY);
100100

101101
List<ChildAssociationRef> children = nodeService.getChildAssocs(documentLibrary);
102-
103-
assertEquals(1, children.size());
102+
int size = children.size();
104103

105104
siteService.deleteSite(siteShortName);
106105
nodeService.deleteNode(spaceTemplate);
106+
107+
assertEquals(1, size);
107108
}
108109
}

share-site-space-templates-repo/src/test/resources/log4j.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,4 @@ log4j.logger.org.apache.pdfbox.pdmodel.font.PDCIDFont=fatal
263263
# no index support
264264
log4j.logger.org.alfresco.repo.search.impl.noindex.NoIndexIndexer=fatal
265265
log4j.logger.org.alfresco.repo.search.impl.noindex.NoIndexSearchService=fatal
266-
log4j.logger.org.alfresco.demoamp.test=DEBUG
266+
log4j.logger.com.ecmarchitect=DEBUG

0 commit comments

Comments
 (0)