{"id":14975611,"url":"https://github.com/sayburgh-solutions/mongoose-permissions","last_synced_at":"2025-07-21T11:05:34.256Z","repository":{"id":54338372,"uuid":"321372252","full_name":"sayburgh-solutions/mongoose-permissions","owner":"sayburgh-solutions","description":"Mongoose plugin for managing roles and permissions (rbac) in a simpler way.","archived":false,"fork":false,"pushed_at":"2022-10-25T10:26:43.000Z","size":106,"stargazers_count":7,"open_issues_count":1,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-24T05:52:58.312Z","etag":null,"topics":["mongoose-plugin","permissions","rbac"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sayburgh-solutions.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-14T14:24:52.000Z","updated_at":"2022-10-27T03:46:49.000Z","dependencies_parsed_at":"2022-08-13T12:30:48.101Z","dependency_job_id":null,"html_url":"https://github.com/sayburgh-solutions/mongoose-permissions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sayburgh-solutions/mongoose-permissions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sayburgh-solutions%2Fmongoose-permissions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sayburgh-solutions%2Fmongoose-permissions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sayburgh-solutions%2Fmongoose-permissions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sayburgh-solutions%2Fmongoose-permissions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sayburgh-solutions","download_url":"https://codeload.github.com/sayburgh-solutions/mongoose-permissions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sayburgh-solutions%2Fmongoose-permissions/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266287824,"owners_count":23905461,"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":["mongoose-plugin","permissions","rbac"],"created_at":"2024-09-24T13:52:17.055Z","updated_at":"2025-07-21T11:05:34.197Z","avatar_url":"https://github.com/sayburgh-solutions.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-permissions\n\nA simple rabc plugin for `mongoose` that is not over engineered.\n\n## Development Task List\n- [x] Authorization check with `can()` method.\n- [x] Role management with `assignRole()` and `revokeRole()` methods.\n- [x] Direct permission management with `givePermissionTo()` and `revokePermissionTo()` methods.\n- [ ] Check, assign, or revoke multiple permissions in a single function call.\n\n## Installation\nInstallation is as simple as any other `npm` package:\n\n```\n$ npm install mongoose-permissions\n```\n\n## Usage\nYou can attach the permissions plugin to your user schema and have the authorization funtionalities injected.\n\n```js\n// models/user.js\n\n// regular mongoose stuff.\nconst mongoose = require('mongoose');\n\n// require mongoose-permissions in your schema.\nconst permissions = require('mongoose-permissions');\n\nconst User = mongoose.model(\n  'User',\n  new mongoose.Schema(\n    {\n      name: {\n        type: String,\n        require: true,\n      },\n      email: {\n        type: String,\n        require: true,\n      },\n      password: {\n        type: String,\n        require: true,\n      },\n    },\n    { timestamps: true },\n  ).plugin(permissions), // registering the plugin.\n);\n\nmodule.exports = User;\n```\n\n### Roles and Permissions\nIn `mongoose-permissions` permissions are king. The roles are collection of permissions for easy assignment to users. You may store them anywhere you want as long as you're following the proper structure. A role for example needs to be in the following structure:\n\n```js\n\n// roles.js\n\nmodule.exports = [\n    {\n        name: \"Admin\",\n        permissions: [\n            {\n                name: \"create-article\"\n            },\n            {\n                name: \"edit-article\"\n            },\n            {\n                name: \"delete-article\"\n            },\n            {\n                name: \"publish-article\"\n            }\n        ]\n    },\n    {\n        name: \"Editor\",\n        permissions: [\n            {\n                name: \"create-article\"\n            },\n            {\n                name: \"edit-article\"\n            }\n        ]\n    }\n]\n\n```\n\nYou can either save the roles and permissions in your database or you may store them in a json or js file and then import them where necessary.\n\n### Role Management\nFor easily assigning and revoking role from a user there are two methods. They are `assignRole()` and `revokeRole()` methods.\n\n```js\nconst user = await User.findById('5fd7586ab8069d56e77e170e');\n\n// the assignRole() method takes a complete role object as it's input.\n// assigning a new role automatically replaces the old one.\nuser.assignRole({\n        name: \"Editor\",\n        permissions: [\n            {\n                name: \"create-article\"\n            },\n            {\n                name: \"edit-article\"\n            }\n        ]\n    });\n\n// whereas the revokeRole() method takes the role name as an input.\n// revoking a role leaves the selected user with no permissions at all.\nuser.revokeRole(\"Admin\");\n```\n\n### Checking Roles\nThe package comes with a `hasRole()` method for checking if a user has the given permission or not.\n\n```js\nconst user = await User.findById('5fd7586ab8069d56e77e170e');\n\n// the hasRole() method takes the role name as input.\n// the method returns true if the user has the role, false otherwise.\nif (user.hasRole(\"Admin\")) {\n    // necessary logic goes here.\n};\n\n```\n\nAlthough checking permissions is advised, you may use this method if necessary.\n\n### Direct Permissions\nIn case you want to assign an extra permission to a user that doesn't exist in their role. For that purpose there are `givePermissionTo()` and `revokePermissionTo()` methods.\n\n```js\nconst user = await User.findById('5fd7586ab8069d56e77e170e');\n\n// the givePermissionTo() method takes a permission name it's input.\nuser.givePermissionTo(\"publish-article\");\n\n// the revokePermissionTo() method takes a permission name it's input as well.\nuser.revokePermissionTo(\"publish-article\");\n```\n\nDirect permissions are stored separately from the role permissions so even if you change the user's role, direct permissions will remain the same.\n\n### Checking Permissions\nThe package comes with a very handy `can()` method for checking permissions on a user.\n\n```js\nconst user = await User.findById('5fd7586ab8069d56e77e170e');\n\n// the can() methods takes the permission name as input.\n// the method returns true if the user has the permission, false otherwise.\nif (user.can(\"edit-articles\")) {\n    // article editing logic goes here.\n};\n\n```\n\nThe `can()` method checks in roles permissions as well as direct permissions.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsayburgh-solutions%2Fmongoose-permissions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsayburgh-solutions%2Fmongoose-permissions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsayburgh-solutions%2Fmongoose-permissions/lists"}