Skip to content

Commit 7963bdd

Browse files
user listing permissions
1 parent 04a8143 commit 7963bdd

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createParamDecorator } from '@nestjs/common';
2+
3+
export const AuthUser = createParamDecorator((data, req) => {
4+
return req.user;
5+
});

src/modules/projects/controllers/projects.controller.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import {AuthGuard} from '@nestjs/passport'
77
import { JoiValidationPipe } from '../../../pipe/joi-validation.pipe'
88
import { ProjectService } from './../services/project/project.service';
99
import { projectSchema } from './../../../schemas/project.schema';
10+
1011
import { JwtAuthGuard } from '../../../modules/auth/guards/jwt-auth.guard'
1112
import { RolesGuard } from '../../../modules/auth/guards/roles.guard'
1213
import { Public } from '../../../modules/auth/decorators/public.decorator'
1314
import { Roles } from '../../../modules/auth/decorators/roles.decorator'
1415
import { Role } from '../../../entities/user.entity'
16+
17+
import { AuthUser } from '../../../modules/auth/decorators/authuser.decorator'
18+
1519
@UseGuards(JwtAuthGuard, RolesGuard)
1620
@Controller('project')
1721
export class ProjectsController {
@@ -33,9 +37,12 @@ export class ProjectsController {
3337
@Roles(Role.DEFAULT)
3438
@HttpCode(HttpStatus.CREATED)
3539
@UsePipes(new JoiValidationPipe(projectSchema))
36-
async create(@Body() payload: any): Promise<object> {
37-
const { name, description, location } = payload
38-
let newProduct = await this.projectService.createProject(name, description, location)
40+
async create(@Body() payload: any, @AuthUser() user: any): Promise<object> {
41+
const { name, description, location } = payload
42+
let owner = "1"
43+
console.log(user);
44+
45+
let newProduct = await this.projectService.createProject(owner, name, description, location)
3946

4047
return { msg: "created", data: newProduct };
4148
}

src/modules/projects/services/project/project.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class ProjectService {
1515
) {
1616
//this.clientPg.query("SELECT * FROM projects")
1717
}
18-
async createProject(name: string, description: string, location: string): Promise<object> {
18+
async createProject(owner: string, name: string, description: string, location: string): Promise<object> {
1919
let poject = this.projectRepo.create({ name, description, location })
2020
await this.projectRepo.save(poject)
2121
return { id: poject.id };
Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
import { Controller, Get, Param, Query, Post, Put, Delete, HttpCode, HttpStatus, ParseIntPipe, UsePipes, Body } from '@nestjs/common';
1+
import { Controller, Get, Param, Query, Post, Put, Delete, HttpCode, HttpStatus, ParseIntPipe, UsePipes, Body, UseGuards } from '@nestjs/common';
22
import { JoiValidationPipe } from '../../../../pipe/joi-validation.pipe'
33
import { UsersService } from './../../services/users/users.service';
44
import { createUserSchema } from './../../../../schemas/user.schema';
55

6+
7+
import { JwtAuthGuard } from '../../../../modules/auth/guards/jwt-auth.guard'
8+
import { RolesGuard } from '../../../../modules/auth/guards/roles.guard'
9+
import { Public } from '../../../../modules/auth/decorators/public.decorator'
10+
import { Roles } from '../../../../modules/auth/decorators/roles.decorator'
11+
import { Role } from '../../../../entities/user.entity'
12+
13+
@UseGuards(JwtAuthGuard, RolesGuard)
614
@Controller('users')
715
export class UsersController {
816
constructor(private usersService: UsersService) { }
17+
18+
@Public()
919
@Post()
1020
@HttpCode(HttpStatus.CREATED)
1121
@UsePipes(new JoiValidationPipe(createUserSchema))
@@ -17,30 +27,33 @@ export class UsersController {
1727
}
1828

1929

20-
/*@Get(":id")
30+
@Get(":id")
2131
@HttpCode(HttpStatus.FOUND)
32+
@Roles(Role.ADMIN)
2233
async get(@Param("id", ParseIntPipe) id: number): Promise<object> {
23-
return { data: await this.languageService.get(id) }
24-
}*/
25-
/**/
26-
@Get()
27-
@HttpCode(HttpStatus.FOUND)
28-
async list(@Query() params: any): Promise<object> {
29-
return { data: await this.usersService.list(params) }
30-
}/*
34+
return { data: await this.usersService.get(id) }
35+
}
36+
@Get()
37+
@HttpCode(HttpStatus.FOUND)
38+
@Roles(Role.ADMIN)
39+
async list(@Query() params: any): Promise<object> {
40+
return { data: await this.usersService.list(params) }
41+
}
3142

32-
@Put(":id")
33-
@HttpCode(HttpStatus.ACCEPTED)
34-
async update(@Param("id", ParseIntPipe) id: number, @Body(new JoiValidationPipe(createUserSchema)) payload: any): Promise<object> {
35-
return { msg: "updated", data: await this.usersService.update(id, payload) }
36-
}
43+
@Put(":id")
44+
@HttpCode(HttpStatus.ACCEPTED)
45+
@Roles(Role.ADMIN)
46+
async update(@Param("id", ParseIntPipe) id: number, @Body(new JoiValidationPipe(createUserSchema)) payload: any): Promise<object> {
47+
return { msg: "updated", data: await this.usersService.update(id, payload) }
48+
}
49+
50+
@Delete(":id")
51+
@HttpCode(HttpStatus.OK)
52+
@Roles(Role.ADMIN)
53+
async delete(@Param("id", ParseIntPipe) id: number): Promise<object> {
54+
let deleted = await this.usersService.delete(id)
55+
return { msg: "deleted" }
56+
}
3757

38-
@Delete(":id")
39-
@HttpCode(HttpStatus.OK)
40-
async delete(@Param("id", ParseIntPipe) id: number): Promise<object> {
41-
let deleted = await this.usersService.delete(id)
42-
return { msg: "deleted" }
43-
}
44-
*/
4558
}
4659

src/modules/users/services/users/users.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class UsersService {
5151
throw new NotFoundException(`no ${this.plural} found`);
5252
}
5353
return users;
54-
}/*
54+
}
5555
async update(id: number, payload: object): Promise<object> {
5656
const element = await this.userRepo.findOne(id);
5757
if (!element) {
@@ -66,5 +66,5 @@ export class UsersService {
6666
throw new NotFoundException(`${this.singular} with id ${id} not found`);
6767
}
6868
return true
69-
}*/
69+
}
7070
}

0 commit comments

Comments
 (0)