Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve DX & refactor CI #161

Merged
merged 13 commits into from
Jul 20, 2023
26 changes: 26 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
parser: '@typescript-eslint/parser',
ignorePatterns: ['.eslintrc.js', '**/dist/**'],
plugins: ['@typescript-eslint/eslint-plugin'],
extends: ['plugin:@typescript-eslint/recommended'],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'no-extra-semi': 'off',
"dot-notation": "off",
"import/order": "off",
"max-len": [2, { "code": 150 }],
"no-useless-constructor": "off",
"@typescript-eslint/no-empty-interface": "off",
"arrow-parens": "off",
"sort-keys": "off",
"comma-dangle": "off"
}
}
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ name: Node.js CI

on:
push:
branches:
- master

pull_request:
branches:
- master

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version: [^14, ^16, ^18]
node-version: [^16, ^18]

steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 0 additions & 3 deletions .prettierrc

This file was deleted.

4 changes: 4 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('prettier').Config} */
module.exports = {
"singleQuote": true,
}
172 changes: 0 additions & 172 deletions jest.config.js

This file was deleted.

23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nest-authz",
"version": "2.4.0",
"description": "基于 node-casbin 实现的 RBAC 权限控制模块。",
"description": "Nest authorization middleware based on Node-Casbin",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"engines": {
Expand All @@ -17,10 +17,10 @@
"prepublish": "yarn lint && yarn build",
"style": "prettier --check \"src/**/*.ts\"",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json -c tslint.json",
"lint": "eslint --cache .",
"release": "standard-version",
"test": "jest",
"coverage": "jest --coverage",
"test": "vitest",
"coverage": "vitest --coverage",
"typecheck": "tsc --noEmit"
},
"keywords": [],
Expand All @@ -32,20 +32,23 @@
"devDependencies": {
"@nestjs/common": "^9.0.3",
"@nestjs/core": "^9.0.3",
"@types/jest": "24.9.0",
"@types/eslint": "^8.44.0",
"@types/node": "11.15.50",
"@typescript-eslint/eslint-plugin": "^6.1.0",
"@typescript-eslint/parser": "^6.1.0",
"@vitest/coverage-v8": "^0.33.0",
"commitizen": "4.2.4",
"cz-conventional-changelog": "3.3.0",
"jest": "24.9.0",
"prettier": "1.19.1",
"eslint": "^8.45.0",
"eslint-plugin-prettier": "^5.0.0",
"prettier": "^3.0.0",
"reflect-metadata": "0.1.13",
"rimraf": "3.0.1",
"rxjs": "^6.0.0",
"standard-version": "8.0.2",
"ts-jest": "24.3.0",
"ts-node": "10.0.0",
"tslint": "5.11.0",
"typescript": "^5.0.0"
"typescript": "^5.0.0",
"vitest": "^0.33.0"
},
"peerDependencies": {
"@nestjs/common": "^9.0.3 || ^10.0.0",
Expand Down
6 changes: 3 additions & 3 deletions src/authz.constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const AUTHZ_MODULE_OPTIONS = 'AUTHZ_MODULE_OPTIONS';
export const AUTHZ_ENFORCER = 'AUTHZ_ENFORCER';
export const PERMISSIONS_METADATA = '__PERMISSIONS__';
export const AUTHZ_MODULE_OPTIONS = Symbol('AUTHZ_MODULE_OPTIONS');
export const AUTHZ_ENFORCER = Symbol('AUTHZ_ENFORCER');
export const PERMISSIONS_METADATA = Symbol('__PERMISSIONS__');
32 changes: 17 additions & 15 deletions src/authz.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@ import {
Injectable,
CanActivate,
ExecutionContext,
Inject
Inject,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import {
AUTHZ_ENFORCER,
PERMISSIONS_METADATA,
AUTHZ_MODULE_OPTIONS
AUTHZ_MODULE_OPTIONS,
} from './authz.constants';
import * as casbin from 'casbin';
import { Permission } from './interfaces/permission.interface';
import type { Permission } from './interfaces/permission.interface';
import { UnauthorizedException } from '@nestjs/common';
import { AuthPossession } from './types';
import { AuthZModuleOptions } from './interfaces/authz-module-options.interface';

@Injectable()
export class AuthZGuard implements CanActivate {
constructor(
private readonly reflector: Reflector,
@Inject(AUTHZ_ENFORCER) private enforcer: casbin.Enforcer,
@Inject(AUTHZ_MODULE_OPTIONS) private options: AuthZModuleOptions
) {}
@Inject(AUTHZ_ENFORCER)
private readonly enforcer: casbin.Enforcer;

@Inject(AUTHZ_MODULE_OPTIONS)
private readonly options: AuthZModuleOptions;

constructor(private readonly reflector: Reflector) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
try {
const permissions: Permission[] = this.reflector.get<Permission[]>(
PERMISSIONS_METADATA,
context.getHandler()
context.getHandler(),
);

if (!permissions) {
Expand All @@ -43,7 +45,7 @@ export class AuthZGuard implements CanActivate {

const hasPermission = async (
user: string,
permission: Permission
permission: Permission,
): Promise<boolean> => {
const { possession, resource, action } = permission;
const poss = [];
Expand All @@ -54,9 +56,9 @@ export class AuthZGuard implements CanActivate {
poss.push(possession);
}

return AuthZGuard.asyncSome<AuthPossession>(poss, async p => {
return AuthZGuard.asyncSome<AuthPossession>(poss, async (p) => {
if (p === AuthPossession.OWN) {
return (permission as any).isOwn(context);
return permission.isOwn!(context);
} else {
return this.enforcer.enforce(user, resource, `${action}:${p}`);
}
Expand All @@ -65,7 +67,7 @@ export class AuthZGuard implements CanActivate {

const result = await AuthZGuard.asyncEvery<Permission>(
permissions,
async permission => hasPermission(username, permission)
async (permission) => hasPermission(username, permission),
);

return result;
Expand All @@ -76,7 +78,7 @@ export class AuthZGuard implements CanActivate {

static async asyncSome<T>(
array: T[],
callback: (value: T, index: number, a: T[]) => Promise<boolean>
callback: (value: T, index: number, a: T[]) => Promise<boolean>,
): Promise<boolean> {
for (let i = 0; i < array.length; i++) {
const result = await callback(array[i], i, array);
Expand All @@ -90,7 +92,7 @@ export class AuthZGuard implements CanActivate {

static async asyncEvery<T>(
array: T[],
callback: (value: T, index: number, a: T[]) => Promise<boolean>
callback: (value: T, index: number, a: T[]) => Promise<boolean>,
): Promise<boolean> {
for (let i = 0; i < array.length; i++) {
const result = await callback(array[i], i, array);
Expand Down
Loading
Loading