{"id":106277,"date":"2025-04-16T12:59:43","date_gmt":"2025-04-16T08:29:43","guid":{"rendered":"https:\/\/nabfollower.com\/blog\/advanced-role-based-access-control-rbac-in-nestjs-with-a-custom-permission-guard-31ah\/"},"modified":"2025-04-16T12:59:43","modified_gmt":"2025-04-16T08:29:43","slug":"advanced-role-based-access-control-rbac-in-nestjs-with-a-custom-permission-guard-31ah","status":"publish","type":"post","link":"https:\/\/nabfollower.com\/blog\/advanced-role-based-access-control-rbac-in-nestjs-with-a-custom-permission-guard-31ah\/","title":{"rendered":"\u06a9\u0646\u062a\u0631\u0644 \u062f\u0633\u062a\u0631\u0633\u06cc \u0645\u0628\u062a\u0646\u06cc \u0628\u0631 \u0646\u0642\u0634 \u067e\u06cc\u0634\u0631\u0641\u062a\u0647 (RBAC) \u062f\u0631 Nestjs \u0628\u0627 \u06cc\u06a9 \u0646\u06af\u0647\u0628\u0627\u0646 \u0645\u062c\u0648\u0632 \u0633\u0641\u0627\u0631\u0634\u06cc"},"content":{"rendered":"<div data-article-id=\"2410764\" id=\"article-body\">\n<p>\u0627\u062c\u0631\u0627\u06cc \u06a9\u0646\u062a\u0631\u0644 \u062f\u0633\u062a\u0631\u0633\u06cc \u0645\u0628\u062a\u0646\u06cc \u0628\u0631 \u0646\u0642\u0634 \u062f\u0631 \u0628\u0631\u0646\u0627\u0645\u0647 Nestjs \u0634\u0645\u0627\u061f \u062f\u0631 \u0627\u06cc\u0646\u062c\u0627 \u06cc\u06a9 \u0631\u0627\u0647 \u062d\u0644 \u062a\u0645\u06cc\u0632 \u0648 \u0645\u0642\u06cc\u0627\u0633 \u067e\u0630\u06cc\u0631 \u0628\u0627 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 \u06cc\u06a9 \u0645\u062d\u0627\u0641\u0638 \u0645\u062c\u0648\u0632 \u0633\u0641\u0627\u0631\u0634\u06cc \u0648\u062c\u0648\u062f \u062f\u0627\u0631\u062f \u06a9\u0647:<br \/>\u2705 \u0627\u0632 \u0645\u0633\u06cc\u0631\u0647\u0627\u06cc public () \u067e\u0634\u062a\u06cc\u0628\u0627\u0646\u06cc \u0645\u06cc \u06a9\u0646\u062f<br \/>\u2705 \u0645\u062c\u0648\u0632\u0647\u0627 \u0631\u0627 \u0627\u0632 \u0637\u0631\u06cc\u0642 \u062f\u06a9\u0648\u0631\u0627\u062a\u0648\u0631 PERMISSION () \u0628\u0631\u0631\u0633\u06cc \u0645\u06cc \u06a9\u0646\u062f<br \/>\u2705 \u0628\u0647 Super_admin \u0627\u062c\u0627\u0632\u0647 \u0645\u06cc \u062f\u0647\u062f \u062a\u0627 \u0647\u0645\u0647 \u0686\u06a9 \u0647\u0627 \u0631\u0627 \u062f\u0648\u0631 \u0628\u0632\u0646\u062f<br \/>\u2705 \u0627\u0632 \u062f\u0633\u062a\u0631\u0633\u06cc Wildcard \u0645\u0627\u0646\u0646\u062f \u06a9\u0627\u0631\u0628\u0631 \u067e\u0634\u062a\u06cc\u0628\u0627\u0646\u06cc \u0645\u06cc \u06a9\u0646\u062f:*<br \/>\ud83d\udd10 \u0645\u062b\u0627\u0644:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>@Permission('user:create')\n@Post('create')\ncreateUser() { ... }\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0631\u0627 \u0648\u0627\u0631\u062f \u06a9\u0646\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>\u062f\u0631 \u06a9\u0646\u062a\u0631\u0644 \u06a9\u0646\u0646\u062f\u0647 \u0647\u0627 \u0686\u06a9 \u0647\u0627\u06cc \u06a9\u062b\u06cc\u0641 \u062f\u06cc\u06af\u0631\u06cc \u0648\u062c\u0648\u062f \u0646\u062f\u0627\u0631\u062f! \u0627\u06cc\u0646 \u0646\u06af\u0647\u0628\u0627\u0646 \u0647\u0645\u0647 \u0686\u06cc\u0632 \u0631\u0627 \u0628\u0647 \u0637\u0648\u0631 \u0645\u0631\u062a\u0628 \u0648 \u06a9\u0627\u0631\u0622\u0645\u062f \u062f\u0631 \u0633\u0637\u062d \u0645\u0633\u06cc\u0631 \u0627\u0646\u062c\u0627\u0645 \u0645\u06cc \u062f\u0647\u062f.<\/p>\n<p>code \u06a9\u062f \u0646\u06af\u0647\u0628\u0627\u0646 \u0645\u062c\u0648\u0632<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>import { Injectable, CanActivate, ExecutionContext } from '@nestjs\/common';\nimport { Reflector } from '@nestjs\/core';\nimport { IS_PUBLIC_KEY } from 'src\/decorators\/public.decorator';\nimport { PERMISSION_KEY } from 'src\/decorators\/Permission.decorator';\nimport { JwtPayload } from 'jsonwebtoken';\nimport { AdministratorRoleEnum } from '@prisma\/client';\n\n@Injectable()\nexport class PermissionGuard implements CanActivate {\n  constructor(private reflector: Reflector) {}\n\n  canActivate(context: ExecutionContext): boolean {\n    const isPublic = this.reflector.get<boolean>(\n      IS_PUBLIC_KEY,\n      context.getHandler(),\n    );\n    if (isPublic) return true;\n\n    const requiredRules = this.reflector.get<string>(\n      PERMISSION_KEY,\n      context.getHandler(),\n    );\n    if (!requiredRules || requiredRules.length === 0) return true;\n\n    const request = context.switchToHttp().getRequest();\n    const user = request.user as JwtPayload;\n\n    if (!user) return false;\n\n    if (user.role === AdministratorRoleEnum.super_admin) return true;\n\n    if (!Array.isArray(user.rules)) return false;\n\n    return requiredRules.every((requiredRule) =&gt; {\n      if (user.rules.includes(requiredRule)) return true;\n      const resource = requiredRule.split(':')?.[0];\n      return user.rules.includes(`${resource}:*`);\n    });\n  }\n}\n<\/string><\/boolean><\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0631\u0627 \u0648\u0627\u0631\u062f \u06a9\u0646\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>\u0627\u06cc\u062f\u0647 \u0622\u0644 \u0628\u0631\u0627\u06cc \u067e\u0627\u0646\u0644 \u0647\u0627\u06cc \u0633\u0631\u067e\u0631\u0633\u062a \u060c \u062f\u0627\u0634\u0628\u0648\u0631\u062f \u06cc\u0627 \u0647\u0631 \u0628\u0631\u0646\u0627\u0645\u0647 \u0627\u06cc \u0628\u0627 \u0686\u0646\u062f\u06cc\u0646 \u0646\u0642\u0634 \u06a9\u0627\u0631\u0628\u0631. \ud83d\udcbc<\/p>\n<p>\ud83d\udc49 RBAC \u062a\u0645\u06cc\u0632 \u060c \u0645\u0642\u06cc\u0627\u0633 \u067e\u0630\u06cc\u0631 \u0648 \u0622\u0645\u0627\u062f\u0647 \u062a\u0648\u0644\u06cc\u062f \u062f\u0631 Nestjs.<\/p>\n<p>\u0628\u0631\u0627\u06cc \u0645\u0637\u0627\u0644\u0639\u0647 \u0628\u06cc\u0634\u062a\u0631 \u0627\u06cc\u0646\u062c\u0627 \u0631\u0627 \u06a9\u0644\u06cc\u06a9 \u06a9\u0646\u06cc\u062f<\/p>\n<p>\u0628\u0627 \u0627\u062d\u062a\u0631\u0627\u0645 \u060c<br \/>NI Rimon<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u0627\u062c\u0631\u0627\u06cc \u06a9\u0646\u062a\u0631\u0644 \u062f\u0633\u062a\u0631\u0633\u06cc \u0645\u0628\u062a\u0646\u06cc \u0628\u0631 \u0646\u0642\u0634 \u062f\u0631 \u0628\u0631\u0646\u0627\u0645\u0647 Nestjs \u0634\u0645\u0627\u061f \u062f\u0631 \u0627\u06cc\u0646\u062c\u0627 \u06cc\u06a9 \u0631\u0627\u0647 \u062d\u0644 \u062a\u0645\u06cc\u0632 \u0648 \u0645\u0642\u06cc\u0627\u0633 \u067e\u0630\u06cc\u0631 \u0628\u0627 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 \u06cc\u06a9 \u0645\u062d\u0627\u0641\u0638 \u0645\u062c\u0648\u0632 \u0633\u0641\u0627\u0631\u0634\u06cc \u0648\u062c\u0648\u062f \u062f\u0627\u0631\u062f \u06a9\u0647:\u2705 \u0627\u0632 \u0645\u0633\u06cc\u0631\u0647\u0627\u06cc public () \u067e\u0634\u062a\u06cc\u0628\u0627\u0646\u06cc \u0645\u06cc \u06a9\u0646\u062f\u2705 \u0645\u062c\u0648\u0632\u0647\u0627 \u0631\u0627 \u0627\u0632 \u0637\u0631\u06cc\u0642 \u062f\u06a9\u0648\u0631\u0627\u062a\u0648\u0631 PERMISSION () \u0628\u0631\u0631\u0633\u06cc \u0645\u06cc \u06a9\u0646\u062f\u2705 \u0628\u0647 Super_admin \u0627\u062c\u0627\u0632\u0647 \u0645\u06cc \u062f\u0647\u062f \u062a\u0627 \u0647\u0645\u0647 \u0686\u06a9 \u0647\u0627 &hellip;<\/p>\n","protected":false},"author":2,"featured_media":106278,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[339],"tags":[],"class_list":["post-106277","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev"],"_links":{"self":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts\/106277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/comments?post=106277"}],"version-history":[{"count":0,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts\/106277\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/media\/106278"}],"wp:attachment":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/media?parent=106277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/categories?post=106277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/tags?post=106277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}