src/helpers/user/Permission.js
import PermissionModel from '../../models/user/Permission';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
import RoleService from '../../services/RoleService';
/**
* Helper class representing a Role Permission.
*
* @extends {PermissionModel}
* @extends {Helper}
*/
class Permission extends aggregate(PermissionModel, Helper) {
constructor(json = {}) {
super(json);
this.deserializeRelationshipStubs(json);
this.syncRelationships(json);
}
/** @type {String} - The type */
static get type() {
return 'permissions';
}
/** @type {AllowedAction[]} */
get allowedActions() {
return this._relationships.allowed_actions;
}
/** @type {AllowedAction[]} */
set allowedActions(allowedActions) {
/** @type {AllowedAction[]} */
this._relationships.allowed_actions = allowedActions;
}
/** @type {Object} */
get authorizable() {
return this._relationships.authorizable;
}
/** @type {Object} - Polymorphic - can only be Section, Flow Process, or Dynamic Content Type */
set authorizable(authorizable) {
if (authorizable.type === 'data_collection__flow_processes' || authorizable.type === 'dynamic_content__content_types'
|| authorizable.type === 'sections') {
/** @type {Object} - Polymorphic - can only be Section, Flow Process, or Dynamic Content Type */
this._relationships.authorizable = authorizable;
}
}
/** @type {Role} */
get role() {
return this._relationships.user_role;
}
/** @type {Role} */
set role(role) {
/** @type {Role} */
this._relationships.user_role = role;
}
/**
* Deletes a permission
* @return {Promise} - Returns a promise via ajax call.
*
* @example
* import { Permission, Client } from 'clinical6';
*
* // Removes permission from server and local storage
* const permission = new Permission({
* "id": 914,
* "type": "permissions",
* "attributes": {
* "enabled": false,
* },
* "relationships": {
* "user_role": {
* "data": {
* "id": 6,
* "type": "mobile_users"
* }
* },
* "authorizable": {
* "data": {
* "id": 177,
* "type": "sections"
* }
* }
* }
* });
* permission.delete();
*
* // No longer in storage
* Client.instance.storageUtility.has('permission', { id: 914 });
*/
delete() {
return (new RoleService()).deletePermission(this);
}
}
export default Permission;