{"id":21301465,"url":"https://github.com/minddocdev/accesscontrol","last_synced_at":"2025-07-11T20:31:12.424Z","repository":{"id":38332744,"uuid":"234557757","full_name":"minddocdev/accesscontrol","owner":"minddocdev","description":"Provides role based access control","archived":false,"fork":false,"pushed_at":"2023-10-02T22:12:38.000Z","size":1168,"stargazers_count":5,"open_issues_count":16,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-27T13:07:25.554Z","etag":null,"topics":["access-control","nodejs","rbac","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/minddocdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-17T13:50:22.000Z","updated_at":"2023-03-17T09:48:32.000Z","dependencies_parsed_at":"2024-10-22T22:01:25.555Z","dependency_job_id":null,"html_url":"https://github.com/minddocdev/accesscontrol","commit_stats":{"total_commits":133,"total_committers":5,"mean_commits":26.6,"dds":"0.30827067669172936","last_synced_commit":"8b87c1a2d875eb537a301d88fd26ffc18449d772"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/minddocdev/accesscontrol","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minddocdev%2Faccesscontrol","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minddocdev%2Faccesscontrol/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minddocdev%2Faccesscontrol/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minddocdev%2Faccesscontrol/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/minddocdev","download_url":"https://codeload.github.com/minddocdev/accesscontrol/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minddocdev%2Faccesscontrol/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264277591,"owners_count":23583633,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["access-control","nodejs","rbac","typescript"],"created_at":"2024-11-21T15:46:02.685Z","updated_at":"2025-07-11T20:31:07.417Z","avatar_url":"https://github.com/minddocdev.png","language":"TypeScript","readme":"# Role Based Access Control (RBAC)\n\nMany Role Based Access Control (RBAC) implementations differ, but the basics is widely adopted since it simulates real life role (job) assignments. But while data is getting more and more complex; you need to define policies on resources, subjects or even environments, this is called Attribute Based Access Control (ABAC).\n\n## Core Features\n\n- Chainable, friendly API, e.g. `ac.can(role).create(resource)`\n- Role hierarchical **inheritance**.\n- Define grants **at once** (e.g. from database result) or **one by one**.\n- Grant/deny permissions by attributes defined by **glob notation**.\n- Ability to control access on **own** or **any** resources.\n- No **silent** errors.\n- **Fast**. (Grants are stored in memory, no database queries.)\n\n## Installation\n\n```bash\nyarn add @minddoc/accesscontrol\n```\n\n## Usage\n\n```typescript\nimport { AccessControl } from 'accesscontrol';\n```\n\n### Basic Example\n\nDefine roles and grants one by one.\n\n```typescript\nconst ac = new AccessControl();\nac.grant('user') // define new or modify existing role. also takes an array.\n  .createOwn('video') // equivalent to .createOwn('video', ['*'])\n  .deleteOwn('video')\n  .readAny('video')\n  .grant('admin') // switch to another role without breaking the chain\n  .extend('user') // inherit role capabilities. also takes an array\n  .updateAny('video', ['title']) // explicitly defined attributes\n  .deleteAny('video');\n\nconst permission = ac.can('user').createOwn('video');\nconsole.log(permission.granted); // —\u003e true\nconsole.log(permission.attributes); // —\u003e ['*'] (all attributes)\n\npermission = ac.can('admin').updateAny('video');\nconsole.log(permission.granted); // —\u003e true\nconsole.log(permission.attributes); // —\u003e ['title']\n```\n\n## Roles\n\nYou can create/define roles simply by calling `.grant(\u003crole\u003e)` or `.deny(\u003crole\u003e)` methods on an `AccessControl` instance.\n\n- Roles can extend other roles.\n\n```typescript\n// user role inherits viewer role permissions\nac.grant('user').extend('viewer');\n// admin role inherits both user and editor role permissions\nac.grant('admin').extend(['user', 'editor']);\n// both admin and superadmin roles inherit moderator permissions\nac.grant(['admin', 'superadmin']).extend('moderator');\n```\n\n- Inheritance is done by reference, so you can grant resource permissions before or after extending a role.\n\n```typescript\n// case #1\nac.grant('admin')\n  .extend('user') // assuming user role already exists\n  .grant('user')\n  .createOwn('video');\n\n// case #2\nac.grant('user')\n  .createOwn('video')\n  .grant('admin')\n  .extend('user');\n\n// below results the same for both cases\nconst permission = ac.can('admin').createOwn('video');\nconsole.log(permission.granted); // true\n```\n\nNotes on inheritance:\n\n- A role cannot extend itself.\n- Cross-inheritance is not allowed.  \n  e.g. `ac.grant('user').extend('admin').grant('admin').extend('user')` will throw.\n- A role cannot (pre)extend a non-existing role. In other words, you should first create the base role. e.g. `ac.grant('baseRole').grant('role').extend('baseRole')`\n\n## Actions and Action-Attributes\n\n[CRUD][crud] operations are the actions you can perform on a resource. There are two action-attributes which define the **possession** of the resource: _own_ and _any_.\n\nFor example, an `admin` role can `create`, `read`, `update` or `delete` (CRUD) **any** `account` resource. But a `user` role might only `read` or `update` its **own** `account` resource.\n\n\u003ctable\u003e\n    \u003cthead\u003e\n        \u003ctr\u003e\n            \u003cth\u003eAction\u003c/th\u003e\n            \u003cth\u003ePossession\u003c/th\u003e\n        \u003c/tr\u003e\n    \u003c/thead\u003e\n    \u003ctbody\u003e\n        \u003ctr\u003e\n            \u003ctd rowspan=\"2\"\u003e\n            \u003cb\u003eC\u003c/b\u003ereate\u003cbr /\u003e\n            \u003cb\u003eR\u003c/b\u003eead\u003cbr /\u003e\n            \u003cb\u003eU\u003c/b\u003epdate\u003cbr /\u003e\n            \u003cb\u003eD\u003c/b\u003eelete\u003cbr /\u003e\n            \u003c/td\u003e\n            \u003ctd\u003eOwn\u003c/td\u003e\n            \u003ctd\u003eThe C|R|U|D action is (or not) to be performed on own resource(s) of the current subject.\u003c/td\u003e\n        \u003c/tr\u003e\n        \u003ctr\u003e\n            \u003ctd\u003eAny\u003c/td\u003e\n            \u003ctd\u003eThe C|R|U|D action is (or not) to be performed on any resource(s); including own.\u003c/td\u003e\n        \u003c/tr\u003e   \n    \u003c/tbody\u003e\n\u003c/table\u003e\n\n```typescript\nac.grant('role').readOwn('resource');\nac.deny('role').deleteAny('resource');\n```\n\n_Note that **own** requires you to also check for the actual possession._\n\n## Checking Permissions\n\nYou can call `.can(\u003crole\u003e).\u003caction\u003e(\u003cresource\u003e)` on an `AccessControl` instance to check for granted permissions for a specific resource and action.\n\n```typescript\nconst permission = ac.can('user').readOwn('account');\npermission.granted; // true\n```\n\n## Defining All Grants at Once\n\nYou can pass the grants directly to the `AccessControl` constructor.\nIt accepts either an `Object`:\n\n```typescript\n// This is actually how the grants are maintained internally.\nlet grantsObject = {\n  admin: {\n    video: {\n      'create:any': ['*'],\n      'read:any': ['*'],\n      'update:any': ['*'],\n      'delete:any': ['*'],\n    },\n  },\n  user: {\n    video: {\n      'create:own': ['*'],\n      'read:own': ['*'],\n      'update:own': ['*'],\n      'delete:own': ['*'],\n    },\n  },\n};\nconst ac = new AccessControl(grantsObject);\n```\n\n... or an `Array` (useful when fetched from a database):\n\n```typescript\n// grant list fetched from DB (to be converted to a valid grants object, internally)\nlet grantList = [\n  { role: 'admin', resource: 'video', action: 'create:any', attributes: '*' },\n  { role: 'admin', resource: 'video', action: 'read:any', attributes: '*' },\n  { role: 'admin', resource: 'video', action: 'update:any', attributes: '*' },\n  { role: 'admin', resource: 'video', action: 'delete:any', attributes: '*' },\n\n  { role: 'user', resource: 'video', action: 'create:own', attributes: '*' },\n  { role: 'user', resource: 'video', action: 'read:any', attributes: '*' },\n  { role: 'user', resource: 'video', action: 'update:own', attributes: '*' },\n  { role: 'user', resource: 'video', action: 'delete:own', attributes: '*' },\n];\nconst ac = new AccessControl(grantList);\n```\n\nYou can set grants any time...\n\n```typescript\nconst ac = new AccessControl();\nac.setGrants(grantsObject);\nconsole.log(ac.getGrants());\n```\n\n## Contribution Guidelines\n\nNever commit directly to master, create a new branch and submit a pull request.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminddocdev%2Faccesscontrol","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fminddocdev%2Faccesscontrol","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminddocdev%2Faccesscontrol/lists"}