* chore: 🤖 reapply previous ember-can upgrade from #3093 * chore: 🤖 add missing ember-inflector dependency * chore: 🤖 move @ember/string to be a dependency in desktop * chore: 🤖 re-run pnpm install to resolve lockfile dupe key error * chore: 🤖 remove unused abilities service injections Co-Authored-By: Lisbet Alvarez <107949262+lisbet-alvarez@users.noreply.github.com> --------- Co-authored-by: Lisbet Alvarez <107949262+lisbet-alvarez@users.noreply.github.com>
70 lines
1.5 KiB
JavaScript
70 lines
1.5 KiB
JavaScript
/**
|
|
* Copyright IBM Corp. 2021, 2026
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
import ModelAbility from './model';
|
|
import { service } from '@ember/service';
|
|
|
|
class InvalidRolePrincipalTypeError extends Error {
|
|
name = 'InvalidRolePrincipalTypeError';
|
|
}
|
|
|
|
/**
|
|
* Provides abilities for roles.
|
|
*/
|
|
export default class RoleAbility extends ModelAbility {
|
|
// =services
|
|
|
|
@service abilities;
|
|
|
|
// =permissions
|
|
|
|
/**
|
|
* @type {boolean}
|
|
*/
|
|
get canSetGrants() {
|
|
return this.hasAuthorizedAction('set-grants');
|
|
}
|
|
|
|
/**
|
|
* @type {boolean}
|
|
*/
|
|
get canSetGrantScopes() {
|
|
return this.hasAuthorizedAction('set-grant-scopes');
|
|
}
|
|
|
|
/**
|
|
* @type {boolean}
|
|
*/
|
|
get canAddPrincipals() {
|
|
return this.hasAuthorizedAction('add-principals');
|
|
}
|
|
|
|
/**
|
|
* @type {boolean}
|
|
*/
|
|
get canRemovePrincipals() {
|
|
return this.hasAuthorizedAction('remove-principals');
|
|
}
|
|
|
|
/**
|
|
* Returns the ability check for a principal model instance.
|
|
* Valid role principal models are: User, Group, and ManagedGroup.
|
|
* Throws an error if the principal type is invalid.
|
|
* @type {boolean}
|
|
* @throws {InvalidRolePrincipalTypeError}
|
|
*/
|
|
get canReadPrincipal() {
|
|
const type = this.model.constructor.modelName;
|
|
const typeIsValid =
|
|
type === 'user' || type === 'group' || type === 'managed-group';
|
|
if (!typeIsValid) {
|
|
throw new InvalidRolePrincipalTypeError(
|
|
`Expected a role principal of type 'user', 'group', or 'managed-group'. Got '${type}'.`,
|
|
);
|
|
}
|
|
return this.abilities.can(`read ${type}`, this.model);
|
|
}
|
|
}
|