From 1a266cdfbd59bde4bbc9f6b04f8329b226b0a6f3 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 21 Apr 2024 23:14:56 +0200 Subject: [PATCH] post User by secretary --- .../EndPoints/ApplicationsController.java | 3 +- .../Clyde/EndPoints/UserController.java | 9 +- .../herisson/Clyde/Services/UserService.java | 92 +++++++------- .../herisson/Clyde/Tables/Applications.java | 2 +- frontend/public/i18n/EN.txt | 7 ++ frontend/public/i18n/FR.txt | 7 ++ frontend/src/Apps/AboutUser.vue | 25 ++-- frontend/src/Apps/CreateUser.vue | 117 ++++++++++++++++++ frontend/src/rest/Users.js | 31 +---- frontend/src/rest/apps.js | 5 +- 10 files changed, 208 insertions(+), 90 deletions(-) create mode 100644 frontend/src/Apps/CreateUser.vue diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java index d95a7ff..36c485e 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java @@ -74,7 +74,8 @@ public class ApplicationsController { authorizedApps.add(Applications.Requests); authorizedApps.add(Applications.StudentsList);} - if (!authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token)){ + if (!authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token)){ + authorizedApps.add(Applications.CreateUser); authorizedApps.add(Applications.UsersList);} if (researchesServ.getResearcherByUser(user) != null) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java index 64eec4a..0496c59 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -86,19 +86,20 @@ public class UserController { * @return a string clarifying the issue (if there is any) */ @PatchMapping("/user/{id}") - public ResponseEntity patchUser(@RequestHeader("Authorization") String token, + public ResponseEntity> patchUser(@RequestHeader("Authorization") String token, @RequestBody Map updates, @PathVariable Long id) { if (token == null) return new UnauthorizedResponse<>(null); User poster = authServ.getUserFromToken(token); - if (poster == null) {return new UnauthorizedResponse<>("bad token");} + if (poster == null) {return new UnauthorizedResponse<>(null);} - if (!userService.modifyData(id, updates, poster)) + User modified = userService.modifyData(id,updates,poster); + if (modified ==null) return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - return new ResponseEntity<>(null, HttpStatus.OK); + return new ResponseEntity<>(ProtectionService.userWithoutPassword(modified), HttpStatus.OK); } @GetMapping("/teachers") diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java index 72eabd5..caa20e6 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -43,61 +43,55 @@ public class UserService { * @param targetId the id of the user to update * @return if the changes were done or not */ - public boolean modifyData(long targetId, Map updates, User poster){ + public User modifyData(long targetId, Map updates, User poster){ User target = userRepo.findById(targetId); if (target == null) - return false; + return null; - if (poster.getRegNo().equals(target.getRegNo())){ - for (Map.Entry entry : updates.entrySet()){ + if (!target.getRegNo().equals(poster.getRegNo()) && !(poster.getRole() == Role.Secretary) && + !(poster.getRole() == Role.Admin)) + return null; - switch (entry.getKey()){ - case "firstName": - target.setFirstName((String) entry.getValue()); - break; - case "lastName": - target.setLastName((String) entry.getValue()); - break; - case "email": - target.setEmail((String) entry.getValue()); - break; - case "address": - target.setAddress((String) entry.getValue()); - break; - case "country": - target.setCountry((String) entry.getValue()); - break; - case "birthDate": - target.setBirthDate((Date) entry.getValue()); - break; - case "profilePictureUrl": - target.setProfilePictureUrl((String) entry.getValue()); - break; - case "password": - target.setPassword(passwordEncoder.encode((String) entry.getValue())); - break; - } - } - userRepo.save(target); - return true; - } - // the secretary can change roles (for example if a student becomes a teacher) - else if (poster.getRole() == Role.Secretary) - { - for (Map.Entry entry : updates.entrySet()){ - - if ( entry.getKey().equals("role")) { - - if (entry.getValue() == Role.Admin) {return false;} - - target.setRole((Role) entry.getValue()); - userRepo.save(target); - return true; - } + for (Map.Entry entry : updates.entrySet()){ + System.out.println(entry.getValue()); + switch (entry.getKey()){ + case "firstName": + target.setFirstName((String) entry.getValue()); + break; + case "lastName": + target.setLastName((String) entry.getValue()); + break; + case "email": + target.setEmail((String) entry.getValue()); + break; + case "address": + target.setAddress((String) entry.getValue()); + break; + case "country": + target.setCountry((String) entry.getValue()); + break; + case "birthDate": + target.setBirthDate((Date) entry.getValue()); + break; + case "profilePictureUrl": + target.setProfilePictureUrl((String) entry.getValue()); + break; + case "password": + target.setPassword((String) entry.getValue()); + break; + case "role": + //a user can't change his own role + if (poster.getRole()==Role.Secretary || poster.getRole() == Role.Admin){ + Role wanted = Role.valueOf((String) entry.getValue()); + if (wanted == Role.Admin && poster.getRole() != Role.Admin) + return null; + target.setRole(wanted); + } } } - return false; + userRepo.save(target); + return target; } @@ -105,7 +99,7 @@ public class UserService { return passwordEncoder.matches(tryingPassword, user.getPassword()); } - public User save(User user){ + public User save(User user){ user.setPassword(passwordEncoder.encode(user.getPassword())); return userRepo.save(user); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java index 0b4514e..8b6fdc5 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java @@ -23,5 +23,5 @@ public enum Applications { ManageResearcherProfile, //the list of all researches (filterable) - ListResearches, StudentsList + ListResearches, CreateUser, StudentsList } diff --git a/frontend/public/i18n/EN.txt b/frontend/public/i18n/EN.txt index 7e49b71..ac78f9c 100644 --- a/frontend/public/i18n/EN.txt +++ b/frontend/public/i18n/EN.txt @@ -30,6 +30,7 @@ app.studentList=Students List app.users=Users app.manage.researcherProfile=Manage researcher profile app.list.researches=List researches +app.Create.User=Create User request.moreInfos=More Infos request.accept=Accept request.refuse=Refuse @@ -123,3 +124,9 @@ Researcher.Delete=Delete Researcher Profile Researcher.Add=Create Researcher Profile Confirm=Confirm Cancel=Cancel +LastName=Last Name +FirstName=First Name +Profile.Picture=Profile Picture +Role=Role +Password=Password +Create.User=Create User diff --git a/frontend/public/i18n/FR.txt b/frontend/public/i18n/FR.txt index 732f41d..9b4e296 100644 --- a/frontend/public/i18n/FR.txt +++ b/frontend/public/i18n/FR.txt @@ -30,6 +30,7 @@ app.studentList=Liste des étudiants app.users=Utilisateurs app.manage.researcherProfile= gérer son profil de chercheur app.list.researches=Lister les recherches +app.Create.User=créer un utilisateur request.moreInfos=Plus d'Infos request.accept=Accepter request.refuse=Refuser @@ -121,3 +122,9 @@ Country=Pays BirthDate=Date de Naissance Confirm=Confirmer Cancel=Annuler +LastName=Nom de Famille +FirstName=Prénom +Profile.Picture=Photo de Profil +Role=Role +Password=Mot de Passe +Create.User=Créer l'utilisateur diff --git a/frontend/src/Apps/AboutUser.vue b/frontend/src/Apps/AboutUser.vue index 88e880f..0a3cce1 100644 --- a/frontend/src/Apps/AboutUser.vue +++ b/frontend/src/Apps/AboutUser.vue @@ -3,36 +3,37 @@ import i18n from "../i18n.js"; import {ref} from "vue"; import {fetchAllResearchers} from "@/rest/ScientificPublications/ManageResearch.js"; import {deleteResearcher, postResearcher} from "@/rest/ScientificPublications/ResearcherProfile.js"; +import {patchUser} from "@/rest/Users.js"; const props = defineProps(['user']) const modifying =ref(false) const toModify = Object.assign({},{}) const toCreate = Object.assign({},{}) const allResearcher = ref( await fetchAllResearchers()) const researcher = ref() -const user = props.user +const user = ref(props.user) const isResearcher = ref(false) const creating = ref(false) for (let i = 0; i < allResearcher.value.length; i++) { - if (user.regNo === allResearcher.value[i].user.regNo){ + if (user.value.regNo === allResearcher.value[i].user.regNo){ researcher.value = allResearcher.value[i] isResearcher.value = true } } function getPP(){ - if(user.profilePictureUrl === null){ + if(user.value.profilePictureUrl === null){ return "/Clyde.png" } - return user.profilePictureUrl + return user.value.profilePictureUrl } async function createResearcher(){ - toCreate.user = user + toCreate.user = user.value await postResearcher(toCreate) creating.value = false for (let i = 0; i < allResearcher.value.length; i++) { - if (user.regNo === allResearcher.value[i].user.regNo){ + if (user.value.regNo === allResearcher.value[i].user.regNo){ researcher.value = allResearcher.value[i] isResearcher.value = true } @@ -46,6 +47,13 @@ async function deleteResearcherById(){ allResearcher.value = await fetchAllResearchers() } +async function modify(){ + if (modifying.value){ + user.value = await patchUser(user.value.regNo, toModify) + } + modifying.value =!modifying.value +} +