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

Format with prettier #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ac-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export interface ACOptions {
* A name of endpoint which serves grants
*/
grantsEndpoint?: string;
}
}
2 changes: 1 addition & 1 deletion src/access-control.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ACGuard<User extends any = any> implements CanActivate {
}

const userRoles = await this.getUserRoles(context);
const hasRoles = roles.every(role => {
const hasRoles = roles.every((role) => {
const queryInfo: IQueryInfo = role;
queryInfo.role = userRoles;
const permission = this.roleBuilder.permission(queryInfo);
Expand Down
33 changes: 9 additions & 24 deletions src/access-control.module.async.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {AccessControlModule} from './access-control.module';
import {Test, TestingModule} from '@nestjs/testing';
import { AccessControlModule } from './access-control.module';
import { Test, TestingModule } from '@nestjs/testing';
import { RolesBuilder } from './roles-builder.class';
import {ROLES_BUILDER_TOKEN} from './constants';
import { ROLES_BUILDER_TOKEN } from './constants';
import { delay } from 'rxjs/operators';
import { GrantsController } from './grants.controller';
import { ACOptions } from './ac-options.interface';
import { Injectable, Module } from '@nestjs/common';

describe('forRootAsync', () => {
it('Can instance with provider method', async () => {

const module: TestingModule = await Test.createTestingModule({
imports: [
AccessControlModule.forRootAsync({
Expand All @@ -24,7 +23,6 @@ describe('forRootAsync', () => {
});

it('Can instance with asnyc provider method', async () => {

const module: TestingModule = await Test.createTestingModule({
imports: [
AccessControlModule.forRootAsync({
Expand All @@ -42,18 +40,14 @@ describe('forRootAsync', () => {
});

it('Can inject a provider', async () => {

@Injectable()
class TestProvider {
}
class TestProvider {}

@Module({
providers: [TestProvider],
exports: [TestProvider],
})
class TestModule {

}
class TestModule {}

const module: TestingModule = await Test.createTestingModule({
imports: [
Expand All @@ -76,14 +70,11 @@ describe('forRootAsync', () => {

describe('forRoles', () => {
it('Expose <grantsEndpoint> when options is provided', async () => {

const roles: RolesBuilder = new RolesBuilder();
const options: ACOptions = { grantsEndpoint: 'grants'};
const options: ACOptions = { grantsEndpoint: 'grants' };

const module: TestingModule = await Test.createTestingModule({
imports: [
AccessControlModule.forRoles(roles, options),
],
imports: [AccessControlModule.forRoles(roles, options)],
}).compile();

const controller = module.get<GrantsController>(GrantsController);
Expand All @@ -93,14 +84,11 @@ describe('forRoles', () => {
});

it('Do not expose <grantsEndpoint> when options with no <grantsEndpoint> provided', async () => {

const roles: RolesBuilder = new RolesBuilder();
const options: ACOptions = {};

const module: TestingModule = await Test.createTestingModule({
imports: [
AccessControlModule.forRoles(roles, options),
],
imports: [AccessControlModule.forRoles(roles, options)],
}).compile();

expect(() => {
Expand All @@ -109,13 +97,10 @@ describe('forRoles', () => {
});

it('Do not expose <grantsEndpoint> when options is not provided', async () => {

const roles: RolesBuilder = new RolesBuilder();

const module: TestingModule = await Test.createTestingModule({
imports: [
AccessControlModule.forRoles(roles),
],
imports: [AccessControlModule.forRoles(roles)],
}).compile();

expect(() => {
Expand Down
8 changes: 2 additions & 6 deletions src/access-control.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,12 @@ export class AccessControlModule {

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

return {
imports: [...(options.imports || [])],
controllers: [
...controllers,
],
controllers: [...controllers],
module: AccessControlModule,
providers: [provider],
exports: [provider],
Expand Down
3 changes: 1 addition & 2 deletions src/decorators/user-roles.decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { createParamDecorator, ExecutionContext } from '@nestjs/common';
* 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.
*/
export const UserRoles = createParamDecorator((data: string, ctx: ExecutionContext) => {

export const UserRoles = createParamDecorator((data: string, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return data ? request.user[data] : request.user.roles;
});
6 changes: 1 addition & 5 deletions src/grants.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ import { RolesBuilder } from './roles-builder.class';
import { ROLES_BUILDER_TOKEN } from './constants';

describe('Grants Controller #getGrants', () => {

let controller: GrantsController;
const roles: RolesBuilder = new RolesBuilder();

beforeEach(async () => {

Reflect.defineMetadata(PATH_METADATA, 'grants', GrantsController);

const module: TestingModule = await Test.createTestingModule({
controllers: [
GrantsController,
],
controllers: [GrantsController],
providers: [
{
provide: ROLES_BUILDER_TOKEN,
Expand Down
7 changes: 2 additions & 5 deletions src/grants.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import { RolesBuilder } from './roles-builder.class';

@Controller()
export class GrantsController {

constructor(
@InjectRolesBuilder() private readonly roleBuilder: RolesBuilder,
) {}
constructor(@InjectRolesBuilder() private readonly roleBuilder: RolesBuilder) {}

@Get()
public getGrants() {
return this.roleBuilder.getGrants();
}
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export * from './access-control.module';
export * from './roles-builder.class';
export * from './role.interface';
export * from './access-control.guard';
export { ROLES_BUILDER_TOKEN } from './constants'
export { ROLES_BUILDER_TOKEN } from './constants';