2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-22 21:39:38 -04:00
|
|
|
import factory from '../generated/factories/role';
|
2023-01-09 08:48:49 -08:00
|
|
|
import { trait } from 'miragejs';
|
2021-08-12 10:35:33 -07:00
|
|
|
import permissions from '../helpers/permissions';
|
2022-06-01 14:02:18 -04:00
|
|
|
import generateId from '../helpers/id';
|
2020-06-22 21:39:38 -04:00
|
|
|
|
2020-08-07 18:56:21 -04:00
|
|
|
export default factory.extend({
|
2021-08-12 10:35:33 -07:00
|
|
|
authorized_actions: () =>
|
2021-08-12 11:48:21 -07:00
|
|
|
permissions.authorizedActionsFor('role') || [
|
2021-08-12 10:35:33 -07:00
|
|
|
'no-op',
|
|
|
|
|
'read',
|
|
|
|
|
'update',
|
|
|
|
|
'delete',
|
2021-09-30 15:21:45 -04:00
|
|
|
'set-grants',
|
2021-09-30 16:04:02 -04:00
|
|
|
'add-principals',
|
|
|
|
|
'remove-principals',
|
2021-08-12 10:35:33 -07:00
|
|
|
],
|
2020-08-07 18:56:21 -04:00
|
|
|
// eslint-disable-next-line ember/avoid-leaking-state-in-ember-objects
|
2020-09-12 12:03:52 -04:00
|
|
|
grant_strings: () => [
|
2020-08-07 18:56:21 -04:00
|
|
|
'id=*;action=*',
|
2021-03-04 15:47:38 -05:00
|
|
|
'id=*;type=host-catalog;actions=create,read',
|
2020-08-07 18:56:21 -04:00
|
|
|
],
|
|
|
|
|
|
2022-06-01 14:02:18 -04:00
|
|
|
id: () => generateId('r_'),
|
|
|
|
|
|
2020-08-12 13:41:23 -04:00
|
|
|
/**
|
2020-08-28 17:34:14 -04:00
|
|
|
* Adds principals to the role.
|
2020-08-12 13:41:23 -04:00
|
|
|
*/
|
|
|
|
|
withPrincipals: trait({
|
|
|
|
|
afterCreate(role, server) {
|
2020-09-10 11:03:44 -04:00
|
|
|
const { scope } = role;
|
2022-12-22 10:23:49 -08:00
|
|
|
const { id: scopeId } = scope;
|
2020-09-10 11:03:44 -04:00
|
|
|
const users = server.createList('user', 2, { scope });
|
|
|
|
|
const groups = server.createList('group', 2, { scope });
|
2022-12-22 10:23:49 -08:00
|
|
|
const managedGroups = server.schema.managedGroups.where({
|
|
|
|
|
scopeId,
|
|
|
|
|
}).models;
|
|
|
|
|
role.update({ users, groups, managedGroups });
|
2021-03-04 15:47:38 -05:00
|
|
|
},
|
2020-11-02 16:55:03 -05:00
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the grant scope ID to match the scope ID.
|
|
|
|
|
*/
|
|
|
|
|
afterCreate(role) {
|
2021-03-04 15:47:38 -05:00
|
|
|
const {
|
|
|
|
|
scope: { id },
|
|
|
|
|
} = role;
|
2020-11-02 16:55:03 -05:00
|
|
|
role.update({ grant_scope_id: id });
|
2021-03-04 15:47:38 -05:00
|
|
|
},
|
2020-08-07 18:56:21 -04:00
|
|
|
});
|