Files
threshold-ui/addons/api/addon/models/role.js
T

98 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-06-22 14:18:51 -04:00
import GeneratedRoleModel from '../generated/models/role';
import { fragmentArray, array } from 'ember-data-model-fragments/attributes';
2020-06-22 14:18:51 -04:00
2020-08-07 15:42:40 -04:00
export default class RoleModel extends GeneratedRoleModel {
// =attributes
2020-08-12 13:41:23 -04:00
/**
* Principals are users and groups assigned to a role. They are represented
* as references to User and Group instances. Since Ember Data relationships
* are wanting, we do not model these as a polymorphic relationship as might
* see obvious. Instead, the application layer is expected to load referenced
* users and groups as needed.
*/
@fragmentArray('fragment-principal', {
readOnly: true,
2020-09-16 14:29:02 -04:00
emptyArrayIfMissing: true,
})
principals;
2020-08-12 13:41:23 -04:00
2020-08-07 15:42:40 -04:00
/**
* Grant strings are read-only. But grants can be persisted via a dedicated
* call to `saveGrantStrings(grants)`.
2020-08-07 15:42:40 -04:00
*/
@array('string', { readOnly: true }) grant_strings;
2020-08-07 15:42:40 -04:00
// =methods
/**
* Saves grant strings on the role via the `set-grants` method.
2020-08-07 15:42:40 -04:00
* See serializer and adapter for more information.
* @param {[string]} grantStrings
2020-08-07 15:42:40 -04:00
* @return {Promise}
*/
saveGrantStrings(grantStrings) {
2020-08-07 15:42:40 -04:00
return this.save({
adapterOptions: {
method: 'set-grants',
2021-03-04 15:47:38 -05:00
grantStrings,
2020-09-16 14:29:02 -04:00
},
2020-08-07 15:42:40 -04:00
});
}
/**
* Adds principals via the `add-principals` method.
* See serializer and adapter for more information.
* @param {[string]} principalIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
2020-09-16 14:29:02 -04:00
addPrincipals(principalIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'add-principals',
2020-09-16 14:29:02 -04:00
principalIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
2020-09-16 14:29:02 -04:00
...options.adapterOptions,
},
});
}
/**
* Delete principals via the `remove-principals` method.
* See serializer and adapter for more information.
* @param {[string]} principalIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
2020-09-16 14:29:02 -04:00
removePrincipals(principalIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'remove-principals',
2020-09-16 14:29:02 -04:00
principalIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
2020-09-16 14:29:02 -04:00
...options.adapterOptions,
},
});
}
/**
* Delete a single principal via the `remove-principals` method.
* @param {number} principalIDs
* @param {object} options
* @return {Promise}
*/
removePrincipal(principalID, options) {
return this.removePrincipals([principalID], options);
}
2020-08-07 15:42:40 -04:00
}