* refactor: 💡 convert brokered cred IDs array to simple attribute * fix: 🐛 missing test assertion * fix: 🐛 simplify computed * refactor: 💡 convert host sources array to simple attribute (#1260) * refactor: 💡 convert host source IDs array to simple attribute * fix: 🐛 host source tests * Refactor role fragment arrays (#1262) * refactor: 💡 convert grant strings to regular attribute * refactor: 💡 convert principals array to simple attribute * Update ui/admin/app/routes/scopes/scope/roles/role.js Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com> Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com> Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com> * fix: 🐛 remove unused import Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>
108 lines
2.8 KiB
JavaScript
108 lines
2.8 KiB
JavaScript
import GeneratedRoleModel from '../generated/models/role';
|
|
import { attr } from '@ember-data/model';
|
|
|
|
export default class RoleModel extends GeneratedRoleModel {
|
|
// =attributes
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
@attr('principal-array', {
|
|
readOnly: true,
|
|
emptyArrayIfMissing: true,
|
|
})
|
|
principals;
|
|
|
|
/**
|
|
* Grant strings are read-only. But grants can be persisted via a dedicated
|
|
* call to `saveGrantStrings(grants)`.
|
|
*/
|
|
@attr({ readOnly: true, emptyArrayIfMissing: true }) grant_strings;
|
|
|
|
/**
|
|
* Convenience for looking up the grant scope, if loaded.
|
|
*/
|
|
get grantScope() {
|
|
return this.grant_scope_id
|
|
? this.store.peekRecord('scope', this.grant_scope_id)
|
|
: null;
|
|
}
|
|
|
|
// =methods
|
|
|
|
/**
|
|
* Saves grant strings on the role via the `set-grants` method.
|
|
* See serializer and adapter for more information.
|
|
* @param {[string]} grantStrings
|
|
* @return {Promise}
|
|
*/
|
|
saveGrantStrings(grantStrings) {
|
|
return this.save({
|
|
adapterOptions: {
|
|
method: 'set-grants',
|
|
grantStrings,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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}
|
|
*/
|
|
addPrincipals(principalIDs, options = { adapterOptions: {} }) {
|
|
const defaultAdapterOptions = {
|
|
method: 'add-principals',
|
|
principalIDs,
|
|
};
|
|
// There is no "deep merge" in ES.
|
|
return this.save({
|
|
...options,
|
|
adapterOptions: {
|
|
...defaultAdapterOptions,
|
|
...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}
|
|
*/
|
|
removePrincipals(principalIDs, options = { adapterOptions: {} }) {
|
|
const defaultAdapterOptions = {
|
|
method: 'remove-principals',
|
|
principalIDs,
|
|
};
|
|
// There is no "deep merge" in ES.
|
|
return this.save({
|
|
...options,
|
|
adapterOptions: {
|
|
...defaultAdapterOptions,
|
|
...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);
|
|
}
|
|
}
|