I want you to act as a Senior Backend developer. I will describe a project details you…
- User
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"
- Assistantcode
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; @Injectable() export class RolesGuard implements CanActivate { constructor(private readonly reflector: Reflector) {} canActivate(context: ExecutionContext): boolean { const roles = this.reflector.get<string[]>('roles', context.getHandler()); if (!roles) { return true; } const request = context.switchToHttp().getRequest(); const user = request.user; return user.roles.some(role => roles.includes(role)); } } - User
show me controller example code with using RolesGuard for different types of roles for each route. Do not write explanations
- Assistantcode
import { Controller, Get, UseGuards } from '@nestjs/common'; import { Roles } from './roles.decorator'; import { RolesGuard } from './roles.guard'; @Controller('permission') export class PermissionController { @Get('admin') @Roles('admin') @UseGuards(RolesGuard) async getAdminPermissions() { return ['adminPermission1', 'adminPermission2']; } @Get('user') @Roles('user') @UseGuards(RolesGuard) async getUserPermissions() { return ['userPermission1', 'userPermission2']; } } - 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
- Assistantcode
import { Controller, Get, UseGuards, HttpException, HttpStatus } from '@nestjs/common'; import { Roles } from './roles.decorator'; import { RolesGuard } from './roles.guard'; @Controller('permission') export class PermissionController { @Get('admin') @Roles('admin') @UseGuards(RolesGuard) async getAdminPermissions(req) { if (req.user.permissions.includes("adminPermission1") && req.user.permissions.includes("adminPermission2")) { return ['adminPermission1', 'adminPermission2']; } else { throw new HttpException("Forbidden", HttpStatus.FORBIDDEN); } } @Get('user') @Roles('user') @UseGuards(RolesGuard) async getUserPermissions(req) { if (req.user.permissions.includes("userPermission1") && req.user.permissions.includes("userPermission2")) { return ['userPermission1', 'userPermission2']; } else { throw new HttpException("Forbidden", HttpStatus.FORBIDDEN); } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.