{"action":"created","comment":{"url":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/comments/200086631","pull_request_review_id":134308485,"id":200086631,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwMDA4NjYzMQ==","diff_hunk":"@@ -0,0 +1,173 @@\n+import * as knex from 'knex';\n+import { PermissionsInitialiser, GetPermissionIds, GetRoleId } from './permissions';\n+\n+const getPermissionIds: GetPermissionIds = ({ client, resource, permissionLevel, access }) =>\n+  client\n+    .select('permission_id')\n+    .table('permission')\n+    .where({\n+      ['permission_entity']: resource,\n+      ['permission_level']: permissionLevel,\n+      ['access_type']: access,\n+    });\n+\n+const getRoleId: GetRoleId = async({ client, role }) =>\n+  client\n+    .select('access_role_id')\n+    .table('access_role')\n+    .where({ ['access_role_name']: role })\n+    .then((res) => res[0].access_role_id);\n+\n+const permissionsInitialiser: PermissionsInitialiser = (config) => {\n+  const client = knex(config);\n+  return {\n+    grantExisting: async ({ resource, permissionLevel, access, role }) => {\n+      const roleId = await getRoleId({ client, role });\n+\n+      const permissionIds = await getPermissionIds({\n+        client,\n+        resource,\n+        permissionLevel,\n+        access });\n+\n+      if (permissionIds.length === 1) {\n+        const permissionId = permissionIds[0].permission_id;\n+\n+        const insertPermission = await client('access_role_permission')\n+            .returning('*')\n+            .insert(\n+              client.select({\n+                ['access_role_id']: roleId,\n+                ['permission_id']: permissionId,\n+              })\n+                .whereNotExists(client('access_role_permission').where({\n+                  ['access_role_id']: roleId,\n+                  ['permission_id']: permissionId,\n+                }))\n+            );\n+        if (insertPermission.length === 0) {\n+          throw new Error('Permission entry is already associated to this role');\n+        }\n+        client.destroy();\n+        return insertPermission;\n+      } else {\n+        client.destroy();\n+        throw new Error('Permission entry does not exists, please use grantNew method');\n+      }\n+    },\n+    grantNew: async ({ resource, permissionLevel, access, role }) => {\n+      const roleId = await getRoleId({ client, role });\n+\n+      const permissionIds = await getPermissionIds({\n+        client,\n+        resource,\n+        permissionLevel,\n+        access });\n+\n+      if (permissionIds.length === 0) {\n+        const createPermissionId = await client('permission')\n+            .returning('permission_id')\n+            .insert({\n+              ['permission_entity']: resource,\n+              ['permission_level']: permissionLevel,\n+              ['access_type']: access,\n+            });\n+\n+        const permissionId = createPermissionId[0];\n+        const insertPermission = await client('access_role_permission')\n+            .returning('*')\n+            .insert({\n+              ['access_role_id']: roleId,\n+              ['permission_id']: permissionId,\n+            });\n+        client.destroy();\n+        return insertPermission;\n+      } else {\n+        throw new Error('Permission already exists, please use grantExisting method');\n+        client.destroy();\n+      }\n+    },\n+    revoke: async ({ resource, permissionLevel, access, role }) => {\n+      const roleId = await getRoleId({ client, role });\n+\n+      const permissionIds = await getPermissionIds({\n+        client,\n+        resource,\n+        permissionLevel,\n+        access });\n+\n+      if (permissionIds.length === 1) {\n+        const permissionId = permissionIds[0].permission_id;\n+\n+        const deleteRow = await client('access_role_permission')\n+            .where({\n+              ['access_role_id']: roleId,\n+              ['permission_id']: permissionId,\n+            })\n+            .del();\n+\n+        if (deleteRow === 1) return deleteRow;\n+        throw new Error('Permission entry is not linked to role');\n+      }\n+    },\n+      // todo - currently no definitive list of permission entries\n+      // grantAll: () => {},\n+      // revokeAll: () => {},\n+    roleHas: async ({ resource, permissionLevel, access, role }) => {\n+      const inner = client\n+          .select()\n+          .table('permission')\n+          .innerJoin('access_role_permission',\n+            'permission.permission_id',\n+            'access_role_permission.permission_id')\n+          .innerJoin('access_role',\n+            'access_role.access_role_id',\n+            'access_role_permission.access_role_id')\n+          .where({\n+            ['access_role.access_role_name']: role,\n+            ['permission.access_type']: access,\n+            ['permission.permission_entity']: resource,\n+            ['permission.permission_level']: permissionLevel,\n+          });\n+      const roleHasPermission = client.raw('SELECT EXISTS ?', [inner]);\n+      const result = await roleHasPermission;","path":"src/auth/permissions/index.ts","position":133,"original_position":133,"commit_id":"dcb5a609dce2aa67a5880d3de34d7e7fb628e295","original_commit_id":"dcb5a609dce2aa67a5880d3de34d7e7fb628e295","user":{"login":"eliascodes","id":12719553,"node_id":"MDQ6VXNlcjEyNzE5NTUz","avatar_url":"https://avatars3.githubusercontent.com/u/12719553?v=4","gravatar_id":"","url":"https://api.github.com/users/eliascodes","html_url":"https://github.com/eliascodes","followers_url":"https://api.github.com/users/eliascodes/followers","following_url":"https://api.github.com/users/eliascodes/following{/other_user}","gists_url":"https://api.github.com/users/eliascodes/gists{/gist_id}","starred_url":"https://api.github.com/users/eliascodes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eliascodes/subscriptions","organizations_url":"https://api.github.com/users/eliascodes/orgs","repos_url":"https://api.github.com/users/eliascodes/repos","events_url":"https://api.github.com/users/eliascodes/events{/privacy}","received_events_url":"https://api.github.com/users/eliascodes/received_events","type":"User","site_admin":false},"body":"Why not combine this line with the one above?","created_at":"2018-07-04T10:48:57Z","updated_at":"2018-07-04T10:55:17Z","html_url":"https://github.com/TwinePlatform/twine-api/pull/45#discussion_r200086631","pull_request_url":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/45","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/comments/200086631"},"html":{"href":"https://github.com/TwinePlatform/twine-api/pull/45#discussion_r200086631"},"pull_request":{"href":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/45"}}},"pull_request":{"url":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/45","id":198975832,"node_id":"MDExOlB1bGxSZXF1ZXN0MTk4OTc1ODMy","html_url":"https://github.com/TwinePlatform/twine-api/pull/45","diff_url":"https://github.com/TwinePlatform/twine-api/pull/45.diff","patch_url":"https://github.com/TwinePlatform/twine-api/pull/45.patch","issue_url":"https://api.github.com/repos/TwinePlatform/twine-api/issues/45","number":45,"state":"open","locked":false,"title":"Permissions module","user":{"login":"astroash","id":14337958,"node_id":"MDQ6VXNlcjE0MzM3OTU4","avatar_url":"https://avatars3.githubusercontent.com/u/14337958?v=4","gravatar_id":"","url":"https://api.github.com/users/astroash","html_url":"https://github.com/astroash","followers_url":"https://api.github.com/users/astroash/followers","following_url":"https://api.github.com/users/astroash/following{/other_user}","gists_url":"https://api.github.com/users/astroash/gists{/gist_id}","starred_url":"https://api.github.com/users/astroash/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/astroash/subscriptions","organizations_url":"https://api.github.com/users/astroash/orgs","repos_url":"https://api.github.com/users/astroash/repos","events_url":"https://api.github.com/users/astroash/events{/privacy}","received_events_url":"https://api.github.com/users/astroash/received_events","type":"User","site_admin":false},"body":"#31 to be merged first\r\n\r\n**Changes**:\r\n- fix migrate:teardown script\r\n- remove db logs in testing env\r\n- create permissions section permissions module corresponding type definitions\r\n- add test data to test db\r\n- add tests\r\n- add test env scripts\r\n- remove `noImplicitAny` typescript setting whilst still using js files\r\n- rename permission_type -> permission_level\r\n**Still to do:**\r\n- roles section of permissions module\r\n\r\nFixes #41 fixes #40","created_at":"2018-07-03T14:07:29Z","updated_at":"2018-07-04T10:55:17Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f158cf1fb4eaf294005f06c39c79d457fb9b7cf9","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":949350690,"node_id":"MDU6TGFiZWw5NDkzNTA2OTA=","url":"https://api.github.com/repos/TwinePlatform/twine-api/labels/in-progress","name":"in-progress","color":"c0d5f7","default":false}],"milestone":null,"commits_url":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/45/commits","review_comments_url":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/45/comments","review_comment_url":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/comments{/number}","comments_url":"https://api.github.com/repos/TwinePlatform/twine-api/issues/45/comments","statuses_url":"https://api.github.com/repos/TwinePlatform/twine-api/statuses/dcb5a609dce2aa67a5880d3de34d7e7fb628e295","head":{"label":"TwinePlatform:permissions-module","ref":"permissions-module","sha":"dcb5a609dce2aa67a5880d3de34d7e7fb628e295","user":{"login":"TwinePlatform","id":35457494,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM1NDU3NDk0","avatar_url":"https://avatars1.githubusercontent.com/u/35457494?v=4","gravatar_id":"","url":"https://api.github.com/users/TwinePlatform","html_url":"https://github.com/TwinePlatform","followers_url":"https://api.github.com/users/TwinePlatform/followers","following_url":"https://api.github.com/users/TwinePlatform/following{/other_user}","gists_url":"https://api.github.com/users/TwinePlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/TwinePlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TwinePlatform/subscriptions","organizations_url":"https://api.github.com/users/TwinePlatform/orgs","repos_url":"https://api.github.com/users/TwinePlatform/repos","events_url":"https://api.github.com/users/TwinePlatform/events{/privacy}","received_events_url":"https://api.github.com/users/TwinePlatform/received_events","type":"Organization","site_admin":false},"repo":{"id":135316191,"node_id":"MDEwOlJlcG9zaXRvcnkxMzUzMTYxOTE=","name":"twine-api","full_name":"TwinePlatform/twine-api","owner":{"login":"TwinePlatform","id":35457494,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM1NDU3NDk0","avatar_url":"https://avatars1.githubusercontent.com/u/35457494?v=4","gravatar_id":"","url":"https://api.github.com/users/TwinePlatform","html_url":"https://github.com/TwinePlatform","followers_url":"https://api.github.com/users/TwinePlatform/followers","following_url":"https://api.github.com/users/TwinePlatform/following{/other_user}","gists_url":"https://api.github.com/users/TwinePlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/TwinePlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TwinePlatform/subscriptions","organizations_url":"https://api.github.com/users/TwinePlatform/orgs","repos_url":"https://api.github.com/users/TwinePlatform/repos","events_url":"https://api.github.com/users/TwinePlatform/events{/privacy}","received_events_url":"https://api.github.com/users/TwinePlatform/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/TwinePlatform/twine-api","description":"☁️ HTTP API for the Twine platform of apps","fork":false,"url":"https://api.github.com/repos/TwinePlatform/twine-api","forks_url":"https://api.github.com/repos/TwinePlatform/twine-api/forks","keys_url":"https://api.github.com/repos/TwinePlatform/twine-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/TwinePlatform/twine-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/TwinePlatform/twine-api/teams","hooks_url":"https://api.github.com/repos/TwinePlatform/twine-api/hooks","issue_events_url":"https://api.github.com/repos/TwinePlatform/twine-api/issues/events{/number}","events_url":"https://api.github.com/repos/TwinePlatform/twine-api/events","assignees_url":"https://api.github.com/repos/TwinePlatform/twine-api/assignees{/user}","branches_url":"https://api.github.com/repos/TwinePlatform/twine-api/branches{/branch}","tags_url":"https://api.github.com/repos/TwinePlatform/twine-api/tags","blobs_url":"https://api.github.com/repos/TwinePlatform/twine-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/TwinePlatform/twine-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/TwinePlatform/twine-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/TwinePlatform/twine-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/TwinePlatform/twine-api/statuses/{sha}","languages_url":"https://api.github.com/repos/TwinePlatform/twine-api/languages","stargazers_url":"https://api.github.com/repos/TwinePlatform/twine-api/stargazers","contributors_url":"https://api.github.com/repos/TwinePlatform/twine-api/contributors","subscribers_url":"https://api.github.com/repos/TwinePlatform/twine-api/subscribers","subscription_url":"https://api.github.com/repos/TwinePlatform/twine-api/subscription","commits_url":"https://api.github.com/repos/TwinePlatform/twine-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/TwinePlatform/twine-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/TwinePlatform/twine-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/TwinePlatform/twine-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/TwinePlatform/twine-api/contents/{+path}","compare_url":"https://api.github.com/repos/TwinePlatform/twine-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/TwinePlatform/twine-api/merges","archive_url":"https://api.github.com/repos/TwinePlatform/twine-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/TwinePlatform/twine-api/downloads","issues_url":"https://api.github.com/repos/TwinePlatform/twine-api/issues{/number}","pulls_url":"https://api.github.com/repos/TwinePlatform/twine-api/pulls{/number}","milestones_url":"https://api.github.com/repos/TwinePlatform/twine-api/milestones{/number}","notifications_url":"https://api.github.com/repos/TwinePlatform/twine-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/TwinePlatform/twine-api/labels{/name}","releases_url":"https://api.github.com/repos/TwinePlatform/twine-api/releases{/id}","deployments_url":"https://api.github.com/repos/TwinePlatform/twine-api/deployments","created_at":"2018-05-29T15:26:17Z","updated_at":"2018-07-03T12:48:53Z","pushed_at":"2018-07-03T16:51:23Z","git_url":"git://github.com/TwinePlatform/twine-api.git","ssh_url":"git@github.com:TwinePlatform/twine-api.git","clone_url":"https://github.com/TwinePlatform/twine-api.git","svn_url":"https://github.com/TwinePlatform/twine-api","homepage":"","size":3064,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":15,"license":null,"forks":0,"open_issues":15,"watchers":0,"default_branch":"master"}},"base":{"label":"TwinePlatform:master","ref":"master","sha":"3538a7bd51fe4e3d8b6e7b346802817f2c5748ba","user":{"login":"TwinePlatform","id":35457494,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM1NDU3NDk0","avatar_url":"https://avatars1.githubusercontent.com/u/35457494?v=4","gravatar_id":"","url":"https://api.github.com/users/TwinePlatform","html_url":"https://github.com/TwinePlatform","followers_url":"https://api.github.com/users/TwinePlatform/followers","following_url":"https://api.github.com/users/TwinePlatform/following{/other_user}","gists_url":"https://api.github.com/users/TwinePlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/TwinePlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TwinePlatform/subscriptions","organizations_url":"https://api.github.com/users/TwinePlatform/orgs","repos_url":"https://api.github.com/users/TwinePlatform/repos","events_url":"https://api.github.com/users/TwinePlatform/events{/privacy}","received_events_url":"https://api.github.com/users/TwinePlatform/received_events","type":"Organization","site_admin":false},"repo":{"id":135316191,"node_id":"MDEwOlJlcG9zaXRvcnkxMzUzMTYxOTE=","name":"twine-api","full_name":"TwinePlatform/twine-api","owner":{"login":"TwinePlatform","id":35457494,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM1NDU3NDk0","avatar_url":"https://avatars1.githubusercontent.com/u/35457494?v=4","gravatar_id":"","url":"https://api.github.com/users/TwinePlatform","html_url":"https://github.com/TwinePlatform","followers_url":"https://api.github.com/users/TwinePlatform/followers","following_url":"https://api.github.com/users/TwinePlatform/following{/other_user}","gists_url":"https://api.github.com/users/TwinePlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/TwinePlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TwinePlatform/subscriptions","organizations_url":"https://api.github.com/users/TwinePlatform/orgs","repos_url":"https://api.github.com/users/TwinePlatform/repos","events_url":"https://api.github.com/users/TwinePlatform/events{/privacy}","received_events_url":"https://api.github.com/users/TwinePlatform/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/TwinePlatform/twine-api","description":"☁️ HTTP API for the Twine platform of apps","fork":false,"url":"https://api.github.com/repos/TwinePlatform/twine-api","forks_url":"https://api.github.com/repos/TwinePlatform/twine-api/forks","keys_url":"https://api.github.com/repos/TwinePlatform/twine-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/TwinePlatform/twine-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/TwinePlatform/twine-api/teams","hooks_url":"https://api.github.com/repos/TwinePlatform/twine-api/hooks","issue_events_url":"https://api.github.com/repos/TwinePlatform/twine-api/issues/events{/number}","events_url":"https://api.github.com/repos/TwinePlatform/twine-api/events","assignees_url":"https://api.github.com/repos/TwinePlatform/twine-api/assignees{/user}","branches_url":"https://api.github.com/repos/TwinePlatform/twine-api/branches{/branch}","tags_url":"https://api.github.com/repos/TwinePlatform/twine-api/tags","blobs_url":"https://api.github.com/repos/TwinePlatform/twine-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/TwinePlatform/twine-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/TwinePlatform/twine-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/TwinePlatform/twine-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/TwinePlatform/twine-api/statuses/{sha}","languages_url":"https://api.github.com/repos/TwinePlatform/twine-api/languages","stargazers_url":"https://api.github.com/repos/TwinePlatform/twine-api/stargazers","contributors_url":"https://api.github.com/repos/TwinePlatform/twine-api/contributors","subscribers_url":"https://api.github.com/repos/TwinePlatform/twine-api/subscribers","subscription_url":"https://api.github.com/repos/TwinePlatform/twine-api/subscription","commits_url":"https://api.github.com/repos/TwinePlatform/twine-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/TwinePlatform/twine-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/TwinePlatform/twine-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/TwinePlatform/twine-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/TwinePlatform/twine-api/contents/{+path}","compare_url":"https://api.github.com/repos/TwinePlatform/twine-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/TwinePlatform/twine-api/merges","archive_url":"https://api.github.com/repos/TwinePlatform/twine-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/TwinePlatform/twine-api/downloads","issues_url":"https://api.github.com/repos/TwinePlatform/twine-api/issues{/number}","pulls_url":"https://api.github.com/repos/TwinePlatform/twine-api/pulls{/number}","milestones_url":"https://api.github.com/repos/TwinePlatform/twine-api/milestones{/number}","notifications_url":"https://api.github.com/repos/TwinePlatform/twine-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/TwinePlatform/twine-api/labels{/name}","releases_url":"https://api.github.com/repos/TwinePlatform/twine-api/releases{/id}","deployments_url":"https://api.github.com/repos/TwinePlatform/twine-api/deployments","created_at":"2018-05-29T15:26:17Z","updated_at":"2018-07-03T12:48:53Z","pushed_at":"2018-07-03T16:51:23Z","git_url":"git://github.com/TwinePlatform/twine-api.git","ssh_url":"git@github.com:TwinePlatform/twine-api.git","clone_url":"https://github.com/TwinePlatform/twine-api.git","svn_url":"https://github.com/TwinePlatform/twine-api","homepage":"","size":3064,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":15,"license":null,"forks":0,"open_issues":15,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/45"},"html":{"href":"https://github.com/TwinePlatform/twine-api/pull/45"},"issue":{"href":"https://api.github.com/repos/TwinePlatform/twine-api/issues/45"},"comments":{"href":"https://api.github.com/repos/TwinePlatform/twine-api/issues/45/comments"},"review_comments":{"href":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/45/comments"},"review_comment":{"href":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/TwinePlatform/twine-api/pulls/45/commits"},"statuses":{"href":"https://api.github.com/repos/TwinePlatform/twine-api/statuses/dcb5a609dce2aa67a5880d3de34d7e7fb628e295"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"eliascodes"}}