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
|
@Autowired
|
||||||
private ConnectionRepository connectionRepository;
|
private ConnectionRepository connectionRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
private RoleRepository roleRepository;
|
private UserRepository userRepository;
|
||||||
|
|
||||||
public boolean checkToken(int idUser, String token) {
|
public boolean checkToken(int idUser, String token) {
|
||||||
List<Connection> connections = connectionRepository.findByIdUser(idUser);
|
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());
|
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);
|
Optional<User> user = userRepository.findById(idUser);
|
||||||
|
if (!user.isPresent()) {
|
||||||
if (connections.isEmpty()) {
|
return Optional.empty();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
// Connection c = connections.getFirst();
|
return Optional.of(user.get().getIdRole());
|
||||||
Optional<Role> roleOption = roleRepository.findById(role);
|
|
||||||
if(!roleOption.isPresent()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Role> getRoles() {
|
|
||||||
return roleRepository.findAll();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@RestController
|
@RestController
|
||||||
|
@ -41,6 +42,14 @@ public class RequestServiceApplication {
|
||||||
if(!administrationService.checkToken(idUser, token)) {
|
if(!administrationService.checkToken(idUser, token)) {
|
||||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User or token invalid.");
|
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();
|
Request request = new Request();
|
||||||
// id_status = 1 means waiting. it is always set to 1 when created.
|
// id_status = 1 means waiting. it is always set to 1 when created.
|
||||||
|
|
|
@ -15,7 +15,7 @@ import java.util.List;
|
||||||
public class RoleServiceApplication {
|
public class RoleServiceApplication {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AdministrationService administrationService;
|
private RoleRepository roleRepository;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(RoleServiceApplication.class, args);
|
SpringApplication.run(RoleServiceApplication.class, args);
|
||||||
|
@ -36,6 +36,6 @@ public class RoleServiceApplication {
|
||||||
|
|
||||||
@GetMapping("/get_roles")
|
@GetMapping("/get_roles")
|
||||||
public List<Role> getRoles() {
|
public List<Role> getRoles() {
|
||||||
return administrationService.getRoles();
|
return roleRepository.findAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue