Files
threshold-ui/addons/api/addon/serializers/role.js
T

84 lines
2.5 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
2020-08-07 15:42:40 -04:00
import ApplicationSerializer from './application';
import { get } from '@ember/object';
export default class RoleSerializer extends ApplicationSerializer {
// =methods
/**
* If `adapterOptions.serializeGrants` is true, the serialization should
* include **only grants** and the version. Normally, grants are not
2020-08-07 15:42:40 -04:00
* serialized.
* If `adapterOptions.principalIDs` is set (to an array of user and
2024-04-11 12:39:24 -04:00
* group IDs), then the payload is serialized via `serializeWithPrincipals`.
2024-06-03 15:27:04 -05:00
* If `adapterOptions.grantScopeIDs` is set (to an array of scope IDs or
* keywords), then the payload is serialized via `serializeWithGrantScopeIDs`.
2020-08-07 15:42:40 -04:00
* @override
* @param {Snapshot} snapshot
* @return {object}
*/
serialize(snapshot) {
let serialized = super.serialize(...arguments);
2024-06-03 15:27:04 -05:00
const grantStrings = get(snapshot, 'adapterOptions.grantStrings');
if (grantStrings) {
2021-03-04 15:47:38 -05:00
serialized = this.serializeWithGrantStrings(snapshot, grantStrings);
2024-06-03 15:27:04 -05:00
}
const principalIDs = snapshot?.adapterOptions?.principalIDs;
2024-06-03 15:27:04 -05:00
if (principalIDs) {
2024-04-11 12:39:24 -04:00
serialized = this.serializeWithPrincipals(snapshot, principalIDs);
2024-06-03 15:27:04 -05:00
}
const grantScopeIDs = snapshot?.adapterOptions?.grantScopeIDs;
if (grantScopeIDs) {
serialized = this.serializeWithGrantScopeIDs(snapshot, grantScopeIDs);
}
2020-08-07 15:42:40 -04:00
return serialized;
}
/**
2024-06-03 15:27:04 -05:00
* Returns a payload containing only the grants array and version.
* @param {Snapshot} snapshot
* @param {[string]} grantStrings
* @return {object}
*/
serializeWithGrantStrings(snapshot, grantStrings) {
2020-08-07 15:42:40 -04:00
return {
version: snapshot.attr('version'),
2021-03-04 15:47:38 -05:00
grant_strings: grantStrings,
2020-08-07 15:42:40 -04:00
};
}
/**
2024-06-03 15:27:04 -05:00
* Returns a payload containing only the principal_ids array using IDs passed
* into the function (rather than existing principals on the model) and version.
* @param {Snapshot} snapshot
* @param {[string]} principalIDs
* @return {object}
*/
2024-04-11 12:39:24 -04:00
serializeWithPrincipals(snapshot, principalIDs) {
return {
version: snapshot.attr('version'),
2020-09-16 14:29:02 -04:00
principal_ids: principalIDs,
};
}
2024-06-03 15:27:04 -05:00
/**
* Returns a payload containing only the grant_scope_ids array and version.
* @param {Snapshot} snapshot
* @param {[string]} grantScopeIDs
* @return {object}
*/
serializeWithGrantScopeIDs(snapshot, grantScopeIDs) {
return {
version: snapshot.attr('version'),
grant_scope_ids: grantScopeIDs,
};
}
2020-08-07 15:42:40 -04:00
}