Add model support for managed groups (#1544)

* chore: 🤖 streamline managed groups getter on role
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2022-12-22 14:09:56 -08:00
1 parent 392da009d0
commit f2c0458fb5
3 files changed
+134 -40

No files matched your search

@@ -8,60 +8,32 @@ import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeRolesRolePrincipalsRoute extends Route {
// =services
@service intl;
@service resourceFilterStore;
// =methods
/**
* Returns users and groups associated with this role.
* Returns users, groups and managed-groups associated with this role.
* @param {object} params
* @return {Promise{RoleModel}}
* @return {Promise{role, principals}}
*/
async model() {
const role = this.modelFor('scopes.scope.roles.role');
// Fetch user and group principals.
const users = await role.users;
const groups = await role.groups;
const managedGroups = await role.managedGroups;
// Merge polymorphic principals.
const principals = [...users, ...groups, ...managedGroups];
// Gather user and group IDs as separate arrays, since these
// will be queried in separate API queries.
const userIDs = role.principals
.filter(({ type }) => type === 'user')
.map(({ principal_id }) => principal_id);
const groupIDs = role.principals
.filter(({ type }) => type === 'group')
.map(({ principal_id }) => principal_id);
// Query for users.
const users = userIDs?.length
? (
await this.resourceFilterStore.queryBy(
'user',
{ id: userIDs },
{ scope_id: 'global', recursive: true }
)
).map((model) => model)
: [];
// Query for groups.
const groups = groupIDs?.length
? (
await this.resourceFilterStore.queryBy(
'group',
{ id: groupIDs },
{ scope_id: 'global', recursive: true }
)
).map((model) => model)
: [];
return {
role,
principals: users.concat(groups),
};
return { role, principals };
}
// =actions
/**
* Remove a principal from the current role and redirect to principals index.
* @param {UserModel, GroupModel} principal
* @param {UserModel, GroupModel, ManagedGroupModel} principal
*/
@action
@loading