# All files / modules/rbac rbac.controller.ts

100% Statements 42/42
100% Branches 0/0
100% Functions 15/15
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    1721x

    1x

    1x
    1x
    1x
    1x
    1x

    1x
    15x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x

    1x

    2x

    2x

    1x
    1x

    1x
    1x

     import {
      Controller,
      Get,
      Post,
      Patch,
      Delete,
      Body,
      Param,
      UseGuards,
      Query,
    } from '@nestjs/common';
    import {
      ApiTags,
      ApiOperation,
      ApiBearerAuth,
      ApiResponse,
    } from '@nestjs/swagger';
    import { RbacService } from './services/rbac.service';
    import { CreateRoleDto, UpdateRoleDto, AssignRoleDto } from './dto';
    import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
    import { PermissionsGuard, RequirePermissions } from './guards/permissions.guard';
    import { CurrentUser } from '../auth/decorators/current-user.decorator';
    import { RequestUser } from '../auth/strategies/jwt.strategy';

    @ApiTags('rbac')
    @Controller('rbac')
    @UseGuards(JwtAuthGuard, PermissionsGuard)
    @ApiBearerAuth()
    export class RbacController {
      constructor(private readonly rbacService: RbacService) {}

      // ==================== Roles ====================

      @Get('roles')
      @RequirePermissions('roles:read')
      @ApiOperation({ summary: 'List all roles' })
      async findAllRoles(@CurrentUser() user: RequestUser) {
        return this.rbacService.findAllRoles(user.tenant_id);
      }

      @Get('roles/:id')
      @RequirePermissions('roles:read')
      @ApiOperation({ summary: 'Get role by ID with permissions' })
      async findRoleById(
        @Param('id') id: string,
        @CurrentUser() user: RequestUser,
      ) {
        return this.rbacService.getRoleWithPermissions(id, user.tenant_id);
      }

      @Post('roles')
      @RequirePermissions('roles:write')
      @ApiOperation({ summary: 'Create new role' })
      @ApiResponse({ status: 201, description: 'Role created' })
      async createRole(
        @Body() dto: CreateRoleDto,
        @CurrentUser() user: RequestUser,
      ) {
        return this.rbacService.createRole(dto, user.tenant_id);
      }

      @Patch('roles/:id')
      @RequirePermissions('roles:write')
      @ApiOperation({ summary: 'Update role' })
      async updateRole(
        @Param('id') id: string,
        @Body() dto: UpdateRoleDto,
        @CurrentUser() user: RequestUser,
      ) {
        return this.rbacService.updateRole(id, dto, user.tenant_id);
      }

      @Delete('roles/:id')
      @RequirePermissions('roles:delete')
      @ApiOperation({ summary: 'Delete role' })
      async deleteRole(
        @Param('id') id: string,
        @CurrentUser() user: RequestUser,
      ) {
        await this.rbacService.deleteRole(id, user.tenant_id);
        return { message: 'Role eliminado correctamente' };
      }

      // ==================== Permissions ====================

      @Get('permissions')
      @RequirePermissions('roles:read')
      @ApiOperation({ summary: 'List all permissions' })
      async findAllPermissions() {
        return this.rbacService.findAllPermissions();
      }

      @Get('permissions/category/:category')
      @RequirePermissions('roles:read')
      @ApiOperation({ summary: 'List permissions by category' })
      async findPermissionsByCategory(@Param('category') category: string) {
        return this.rbacService.findPermissionsByCategory(category);
      }

      // ==================== User Roles ====================

      @Get('users/:userId/roles')
      @RequirePermissions('users:read', 'roles:read')
      @ApiOperation({ summary: 'Get user roles' })
      async getUserRoles(
        @Param('userId') userId: string,
        @CurrentUser() user: RequestUser,
      ) {
        return this.rbacService.getUserRoles(userId, user.tenant_id);
      }

      @Get('users/:userId/permissions')
      @RequirePermissions('users:read', 'roles:read')
      @ApiOperation({ summary: 'Get user permissions' })
      async getUserPermissions(
        @Param('userId') userId: string,
        @CurrentUser() user: RequestUser,
      ) {
        return this.rbacService.getUserPermissions(userId, user.tenant_id);
      }

      @Post('users/assign-role')
      @RequirePermissions('roles:assign')
      @ApiOperation({ summary: 'Assign role to user' })
      async assignRoleToUser(
        @Body() dto: AssignRoleDto,
        @CurrentUser() user: RequestUser,
      ) {
        return this.rbacService.assignRoleToUser(dto, user.tenant_id, user.id);
      }

      @Delete('users/:userId/roles/:roleId')
      @RequirePermissions('roles:assign')
      @ApiOperation({ summary: 'Remove role from user' })
      async removeRoleFromUser(
        @Param('userId') userId: string,
        @Param('roleId') roleId: string,
        @CurrentUser() user: RequestUser,
      ) {
        await this.rbacService.removeRoleFromUser(userId, roleId, user.tenant_id);
        return { message: 'Role removido correctamente' };
      }

      // ==================== Permission Check ====================

      @Get('check')
      @ApiOperation({ summary: 'Check if current user has permission' })
      async checkPermission(
        @Query('permission') permission: string,
        @CurrentUser() user: RequestUser,
      ) {
        const hasPermission = await this.rbacService.userHasPermission(
          user.id,
          user.tenant_id,
          permission,
        );
        return { hasPermission };
      }

      @Get('me/roles')
      @ApiOperation({ summary: 'Get current user roles' })
      async getMyRoles(@CurrentUser() user: RequestUser) {
        return this.rbacService.getUserRoles(user.id, user.tenant_id);
      }

      @Get('me/permissions')
      @ApiOperation({ summary: 'Get current user permissions' })
      async getMyPermissions(@CurrentUser() user: RequestUser) {
        return this.rbacService.getUserPermissions(user.id, user.tenant_id);
      }
    }