Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 631e4ba6 rédigé par Anthony N.'s avatar Anthony N.
Parcourir les fichiers

Merge branch 'master' into 'Issue4'

# Conflicts:
#   src/main/java/fr/unantes/software/construction/calendar/Correspondence.java
#   src/main/java/fr/unantes/software/construction/calendar/Travel.java
#   src/test/java/fr/unantes/software/construction/calendar/CalendarTest.java
#   src/test/java/fr/unantes/software/construction/calendar/CorrespondenceTest.java
#   src/test/java/fr/unantes/software/construction/calendar/TravelTest.java
parents eba1b88c f61c17a7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!1Issue4
......@@ -5,7 +5,14 @@ import java.time.LocalDateTime;
public interface Correspondence {
SingleReference<Travel> getTravel();
City getStartCity();
public Correspondence(Travel travel, City startCity, City destinationCity, LocalDateTime startTime, LocalDateTime arrivalTime) {
this.travel = travel;
this.startCity = startCity;
this.destinationCity = destinationCity;
if(startTime.isAfter(arrivalTime) || startTime.isEqual(arrivalTime)) throw new IllegalArgumentException();
this.startTime = startTime;
this.arrivalTime = arrivalTime;
}
void setStartCity(City startCity);
......
......@@ -15,5 +15,19 @@ public interface Travel {
boolean removeCorrespondence(Correspondence step);
MultipleReference<Correspondence> getCorrespondence();
public Correspondence getLastStep() {
return steps.get(steps.size() - 1);
} // mettre un throw pou gerer les cas du tableau vide
public boolean addCorrespondence(Correspondence step) throws IllegalArgumentException {
if ( steps.size()>=10) {
// un voyage peux avoir jusqu'a 10 correspondances
throw new IllegalArgumentException();
}
return steps.add(step);
}
public boolean removeCorrespondence(Correspondence step) {
return steps.remove(step);
}
}
package fr.unantes.software.construction.security;
import fr.unantes.software.construction.people.Person;
/**
* The controller is used to manager the users and passwords
*/
public class Controller {
private PasswordManager pwdManager;
private UserManager usrManager;
public Controller(PasswordManager pwd, UserManager usr) {
this.pwdManager = pwd;
this.usrManager = usr;
}
/**
* Adds user information to the managers
* @param person - User
* @param password - User's password
* @return True if everything went smoothly, False otherwise
* @throws IllegalArgumentException
*/
public boolean addUser(Person person, String password) throws IllegalArgumentException {
if (usrManager.contains(person)) {
throw new IllegalArgumentException("Invalid argument: the person is already registered.");
}
usrManager.addUser(person);
pwdManager.addPassword(person,password);
return true;
}
/**
* Removes the user for the userManager, and passwordManager
* @param person - User
* @return true if completed successfully , false otherwise
*/
public boolean removeUser(Person person){
if(usrManager.contains(person)){
usrManager.removeUser(person);
pwdManager.removePassword(person,usrManager);
}
return true;
}
}
\ No newline at end of file
package fr.unantes.software.construction.security;
import fr.unantes.software.construction.people.Person;
import java.util.HashMap;
import java.util.Map;
import java.lang.*;
public class PasswordManager {
private Map<String, String> usersToPasswords;
public PasswordManager(){
usersToPasswords = new HashMap<>();
}
/**
* Valid a password
* @param person - User associated to the password
* @param password - password to validate
* @return True if the password is valid, false otherwise
*/
public boolean validatePassword(Person person, String password,UserManager usr) {
if (usr.namesToUsers.containsKey(person.getName())) {
Person p = usr.namesToUsers.get(person.getName());
String reference = usersToPasswords.get(p.getName());
return decryptPassword(reference).equals(password);
}
return false;
}
/**
* Encrypt a password
* @param password - Password to encrypt
* @return Encrypted password
* @throws IllegalArgumentException
*/
private String encryptPassword(String password) throws IllegalArgumentException {
if (password.contains("a")) {
throw new IllegalArgumentException("The password contains unsecure characters, cannot perform encryption.");
}
return password.replaceAll("a", "e");
}
/**
* Decrypt a password
* @param encrypted - Password to decrypt
* @return Decrypted password
*/
private String decryptPassword(String encrypted) {
return encrypted.replaceAll("e", "a");
}
/**
* Adds an encrypted password
* @param person - person involved in the transaction
* @param password - User's password
*/
protected void addPassword(Person person,String password){
usersToPasswords.put(person.getName(), encryptPassword(password));
}
/**
* Removes a password
* @param person - User to remove
* @param usr - User Manager used
*/
protected void removePassword(Person person,UserManager usr){
Person p = usr.namesToUsers.get(person.getName());
usersToPasswords.remove(p.getName());
}
}
......@@ -6,12 +6,11 @@ import java.util.HashMap;
import java.util.Map;
public class UserManager {
private Map<String, Person> namesToUsers;
private Map<String, String> usersToPasswords;
protected Map<String, Person> namesToUsers;
public UserManager() {
namesToUsers = new HashMap<>();
usersToPasswords = new HashMap<>();
}
/**
......@@ -26,67 +25,33 @@ public class UserManager {
/**
* Add a new user to the manager
* @param person - User to add
* @param password - User's password
* @return True if everything went smoothly, False otherwise
* @throws IllegalArgumentException
*/
public boolean addUser(Person person, String password) throws IllegalArgumentException {
if (namesToUsers.containsKey(person.getName())) {
throw new IllegalArgumentException("Invalid argument: the person is already registered.");
}
protected boolean addUser(Person person){
namesToUsers.put(person.toString(), person);
usersToPasswords.put(person.getName(), encryptPassword(password));
return true;
}
/**
* Check if a Person is contained inside the manager
* @param person
* @return true if the person is contained, false otherwise
*/
protected boolean contains(Person person){
return namesToUsers.containsKey(person.getName());
}
/**
* Remove a user from the manager
* @param person - User to remove
* @return True if everything went smoothly, False otherwise
*/
public boolean removeUser(Person person) {
protected boolean removeUser(Person person) {
if (namesToUsers.containsKey(person.getName())) {
Person p = namesToUsers.get(person.getName());
usersToPasswords.remove(p.getName());
namesToUsers.remove(p.getName());
}
return true;
}
/**
* Valid a password
* @param person - User associated to the password
* @param password - password to validate
* @return True if the password is valid, false otherwise
*/
public boolean validatePassword(Person person, String password) {
if (namesToUsers.containsKey(person.getName())) {
Person p = namesToUsers.get(person.getName());
String reference = usersToPasswords.get(p.getName());
return decryptPassword(reference).equals(password);
}
return false;
}
/**
* Encrypt a password
* @param password - Password to encrypt
* @return Encrypted password
* @throws IllegalArgumentException
*/
private String encryptPassword(String password) throws IllegalArgumentException {
if (password.contains("a")) {
throw new IllegalArgumentException("The password contains unsecure characters, cannot perform encryption.");
}
return password.replaceAll("a", "e");
}
/**
* Decrypt a password
* @param encrypted - Password to decrypt
* @return Decrypted password
*/
private String decryptPassword(String encrypted) {
return encrypted.replaceAll("e", "a");
}
}
......@@ -17,7 +17,7 @@ public class CalendarTest {
@Test
private void testAddTravel(){
//assertTrue();
}
@AfterEach
......
package fr.unantes.software.construction.calendar;
import fr.unantes.software.construction.people.Person;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import static org.junit.jupiter.api.Assertions.*;
import java.time.LocalDateTime;
class CorrespondenceTest {
private Correspondence c1, c2;
......
......@@ -7,7 +7,6 @@ import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import static org.junit.jupiter.api.Assertions.*;
public class TravelTest {
......@@ -59,5 +58,6 @@ public class TravelTest {
public void TestAddCorrespondenceIsFalse() {
assertFalse(XX.addCorrespondence(A));
assertThrows(IllegalArgumentException.class,() ->{XX.addCorrespondence(A);});
}
}
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter