feat: 🎸 add support for role/principals custom methods

This commit is contained in:
Randall Morey committed 2020-08-28 16:26:02 -04:00
1 parent b744f74156
commit 42d21c3a73
3 files changed
+178

No files matched your search

+19
View File
@@ -9,6 +9,8 @@ export default class RoleSerializer extends ApplicationSerializer {
* If `adapterOptions.serializeGrants` is true, the serialization should
* include **only grants** and the version. Normally, grants are not
* serialized.
* If `adapterOptions.principalIDs` is set (to an array of user and
* group IDs), then the payload is serialized via `serializeWithPrincipals`.
* @override
* @param {Snapshot} snapshot
* @return {object}
@@ -17,6 +19,9 @@ export default class RoleSerializer extends ApplicationSerializer {
const serializeGrants = get(snapshot, 'adapterOptions.serializeGrants');
let serialized = super.serialize(...arguments);
if (serializeGrants) serialized = this.serializeWithGrants(snapshot);
const principalIDs = snapshot?.adapterOptions?.principalIDs;
if (principalIDs) serialized =
this.serializeWithPrincipals(snapshot, principalIDs);
return serialized;
}
@@ -32,6 +37,20 @@ export default class RoleSerializer extends ApplicationSerializer {
};
}
/**
* Returns a payload containing only the principal_ids array using IDs
* passed into the function (rather than existing principals on the model).
* @param {Snapshot} snapshot
* @param {[string]} principalIDs
* @return {object}
*/
serializeWithPrincipals(snapshot, principalIDs) {
return {
version: snapshot.attr('version'),
principal_ids: principalIDs
};
}
/**
* Converts string arrays to object arrays for use with `fragment-string`.
* E.g. `"foo"` -> `{value: "foo"}`