///
///
declare let wsAmeActorData: any;
declare var wsAmeLodash: _.LoDashStatic;
declare let AmeActors: AmeActorManager;
type Falsy = false | null | '' | undefined | 0;
type Truthy = true | string | 1;
interface CapabilityMap {
[capabilityName: string] : boolean;
}
interface IAmeActor {
getId(): string;
getDisplayName(): string;
}
interface IAmeUser extends IAmeActor {
userLogin: string;
isSuperAdmin: boolean;
}
abstract class AmeBaseActor implements IAmeActor {
public id: string;
public displayName: string = '[Error: No displayName set]';
public capabilities: CapabilityMap;
public metaCapabilities: CapabilityMap;
groupActors: string[] = [];
protected constructor(id: string, displayName: string, capabilities: CapabilityMap, metaCapabilities: CapabilityMap = {}) {
this.id = id;
this.displayName = displayName;
this.capabilities = capabilities;
this.metaCapabilities = metaCapabilities;
}
/**
* Get the capability setting directly from this actor, ignoring capabilities
* granted by roles, the Super Admin flag, or the grantedCapabilities feature.
*
* Returns NULL for capabilities that are neither explicitly granted nor denied.
*
* @param {string} capability
* @returns {boolean|null}
*/
hasOwnCap(capability: string): boolean {
if (this.capabilities.hasOwnProperty(capability)) {
return this.capabilities[capability];
}
if (this.metaCapabilities.hasOwnProperty(capability)) {
return this.metaCapabilities[capability];
}
return null;
}
static getActorSpecificity(actorId: string) {
let actorType = actorId.substring(0, actorId.indexOf(':')),
specificity = 0;
switch (actorType) {
case 'role':
specificity = 1;
break;
case 'special':
specificity = 2;
break;
case 'user':
specificity = 10;
break;
default:
specificity = 0;
}
return specificity;
}
toString(): string {
return this.displayName + ' [' + this.id + ']';
}
getId(): string {
return this.id;
}
getDisplayName(): string {
return this.displayName;
}
}
class AmeRole extends AmeBaseActor {
name: string;
constructor(roleId: string, displayName: string, capabilities: CapabilityMap, metaCapabilities: CapabilityMap = {}) {
super('role:' + roleId, displayName, capabilities, metaCapabilities);
this.name = roleId;
}
hasOwnCap(capability: string): boolean {
//In WordPress, a role name is also a capability name. Users that have the role "foo" always
//have the "foo" capability. It's debatable whether the role itself actually has that capability
//(WP_Role says no), but it's convenient to treat it that way.
if (capability === this.name) {
return true;
}
return super.hasOwnCap(capability);
}
}
interface AmeUserPropertyMap {
user_login: string;
display_name: string;
capabilities: CapabilityMap;
meta_capabilities: CapabilityMap;
roles : string[];
is_super_admin: boolean;
id?: number;
avatar_html?: string;
}
class AmeUser extends AmeBaseActor implements IAmeUser {
userLogin: string;
userId: number = 0;
roles: string[];
isSuperAdmin: boolean = false;
groupActors: string[];
avatarHTML: string = '';
constructor(
userLogin: string,
displayName: string,
capabilities: CapabilityMap,
roles: string[],
isSuperAdmin: boolean = false,
userId?: number,
metaCapabilities: CapabilityMap = {}
) {
super('user:' + userLogin, displayName, capabilities, metaCapabilities);
this.userLogin = userLogin;
this.roles = roles;
this.isSuperAdmin = isSuperAdmin;
this.userId = userId || 0;
if (this.isSuperAdmin) {
this.groupActors.push(AmeSuperAdmin.permanentActorId);
}
for (let i = 0; i < this.roles.length; i++) {
this.groupActors.push('role:' + this.roles[i]);
}
}
static createFromProperties(properties: AmeUserPropertyMap): AmeUser {
let user = new AmeUser(
properties.user_login,
properties.display_name,
properties.capabilities,
properties.roles,
properties.is_super_admin,
properties.hasOwnProperty('id') ? properties.id : null,
properties.meta_capabilities
);
if (properties.avatar_html) {
user.avatarHTML = properties.avatar_html;
}
return user;
}
}
class AmeSuperAdmin extends AmeBaseActor {
static permanentActorId = 'special:super_admin';
constructor() {
super(AmeSuperAdmin.permanentActorId, 'Super Admin', {});
}
hasOwnCap(capability: string): boolean {
//The Super Admin has all possible capabilities except the special "do_not_allow" flag.
return (capability !== 'do_not_allow');
}
}
interface AmeGrantedCapabilityMap {
[actorId: string]: {
[capability: string] : any
}
}
interface AmeCapabilitySuggestion {
role: AmeRole;
capability: string;
}
class AmeActorManager implements AmeActorManagerInterface {
private static _ = wsAmeLodash;
private roles: {[roleId: string] : AmeRole} = {};
private users: {[userLogin: string] : AmeUser} = {};
private grantedCapabilities: AmeGrantedCapabilityMap = {};
public readonly isMultisite: boolean = false;
private readonly superAdmin: AmeSuperAdmin;
private exclusiveSuperAdminCapabilities = {};
private tagMetaCaps = {};
private suspectedMetaCaps: CapabilityMap;
private suggestedCapabilities: AmeCapabilitySuggestion[] = [];
constructor(roles, users, isMultisite: Truthy | Falsy = false, suspectedMetaCaps: CapabilityMap = {}) {
this.isMultisite = !!isMultisite;
AmeActorManager._.forEach(roles, (roleDetails, id) => {
const role = new AmeRole(
id,
roleDetails.name,
roleDetails.capabilities,
AmeActorManager._.get(roleDetails, 'meta_capabilities', {})
);
this.roles[role.name] = role;
});
AmeActorManager._.forEach(users, (userDetails: AmeUserPropertyMap) => {
const user = AmeUser.createFromProperties(userDetails);
this.users[user.userLogin] = user;
});
if (this.isMultisite) {
this.superAdmin = new AmeSuperAdmin();
}
this.suspectedMetaCaps = suspectedMetaCaps;
const exclusiveCaps: string[] = [
'update_core', 'update_plugins', 'delete_plugins', 'install_plugins', 'upload_plugins', 'update_themes',
'delete_themes', 'install_themes', 'upload_themes', 'update_core', 'edit_css', 'unfiltered_html',
'edit_files', 'edit_plugins', 'edit_themes', 'delete_user', 'delete_users'
];
for (let i = 0; i < exclusiveCaps.length; i++) {
this.exclusiveSuperAdminCapabilities[exclusiveCaps[i]] = true;
}
const tagMetaCaps = [
'manage_post_tags', 'edit_categories', 'edit_post_tags', 'delete_categories',
'delete_post_tags'
];
for (let i = 0; i < tagMetaCaps.length; i++) {
this.tagMetaCaps[tagMetaCaps[i]] = true;
}
}
// noinspection JSUnusedGlobalSymbols
actorCanAccess(
actorId: string,
grantAccess: {[actorId: string] : boolean},
defaultCapability: string = null
): boolean {
if (grantAccess.hasOwnProperty(actorId)) {
return grantAccess[actorId];
}
if (defaultCapability !== null) {
return this.hasCap(actorId, defaultCapability, grantAccess);
}
return true;
}
getActor(actorId): AmeBaseActor {
if (actorId === AmeSuperAdmin.permanentActorId) {
return this.superAdmin;
}
const separator = actorId.indexOf(':'),
actorType = actorId.substring(0, separator),
actorKey = actorId.substring(separator + 1);
if (actorType === 'role') {
return this.roles.hasOwnProperty(actorKey) ? this.roles[actorKey] : null;
} else if (actorType === 'user') {
return this.users.hasOwnProperty(actorKey) ? this.users[actorKey] : null;
}
throw {
name: 'InvalidActorException',
message: "There is no actor with that ID, or the ID is invalid.",
value: actorId
};
}
actorExists(actorId: string): boolean {
try {
return (this.getActor(actorId) !== null);
} catch (exception) {
if (exception.hasOwnProperty('name') && (exception.name === 'InvalidActorException')) {
return false;
} else {
throw exception;
}
}
}
hasCap(actorId: string, capability, context?: {[actor: string] : any}): boolean {
context = context || {};
return this.actorHasCap(actorId, capability, [context, this.grantedCapabilities]);
}
hasCapByDefault(actorId, capability) {
return this.actorHasCap(actorId, capability);
}
private actorHasCap(actorId: string, capability: string, contextList?: Array