npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fw-rolepermission

v1.0.3

Published

Nest JS FW packages

Downloads

11

Readme

Description

Nest JS based Permission based authorization module

installation

npm install --save nestjs-rolepermission

Usage

1. Module configuration

import {RolePermissionModule} from "nestjs-rolepermission"
@Module({
  imports: [
    RolePermissionModule.forRootAsync(RolePermissionDataService),
  ],
  controllers: []
})
export class AppModule {}

2. implement RolePermissionDataService

This service responsible to provide role and permission data to the package in RolePermissionData format. Developer will implement this service using IRolePermissionDataService pull the role prmission data from external source or configure static data in RolePermissionData under getRolePermissionData() method. please follow the code snipt below.

import { Injectable } from "@nestjs/common";
import { IRolePermissionDataService, RolePermissionData } from "nestjs-rolepermissions";

@Injectable()
export class RolePermissionDataStorageService
  implements IRolePermissionDataService {
  getRolePermissionData(): RolePermissionData {
    return {
      roles: ["admin"],
      permissions: ["addadmin", "one", "two", "three"],
      mapping: [
        {
          role: "admin",
          permissions: ["addadmin"],
        },
      ],
    };
  }
}

RolePermissionData

export interface RolePermissionData {
  roles: string[];
  permissions: string[];
  mapping: RolePermissionMapping[];
}

RolePermissionData consists of the following keys:

  • roles: string array of roles.
  • permissions: string array of permissions.
  • mapping: array of RolePermissionMapping objects.

3. Setup guard to check authorizatioin

Authorization Controller level

For implementing permission authorization globally for all methods of the controller then instead of defining at method level just declare RolePermissionGuard at controller and define permission like in snipt below.

import { RolePermissionGuard, RolePermissions } from "fw-permissions";
@Controller("user")
@UseGuards(RolePermissionGuard)
@RolePermissions("one")
export class UserController {
    @UseGuards(RolePermissionGuard) // permission guard
    @RolePermissions("addadmin") // permissions array. action executes only if role has the permission. 
    @Get("/index")
  index() {
    return "Welcome to Pooja Loyalty";
  }
}

Authorization method level For implementing permission authorization only to sepefic action then declare RolePermissionGuard and RolePermissions like in code snippet below.

import { RolePermissionGuard, RolePermissions } from "fw-permissions";
@Controller("user")
export class UserController {
    @UseGuards(RolePermissionGuard) // permission guard
    @RolePermissions("addadmin") // permissions array. action executes only if role has the permission. 
    @Get("/index")
  index() {
    return "Welcome to Pooja Loyalty";
  }
}