actionRelationships.js
11.5 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* ADOBE CONFIDENTIAL
*
* Copyright 2015 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
/* globals org, com, javax, resource, request, use */
use(['helper.js'], function(helper) {
'use strict';
/**
* Check if the user has the permission to perform the the desired action.
*
* @param {AccessControlManager} acm The access control manager to use
* @param {Resource} resource The resource the action is to be performed on
* @param {String} privilege The action to perform
*
* @return {Boolean} Returns `true` if the action is permitted, `false` otherwise
*/
var hasPermission = function(acm, resource, privilege) {
if (acm !== null) {
var p = acm.privilegeFromName(privilege);
return acm.hasPrivileges(resource.getPath(), [p]);
}
return false;
};
/**
* Get the list of permitted actions on the specified resource.
*
* @param {Resource} resource The resource the actions are to be checked on
*
* @return {String[]} Returns an array with the permitted actions
*/
var getActionRels = function(resource) {
var unwrappedResource, unwrappedResourceProps,
isLegacyChannelAssignmentWithoutRT, isChannelAssignmentAssignedToSchedule;
var actionRels = {};
var resourceResolver = resource.resourceResolver;
var acm = resourceResolver.adaptTo(javax.jcr.Session).getAccessControlManager();
actionRels[helper.constants.screens.ACT_COPY] = true;
var page = resource.adaptTo(com.day.cq.wcm.api.Page);
if (!page) {
page = resource.parent.adaptTo(com.day.cq.wcm.api.Page);
}
// Stop here if we cannot even read the resource
if (!hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_READ)) {
return actionRels;
}
if (page) {
var device;
// Displays
if (helper.isResourceType(page.getContentResource(), [helper.constants.screens.RT_DISPLAY])) {
actionRels[helper.constants.screens.ACT_VIEW_FIRMWARE] = true;
actionRels[helper.constants.screens.ACT_VIEW_DASHBOARD_DISPLAY] = true;
if (hasPermission(acm, page.getContentResource().getChild(helper.constants.screens.NN_CHANNELS), javax.jcr.security.Privilege.JCR_ADD_CHILD_NODES)) {
actionRels[helper.constants.screens.ACT_LINK_CHANNEL] = true;
}
}
// Device Configs
if (helper.isResourceType(page.getContentResource(), [helper.constants.screens.RT_DEVICE_CONFIG])) {
actionRels[helper.constants.screens.ACT_VIEW_FIRMWARE] = true;
actionRels[helper.constants.screens.ACT_VIEW_DASHBOARD_DEVICE] = true;
var deviceManager = resource.getResourceResolver().adaptTo(com.adobe.cq.screens.device.DeviceManager);
var deviceConfig = resource.adaptTo(com.adobe.cq.screens.device.DeviceConfig);
var deviceId = deviceConfig.getAssignedDeviceId();
device = deviceId ? deviceManager.getDevice(deviceId) : null;
var isAssigned = !!(deviceId && device);
if (isAssigned) {
actionRels['screens-dcc-actions-Device-unassign-activator'] = true;
actionRels['screens-dcc-actions-Device-pushconfig-activator'] = true;
actionRels['screens-dcc-actions-Device-configupdate-activator'] = true;
}
else {
actionRels['screens-dcc-actions-Device-assign-activator'] = true;
actionRels[helper.constants.screens.ACT_DELETE] = true;
}
}
// Device Folder
if (helper.isResourceType(page.getContentResource(), [helper.constants.screens.RT_DEVICE_FOLDER])) {
actionRels[helper.constants.screens.ACT_VIEW_DEVICE_MANAGER] = true;
}
// Channels
if (helper.isResourceType(page.getContentResource(), [helper.constants.screens.RT_CHANNEL])) {
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_ADD_CHILD_NODES)) {
actionRels[helper.constants.screens.ACT_EDIT] = true;
}
actionRels[helper.constants.screens.ACT_VIEW_CONTENT] = true;
actionRels[helper.constants.screens.ACT_VIEW_DASHBOARD_CHANNEL] = true;
}
// Locations
if (helper.isResourceType(page.getContentResource(), [helper.constants.screens.RT_LOCATION])) {
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_ADD_CHILD_NODES)) {
actionRels[helper.constants.screens.ACT_CREATE] = true;
}
}
// Schedules
if (helper.isResourceType(page.getContentResource(), [helper.constants.screens.RT_SCHEDULE])) {
actionRels[helper.constants.screens.ACT_VIEW_DASHBOARD_SCHEDULE] = true;
}
// Folders
if (helper.isResourceType(page.getContentResource(), [helper.constants.screens.RT_APPLICATION_FOLDER,
helper.constants.screens.RT_LOCATION_FOLDER, helper.constants.screens.RT_CHANNEL_FOLDER,
helper.constants.screens.RT_SCHEDULE_FOLDER, helper.constants.screens.RT_PROJECT])) {
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_ADD_CHILD_NODES)) {
actionRels[helper.constants.screens.ACT_CREATE] = true;
}
}
// Applications
if (helper.isResourceType(page.getContentResource(), [helper.constants.screens.RT_APPLICATION])
&& page.depth > 4) {
if (page.getProperties().get('isEditable')
&& hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_ADD_CHILD_NODES)) {
actionRels[helper.constants.screens.ACT_EDIT] = true;
}
actionRels[helper.constants.screens.ACT_VIEW_CONTENT] = true;
}
// More specifc rules
if (!helper.isResourceType(page.getContentResource(), [helper.constants.screens.RT_APPLICATION])) {
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_MODIFY_PROPERTIES)) {
actionRels[helper.constants.screens.ACT_VIEW_PROPERTIES] = true;
}
}
if (!helper.isResourceType(page.getContentResource(), [helper.constants.screens.RT_APPLICATION,
helper.constants.screens.RT_DEVICE_CONFIG])) {
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_REMOVE_NODE)) {
actionRels[helper.constants.screens.ACT_MOVE] = true;
actionRels[helper.constants.screens.ACT_DELETE] = true;
}
}
}
else {
device = resource.adaptTo(com.adobe.cq.screens.device.Device);
unwrappedResource = resourceResolver.getResource(resource.path);
unwrappedResourceProps = unwrappedResource.adaptTo(org.apache.sling.api.resource.ValueMap);
isChannelAssignmentAssignedToSchedule = request.requestPathInfo.selectorString == 'channelAssignmentAssignedToSchedule'; // eslint-disable-line
isLegacyChannelAssignmentWithoutRT = unwrappedResourceProps.get('role') != null && unwrappedResourceProps.get('path') != null; // eslint-disable-line
if (device) {
// Device specific actions
if (!device.getDeviceConfig()) {
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_MODIFY_PROPERTIES)) {
actionRels[helper.constants.screens.ACT_ASSIGN_DEVICE] = true;
}
}
actionRels[helper.constants.screens.ACT_VIEW_DASHBOARD_DEVICE] = true;
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_REMOVE_NODE)) {
actionRels['screens-dcc-actions-Device-delete-activator'] = true;
}
}
else if (helper.isResourceType(unwrappedResource, [helper.constants.screens.RT_SCHEDULE_ASSIGNMENT_ABSOLUTE])) {
actionRels['screens-dcc-actions-scheduleDashboard-activator'] = true;
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_REMOVE_NODE)) {
actionRels[helper.constants.screens.ACT_DELETE] = true;
}
}
else if (helper.isResourceType(unwrappedResource, [
helper.constants.screens.RT_CHANNEL_ASSIGNMENT_ABSOLUTE,
helper.constants.screens.RT_CHANNEL_ASSIGNMENT_DYNAMIC
]) || isLegacyChannelAssignmentWithoutRT) {
actionRels['screens-dcc-actions-channelDashboard-activator'] = true;
// Try to resolve the assignment to an actual channel
var assignmentSvc = resourceResolver.adaptTo(com.adobe.cq.screens.assignment.AssignmentService);
var contextResource = resourceResolver.resolve(request.requestPathInfo.suffix);
var channelResource = assignmentSvc ? assignmentSvc.resolveEntity(resource, contextResource) : null;
// A ChannelAssignment that is assigned to a Schedule shall only be editable
if (channelResource && hasPermission(acm, channelResource, javax.jcr.security.Privilege.JCR_ADD_CHILD_NODES)) {
actionRels[helper.constants.screens.ACT_EDIT] = true;
}
if (!isChannelAssignmentAssignedToSchedule) {
if (hasPermission(acm, resource.getParent(), javax.jcr.security.Privilege.JCR_REMOVE_CHILD_NODES)) {
actionRels[helper.constants.screens.ACT_DELETE] = true;
}
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_MODIFY_PROPERTIES)) {
actionRels['screens-dcc-actions-ChannelLink-edit-activator'] = true;
}
}
}
else {
// Folders
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_MODIFY_PROPERTIES)) {
actionRels[helper.constants.screens.ACT_VIEW_FOLDERPROPERTIES] = true;
}
if (hasPermission(acm, resource, javax.jcr.security.Privilege.JCR_REMOVE_NODE)) {
actionRels[helper.constants.screens.ACT_DELETE] = true;
}
}
}
return actionRels;
};
return function(res) {
return {
getActionRels: getActionRels,
array: function() {
return getActionRels(res || resource) || [];
},
list: function() {
return Object.keys(getActionRels(res || resource)).join(' ') || '';
}
};
};
});