Skip to content

Commit edd555e

Browse files
authored
Merge pull request #44 from WildCodeSchool/US18-dette_technique-autorisation-codegen
Us18 dette technique autorisation codegen
2 parents 8b112b0 + cce8b68 commit edd555e

31 files changed

+786
-841
lines changed

backend/src/resolvers/GroupResolver.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default class GroupResolver {
9595
return messages || [];
9696
}
9797

98+
@UseMiddleware(RoleMiddleware())
9899
@Mutation(() => Group)
99100
async createGroup(@Arg("data") data: CreateGroupInput, @Ctx() ctx: ContextType) {
100101
//TO DO: vérifier les inputs et les nettoyer

backend/src/resolvers/GroupWishlistResolver.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Arg, Ctx, Field, Int, Mutation, ObjectType, Query, Resolver } from "type-graphql";
1+
import { Arg, Ctx, Field, Int, Mutation, ObjectType, Query, Resolver, UseMiddleware } from "type-graphql";
22
import { Gift } from "../entities/Gift";
33
import Group from "../entities/Group";
44
import { GroupMember } from "../entities/GroupMember";
55

66
// biome-ignore lint/style/useImportType: bypass biome linting
77
import { AddGiftInput } from "../inputs/AddGiftInput";
8+
import { RoleMiddleware } from "../middleware/RoleMiddleware";
89
import type { ContextType } from "../types/context";
910
import { getOrCreateUserWishlist } from "../utils/getOrCreateUserWishlist";
1011

@@ -18,6 +19,7 @@ class GroupWishlistItems {
1819
}
1920

