- Implémentation de la limite de 5 tokens par utilisateurs

This commit is contained in:
LeoMoulin 2024-03-09 10:45:04 +01:00
parent a297641fa9
commit 54c0f9069e
3 changed files with 21 additions and 0 deletions

View File

@ -67,6 +67,17 @@ public class MockController {
} }
} }
//Nous allons donner des tokens a herobrine pour tester la limite de token
@PostMapping("/herobrine")
public void giveHerobrineTokens(){
User herobrine = userRepo.findById(1);
Calendar c = Calendar.getInstance();
for (int i = 1; i <= 7; i++){
Token t = new Token(herobrine, herobrine.getPassword(), c.getTime());
c.add(Calendar.DAY_OF_YEAR, 1);
tokenService.saveToken(t);
}
}
@DeleteMapping("/mock") @DeleteMapping("/mock")
public void deleteMock(){ public void deleteMock(){
for (User user:mockUsers){ for (User user:mockUsers){

View File

@ -4,9 +4,13 @@ import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
import java.util.ArrayList;
public interface TokenRepository extends CrudRepository<Token,Long> { public interface TokenRepository extends CrudRepository<Token,Long> {
Token getByToken(String token); Token getByToken(String token);
Iterable<Token> getByUser(User user); Iterable<Token> getByUser(User user);
ArrayList <Token> getByUserOrderByExpirationDate(User user);
} }

View File

@ -8,6 +8,7 @@ import ovh.herisson.Clyde.Tables.User;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
@ -34,6 +35,11 @@ public class TokenService {
} }
public void saveToken(Token token){ public void saveToken(Token token){
//Si l'utilisateur a déja 5 token delete celui qui devait expirer le plus vite
ArrayList<Token> tokenList = tokenRepo.getByUserOrderByExpirationDate(token.getUser());
if (tokenList.size() == 5){
tokenRepo.delete(tokenList.get(0));
}
tokenRepo.save(token); tokenRepo.save(token);
} }