File

src/iam/role/role.controller.ts

Prefix

role

Index

Methods

Methods

Async create
create(role: CreateRoleDto, request)
Decorators :
@Post('')
Parameters :
Name Type Optional
role CreateRoleDto No
request No
Returns : Promise<Role>
Async delete
delete(id: number, request)
Decorators :
@Delete(':id')
Parameters :
Name Type Optional
id number No
request No
Returns : Promise<Role>
Async role
role(id: number, request)
Decorators :
@Get(':id')
Parameters :
Name Type Optional
id number No
request No
Returns : Promise<Role>
Async roles
roles(request)
Decorators :
@Get('')
Parameters :
Name Optional
request No
Returns : Promise<Role[]>
Async update
update(id: number, role: CreateRoleDto, request)
Decorators :
@Post(':id')
Parameters :
Name Type Optional
id number No
role CreateRoleDto No
request No
Returns : Promise<Role>
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Post,
  Request,
  UnauthorizedException,
  ValidationPipe,
} from "@nestjs/common";
import { ApiBearerAuth } from "@nestjs/swagger";
import { CreateRoleDto } from "./dto/create-role.dto";
import { Role } from "./entity/role.entity";
import { RoleService } from "./role.service";
import { PolicyService } from "../policy/policy.service";
import { IAM_CREATE_ROLE_ACTION, IAM_DELETE_ROLE_ACTION, IAM_READ_ROLE_ACTION, IAM_RESOURCE_NAME, IAM_UPDATE_ROLE_ACTION } from "../iam.constants";

@ApiBearerAuth("access-token") // this appends authorization header for swagger UI
@Controller("role")
export class RoleController {
  constructor(
    private readonly roleService: RoleService,
    private readonly policyService: PolicyService,
  ) {}

  // create role
  @Post("")
  async create(
    @Body(ValidationPipe) role: CreateRoleDto,
    @Request() request,
  ): Promise<Role> {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        0,
        IAM_RESOURCE_NAME,
        IAM_CREATE_ROLE_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }

    return this.roleService.create(role);
  }

  // read role by id
  @Get(":id")
  async role(
    @Param("id", new ParseIntPipe()) id: number,
    @Request() request,
  ): Promise<Role> {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        0,
        IAM_RESOURCE_NAME,
        IAM_READ_ROLE_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }

    return this.roleService.role(id);
  }

  // read all roles
  @Get("")
  async roles(@Request() request): Promise<Role[]> {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        0,
        IAM_RESOURCE_NAME,
        IAM_READ_ROLE_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }

    return this.roleService.roles();
  }

  //update role
  @Post(":id")
  async update(
    @Param("id", new ParseIntPipe()) id: number,
    @Body(ValidationPipe) role: CreateRoleDto,
    @Request() request,
  ): Promise<Role> {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        0,
        IAM_RESOURCE_NAME,
        IAM_UPDATE_ROLE_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }

    return this.roleService.update(id, role);
  }

  //delete role
  @Delete(":id")
  async delete(
    @Param("id", new ParseIntPipe()) id: number,
    @Request() request,
  ): Promise<Role> {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        0,
        IAM_RESOURCE_NAME,
        IAM_DELETE_ROLE_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }

    return this.roleService.delete(id);
  }
}

results matching ""

    No results matching ""