Skip to content

Commit

Permalink
Merge 1b07a27 into a0b206e
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Juna committed Nov 13, 2019
2 parents a0b206e + 1b07a27 commit f20869f
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 49 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ typings/
# next.js build output
.next

dist
dist

.vscode
19 changes: 10 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
language: node_js
node_js:
- "8"
- "10"
- '8'
- '10'
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- export DISPLAY=:99.0
services:
- xvfb
install:
- npm install
- gulp build
- npm install
- gulp build
script:
- npm test
- npm test
after_success:
- npm run test:coverage
- "npm install coveralls && cat ./coverage/lcov.info | coveralls"
- npm run test:coverage
- 'npm install coveralls && cat ./coverage/lcov.info | coveralls'
8 changes: 4 additions & 4 deletions lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ First we introduced two new decorators, actually they are three, but let's see w
for more information about the structure of roles see `roles.interface.ts` file or read the original [documentation](https://onury.io/accesscontrol/) form `accesscontrol` library [here](https://onury.io/accesscontrol/?api=ac#AccessControl~IQueryInfo) .
> \*note: for those who are asking what ACGuard stands for, it of course stands for Access Control Guard :smile:
- `UserRoles(<prop>)`: if you want to get access to the user roles directly, maybe you want to check it's roles manually instead of `ACGuard` doing it for you, then that decorator what you are looking for.
the decorator it really simple, it just return the `req.user.roles` value from the `request` object, but wait, what if the user roles isn't exist in `prop: role` , we know that you will ask this question, so that You can pass an optional property key to the decorator to get it from the user object e.g `@UserRoles('permissions')` will return the `req.user.permissions` instead.
- `UserRoles(<prop>)`: if you want to get access to the user roles directly, maybe you want to check it's roles manually instead of `ACGuard` doing it for you, then that decorator is what you are looking for.
the decorator it really simple, it just return the `req.user.roles` value from the `request` object, but wait, what if the user roles isn't exist in `prop: role`, we know that you will ask this question, so that You can pass an optional property key to the decorator to get it from the user object e.g `@UserRoles('permissions')` will return the `req.user.permissions` instead.

- `@InjectRolesBuilder()`: If you hate the `ACGuard` - _imo it's a good guard_ - and want to build your own Guard instead, you will likely need to access to the underlying `RolesBuilder` Object , then that decorator for you, it will inject the `Roles` you have defined before, i.e the object passed to the `AccessControlModule.forRoles(roles)`.
- `@InjectRolesBuilder()`: If you hate the `ACGuard` - _imo it's a good guard_ - and want to build your own Guard instead, you will likely need to access to the underlying `RolesBuilder` Object , then that decorator is for you, it will inject the `Roles` you have defined before, i.e the object passed to the `AccessControlModule.forRoles(roles)`.

4. are you still there ? Ok, that's it, you can go and run the application now, but wait, did someone asked for the `AuthGuard` ?
Ok let's discuss the **LIMITATIONS**
Expand All @@ -162,7 +162,7 @@ you are welcome with this project for contributing, just make a PR

* **Shady Khalifa** - _Initial work_

See also the list of [contributors](https://github.com/shekohex/nest-access-control/contributors) who participated in this project.
See also the list of [contributors](https://github.com/nestjsx/nest-access-control/contributors) who participated in this project.

## License

Expand Down
6 changes: 6 additions & 0 deletions lib/ac-options.interface.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface ACOptions {
/**
* A name of endpoint which serves grants
*/
grantsEndpoint?: string;
}
2 changes: 2 additions & 0 deletions lib/ac-options.interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
11 changes: 7 additions & 4 deletions lib/access-control.module.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { DynamicModule, Provider } from '@nestjs/common';
import { DynamicModule, Type, ForwardReference, Abstract } from '@nestjs/common';
import { RolesBuilder } from './roles-builder.class';
import { ACOptions } from './ac-options.interface';
export declare class AccessControlModule {
/**
* Register a pre-defined roles
* @param {RolesBuilder} roles A list containing the access grant
* @param {ACOptions} options A configurable options
* definitions. See the structure of this object in the examples.
*/
static forRoles(roles: RolesBuilder): DynamicModule;
static forRoles(roles: RolesBuilder, options?: ACOptions): DynamicModule;
static forRootAsync(options: {
inject?: Provider[];
useFactory: (...args: any) => RolesBuilder;
imports?: Array<Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference>;
inject?: Array<Type<any> | string | symbol | Abstract<any> | Function>;
useFactory: (...args: any) => RolesBuilder | Promise<RolesBuilder>;
}): DynamicModule;
}
28 changes: 17 additions & 11 deletions lib/access-control.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,50 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
Object.defineProperty(exports, "__esModule", { value: true });
var AccessControlModule_1;
const common_1 = require("@nestjs/common");
const constants_1 = require("./constants");
const constants_1 = require("@nestjs/common/constants");
const constants_2 = require("./constants");
const grants_controller_1 = require("./grants.controller");
let AccessControlModule = AccessControlModule_1 = class AccessControlModule {
/**
* Register a pre-defined roles
* @param {RolesBuilder} roles A list containing the access grant
* @param {ACOptions} options A configurable options
* definitions. See the structure of this object in the examples.
*/
static forRoles(roles) {
static forRoles(roles, options) {
let controllers = [];
if (options) {
Reflect.defineMetadata(constants_1.PATH_METADATA, options.grantsEndpoint, grants_controller_1.GrantsController);
controllers = [...(options.grantsEndpoint ? [grants_controller_1.GrantsController] : [])];
}
return {
module: AccessControlModule_1,
controllers: [...controllers],
providers: [
{
provide: constants_1.ROLES_BUILDER_TOKEN,
provide: constants_2.ROLES_BUILDER_TOKEN,
useValue: roles,
},
],
exports: [
{
provide: constants_1.ROLES_BUILDER_TOKEN,
provide: constants_2.ROLES_BUILDER_TOKEN,
useValue: roles,
},
],
};
}
static forRootAsync(options) {
const provider = {
provide: constants_1.ROLES_BUILDER_TOKEN,
provide: constants_2.ROLES_BUILDER_TOKEN,
useFactory: options.useFactory,
inject: options.inject || [],
};
return {
imports: [...(options.imports || [])],
module: AccessControlModule_1,
providers: [
provider,
],
exports: [
provider,
],
providers: [provider],
exports: [provider],
};
}
};
Expand Down
6 changes: 6 additions & 0 deletions lib/grants.controller.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RolesBuilder } from './roles-builder.class';
export declare class GrantsController {
private readonly roleBuilder;
constructor(roleBuilder: RolesBuilder);
getGrants(): any;
}
37 changes: 37 additions & 0 deletions lib/grants.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common");
const inject_roles_builder_decorator_1 = require("./decorators/inject-roles-builder.decorator");
const roles_builder_class_1 = require("./roles-builder.class");
let GrantsController = class GrantsController {
constructor(roleBuilder) {
this.roleBuilder = roleBuilder;
}
getGrants() {
return this.roleBuilder.getGrants();
}
};
__decorate([
common_1.Get(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], GrantsController.prototype, "getGrants", null);
GrantsController = __decorate([
common_1.Controller(),
__param(0, inject_roles_builder_decorator_1.InjectRolesBuilder()),
__metadata("design:paramtypes", [roles_builder_class_1.RolesBuilder])
], GrantsController);
exports.GrantsController = GrantsController;
6 changes: 3 additions & 3 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/shekohex/nest-access-control.git"
"url": "git+https://github.com/nestjsx/nest-access-control.git"
},
"keywords": [
"nestjs",
Expand Down Expand Up @@ -43,9 +43,9 @@
"typescript": "^3.2"
},
"bugs": {
"url": "https://github.com/shekohex/nest-access-control/issues"
"url": "https://github.com/nestjsx/nest-access-control/issues"
},
"homepage": "https://github.com/shekohex/nest-access-control#readme",
"homepage": "https://github.com/nestjsx/nest-access-control#readme",
"dependencies": {
"accesscontrol": "^2.2.1"
}
Expand Down
26 changes: 9 additions & 17 deletions src/access-control.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module, DynamicModule, Global, Abstract, Type } from '@nestjs/common';
import { Module, DynamicModule, Global, Type, ForwardReference, Abstract } from '@nestjs/common';
import { PATH_METADATA } from '@nestjs/common/constants';
import { RolesBuilder } from './roles-builder.class';
import { ROLES_BUILDER_TOKEN } from './constants';
Expand All @@ -15,21 +15,16 @@ export class AccessControlModule {
* definitions. See the structure of this object in the examples.
*/
public static forRoles(roles: RolesBuilder, options?: ACOptions): DynamicModule {

let controllers = [];

if (options) {
Reflect.defineMetadata(PATH_METADATA, options.grantsEndpoint, GrantsController);
controllers = [
...options.grantsEndpoint ? [GrantsController] : [],
];
controllers = [...(options.grantsEndpoint ? [GrantsController] : [])];
}

return {
module: AccessControlModule,
controllers: [
...controllers,
],
controllers: [...controllers],
providers: [
{
provide: ROLES_BUILDER_TOKEN,
Expand All @@ -46,24 +41,21 @@ export class AccessControlModule {
}

public static forRootAsync(options: {
inject?: Array<Type<any> | string | symbol | Abstract<any> | Function>,
useFactory: (...args: any) => RolesBuilder | Promise<RolesBuilder>,
imports?: Array<Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference>;
inject?: Array<Type<any> | string | symbol | Abstract<any> | Function>;
useFactory: (...args: any) => RolesBuilder | Promise<RolesBuilder>;
}): DynamicModule {

const provider = {
provide: ROLES_BUILDER_TOKEN,
useFactory: options.useFactory,
inject: options.inject || [],
};

return {
imports: [...(options.imports || [])],
module: AccessControlModule,
providers: [
provider,
],
exports: [
provider,
],
providers: [provider],
exports: [provider],
};
}
}

0 comments on commit f20869f

Please sign in to comment.