2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-01 09:39:41 -06:00
|
|
|
*/
|
|
|
|
|
|
2021-09-30 15:21:45 -04:00
|
|
|
import ModelAbility from './model';
|
2025-03-12 08:29:54 -07:00
|
|
|
import { service } from '@ember/service';
|
2022-12-20 16:31:39 -05:00
|
|
|
|
|
|
|
|
class InvalidRolePrincipalTypeError extends Error {
|
|
|
|
|
name = 'InvalidRolePrincipalTypeError';
|
|
|
|
|
}
|
2021-09-30 15:21:45 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provides abilities for roles.
|
|
|
|
|
*/
|
|
|
|
|
export default class RoleAbility extends ModelAbility {
|
2022-12-20 16:31:39 -05:00
|
|
|
// =services
|
|
|
|
|
|
|
|
|
|
@service can;
|
|
|
|
|
|
2021-09-30 15:21:45 -04:00
|
|
|
// =permissions
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
*/
|
|
|
|
|
get canSetGrants() {
|
|
|
|
|
return this.hasAuthorizedAction('set-grants');
|
|
|
|
|
}
|
2021-09-30 16:04:02 -04:00
|
|
|
|
2024-06-13 12:45:20 -05:00
|
|
|
/**
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
*/
|
|
|
|
|
get canSetGrantScopes() {
|
|
|
|
|
return this.hasAuthorizedAction('set-grant-scopes');
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 16:04:02 -04:00
|
|
|
/**
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
*/
|
|
|
|
|
get canAddPrincipals() {
|
|
|
|
|
return this.hasAuthorizedAction('add-principals');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
*/
|
|
|
|
|
get canRemovePrincipals() {
|
|
|
|
|
return this.hasAuthorizedAction('remove-principals');
|
|
|
|
|
}
|
2022-12-20 16:31:39 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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';
|
2025-10-13 10:53:21 -04:00
|
|
|
if (!typeIsValid) {
|
2022-12-20 16:31:39 -05:00
|
|
|
throw new InvalidRolePrincipalTypeError(
|
2023-11-22 14:07:41 -05:00
|
|
|
`Expected a role principal of type 'user', 'group', or 'managed-group'. Got '${type}'.`,
|
2022-12-20 16:31:39 -05:00
|
|
|
);
|
2025-10-13 10:53:21 -04:00
|
|
|
}
|
2022-12-20 16:31:39 -05:00
|
|
|
return this.can.can(`read ${type}`, this.model);
|
|
|
|
|
}
|
2021-09-30 15:21:45 -04:00
|
|
|
}
|