Merge branch 'main' into feature-role-grant-scope

This commit is contained in:
Randall Morey authored and GitHub committed 2020-11-04 14:16:49 -05:00
commit 4f461ede90
10 files changed
+54 -54

No files matched your search

+2 -2
View File
@@ -6,8 +6,8 @@ export default class HostSetModel extends GeneratedHostSetModel {
// =attributes
/**
* Grants is read-only _most_ under normal circumstances. But grants can
* be persisted via a dedicated call to `saveGrants()`.
* Host IDs are read-only under normal circumstances. But these can
* be persisted via a dedicated call to `addHosts()`.
*/
@fragmentArray('fragment-string', {
readOnly: true,
+8 -7
View File
@@ -1,5 +1,5 @@
import GeneratedRoleModel from '../generated/models/role';
import { fragmentArray } from 'ember-data-model-fragments/attributes';
import { fragmentArray, array } from 'ember-data-model-fragments/attributes';
export default class RoleModel extends GeneratedRoleModel {
// =attributes
@@ -18,23 +18,24 @@ export default class RoleModel extends GeneratedRoleModel {
principals;
/**
* Grants is read-only _most_ under normal circumstances. But grants can
* be persisted via a dedicated call to `saveGrants()`.
* Grant strings are read-only. But grants can be persisted via a dedicated
* call to `saveGrantStrings(grants)`.
*/
@fragmentArray('fragment-string', { readOnly: true }) grants;
@array('string', { readOnly: true }) grant_strings;
// =methods
/**
* Saves the `grants` array on the role via the `set-grants` method.
* Saves grant strings on the role via the `set-grants` method.
* See serializer and adapter for more information.
* @param {[string]} grantStrings
* @return {Promise}
*/
saveGrants() {
saveGrantStrings(grantStrings) {
return this.save({
adapterOptions: {
method: 'set-grants',
serializeGrants: true,
grantStrings
},
});
}
+6 -24
View File
@@ -15,9 +15,10 @@ export default class RoleSerializer extends ApplicationSerializer {
* @return {object}
*/
serialize(snapshot) {
const serializeGrants = get(snapshot, 'adapterOptions.serializeGrants');
const grantStrings = get(snapshot, 'adapterOptions.grantStrings');
let serialized = super.serialize(...arguments);
if (serializeGrants) serialized = this.serializeWithGrants(snapshot);
if (grantStrings) serialized =
this.serializeWithGrantStrings(snapshot, grantStrings);
const principalIDs = snapshot?.adapterOptions?.principalIDs;
if (principalIDs)
serialized = this.serializewithPrincipals(snapshot, principalIDs);
@@ -27,14 +28,13 @@ export default class RoleSerializer extends ApplicationSerializer {
/**
* Returns a payload containing only the grants array.
* @param {Snapshot} snapshot
* @param {[string]} grantStrings
* @return {object}
*/
serializeWithGrants(snapshot) {
serializeWithGrantStrings(snapshot, grantStrings) {
return {
version: snapshot.attr('version'),
grant_strings: snapshot
.attr('grants')
.map((grant) => grant.attr('value')),
grant_strings: grantStrings
};
}
@@ -51,22 +51,4 @@ export default class RoleSerializer extends ApplicationSerializer {
principal_ids: principalIDs,
};
}
/**
* Converts string arrays to object arrays for use with `fragment-string`.
* E.g. `"foo"` -> `{value: "foo"}`
* @override
* @param {Model} typeClass
* @param {Object} hash
* @return {Object}
*/
normalize(modelClass, resourceHash) {
// TODO: can fragment string normalization be handled generically?
if (resourceHash.grant_strings) {
resourceHash.grants = resourceHash.grant_strings.map((value) => ({
value,
}));
}
return super.normalize(modelClass, resourceHash);
}
}