File

src/iam/policy/policy.controller.ts

Prefix

policy

Index

Methods

Methods

Async createPolicy
createPolicy(policy: CreatePolicyDto, request)
Decorators :
@Post()
Parameters :
Name Type Optional
policy CreatePolicyDto No
request No
Returns : unknown
Async deletePolicy
deletePolicy(name: string, request)
Decorators :
@Delete(':name')
Parameters :
Name Type Optional
name string No
request No
Returns : Promise<Policy>
getTuroPermissions
getTuroPermissions(req)
Decorators :
@Get('turo-permissions')

this function gets list of policies attached to user role

Parameters :
Name Optional Description
req No

Request header and body

Returns : Promise<object>
getUserPolicies
getUserPolicies(req)
Decorators :
@Get()

this function gets list of policies attached to user role

Parameters :
Name Optional Description
req No

Request header and body

Returns : Promise<object>
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  ValidationPipe,
  Request,
  UnauthorizedException,
} from "@nestjs/common";
import { ApiBearerAuth } from "@nestjs/swagger";
import { CreatePolicyDto } from "./dto/create-policy.dto";
import { PolicyService } from "./policy.service";
import { Policy } from "./types/policy.interface";
import { IAM_CREATE_POLICY_ACTION, IAM_DELETE_POLICY_ACTION, IAM_RESOURCE_NAME } from "../iam.constants";

@ApiBearerAuth("access-token") // this appends authorization header for swagger UI
@Controller("policy")
export class PolicyController {
  constructor(private readonly policyService: PolicyService) {}

  @Post()
  async createPolicy(
    @Body(ValidationPipe) policy: CreatePolicyDto,
    @Request() request,
  ) {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        0,
        IAM_RESOURCE_NAME,
        IAM_CREATE_POLICY_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }

    return this.policyService.createPolicy(policy);
  }

  @Delete(":name")
  async deletePolicy(
    @Param("name") name: string,
    @Request() request,
  ): Promise<Policy> {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        0,
        IAM_RESOURCE_NAME,
        IAM_DELETE_POLICY_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }

    return this.policyService.deletePolicy({ name: name });
  }

  /**
   * this function gets list of policies attached to
   * user role
   * @param req Request header and body
   * @returns {Promise<object>}
   */
  @Get()
  getUserPolicies(@Request() req): Promise<object> {
    return this.policyService.getUserPolicies(req.user.id);
  }

  /**
   * this function gets list of policies attached to
   * user role
   * @param req Request header and body
   * @returns {Promise<object>}
   */
  @Get("turo-permissions")
  getTuroPermissions(@Request() req): Promise<object> {
    return this.policyService.getTuroPermissions(req.user.id);
  }
}

results matching ""

    No results matching ""