{"id":13651368,"url":"https://github.com/Embraser01/moleculer-middleware-permissions","last_synced_at":"2025-04-22T22:31:06.976Z","repository":{"id":46930551,"uuid":"158300013","full_name":"Embraser01/moleculer-middleware-permissions","owner":"Embraser01","description":"Moleculer middleware to check for a set of permissions","archived":true,"fork":false,"pushed_at":"2021-09-21T08:15:42.000Z","size":775,"stargazers_count":13,"open_issues_count":10,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-09-17T14:21:01.164Z","etag":null,"topics":["jwt","middleware","moleculer","moleculerjs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/Embraser01.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}},"created_at":"2018-11-19T22:47:56.000Z","updated_at":"2023-01-28T06:25:05.000Z","dependencies_parsed_at":"2022-09-26T18:41:39.164Z","dependency_job_id":null,"html_url":"https://github.com/Embraser01/moleculer-middleware-permissions","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Embraser01%2Fmoleculer-middleware-permissions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Embraser01%2Fmoleculer-middleware-permissions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Embraser01%2Fmoleculer-middleware-permissions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Embraser01%2Fmoleculer-middleware-permissions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Embraser01","download_url":"https://codeload.github.com/Embraser01/moleculer-middleware-permissions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223906229,"owners_count":17223045,"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":["jwt","middleware","moleculer","moleculerjs"],"created_at":"2024-08-02T02:00:48.657Z","updated_at":"2024-11-10T02:30:49.636Z","avatar_url":"https://github.com/Embraser01.png","language":"JavaScript","funding_links":[],"categories":["Middlewares"],"sub_categories":["Security"],"readme":"# Moleculer Middleware Permissions\n\nCheck action permissions.\n\n[![codecov](https://codecov.io/gh/Embraser01/moleculer-middleware-permissions/branch/master/graph/badge.svg)](https://codecov.io/gh/Embraser01/moleculer-middleware-permissions)\n[![Travis (.com)](https://img.shields.io/travis/com/Embraser01/moleculer-middleware-permissions.svg)](https://github.com/Embraser01/moleculer-middleware-permissions)\n![NpmLicense](https://img.shields.io/npm/l/moleculer-middleware-permissions.svg)\n![npm](https://img.shields.io/npm/v/moleculer-middleware-permissions.svg)\n![node](https://img.shields.io/node/v/moleculer-middleware-permissions.svg)\n\n\n## Install\n\n\u003e This module requires at least Node v8.3.0.\n\n```bash\nyarn add moleculer-middleware-permissions\n```\n\n## Usage\n\n```js\n// moleculer.config.js\nconst PermissionGuard = require('moleculer-middleware-permissions');\n\nconst guard = new PermissionGuard({options});\n\nmodule.exports = {\n    ...\n    middlewares: [\n        guard.middleware(),\n    ],\n};\n```\n\n```js\n// service.js\nmodule.exports = {\n  name: 'awesome.service',\n   actions: {\n       hello: {\n         // The user must have both 'hello:read' AND 'hello:name'\n         // You can override this behaviour by passing your 'checkFunction'\n         permissions: ['hello.read', '$owner', (ctx) =\u003e ctx.call('acl.canSayHello')],\n         handler (ctx) {\n           const {name} = ctx.params;\n           return `Hello ${name}`;\n         }\n       },\n       me: {\n          // Will check for these permissions: ['awesome.service.me']\n          permissions: true,\n          handler (ctx) {\n            return `Hello me`;\n          }\n        }\n     }\n};\n```\n\n## Options\n\n- `checkFunction(current, requested)`: A function that return `true` if the request has enough\n  permissions. Else, the return value will be send in the rejected `PermissionError`.\n- `getPermissionsFromAction(action)`: Called to return an array of permissions from an action.\n- `getUserPermissions(ctx)`: Function called to retrieve user's permissions. By default will\n  return `meta.user.permissions`.\n\n## Permissions type\n\n### A string\n\nThe simplest way to add permissions is to use a list of strings, representing each a\npermissions, like this:\n- `members.read`: Can list/get/find members\n- `members.write`: Can update/remove/create members\n\nIt will be checked before any functions and if it allows to access, function **will not** be\nchecked!\n\n### `$owner`\n\nIf you want the owner of the entity to be able to update it but not other ones, you can use this\nspecial permissions. It will try to call the method `isEntityOwner(ctx)` of your service.\nReturning a truthy value will act as allowed.\n\nThis method can be async.\n\n### A function\n\nYou can also provide functions to check if the user is allowed to access an action. It will be\ncalled only if strings aren't allowed first. Only one function needs to return a truthy value to\nbe allowed!\n\nThis method can be async.\n\n\u003e You can override this behaviour by overriding the `check` method the class.\n\n# Notes\n\nThe v2 was inspired by @icebob's [kantab](https://github.com/icebob/kantab/) project.\n\n# License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEmbraser01%2Fmoleculer-middleware-permissions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEmbraser01%2Fmoleculer-middleware-permissions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEmbraser01%2Fmoleculer-middleware-permissions/lists"}