I want you to act as a Senior Backend developer. I will describe a project details you will code project with this tools: Nest.js, TypeORM. You should merge files in single index.js file and nothing else. Do not write explanations. My first request is "Create App with professional access permissions guards based on user roles with a list a permissions which should look like a array with permission names"
Assistant
Here is the code for creating an app with professional access permissions guards based on user roles using Nest.js and TypeORM. The permissions are stored as an array of permission names.
javascript
User
show me controller example code with using RolesGuard for different types of roles for each route. Do not write explanations
Assistant
Here is an example of a controller that uses the RolesGuard for different types of roles for each route:
javascript
User
show me controller example code with using RolesGuard for the checking of different types of roles and permissions for each route. If permissions matched -> return data -> else -> throw error. Do not write explanations
Assistant
javascript
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This code creates a guard called RolesGuard that checks if the user has the required role for a specific route. The appRoles array contains the list of available roles. The configure method in the AppModule applies the RolesGuard to specific routes.
import { Controller, Get, Post, Put, Delete, UseGuards, Req, Res } from '@nestjs/common';
import { UserService } from './user.service';
import { RolesGuard } from './guards/roles.guard';
import { ROLES } from './roles.enum';
@Controller('users')
export class UserController {
constructor(private readonly userService: UserService) {}
@Get()
@UseGuards(RolesGuard(ROLES.ADMIN))
async findAll() {
return this.userService.findAll();
}
@Get(':id')
@UseGuards(RolesGuard(ROLES.MODERATOR))
async findOne(@Req() req, @Res() res) {
const id = req.params.id;
const user = await this.userService.findOne(id);
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
return user;
}
@Post()
@UseGuards(RolesGuard(ROLES.ADMIN))
async create(@Req() req, @Res() res) {
const user = await this.userService.create(req.body);
return res.status(201).json(user);
}
@Put(':id')
@UseGuards(RolesGuard(ROLES.MODERATOR))
async update(@Req() req, @Res() res, @Param('id') id: number) {
const user = await this.userService.findOne(id);
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
await this.userService.update(id, req.body);
return res.status(200).json({ message: 'User updated successfully' });
}
@Delete(':id')
@UseGuards(RolesGuard(ROLES.ADMIN))
async remove(@Req() req, @Res() res, @Param('id') id: number) {
const user = await this.userService.findOne(id);
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
await this.userService.remove(id);
return res.status(200).json({ message: 'User removed successfully' });
}
}
In this example, the findAll method is only accessible to users with the ROLES.ADMIN role, while the findOne, create, update, and remove methods are only accessible to users with the ROLES.MODERATOR and ROLES.ADMIN roles. The RolesGuard is used to enforce these access restrictions.
import { Controller, Get, Post, Put, Delete, UseGuards, Req, Res } from '@nestjs/common';
import { UserService } from './user.service';
import { RolesGuard } from './guards/roles.guard';
import { PermissionsGuard } from './guards/permissions.guard';
@Controller('users')
export class UserController {
constructor(private readonly userService: UserService) {}
@Get()
@UseGuards(RolesGuard([ROLES.ADMIN, ROLES.MODERATOR]))
async findAll() {
return this.userService.findAll();
}
@Get(':id')
@UseGuards(RolesGuard([ROLES.ADMIN, ROLES.MODERATOR]))
async findOne(@Req() req, @Res() res) {
const id = req.params.id;
const user = await this.userService.findOne(id);
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
return user;
}
@Post()
@UseGuards(RolesGuard([ROLES.ADMIN]))
async create(@Req() req, @Res() res) {
const user = await this.userService.create(req.body);
return res.status(201).json(user);
}
@Put(':id')
@UseGuards(RolesGuard([ROLES.ADMIN, ROLES.MODERATOR]))
async update(@Req() req, @Res() res, @Param('id') id: number) {
const user = await this.userService.findOne(id);
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
await this.userService.update(id, req.body);
return res.status(200).json({ message: 'User updated successfully' });
}
@Delete(':id')
@UseGuards(RolesGuard([ROLES.ADMIN]))
async remove(@Req() req, @Res() res, @Param('id') id: number) {
const user = await this.userService.findOne(id);
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
await this.userService.remove(id);
return res.status(200).json({ message: 'User removed successfully' });
}
}
This code demonstrates how to use both RolesGuard and PermissionsGuard to check for different types of roles and permissions for each route.