This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Tom Moor e2b28dfeb7
refactor: Policies Architecture (#1016)
* add policy serialize method

* Add policies to collection responses

* wip

* test: remove .only

* refactor: Return policies with team and document requests

* store policies on the client

* refactor: drive admin UI from policies
2019-08-21 21:41:37 -07:00

46 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// @flow
import { Team, User, Collection, Document } from '../models';
import policy from './policy';
import './apiKey';
import './collection';
import './document';
import './integration';
import './notificationSetting';
import './share';
import './user';
import './team';
const { can, abilities } = policy;
type Policy = {
[key: string]: boolean,
};
/*
* Given a user and a model output an object which describes the actions the
* user may take against the model. This serialized policy is used for testing
* and sent in API responses to allow clients to adjust which UI is displayed.
*/
export function serialize(
model: User,
target: Team | Collection | Document
): Policy {
let output = {};
abilities.forEach(ability => {
if (model instanceof ability.model && target instanceof ability.target) {
let response = true;
try {
response = can(model, ability.action, target);
} catch (err) {
response = false;
}
output[ability.action] = response;
}
});
return output;
}
export default policy;