Files
talk/src/core/client/stream/permissions.tsx
T
KiwiandGitHub 6da97c57d7 [CORL-314] Rename to Coral (#2318)
* chore: rename talk to coral

* fix: lint and unit tests

* fix: snapshot
2019-05-22 21:32:24 +02:00

33 lines
1.1 KiB
TypeScript

import { GQLUSER_ROLE, GQLUSER_ROLE_RL } from "coral-framework/schema";
import { mapValues } from "lodash";
/**
* permissionMap describes what abilities certain roles have.
*
* This list is currently manually managed. We want to
* get to a point where this is generated from the schema.
*
* We currently specify in the comments which endpoints of
* the graph is important for the ability, which we can later
* used to auto generate the map making the schema become
* the single point of truth.
*/
const permissionMap = {
// Mutation.updateStorySettings
CHANGE_STORY_CONFIGURATION: [GQLUSER_ROLE.ADMIN, GQLUSER_ROLE.MODERATOR],
};
export type AbilityType = keyof typeof permissionMap;
export const Ability = mapValues(permissionMap, (_, key) => key) as {
[P in AbilityType]: P
};
/**
* can is used to check if the `viewer` has permission for `ability`.
*
* Example: `can(props.me, Ability.CHANGE_ROLE)`.
*/
export function can(viewer: { role: GQLUSER_ROLE_RL }, ability: AbilityType) {
return permissionMap[ability].includes(viewer.role as GQLUSER_ROLE);
}