2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-22 14:18:51 -04:00
|
|
|
import GeneratedRoleModel from '../generated/models/role';
|
2022-08-31 09:55:59 -07:00
|
|
|
import { attr } from '@ember-data/model';
|
2022-11-07 15:17:23 -08:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-06-22 14:18:51 -04:00
|
|
|
|
2020-08-07 15:42:40 -04:00
|
|
|
export default class RoleModel extends GeneratedRoleModel {
|
2022-11-07 15:17:23 -08:00
|
|
|
// =services
|
|
|
|
|
|
|
|
|
|
@service store;
|
|
|
|
|
|
2020-08-07 15:42:40 -04:00
|
|
|
// =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.
|
|
|
|
|
*/
|
2024-04-05 16:35:23 -07:00
|
|
|
@attr('array', {
|
2020-09-12 11:40:21 -04:00
|
|
|
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
|
|
|
/**
|
2020-10-30 15:33:08 -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
|
|
|
*/
|
2022-08-31 09:55:59 -07:00
|
|
|
@attr({ readOnly: true, emptyArrayIfMissing: true }) grant_strings;
|
2020-08-07 15:42:40 -04:00
|
|
|
|
2022-06-20 12:28:13 -04:00
|
|
|
/**
|
|
|
|
|
* Convenience for looking up the grant scope, if loaded.
|
|
|
|
|
*/
|
|
|
|
|
get grantScope() {
|
2022-07-28 17:49:51 -07:00
|
|
|
return this.grant_scope_id
|
|
|
|
|
? this.store.peekRecord('scope', this.grant_scope_id)
|
|
|
|
|
: null;
|
2022-06-20 12:28:13 -04:00
|
|
|
}
|
|
|
|
|
|
2022-12-22 14:09:56 -08:00
|
|
|
/**
|
|
|
|
|
* A list of IDs for principals of type `user`.
|
|
|
|
|
* @type {string[]}
|
|
|
|
|
*/
|
|
|
|
|
get userIDs() {
|
|
|
|
|
return this.principals
|
|
|
|
|
.filter(({ type }) => type === 'user')
|
2024-04-05 16:35:23 -07:00
|
|
|
.map(({ id }) => id);
|
2022-12-22 14:09:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A list of IDs for principals of type `group`.
|
|
|
|
|
* @type {string[]}
|
|
|
|
|
*/
|
|
|
|
|
get groupIDs() {
|
|
|
|
|
return this.principals
|
|
|
|
|
.filter(({ type }) => type === 'group')
|
2024-04-05 16:35:23 -07:00
|
|
|
.map(({ id }) => id);
|
2022-12-22 14:09:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A list of IDs for principals of type `managed group`.
|
|
|
|
|
* @type {string[]}
|
|
|
|
|
*/
|
|
|
|
|
get managedGroupIDs() {
|
|
|
|
|
return this.principals
|
|
|
|
|
.filter(({ type }) => type === 'managed group')
|
2024-04-05 16:35:23 -07:00
|
|
|
.map(({ id }) => id);
|
2022-12-22 14:09:56 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-07 15:42:40 -04:00
|
|
|
// =methods
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-30 15:33:08 -04:00
|
|
|
* 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.
|
2020-10-30 15:33:08 -04:00
|
|
|
* @param {[string]} grantStrings
|
2020-08-07 15:42:40 -04:00
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
2020-10-30 15:33:08 -04:00
|
|
|
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
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 16:26:02 -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: {} }) {
|
2020-08-28 16:26:02 -04:00
|
|
|
const defaultAdapterOptions = {
|
|
|
|
|
method: 'add-principals',
|
2020-09-16 14:29:02 -04:00
|
|
|
principalIDs,
|
2020-08-28 16:26:02 -04:00
|
|
|
};
|
|
|
|
|
// There is no "deep merge" in ES.
|
|
|
|
|
return this.save({
|
|
|
|
|
...options,
|
|
|
|
|
adapterOptions: {
|
|
|
|
|
...defaultAdapterOptions,
|
2020-09-16 14:29:02 -04:00
|
|
|
...options.adapterOptions,
|
|
|
|
|
},
|
2020-08-28 16:26:02 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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: {} }) {
|
2020-08-28 16:26:02 -04:00
|
|
|
const defaultAdapterOptions = {
|
|
|
|
|
method: 'remove-principals',
|
2020-09-16 14:29:02 -04:00
|
|
|
principalIDs,
|
2020-08-28 16:26:02 -04:00
|
|
|
};
|
|
|
|
|
// There is no "deep merge" in ES.
|
|
|
|
|
return this.save({
|
|
|
|
|
...options,
|
|
|
|
|
adapterOptions: {
|
|
|
|
|
...defaultAdapterOptions,
|
2020-09-16 14:29:02 -04:00
|
|
|
...options.adapterOptions,
|
|
|
|
|
},
|
2020-08-28 16:26:02 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
}
|