added mocks users with their role as password and token
This commit is contained in:
		| @ -18,6 +18,7 @@ public class LoginController { | ||||
|     public LoginController(UserService userService, TokenService tokenService){ | ||||
|         this.userService =userService; | ||||
|         this.tokenService = tokenService; | ||||
|  | ||||
|     } | ||||
|     @PostMapping("/login") | ||||
|     public ResponseEntity<String> login(@RequestParam String identifier, String password, Date expirationDate){ | ||||
|  | ||||
| @ -6,6 +6,9 @@ import org.springframework.http.HttpStatus; | ||||
| import org.springframework.http.ResponseEntity; | ||||
| import org.springframework.web.bind.annotation.*; | ||||
| import ovh.herisson.Clyde.Repositories.UserRepository; | ||||
| import ovh.herisson.Clyde.Responses.UnauthorizedResponse; | ||||
| import ovh.herisson.Clyde.Services.TokenService; | ||||
| import ovh.herisson.Clyde.Services.UserService; | ||||
| import ovh.herisson.Clyde.Tables.User; | ||||
|  | ||||
|  | ||||
| @ -13,30 +16,36 @@ import ovh.herisson.Clyde.Tables.User; | ||||
| @CrossOrigin(origins = "http://localhost:5173") | ||||
| public class UserController { | ||||
|  | ||||
|     private final UserRepository userRepo; | ||||
|     private final UserService userService; | ||||
|  | ||||
|     public UserController(UserRepository userRepo){ | ||||
|         this.userRepo = userRepo; | ||||
|     private final TokenService tokenService; | ||||
|     public UserController(UserService userService, TokenService tokenService){ | ||||
|         this.userService =userService; | ||||
|         this.tokenService = tokenService; // todo find a way to be clearer | ||||
|  | ||||
|         tokenService.postMockToken(userService.postMockUsers());// todo find a better place to put that | ||||
|     } | ||||
|  | ||||
|     @GetMapping("/user") | ||||
|     public ResponseEntity<User> getUsers(@RequestHeader("Authorization") String token){ | ||||
|         //TODO | ||||
|         // Get the token thru the data base | ||||
|         // tokenRepo.findToken(token) => User userFromToken | ||||
|         // si role != secretary => return error : ResponseEntity<User>(null, HttpStatus.UNAUTHORIZED) | ||||
|         return new ResponseEntity<User>(/**userRepo.findById(userFromToken.id),**/ HttpStatus.OK); | ||||
|  | ||||
|         User user = tokenService.getUserFromToken(token); | ||||
|  | ||||
|         if (user == null) { | ||||
|             return new UnauthorizedResponse<User>(null); | ||||
|         } | ||||
|         return new ResponseEntity<User>(user, HttpStatus.OK); | ||||
|     } | ||||
|  | ||||
|     @PostMapping("/user") | ||||
|     public ResponseEntity<String> postUser(@RequestBody User user){ | ||||
|         userRepo.save(user); | ||||
|         userService.save(user); | ||||
|         return new ResponseEntity<String>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED); | ||||
|     } | ||||
|  | ||||
|     @GetMapping("/users") | ||||
|     public Iterable<User> getAllUsers(){//TODO ne l'accepter que si c'est le secrétariat | ||||
|         return userRepo.findAll(); | ||||
|         return userService.getAll(); | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
| @ -2,6 +2,9 @@ package ovh.herisson.Clyde.Repositories; | ||||
|  | ||||
| import org.springframework.data.repository.CrudRepository; | ||||
| import ovh.herisson.Clyde.Tables.Token; | ||||
| import ovh.herisson.Clyde.Tables.User; | ||||
|  | ||||
| public interface TokenRepository extends CrudRepository<Token,Long> { | ||||
|  | ||||
|     Token getByToken(String token); | ||||
| } | ||||
|  | ||||
| @ -5,8 +5,8 @@ import org.springframework.http.HttpStatus; | ||||
| import org.springframework.http.ResponseEntity; | ||||
|  | ||||
|  | ||||
| public class UnauthorizedResponse extends ResponseEntity<String> { | ||||
|     public UnauthorizedResponse(String message) { | ||||
| public class UnauthorizedResponse<T> extends ResponseEntity<T> { | ||||
|     public UnauthorizedResponse(T message) { | ||||
|         super(message,HttpStatus.UNAUTHORIZED); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -33,4 +33,19 @@ public class TokenService { | ||||
|         tokenRepo.save(new Token(user,token)); | ||||
|     } | ||||
|  | ||||
|     public User getUserFromToken(String token){ | ||||
|         return tokenRepo.getByToken(token).getUser(); | ||||
|     } | ||||
|  | ||||
|     /** Take the list of mock user to save them in the Token DB | ||||
|      * With token being the password of the user (also his role) | ||||
|      * @param users an | ||||
|      */ | ||||
|     public void postMockToken(Iterable<User> users){ | ||||
|         for (User user: users){ | ||||
|             tokenRepo.save(new Token(user,user.getPassword())); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
| @ -3,15 +3,21 @@ package ovh.herisson.Clyde.Services; | ||||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||||
| import org.springframework.stereotype.Service; | ||||
| import ovh.herisson.Clyde.Repositories.UserRepository; | ||||
| import ovh.herisson.Clyde.Tables.Role; | ||||
| import ovh.herisson.Clyde.Tables.User; | ||||
|  | ||||
| import java.text.DateFormat; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.Date; | ||||
| import java.util.List; | ||||
|  | ||||
| @Service | ||||
| public class UserService { | ||||
|  | ||||
|     private final UserRepository userRepo; | ||||
|     private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); | ||||
|  | ||||
|  | ||||
|     public UserService(UserRepository userRepo){ | ||||
|         this.userRepo = userRepo; | ||||
|     } | ||||
| @ -33,4 +39,33 @@ public class UserService { | ||||
|         return passwordEncoder.matches(tryingPassword,  user.getPassword()); | ||||
|     } | ||||
|  | ||||
| } | ||||
|     /** Saves an example of : | ||||
|      * an Admin with id 1, email : admin@admin.com and password: admin | ||||
|      * a Student with id 2, email: student@student.com and password: student (no cursus yet) | ||||
|      * a Secretary with id 3, email: secretary@secretary.com and password: secretary | ||||
|      * a Teacher (same) | ||||
|      * and they all have silly names (hihi) | ||||
|      */ | ||||
|     public Iterable<User> postMockUsers(){ | ||||
|         User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), Role.Admin,passwordEncoder.encode("admin")); | ||||
|         User Joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), Role.Student,passwordEncoder.encode("student")); | ||||
|         User Meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), Role.Teacher,passwordEncoder.encode("secretary")); | ||||
|         User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), Role.Teacher,passwordEncoder.encode("teacher")); | ||||
|  | ||||
|         userRepo.save(herobrine); | ||||
|         userRepo.save(Joe); | ||||
|         userRepo.save(Meh); | ||||
|         userRepo.save(joke); | ||||
|  | ||||
|         return new ArrayList<User>(Arrays.asList(herobrine,Joe,Meh,joke)); | ||||
|     } | ||||
|  | ||||
|     public void save(User  user){ | ||||
|         userRepo.save(user); | ||||
|     } | ||||
|  | ||||
|     public Iterable<User> getAll(){ | ||||
|         return userRepo.findAll(); | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user