plugin-visibility.ts 14.4 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
/// <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: AmePluginVisibilityModule;
declare const wsPluginVisibilityData: PluginVisibilityScriptData;

interface PluginVisibilityScriptData {
	isMultisite: boolean,
	canManagePlugins: {[roleId : string] : boolean},
	selectedActor: string,
	installedPlugins: Array<PvPluginInfo>,
	settings: PluginVisibilitySettings,
	isProVersion: boolean
}

interface PluginVisibilitySettings {
	grantAccessByDefault: GrantAccessMap,
	plugins: {
		[fileName : string] : {
			isVisibleByDefault?: boolean,
			grantAccess?: GrantAccessMap,
			customName?: string,
			customDescription?: string;
			customAuthor?: string;
			customSiteUrl?: string;
			customVersion?: string;
		}
	}
}

interface GrantAccessMap {
	[actorId : string] : boolean
}

interface PvPluginInfo {
	name: string,
	description: string,
	author: string,
	version: string,
	siteUrl: string,

	fileName: string,
	isActive: boolean;
}

class AmePluginVisibilityModule {
	static _ = wsAmeLodash;

	plugins: Array<AmePlugin>;
	private readonly canRoleManagePlugins: {[roleId: string] : boolean};
	grantAccessByDefault: {[actorId: string] : KnockoutObservable<boolean>};
	private readonly isMultisite: boolean;

	actorSelector: AmeActorSelector;
	selectedActor: KnockoutComputed<string|null>;
	settingsData: KnockoutObservable<string>;

	areAllPluginsChecked: KnockoutComputed<boolean>;
	areNewPluginsVisible: KnockoutComputed<boolean>;

	/**
	 * Actors that don't lose access to a plugin when you uncheck it in the "All" view.
	 * This is a convenience feature that lets the user quickly hide a bunch of plugins from everyone else.
	 */
	private readonly privilegedActors: Array<IAmeActor>;

	constructor(scriptData: PluginVisibilityScriptData) {
		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: string) => {
				this.actorSelector.setSelectedActor(newActor);
			}
		});
		this.actorSelector.onChange((newSelectedActor: string|null) => {
			_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: AmeBaseActor) => {
			this.grantAccessByDefault[actor.id] = ko.observable<boolean>(
				_.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: AmeBaseActor) => {
					//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: AmeBaseActor) => {
					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: AmePlugin): boolean {
		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: AmePlugin, isVisible: boolean) {
		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: AmeBaseActor) => {
				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);
		}
	}

	private canManagePlugins(actor: AmeBaseActor|null): boolean {
		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;
	}

	private getGrantAccessByDefault(actorId: string): KnockoutObservable<boolean> {
		if (!this.grantAccessByDefault.hasOwnProperty(actorId)) {
			this.grantAccessByDefault[actorId] = ko.observable(this.canManagePlugins(AmeActors.getActor(actorId)));
		}
		return this.grantAccessByDefault[actorId];
	}

	private getSettings(): PluginVisibilitySettings {
		const _ = AmePluginVisibilityModule._;
		let result: PluginVisibilitySettings = <PluginVisibilitySettings>{};

		result.grantAccessByDefault = _.mapValues(this.grantAccessByDefault, (allow): boolean => {
			return allow();
		});
		result.plugins = {};
		_.forEach(this.plugins, (plugin: AmePlugin) => {
			result.plugins[plugin.fileName] = {
				isVisibleByDefault: plugin.isVisibleByDefault(),
				grantAccess: _.mapValues(plugin.grantAccess, (allow): boolean => {
					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] as Record<string,any>)['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<GrantAccessMap, GrantAccessMap>(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;
	}
}

interface AmeStringObservableMap {
	[key: string]: KnockoutObservable<string>;
}

class AmePlugin {
	name: KnockoutComputed<string>;
	fileName: string;
	description: KnockoutComputed<string>;
	isActive: boolean;

	static readonly editablePropertyNames = ['name', 'description', 'author', 'siteUrl', 'version'];

	defaultProperties: AmeStringObservableMap = {};
	customProperties: AmeStringObservableMap = {};
	editableProperties: AmeStringObservableMap = {};

	isBeingEdited: KnockoutObservable<boolean>;
	isChecked: KnockoutComputed<boolean>;

	isVisibleByDefault: KnockoutObservable<boolean>;
	grantAccess: {[actorId : string] : KnockoutObservable<boolean>};

	constructor(details: PvPluginInfo, settings: Object, module: AmePluginVisibilityModule) {
		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: { [actorId: string]: boolean } = {};
		this.grantAccess = _.mapValues(_.get(settings, 'grantAccess', emptyGrant), (hasAccess) => {
			return ko.observable<boolean>(hasAccess);
		});

		this.isChecked = ko.computed<boolean>({
			read: () => {
				return module.isPluginVisible(this);
			},
			write: (isVisible: boolean) => {
				return module.setPluginVisibility(this, isVisible);
			}
		});
	}

	getGrantObservable(actorId: string, defaultValue: boolean = true): KnockoutObservable<boolean> {
		if (!this.grantAccess.hasOwnProperty(actorId)) {
			this.grantAccess[actorId] = ko.observable<boolean>(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: string): string {
		//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, '');
	}
}

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();
	});
});