From 5e21307980ab844356987d5565ac9cdc81514223 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Sun, 19 May 2019 20:12:30 +0200 Subject: [PATCH] + database methods for user Signed-off-by: Johannes Theiner --- pom.xml | 7 ++ src/main/java/de/hsel/itech/db/Database.java | 10 +++ src/main/java/de/hsel/itech/db/UserDB.java | 89 +++++++++++++++++++ .../java/de/hsel/itech/db/pojo/Address.java | 2 +- .../hsel/itech/db/pojo/CreditCardPayment.java | 2 +- .../hsel/itech/db/pojo/DebitCardPayment.java | 2 +- .../de/hsel/itech/db/pojo/InvoicePayment.java | 2 +- .../de/hsel/itech/db/pojo/PayPalPayment.java | 2 +- .../de/hsel/itech/db/pojo/ShoppingCart.java | 2 +- .../db/pojo/{Customer.java => User.java} | 6 +- 10 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 src/main/java/de/hsel/itech/db/UserDB.java rename src/main/java/de/hsel/itech/db/pojo/{Customer.java => User.java} (82%) diff --git a/pom.xml b/pom.xml index d666d95..cc3a8ab 100644 --- a/pom.xml +++ b/pom.xml @@ -154,6 +154,13 @@ 2.4.1 + + + org.mindrot + jbcrypt + 0.3m + + org.junit.jupiter diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index a9b89a0..10f34f1 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -32,11 +32,15 @@ public class Database { final String tableAuthorBook = "author_book"; final String tablePublisher = "publisher"; final String tableCategory = "category"; + final String tableUser = "user"; + final String tableUserAddress = "user_address"; + final String tableAddress = "address"; private AuthorDB authorDB; private BookDB bookDB; private CategoryDB categoryDB; private PublisherDB publisherDB; + private UserDB userDB; private static Database instance; @@ -124,6 +128,12 @@ public class Database { return publisherDB; } + public UserDB user() { + if(userDB == null) + userDB = new UserDB(this); + return userDB; + } + /** diff --git a/src/main/java/de/hsel/itech/db/UserDB.java b/src/main/java/de/hsel/itech/db/UserDB.java new file mode 100644 index 0000000..dd562ac --- /dev/null +++ b/src/main/java/de/hsel/itech/db/UserDB.java @@ -0,0 +1,89 @@ +package de.hsel.itech.db; + +import de.hsel.itech.db.pojo.User; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.mindrot.jbcrypt.BCrypt; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.5 + **/ +public class UserDB { + + private Database database; + + private final int logRounds = 11; + + UserDB(Database database) { + this.database = database; + } + + @Nullable + public User get(long id) { + Map.Entry entry = database.getResultSetById(database.tableUser, id); + assert entry != null; + + ResultSet rs = entry.getKey(); + try { + return new User(rs.getString("email"), rs.getString("name"), rs.getString("password"), rs.getShort("type")); + } catch (SQLException e) { + e.printStackTrace(); + } + + try { + entry.getValue().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + public boolean verify(@NotNull User user, @NotNull String password) { + return verifyHash(password, user.getPassword()); + } + + + public int insert(@NotNull User user) { + user.setPassword(hash(user.getPassword())); + + + Connection connection = database.getConnection(); + int updateCount = 0; + + try { + assert connection != null; + PreparedStatement statement = connection.prepareStatement("INSERT INTO user (name, email, password, type) values (?, ?, ?, ?)"); + statement.setString(1, user.getName()); + statement.setString(2, user.getEmail()); + statement.setString(3, user.getEmail()); + statement.setShort(4, user.getType()); + + updateCount += statement.executeUpdate(); + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return updateCount; + } + + public int delete(@NotNull User user) { + return database.delete(user.getId(), database.tableUser); + } + + @NotNull + private String hash(@NotNull String password) { + return BCrypt.hashpw(password, BCrypt.gensalt(logRounds)); + } + + private boolean verifyHash(@NotNull String plaintext, @NotNull String hash) { + return BCrypt.checkpw(plaintext, hash); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/Address.java b/src/main/java/de/hsel/itech/db/pojo/Address.java index 030e0be..39ea954 100644 --- a/src/main/java/de/hsel/itech/db/pojo/Address.java +++ b/src/main/java/de/hsel/itech/db/pojo/Address.java @@ -18,7 +18,7 @@ import lombok.RequiredArgsConstructor; @RequiredArgsConstructor public class Address { - @NonNull private Customer customer; + @NonNull private User user; private long id; @NonNull private String name; @NonNull private String street; diff --git a/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java b/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java index 0145e55..0d31b17 100644 --- a/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java @@ -21,7 +21,7 @@ import java.time.LocalDate; public class CreditCardPayment { private long id; - @NonNull private Customer customer; + @NonNull private User user; @NonNull private int number; @NonNull private String owner; @NonNull private LocalDate expiration; diff --git a/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java b/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java index 5e7afdd..35e3985 100644 --- a/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java @@ -19,7 +19,7 @@ import lombok.RequiredArgsConstructor; public class DebitCardPayment { private long id; - @NonNull private Customer customer; + @NonNull private User user; @NonNull private String iban; @NonNull private String bic; @NonNull private String owner; diff --git a/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java b/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java index b250a97..6e1411b 100644 --- a/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java @@ -19,5 +19,5 @@ import lombok.RequiredArgsConstructor; public class InvoicePayment { private long id; - @NonNull private Customer customer; + @NonNull private User user; } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java b/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java index be55830..0c4044b 100644 --- a/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java @@ -19,7 +19,7 @@ import lombok.RequiredArgsConstructor; public class PayPalPayment { private long id; - @NonNull private Customer customer; + @NonNull private User user; @NonNull private String email; @NonNull private String authCode; } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/ShoppingCart.java b/src/main/java/de/hsel/itech/db/pojo/ShoppingCart.java index 547743a..c7fd9ec 100644 --- a/src/main/java/de/hsel/itech/db/pojo/ShoppingCart.java +++ b/src/main/java/de/hsel/itech/db/pojo/ShoppingCart.java @@ -19,7 +19,7 @@ import lombok.RequiredArgsConstructor; public class ShoppingCart { private long id; - @NonNull private Customer customer; + @NonNull private User user; @NonNull private Book article; @NonNull private int count; } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/Customer.java b/src/main/java/de/hsel/itech/db/pojo/User.java similarity index 82% rename from src/main/java/de/hsel/itech/db/pojo/Customer.java rename to src/main/java/de/hsel/itech/db/pojo/User.java index 566afd3..e24b2e9 100644 --- a/src/main/java/de/hsel/itech/db/pojo/Customer.java +++ b/src/main/java/de/hsel/itech/db/pojo/User.java @@ -6,7 +6,7 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; /** - * POJO for Customer. + * POJO for User. * * @author Johannes Theiner * @version 0.1 @@ -16,11 +16,11 @@ import lombok.RequiredArgsConstructor; @Data @AllArgsConstructor @RequiredArgsConstructor -public class Customer { +public class User { private long id; @NonNull private String email; @NonNull private String name; @NonNull private String password; - @NonNull private boolean admin; + @NonNull private short type; } \ No newline at end of file