Commit 93b51390 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate permissions to TypeScript

parent a6a89b6b
...@@ -6,16 +6,18 @@ ...@@ -6,16 +6,18 @@
* *
* Principals are strings of the form `type:id` where `type` is `'acct'` (for a * Principals are strings of the form `type:id` where `type` is `'acct'` (for a
* specific user) or `'group'` (for a group). * specific user) or `'group'` (for a group).
*
* @typedef Permissions
* @prop {string[]} read - List of principals that can read the annotation
* @prop {string[]} update - List of principals that can edit the annotation
* @prop {string[]} delete - List of principals that can delete the
* annotation
*/ */
/** @param {string|null} savedLevel */ type Permissions = {
function defaultLevel(savedLevel) { /** List of principals that can read the annotation */
read: string[];
/** List of principals that can edit the annotation */
update: string[];
/** List of principals that can delete the annotation */
delete: string[];
};
function defaultLevel(savedLevel: string | null): string {
switch (savedLevel) { switch (savedLevel) {
case 'private': case 'private':
case 'shared': case 'shared':
...@@ -30,10 +32,9 @@ function defaultLevel(savedLevel) { ...@@ -30,10 +32,9 @@ function defaultLevel(savedLevel) {
* *
* A private annotation is one which is readable only by its author. * A private annotation is one which is readable only by its author.
* *
* @param {string} userid - User ID of the author * @param userid - User ID of the author
* @return {Permissions}
*/ */
export function privatePermissions(userid) { export function privatePermissions(userid: string): Permissions {
return { return {
read: [userid], read: [userid],
update: [userid], update: [userid],
...@@ -45,26 +46,29 @@ export function privatePermissions(userid) { ...@@ -45,26 +46,29 @@ export function privatePermissions(userid) {
* Return the permissions for an annotation that is shared with the given * Return the permissions for an annotation that is shared with the given
* group. * group.
* *
* @param {string} userid - User ID of the author * @param userid - User ID of the author
* @param {string} groupid - ID of the group the annotation is being * @param groupid - ID of the group the annotation is being shared with
* shared with
* @return {Permissions}
*/ */
export function sharedPermissions(userid, groupid) { export function sharedPermissions(
userid: string,
groupid: string
): Permissions {
return Object.assign(privatePermissions(userid), { return Object.assign(privatePermissions(userid), {
read: ['group:' + groupid], read: ['group:' + groupid],
}); });
} }
/** /**
* Return the default permissions for an annotation in a given group. * Return the default permissions for an annotation in a given group.
* *
* @param {string} userid - User ID of the author * @param userid - User ID of the author
* @param {string} groupid - ID of the group the annotation is being shared * @param groupid - ID of the group the annotation is being shared with
* with
* @param {string|null} savedLevel
* @return {Permissions}
*/ */
export function defaultPermissions(userid, groupid, savedLevel) { export function defaultPermissions(
userid: string,
groupid: string,
savedLevel: string | null
): Permissions {
if (defaultLevel(savedLevel) === 'private' && userid) { if (defaultLevel(savedLevel) === 'private' && userid) {
return privatePermissions(userid); return privatePermissions(userid);
} else { } else {
...@@ -75,11 +79,8 @@ export function defaultPermissions(userid, groupid, savedLevel) { ...@@ -75,11 +79,8 @@ export function defaultPermissions(userid, groupid, savedLevel) {
/** /**
* Return true if an annotation with the given permissions is shared with any * Return true if an annotation with the given permissions is shared with any
* group. * group.
*
* @param {Permissions} perms
* @return {boolean}
*/ */
export function isShared(perms) { export function isShared(perms: Permissions): boolean {
return perms.read.some(principal => { return perms.read.some(principal => {
return principal.indexOf('group:') === 0; return principal.indexOf('group:') === 0;
}); });
...@@ -87,22 +88,18 @@ export function isShared(perms) { ...@@ -87,22 +88,18 @@ export function isShared(perms) {
/** /**
* Return true if an annotation with the given permissions is private. * Return true if an annotation with the given permissions is private.
*
* @param {Permissions} perms
* @return {boolean}
*/ */
export function isPrivate(perms) { export function isPrivate(perms: Permissions): boolean {
return !isShared(perms); return !isShared(perms);
} }
/** /**
* Return true if a user can perform the given `action` on an annotation. * Return true if a user can perform the given `action` on an annotation.
*
* @param {Permissions} perms
* @param {'update'|'delete'} action
* @param {string|null} userid
* @return {boolean}
*/ */
export function permits(perms, action, userid) { export function permits(
perms: Permissions,
action: 'update' | 'delete',
userid: string | null
): boolean {
return perms[action].indexOf(userid || '') !== -1; return perms[action].indexOf(userid || '') !== -1;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment