fix: 🐛 fixes issue where grant strings become corrupted in ui

This commit is contained in:
Randall Morey committed 2020-10-30 13:23:19 -04:00
1 parent cf5e17af28
commit efa851928a
8 files changed
+41 -49

No files matched your search

+7 -6
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()`.
* Grants is read-only. But grants can be persisted via a dedicated
* call to `saveGrants(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.
* See serializer and adapter for more information.
* @param {[string]} grants
* @return {Promise}
*/
saveGrants() {
saveGrants(grants) {
return this.save({
adapterOptions: {
method: 'set-grants',
serializeGrants: true,
grants
},
});
}
+5 -24
View File
@@ -15,9 +15,9 @@ export default class RoleSerializer extends ApplicationSerializer {
* @return {object}
*/
serialize(snapshot) {
const serializeGrants = get(snapshot, 'adapterOptions.serializeGrants');
const grants = get(snapshot, 'adapterOptions.grants');
let serialized = super.serialize(...arguments);
if (serializeGrants) serialized = this.serializeWithGrants(snapshot);
if (grants) serialized = this.serializeWithGrants(snapshot, grants);
const principalIDs = snapshot?.adapterOptions?.principalIDs;
if (principalIDs)
serialized = this.serializewithPrincipals(snapshot, principalIDs);
@@ -27,14 +27,13 @@ export default class RoleSerializer extends ApplicationSerializer {
/**
* Returns a payload containing only the grants array.
* @param {Snapshot} snapshot
* @param {[string]} grants
* @return {object}
*/
serializeWithGrants(snapshot) {
serializeWithGrants(snapshot, grants) {
return {
version: snapshot.attr('version'),
grant_strings: snapshot
.attr('grants')
.map((grant) => grant.attr('value')),
grant_strings: grants
};
}
@@ -51,22 +50,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);
}
}