added the GET /teachers
All checks were successful
Build and test backend / Build-backend (pull_request) Successful in 2m3s
Build and test backend / Test-backend (pull_request) Successful in 2m0s
Build and test FrontEnd / Build-frontend (pull_request) Successful in 24s

had to fix the mock and the UserController isAdminOrSecretary
This commit is contained in:
Bartha Maxime 2024-03-16 09:34:16 +01:00
parent 9f02207eb0
commit 74593ca851
4 changed files with 31 additions and 2 deletions

View File

@ -49,7 +49,7 @@ public class MockController {
User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), null,Role.Admin,passwordEncoder.encode("admin"));
User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), null,Role.Student,passwordEncoder.encode("student"));
User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), null,Role.Teacher,passwordEncoder.encode("secretary"));
User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), null,Role.Secretary,passwordEncoder.encode("secretary"));
User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), null,Role.Teacher,passwordEncoder.encode("teacher"));
mockUsers = new ArrayList<User>(Arrays.asList(herobrine,joe,meh,joke));

View File

@ -9,6 +9,8 @@ import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.UserService;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User;
import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@ -72,6 +74,23 @@ public class UserController {
return new ResponseEntity<>("data modified", HttpStatus.OK);
}
@GetMapping("/teachers")
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllTeachers(@RequestHeader("Authorization") String token){
if (authServ.getUserFromToken(token) == null)
return new UnauthorizedResponse<>(null);
Iterable<User> teachers = userService.getAllTeachers();
ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>();
for (User t: teachers){
withoutPassword.add(userWithoutPassword(t));
}
return new ResponseEntity<>(withoutPassword, HttpStatus.OK);
}
/** return user's data except password
* @param user the user to return
* @return all the user data without the password
@ -91,13 +110,15 @@ public class UserController {
}
private boolean isSecretaryOrAdmin(String authorization){
System.out.println(authorization);
if (authorization ==null)
return false;
User poster = authServ.getUserFromToken(authorization);
if (poster == null) return false;
System.out.println(poster.getRole());
return poster.getRole() == Role.Secretary && poster.getRole() == Role.Admin;
return poster.getRole() == Role.Secretary || poster.getRole() == Role.Admin;
}
}

View File

@ -15,4 +15,8 @@ public interface UserRepository extends CrudRepository<User, Long> {
/**
@Query(value = "select a.* from Users a ",nativeQuery = true)
Iterable<User> findAllUsers();**/
@Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher")
Iterable<User> findAllTeachers();
}

View File

@ -103,4 +103,8 @@ public class UserService {
public Iterable<User> getAll(){
return userRepo.findAll();
}
public Iterable<User> getAllTeachers (){return userRepo.findAllTeachers();}
}