2021
@Resolver()
22+
@UseMiddleware(RoleMiddleware())
2123
export default class GroupWishlistResolver {
2224
@Query(() => GroupWishlistItems)
2325
async groupWishlistItems(
@@ -105,6 +107,7 @@ export default class GroupWishlistResolver {
105107
}
106108

107109
@Mutation(() => Gift)
110+
@UseMiddleware(RoleMiddleware())
108111
async addGiftToGroupList(
109112
@Arg("groupId", () => Int) groupId: number,
110113
@Arg("data") data: AddGiftInput,

backend/src/resolvers/MyWishlistResolver.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import { Arg, Ctx, Int, Mutation, Query, Resolver } from "type-graphql";
1+
import { Arg, Ctx, Int, Mutation, Query, Resolver, UseMiddleware } from "type-graphql";
22
import { Gift } from "../entities/Gift";
33
import List from "../entities/List";
44
import User from "../entities/User";
55
// biome-ignore lint/style/useImportType: bypass biome linting
66
import { AddGiftInput } from "../inputs/AddGiftInput";
77
// biome-ignore lint/style/useImportType: bypass biome linting
88
import { UpdateGiftInput } from "../inputs/UpdateGiftInput";
9+
import { RoleMiddleware } from "../middleware/RoleMiddleware";
910
import type { ContextType } from "../types/context";
1011
import { getOrCreateUserWishlist } from "../utils/getOrCreateUserWishlist";
1112

1213
@Resolver()
1314
export default class MyWishlistResolver {
1415
@Query(() => [Gift])
16+
@UseMiddleware(RoleMiddleware())
1517
async myWishlistItems(@Ctx() ctx: ContextType): Promise<Gift[]> {
1618
if (!ctx.user) throw new Error("Utilisateur non connecté");
1719

@@ -25,6 +27,7 @@ export default class MyWishlistResolver {
2527
}
2628

2729
@Mutation(() => Gift)
30+
@UseMiddleware(RoleMiddleware())
2831
async addGift(@Arg("data") data: AddGiftInput, @Ctx() ctx: ContextType): Promise<Gift> {
2932
if (!ctx.user) throw new Error("Utilisateur non connecté");
3033

@@ -60,6 +63,7 @@ export default class MyWishlistResolver {
6063
}
6164

6265
@Mutation(() => Gift)
66+
@UseMiddleware(RoleMiddleware())
6367
async updateGift(
6468
@Arg("id", () => Int) id: number,
6569
@Arg("data") data: UpdateGiftInput,
@@ -81,6 +85,7 @@ export default class MyWishlistResolver {
8185
return gift;
8286
}
8387

88+
@UseMiddleware(RoleMiddleware())
8489
@Mutation(() => Int)
8590
async deleteGift(@Arg("id", () => Int) id: number, @Ctx() ctx: ContextType): Promise<number> {
8691
if (!ctx.user) throw new Error("Utilisateur non connecté");

backend/src/resolvers/UserResolver.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export default class UserResolver {
9898
}
9999

100100
@Query(() => User)
101+
@UseMiddleware(RoleMiddleware())
101102
async getMyProfile(@Ctx() ctx: ContextType) {
102103
if (!ctx.user) throw new Error("Utilisateur non connecté");
103104
const user = await User.findOne({
@@ -121,7 +122,7 @@ export default class UserResolver {
121122

122123
@Query(() => [User])
123124
@UseMiddleware(RoleMiddleware(true))
124-
async getAllUsersForAdmin(@Ctx() _ctx: ContextType) {
125+
async getAllUsersForAdmin() {
125126
// Récupérer tous les utilisateurs (y compris les bannis, mais pas les supprimés)
126127
const allUsers = await User.find({
127128
where: { deletedAt: IsNull() },
@@ -227,6 +228,7 @@ export default class UserResolver {
227228
}
228229

229230
@Mutation(() => Boolean)
231+
@UseMiddleware(RoleMiddleware())
230232
async logout(@Ctx() ctx: ContextType) {
231233
// set le cookie vide pour déconnecter l'utilisateur
232234
cookieManager.delCookie(ctx, "token", { secure: false });
@@ -236,6 +238,7 @@ export default class UserResolver {
236238
}
237239

238240
@Mutation(() => User)
241+
@UseMiddleware(RoleMiddleware())
239242
async UpdateMyProfile(@Arg("data") data: UpdateMyProfileInput, @Ctx() ctx: ContextType) {
240243
if (!ctx.user) throw new Error("Utilisateur non connecté update impossible");
241244

@@ -251,7 +254,7 @@ export default class UserResolver {
251254
}
252255

253256
// hash le mot de passe
254-
const password_hashed = await argon2.hash(data.password);
257+
const password_hashed = data.password ? await argon2.hash(data.password) : undefined;
255258
const newData = {
256259
...data,
257260
password_hashed,
@@ -419,6 +422,7 @@ export default class UserResolver {
419422
}
420423

421424
@Mutation(() => DeleteUserResponse)
425+
@UseMiddleware(RoleMiddleware())
422426
async deleteMyProfile(@Ctx() ctx: ContextType): Promise<DeleteUserResponse> {
423427
// Vérifier que l'utilisateur est connecté
424428
if (!ctx.user) {

compose.dev.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ services:
55
build: ./frontend
66
volumes:
77
- ./frontend/src/:/app/src/
8+
- ./frontend/public/:/app/public/
89
healthcheck:
910
test: [ "CMD-SHELL", "curl --fail --request GET --url 'http://localhost:${FRONTEND_PORT}' || exit 1" ]
1011
interval: 10s

frontend/codegen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import type { CodegenConfig } from "@graphql-codegen/cli";
22

33
const config: CodegenConfig = {
44
schema: "http://backend:3310/graphql",
5-
documents: ["src/graphql/operations.ts"],
5+
documents: ["src/graphql/operations/**/*.ts"],
66
overwrite: true,
77
generates: {
8-
"src/generated/graphql-types.ts": {
8+
"src/graphql/generated/graphql-types.ts": {
99
plugins: [
1010
"typescript",
1111
"typescript-operations",
138 KB
Loading

frontend/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect } from "react";
22
import { Navigate, Outlet, Route, Routes, useNavigate } from "react-router";
33
import AdminRoute from "./components/AdminRoute";
4-
import { useGetMyProfileQuery } from "./generated/graphql-types";
4+
import { useGetMyProfileQuery } from "./graphql/generated/graphql-types";
55
import AdminPage from "./pages/AdminPage";
66
import ContactPage from "./pages/ContactPage";
77
import Conversations from "./pages/Conversations";

frontend/src/components/Wishlist.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// src/components/Wishlist.tsx
22
import { useMutation, useQuery } from "@apollo/client";
33
import { useState } from "react";
4-
import { ADD_GIFT, DELETE_GIFT, MY_WISHLIST_ITEMS, UPDATE_GIFT } from "../graphql/operations";
4+
import {
5+
ADD_GIFT,
6+
DELETE_GIFT,
7+
MY_WISHLIST_ITEMS,
8+
UPDATE_GIFT,
9+
} from "../graphql/operations/wishlistOperations";
510
import type { Gift } from "../types/Gift";
611
import { useMyProfileStore } from "../zustand/myProfileStore";
712
import Button from "./utils/Button";

frontend/src/components/auth/LoginForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState } from "react";
22
import { useNavigate } from "react-router";
3-
import { useLoginMutation } from "../../generated/graphql-types";
3+
import { useLoginMutation } from "../../graphql/generated/graphql-types";
44
import consoleErrorDev from "../../hooks/erreurMod";
55
import { useMyProfileStore } from "../../zustand/myProfileStore";
66
import Button from "../utils/Button";

0 commit comments

Comments
 (0)