From 1f309f2ac468084818e3689ffeb7b93329ac8b12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Sun, 19 May 2024 23:15:43 +0200 Subject: [PATCH] #8: Setup Model and DAO --- src/main/java/de/hitec/nhplus/login/User.java | 34 +++++++ .../hitec/nhplus/login/database/UserDao.java | 90 +++++++++++++++++++ .../de/hitec/nhplus/login/database/User.sql | 2 +- .../nhplus/login/database/UserPermission.sql | 2 +- 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/hitec/nhplus/login/User.java create mode 100644 src/main/java/de/hitec/nhplus/login/database/UserDao.java diff --git a/src/main/java/de/hitec/nhplus/login/User.java b/src/main/java/de/hitec/nhplus/login/User.java new file mode 100644 index 0000000..52ab712 --- /dev/null +++ b/src/main/java/de/hitec/nhplus/login/User.java @@ -0,0 +1,34 @@ +package de.hitec.nhplus.login; + +import de.hitec.nhplus.nurse.Nurse; + +public class User { + + private int id; + private String username; + private int permissions = 0; + private Nurse nurse; + + public User( + int id, + String username, + int permissions, + Nurse nurse + ) { + this.id = id; + this.username = username; + this.permissions = permissions; + this.nurse = nurse; + } + + public User( + String username, + int permissions, + Nurse nurse, + boolean admin + ) { + this.username = username; + this.permissions = permissions; + this.nurse = nurse; + } +} diff --git a/src/main/java/de/hitec/nhplus/login/database/UserDao.java b/src/main/java/de/hitec/nhplus/login/database/UserDao.java new file mode 100644 index 0000000..eea6bfb --- /dev/null +++ b/src/main/java/de/hitec/nhplus/login/database/UserDao.java @@ -0,0 +1,90 @@ +package de.hitec.nhplus.login.database; + +import de.hitec.nhplus.datastorage.Dao; +import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.login.User; +import de.hitec.nhplus.nurse.Nurse; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +public class UserDao implements Dao { + + + protected final Connection connection; + + public UserDao(Connection connection) { + this.connection = connection; + } + + public int readUserId(String username) throws SQLException { + final String SQL = "SELECT id FROM user WHERE username = ?"; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setString(1, username); + return statement.executeQuery().getInt(1); + } + + public byte[] readPasswordSalt(int id) throws SQLException { + final String SQL = "SELECT passwordSalt FROM user WHERE id = ?"; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement.executeQuery().getBytes(1); + } + + public byte[] readPasswordHash(int id) throws SQLException { + final String SQL = "SELECT passwordHash FROM user WHERE id = ?"; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement.executeQuery().getBytes(1); + } + + // User + @Override + public User read(int id) throws SQLException { + final String SQL = """ + SELECT user.username, user__permissions.permissions, user__nurse.nurseId + FROM user + LEFT JOIN user__permissions ON user.id = user__permissions.userId + LEFT JOIN user__nurse ON user.id = user__nurse.userId + WHERE user.id = ?; + """; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + ResultSet result = statement.executeQuery(); + int nurseId = result.getInt(3); + Nurse nurse = null; + if (!result.wasNull()) { + nurse = DaoFactory.getInstance().createNurseDAO().read(nurseId); + } + return new User( + id, + result.getString(1), + result.getInt(2), + nurse + ); + } + + // Administration + @Override + public void create(User user) throws SQLException { + + } + + @Override + public void update(User user) throws SQLException { + + } + + @Override + public void delete(int id) throws SQLException { + + } + + @Override + public List readAll() throws SQLException { + return null; + } +} diff --git a/src/main/resources/de/hitec/nhplus/login/database/User.sql b/src/main/resources/de/hitec/nhplus/login/database/User.sql index b92a04a..8773d00 100644 --- a/src/main/resources/de/hitec/nhplus/login/database/User.sql +++ b/src/main/resources/de/hitec/nhplus/login/database/User.sql @@ -1,7 +1,7 @@ CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, - username TEXT NOT NULL, + username TEXT NOT NULL UNIQUE, passwordSalt BLOB NOT NULL, passwordHash BLOB NOT NULL ) \ No newline at end of file diff --git a/src/main/resources/de/hitec/nhplus/login/database/UserPermission.sql b/src/main/resources/de/hitec/nhplus/login/database/UserPermission.sql index 536dce0..4a266d8 100644 --- a/src/main/resources/de/hitec/nhplus/login/database/UserPermission.sql +++ b/src/main/resources/de/hitec/nhplus/login/database/UserPermission.sql @@ -1,6 +1,6 @@ CREATE TABLE user__permissions ( userId INTEGER NOT NULL, - permissions INTEGER, -- Binary Bitmask for Permissions + permissions INTEGER NOT NULL, -- Binary Bitmask for Permissions FOREIGN KEY (userId) REFERENCES user (id) ON DELETE CASCADE ) \ No newline at end of file