mirror of
https://github.com/Lemonochrme/service-architecture.git
synced 2025-06-08 13:40:50 +02:00
Added RoleEnum and checking if the user can or not send a request.
This commit is contained in:
parent
e1ea269782
commit
f051f41701
4 changed files with 36 additions and 18 deletions
|
@ -12,7 +12,7 @@ public class AdministrationService {
|
|||
@Autowired
|
||||
private ConnectionRepository connectionRepository;
|
||||
@Autowired
|
||||
private RoleRepository roleRepository;
|
||||
private UserRepository userRepository;
|
||||
|
||||
public boolean checkToken(int idUser, String token) {
|
||||
List<Connection> connections = connectionRepository.findByIdUser(idUser);
|
||||
|
@ -24,22 +24,12 @@ public class AdministrationService {
|
|||
return c.getToken().equals(token) && c.getExpiresAt().isAfter(java.time.LocalDateTime.now());
|
||||
}
|
||||
|
||||
public boolean checkRole(int idUser, int role)
|
||||
public Optional<Integer> getRole(int idUser)
|
||||
{
|
||||
List<Connection> connections = connectionRepository.findByIdUser(idUser);
|
||||
|
||||
if (connections.isEmpty()) {
|
||||
return false;
|
||||
Optional<User> user = userRepository.findById(idUser);
|
||||
if (!user.isPresent()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
// Connection c = connections.getFirst();
|
||||
Optional<Role> roleOption = roleRepository.findById(role);
|
||||
if(!roleOption.isPresent()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<Role> getRoles() {
|
||||
return roleRepository.findAll();
|
||||
return Optional.of(user.get().getIdRole());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package insa.application.helpapp.rest;
|
||||
|
||||
public enum RoleEnum {
|
||||
USER(1),
|
||||
VOLUNTEER(2),
|
||||
ADMIN(3);
|
||||
|
||||
private final int value;
|
||||
|
||||
// Constructor
|
||||
RoleEnum(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
// Getter
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
|
@ -41,6 +42,14 @@ public class RequestServiceApplication {
|
|||
if(!administrationService.checkToken(idUser, token)) {
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User or token invalid.");
|
||||
};
|
||||
Optional<Integer> idRoleOption = administrationService.getRole(idUser);
|
||||
if(!idRoleOption.isPresent()) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("User invalid.");
|
||||
};
|
||||
int idRole = idRoleOption.get();
|
||||
if(idRole == RoleEnum.VOLUNTEER.getValue()) {
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Volunteers cannot post a request.");
|
||||
}
|
||||
|
||||
Request request = new Request();
|
||||
// id_status = 1 means waiting. it is always set to 1 when created.
|
||||
|
|
|
@ -15,7 +15,7 @@ import java.util.List;
|
|||
public class RoleServiceApplication {
|
||||
|
||||
@Autowired
|
||||
private AdministrationService administrationService;
|
||||
private RoleRepository roleRepository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RoleServiceApplication.class, args);
|
||||
|
@ -36,6 +36,6 @@ public class RoleServiceApplication {
|
|||
|
||||
@GetMapping("/get_roles")
|
||||
public List<Role> getRoles() {
|
||||
return administrationService.getRoles();
|
||||
return roleRepository.findAll();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue