plugin-visibility.js
14.7 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"use strict";
/// <reference path="../../js/knockout.d.ts" />
/// <reference path="../../js/jquery.d.ts" />
/// <reference path="../../js/jqueryui.d.ts" />
/// <reference path="../../js/lodash-3.10.d.ts" />
/// <reference path="../../modules/actor-selector/actor-selector.ts" />
/// <reference path="../../ajax-wrapper/ajax-action-wrapper.d.ts" />
let amePluginVisibility;
class AmePluginVisibilityModule {
constructor(scriptData) {
const _ = AmePluginVisibilityModule._;
this.actorSelector = new AmeActorSelector(AmeActors, scriptData.isProVersion);
//Wrap the selected actor in a computed observable so that it can be used with Knockout.
let _selectedActor = ko.observable(this.actorSelector.selectedActor);
this.selectedActor = ko.computed({
read: function () {
return _selectedActor();
},
write: (newActor) => {
this.actorSelector.setSelectedActor(newActor);
}
});
this.actorSelector.onChange((newSelectedActor) => {
_selectedActor(newSelectedActor);
});
//Re-select the previously selected actor, or select "All" (null) by default.
this.selectedActor(scriptData.selectedActor);
this.canRoleManagePlugins = scriptData.canManagePlugins;
this.isMultisite = scriptData.isMultisite;
this.grantAccessByDefault = {};
_.forEach(this.actorSelector.getVisibleActors(), (actor) => {
this.grantAccessByDefault[actor.id] = ko.observable(_.get(scriptData.settings.grantAccessByDefault, actor.id, this.canManagePlugins(actor)));
});
this.plugins = _.map(scriptData.installedPlugins, (plugin) => {
return new AmePlugin(plugin, _.get(scriptData.settings.plugins, plugin.fileName, {}), this);
});
//Normally, the plugin list is sorted by the (real) plugin name. Re-sort taking custom names into account.
this.plugins.sort(function (a, b) {
return a.name().localeCompare(b.name());
});
this.privilegedActors = [];
const currentUser = this.actorSelector.getCurrentUserActor();
if (currentUser) {
this.privilegedActors.push(currentUser);
}
if (this.isMultisite) {
this.privilegedActors.push(AmeActors.getSuperAdmin());
}
this.areNewPluginsVisible = ko.computed({
read: () => {
const selectedActor = this.selectedActor();
if (selectedActor !== null) {
let canSeePluginsByDefault = this.getGrantAccessByDefault(selectedActor);
return canSeePluginsByDefault();
}
return _.every(this.actorSelector.getVisibleActors(), (actor) => {
//Only consider roles than can manage plugins.
if (!this.canManagePlugins(actor)) {
return true;
}
let canSeePluginsByDefault = this.getGrantAccessByDefault(actor.getId());
return canSeePluginsByDefault();
});
},
write: (isChecked) => {
const selectedActor = this.selectedActor();
if (selectedActor !== null) {
let canSeePluginsByDefault = this.getGrantAccessByDefault(selectedActor);
canSeePluginsByDefault(isChecked);
return;
}
//Update everyone except the current user and Super Admin.
_.forEach(this.actorSelector.getVisibleActors(), (actor) => {
let isAllowed = this.getGrantAccessByDefault(actor.getId());
if (!this.canManagePlugins(actor)) {
isAllowed(false);
}
else if (_.includes(this.privilegedActors, actor)) {
isAllowed(true);
}
else {
isAllowed(isChecked);
}
});
}
});
this.areAllPluginsChecked = ko.computed({
read: () => {
return _.every(this.plugins, (plugin) => {
return this.isPluginVisible(plugin);
}) && this.areNewPluginsVisible();
},
write: (isChecked) => {
this.areNewPluginsVisible(isChecked);
_.forEach(this.plugins, (plugin) => {
this.setPluginVisibility(plugin, isChecked);
});
}
});
//This observable will be populated when saving changes.
this.settingsData = ko.observable('');
}
isPluginVisible(plugin) {
let actorId = this.selectedActor();
if (actorId === null) {
return plugin.isVisibleByDefault();
}
else {
let canSeePluginsByDefault = this.getGrantAccessByDefault(actorId), isVisible = plugin.getGrantObservable(actorId, plugin.isVisibleByDefault() && canSeePluginsByDefault());
return isVisible();
}
}
setPluginVisibility(plugin, isVisible) {
const selectedActor = this.selectedActor();
if (selectedActor === null) {
plugin.isVisibleByDefault(isVisible);
//Show/hide from everyone except the current user and Super Admin.
//However, don't enable plugins for roles that can't access the "Plugins" page in the first place.
const _ = AmePluginVisibilityModule._;
_.forEach(this.actorSelector.getVisibleActors(), (actor) => {
let allowAccess = plugin.getGrantObservable(actor.id, isVisible);
if (!this.canManagePlugins(actor)) {
allowAccess(false);
}
else if (_.includes(this.privilegedActors, actor)) {
allowAccess(true);
}
else {
allowAccess(isVisible);
}
});
}
else {
//Show/hide from the selected role or user.
let allowAccess = plugin.getGrantObservable(selectedActor, isVisible);
allowAccess(isVisible);
}
}
canManagePlugins(actor) {
const _ = AmePluginVisibilityModule._;
if ((actor instanceof AmeRole) && _.has(this.canRoleManagePlugins, actor.name)) {
return this.canRoleManagePlugins[actor.name];
}
if (actor instanceof AmeSuperAdmin) {
return true;
}
if (actor instanceof AmeUser) {
//Can any of the user's roles manage plugins?
let result = false;
_.forEach(actor.roles, (roleId) => {
if (_.get(this.canRoleManagePlugins, roleId, false)) {
result = true;
return false;
}
});
return (result || (!!AmeActors.hasCap(actor.id, 'activate_plugins')));
}
return false;
}
getGrantAccessByDefault(actorId) {
if (!this.grantAccessByDefault.hasOwnProperty(actorId)) {
this.grantAccessByDefault[actorId] = ko.observable(this.canManagePlugins(AmeActors.getActor(actorId)));
}
return this.grantAccessByDefault[actorId];
}
getSettings() {
const _ = AmePluginVisibilityModule._;
let result = {};
result.grantAccessByDefault = _.mapValues(this.grantAccessByDefault, (allow) => {
return allow();
});
result.plugins = {};
_.forEach(this.plugins, (plugin) => {
result.plugins[plugin.fileName] = {
isVisibleByDefault: plugin.isVisibleByDefault(),
grantAccess: _.mapValues(plugin.grantAccess, (allow) => {
return allow();
})
};
//Filter out grants that match the default settings.
const grantAccess = result.plugins[plugin.fileName].grantAccess;
if (typeof grantAccess !== 'undefined') {
result.plugins[plugin.fileName].grantAccess = _.pick(grantAccess, (allowed, actorId) => {
if (typeof actorId === 'undefined') {
return false;
}
const defaultState = this.getGrantAccessByDefault(actorId)() && plugin.isVisibleByDefault();
return (allowed !== defaultState);
});
}
//Don't store the "grantAccess" map if it's empty.
if (_.isEmpty(result.plugins[plugin.fileName].grantAccess)) {
delete result.plugins[plugin.fileName].grantAccess;
}
//All plugins are visible by default, so it's not necessary to store this flag if it's TRUE.
if (result.plugins[plugin.fileName].isVisibleByDefault) {
delete result.plugins[plugin.fileName].isVisibleByDefault;
}
for (let i = 0; i < AmePlugin.editablePropertyNames.length; i++) {
let key = AmePlugin.editablePropertyNames[i], upperKey = key.substring(0, 1).toUpperCase() + key.substring(1), value = plugin.customProperties[key]();
if (value !== '') {
result.plugins[plugin.fileName]['custom' + upperKey] = value;
}
}
});
return result;
}
//noinspection JSUnusedGlobalSymbols Used in KO template.
saveChanges() {
const settings = this.getSettings();
//Remove settings associated with roles and users that no longer exist or are not visible.
const _ = AmePluginVisibilityModule._, visibleActorIds = _.pluck(this.actorSelector.getVisibleActors(), 'id');
_.forEach(settings.plugins, (plugin) => {
if (plugin.grantAccess) {
plugin.grantAccess = _.pick(plugin.grantAccess, visibleActorIds);
}
});
//Remove plugins that don't have any custom settings.
settings.plugins = _.pick(settings.plugins, (value) => {
return !_.isEmpty(value);
});
//Populate form field(s).
this.settingsData(JSON.stringify(settings));
return true;
}
}
AmePluginVisibilityModule._ = wsAmeLodash;
class AmePlugin {
constructor(details, settings, module) {
this.defaultProperties = {};
this.customProperties = {};
this.editableProperties = {};
const _ = AmePluginVisibilityModule._;
for (let i = 0; i < AmePlugin.editablePropertyNames.length; i++) {
let key = AmePlugin.editablePropertyNames[i], upperKey = key.substring(0, 1).toUpperCase() + key.substring(1);
this.defaultProperties[key] = ko.observable(_.get(details, key, ''));
this.customProperties[key] = ko.observable(_.get(settings, 'custom' + upperKey, ''));
this.editableProperties[key] = ko.observable(this.defaultProperties[key]());
}
this.name = ko.computed(() => {
let value = this.customProperties['name']();
if (value === '') {
value = this.defaultProperties['name']();
}
return AmePlugin.stripAllTags(value);
});
this.description = ko.computed(() => {
let value = this.customProperties['description']();
if (value === '') {
value = this.defaultProperties['description']();
}
return AmePlugin.stripAllTags(value);
});
this.fileName = details.fileName;
this.isActive = details.isActive;
this.isBeingEdited = ko.observable(false);
this.isVisibleByDefault = ko.observable(_.get(settings, 'isVisibleByDefault', true));
const emptyGrant = {};
this.grantAccess = _.mapValues(_.get(settings, 'grantAccess', emptyGrant), (hasAccess) => {
return ko.observable(hasAccess);
});
this.isChecked = ko.computed({
read: () => {
return module.isPluginVisible(this);
},
write: (isVisible) => {
return module.setPluginVisibility(this, isVisible);
}
});
}
getGrantObservable(actorId, defaultValue = true) {
if (!this.grantAccess.hasOwnProperty(actorId)) {
this.grantAccess[actorId] = ko.observable(defaultValue);
}
return this.grantAccess[actorId];
}
//noinspection JSUnusedGlobalSymbols Used in KO template.
openInlineEditor() {
for (let i = 0; i < AmePlugin.editablePropertyNames.length; i++) {
let key = AmePlugin.editablePropertyNames[i], customValue = this.customProperties[key]();
this.editableProperties[key](customValue === '' ? this.defaultProperties[key]() : customValue);
}
this.isBeingEdited(true);
}
//noinspection JSUnusedGlobalSymbols Used in KO template.
cancelEdit() {
this.isBeingEdited(false);
}
//noinspection JSUnusedGlobalSymbols Used in KO template.
confirmEdit() {
for (let i = 0; i < AmePlugin.editablePropertyNames.length; i++) {
let key = AmePlugin.editablePropertyNames[i], customValue = this.editableProperties[key]();
if (customValue === this.defaultProperties[key]()) {
customValue = '';
}
this.customProperties[key](customValue);
}
this.isBeingEdited(false);
}
//noinspection JSUnusedGlobalSymbols Used in KO template.
resetNameAndDescription() {
for (let i = 0; i < AmePlugin.editablePropertyNames.length; i++) {
let key = AmePlugin.editablePropertyNames[i];
this.customProperties[key]('');
}
this.isBeingEdited(false);
}
static stripAllTags(input) {
//Based on: http://phpjs.org/functions/strip_tags/
const tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi, commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, '');
}
}
AmePlugin.editablePropertyNames = ['name', 'description', 'author', 'siteUrl', 'version'];
jQuery(function ($) {
amePluginVisibility = new AmePluginVisibilityModule(wsPluginVisibilityData);
ko.applyBindings(amePluginVisibility, document.getElementById('ame-plugin-visibility-editor'));
//Permanently dismiss the usage hint via AJAX.
jQuery('#ame-pv-usage-notice').on('click', '.notice-dismiss', function () {
AjawV1.getAction('ws_ame_dismiss_pv_usage_notice').request();
});
});
//# sourceMappingURL=plugin-visibility.js.map