Compare commits
No commits in common. "f2b80ba89f1352718aac0de208c7f04dec2cdb6d" and "924faca13f532a1360ea28f9cc197fb56b2b4228" have entirely different histories.
f2b80ba89f
...
924faca13f
@ -1,7 +1,10 @@
|
|||||||
package ovh.herisson.Clyde.EndPoints;
|
package ovh.herisson.Clyde.EndPoints;
|
||||||
|
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
|
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
|
||||||
@ -10,7 +13,7 @@ import ovh.herisson.Clyde.Services.UserService;
|
|||||||
import ovh.herisson.Clyde.Tables.Role;
|
import ovh.herisson.Clyde.Tables.Role;
|
||||||
import ovh.herisson.Clyde.Tables.User;
|
import ovh.herisson.Clyde.Tables.User;
|
||||||
|
|
||||||
import java.security.Key;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -40,7 +43,7 @@ public class UserController {
|
|||||||
@PostMapping("/user")
|
@PostMapping("/user")
|
||||||
public ResponseEntity<String> postUser(@RequestBody User user,@RequestHeader("Authorization") String authorization){
|
public ResponseEntity<String> postUser(@RequestBody User user,@RequestHeader("Authorization") String authorization){
|
||||||
|
|
||||||
if (authServ.isNotSecretaryOrAdmin(authorization))
|
if (!isSecretaryOrAdmin(authorization))
|
||||||
return new UnauthorizedResponse<>(null);
|
return new UnauthorizedResponse<>(null);
|
||||||
|
|
||||||
userService.save(user);
|
userService.save(user);
|
||||||
@ -50,7 +53,7 @@ public class UserController {
|
|||||||
@GetMapping("/users")
|
@GetMapping("/users")
|
||||||
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllUsers(@RequestHeader("Authorization") String authorization){
|
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllUsers(@RequestHeader("Authorization") String authorization){
|
||||||
|
|
||||||
if (authServ.isNotSecretaryOrAdmin(authorization))
|
if (!isSecretaryOrAdmin(authorization))
|
||||||
return new UnauthorizedResponse<>(null);
|
return new UnauthorizedResponse<>(null);
|
||||||
|
|
||||||
Iterable<User> users = userService.getAll();
|
Iterable<User> users = userService.getAll();
|
||||||
@ -75,39 +78,6 @@ public class UserController {
|
|||||||
return new ResponseEntity<>("data modified", HttpStatus.OK);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/students")
|
|
||||||
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllStudent(@RequestHeader("Authorization") String token){
|
|
||||||
if (authServ.getUserFromToken(token) == null)
|
|
||||||
return new UnauthorizedResponse<>(null);
|
|
||||||
|
|
||||||
Iterable<User> teachers = userService.getAllStudents();
|
|
||||||
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
|
/** return user's data except password
|
||||||
* @param user the user to return
|
* @param user the user to return
|
||||||
@ -115,6 +85,7 @@ public class UserController {
|
|||||||
*/
|
*/
|
||||||
private HashMap<String,Object> userWithoutPassword(User user){
|
private HashMap<String,Object> userWithoutPassword(User user){
|
||||||
HashMap<String,Object> toReturn = new HashMap<>();
|
HashMap<String,Object> toReturn = new HashMap<>();
|
||||||
|
|
||||||
toReturn.put("regNo",user.getRegNo());
|
toReturn.put("regNo",user.getRegNo());
|
||||||
toReturn.put("firstName",user.getFirstName());
|
toReturn.put("firstName",user.getFirstName());
|
||||||
toReturn.put("lastName",user.getLastName());
|
toReturn.put("lastName",user.getLastName());
|
||||||
@ -122,7 +93,18 @@ public class UserController {
|
|||||||
toReturn.put("country",user.getCountry());
|
toReturn.put("country",user.getCountry());
|
||||||
toReturn.put("address",user.getAddress());
|
toReturn.put("address",user.getAddress());
|
||||||
toReturn.put("role",user.getRole());
|
toReturn.put("role",user.getRole());
|
||||||
|
|
||||||
return toReturn;
|
return toReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isSecretaryOrAdmin(String authorization){
|
||||||
|
if (authorization ==null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
User poster = authServ.getUserFromToken(authorization);
|
||||||
|
if (poster == null) return false;
|
||||||
|
|
||||||
|
return poster.getRole() == Role.Secretary || poster.getRole() == Role.Admin;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,4 @@ public interface UserRepository extends CrudRepository<User, Long> {
|
|||||||
@Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher")
|
@Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher")
|
||||||
Iterable<User> findAllTeachers();
|
Iterable<User> findAllTeachers();
|
||||||
|
|
||||||
|
|
||||||
@Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Student")
|
|
||||||
Iterable<User> findAllStudents();
|
|
||||||
}
|
}
|
@ -47,7 +47,7 @@ public class AuthenticatorService {
|
|||||||
User poster = getUserFromToken(authorization);
|
User poster = getUserFromToken(authorization);
|
||||||
if (poster == null) return true;
|
if (poster == null) return true;
|
||||||
|
|
||||||
return poster.getRole() != Role.Secretary && poster.getRole() != Role.Admin;
|
return poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean IsNotIn(Role[] roles, String token){
|
public boolean IsNotIn(Role[] roles, String token){
|
||||||
|
@ -106,6 +106,4 @@ public class UserService {
|
|||||||
|
|
||||||
|
|
||||||
public Iterable<User> getAllTeachers (){return userRepo.findAllTeachers();}
|
public Iterable<User> getAllTeachers (){return userRepo.findAllTeachers();}
|
||||||
|
|
||||||
public Iterable<User> getAllStudents(){return userRepo.findAllStudents();}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user