1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.site.templates;
35
36 import info.magnolia.context.MgnlContext;
37 import info.magnolia.rendering.template.TemplateAvailability;
38 import info.magnolia.rendering.template.TemplateDefinition;
39 import info.magnolia.repository.RepositoryConstants;
40
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.LinkedHashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 import javax.jcr.Node;
48 import javax.jcr.RepositoryException;
49 import javax.jcr.Workspace;
50
51 import org.apache.commons.collections4.CollectionUtils;
52 import org.apache.commons.lang3.BooleanUtils;
53 import org.apache.commons.lang3.StringUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public class ConfiguredSiteTemplateAvailability implements TemplateAvailability {
72
73 private static final Logger log = LoggerFactory.getLogger(ConfiguredSiteTemplateAvailability.class);
74
75 private Map<String, TemplateConfig> templates = new LinkedHashMap<>();
76
77 private boolean enableAll;
78
79 private List<String> enableAllWithRenderType = new ArrayList<>();
80
81 public Map<String, TemplateConfig> getTemplates() {
82 return this.templates;
83 }
84
85
86
87
88
89
90
91 public void setTemplates(Map<String, TemplateConfig> templates) {
92 this.templates.clear();
93 for (TemplateConfig templateConfig : templates.values()) {
94 this.templates.put(templateConfig.getId(), templateConfig);
95 }
96 }
97
98
99
100
101 public void addTemplate(TemplateConfig templateConfig) {
102 templates.put(templateConfig.getId(), templateConfig);
103 }
104
105 public boolean isEnableAll() {
106 return enableAll;
107 }
108
109 public void setEnableAll(boolean enableAll) {
110 this.enableAll = enableAll;
111 }
112
113 public List<String> getEnableAllWithRenderType() {
114 return enableAllWithRenderType;
115 }
116
117 public void setEnableAllWithRenderType(List<String> enableAllWithRenderType) {
118 this.enableAllWithRenderType = enableAllWithRenderType;
119 }
120
121 @Override
122 public boolean isAvailable(Node node, TemplateDefinition templateDefinition) {
123 if (node == null || templateDefinition == null) {
124 return false;
125 }
126
127
128 if (BooleanUtils.isFalse(templateDefinition.getVisible())) {
129 return false;
130 }
131
132 try {
133 final Workspace workspace = node.getSession().getWorkspace();
134 if (!RepositoryConstants.WEBSITE.equals(workspace.getName())) {
135 return false;
136 }
137 } catch (RepositoryException e) {
138 log.error("Not able to access the Node's session or workspace.", e);
139 return false;
140 }
141
142 if ((isEnableAll() || getEnableAllWithRenderType().contains(templateDefinition.getRenderType())) && StringUtils.substringAfter(templateDefinition.getId(), ":").startsWith("pages/")) {
143 return true;
144 }
145
146 if (!getTemplates().containsKey(templateDefinition.getId())) {
147 return false;
148 }
149
150
151 final TemplateConfig templateConfig = getTemplates().get(templateDefinition.getId());
152 final Collection<String> roles = templateConfig.getRoles();
153 if (!roles.isEmpty()) {
154 if (!CollectionUtils.containsAny(MgnlContext.getUser().getAllRoles(), roles)) {
155 return false;
156 }
157 }
158
159 return true;
160 }
161 }