well working user authentificatorisation
This commit is contained in:
parent
8e0135b8d9
commit
8d5231c221
4 changed files with 60 additions and 7 deletions
|
@ -40,6 +40,13 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<version>9.1.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
package fr.insa_toulouse.mas.benevolat.Authentication.model;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
@ -11,26 +16,67 @@ public class User {
|
|||
private Boolean isAdmin;
|
||||
|
||||
static HashMap<Integer, User> users = new HashMap<Integer, User>();
|
||||
static Connection connection;
|
||||
|
||||
static {
|
||||
try {
|
||||
User.connection = DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306/projet_gei_052?user=projet_gei_052&password=ohc1Eet7");
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
static public void saveUser(String name, String password, Boolean isAdmin) {
|
||||
Random rand = new Random();
|
||||
int id = rand.nextInt(50000);
|
||||
User user = new User(name, password, id,isAdmin);
|
||||
users.put(id, user);
|
||||
try {
|
||||
Statement statement = User.connection.createStatement();
|
||||
statement.addBatch("INSERT INTO user (id, name, password, isAdmin) VALUES (" + id + ", \"" + user.name + "\", \"" + user.password + "\", " + isAdmin + ");");
|
||||
statement.executeBatch();
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
// users.put(id, user);
|
||||
}
|
||||
|
||||
static public int getUserId(String name, String password) {
|
||||
for(Map.Entry<Integer, User> entry: User.users.entrySet()) {
|
||||
|
||||
int result = -1;
|
||||
try {
|
||||
Statement statement = User.connection.createStatement();
|
||||
System.out.println("Select id FROM user WHERE name = \"" + name + "\" and password = \"" + password + "\";");
|
||||
|
||||
ResultSet res = statement.executeQuery("Select id FROM user WHERE name = \"" + name + "\" and password = \"" + password + "\";");
|
||||
System.out.println("We have a result");
|
||||
result = res.getInt(0);
|
||||
System.out.println("result extracted");
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
/* for(Map.Entry<Integer, User> entry: User.users.entrySet()) {
|
||||
User user = entry.getValue();
|
||||
if (user.name.equals(name) && user.password.equals(password)) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}*/
|
||||
return result;
|
||||
}
|
||||
|
||||
static public Boolean isAdmin(int id) {
|
||||
return User.users.get(id).isAdmin;
|
||||
Boolean result = false;
|
||||
try {
|
||||
Statement statement = User.connection.createStatement();
|
||||
ResultSet res = statement.executeQuery("Select isAdmin FROM user WHERE id = " + id + ";");
|
||||
|
||||
result = res.getBoolean("isAdmin");
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// return User.users.get(id).isAdmin;
|
||||
return result;
|
||||
}
|
||||
|
||||
public User(String name, String password, int id, Boolean isAdmin) {
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
spring.application.name=Authentication
|
||||
server.port=8090
|
||||
server.port=8091
|
|
@ -1,5 +1,5 @@
|
|||
#Generated by Maven Integration for Eclipse
|
||||
#Fri Nov 08 09:37:25 CET 2024
|
||||
#Wed Nov 13 16:53:34 CET 2024
|
||||
artifactId=RestProject
|
||||
groupId=fr.insa.rest
|
||||
m2e.projectLocation=/home/nbillard/Desktop/5A/SOA/SOA/RestProject
|
||||
|
|
Loading…
Reference in a new issue