src/iam/role/role.controller.ts
role
Methods |
| Async create | |||||||||
create(role: CreateRoleDto, request)
|
|||||||||
Decorators :
@Post('')
|
|||||||||
|
Defined in src/iam/role/role.controller.ts:30
|
|||||||||
|
Parameters :
Returns :
Promise<Role>
|
| Async delete | |||||||||
delete(id: number, request)
|
|||||||||
Decorators :
@Delete(':id')
|
|||||||||
|
Defined in src/iam/role/role.controller.ts:108
|
|||||||||
|
Parameters :
Returns :
Promise<Role>
|
| Async role | |||||||||
role(id: number, request)
|
|||||||||
Decorators :
@Get(':id')
|
|||||||||
|
Defined in src/iam/role/role.controller.ts:50
|
|||||||||
|
Parameters :
Returns :
Promise<Role>
|
| Async roles | ||||
roles(request)
|
||||
Decorators :
@Get('')
|
||||
|
Defined in src/iam/role/role.controller.ts:70
|
||||
|
Parameters :
Returns :
Promise<Role[]>
|
| Async update | ||||||||||||
update(id: number, role: CreateRoleDto, request)
|
||||||||||||
Decorators :
@Post(':id')
|
||||||||||||
|
Defined in src/iam/role/role.controller.ts:87
|
||||||||||||
|
Parameters :
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);
}
}