From c5fb75f515419a3bdf7842bcc114b6dbdcf98e31 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Fri, 10 Jun 2022 01:12:27 +0800 Subject: [PATCH] Added changePasswd() --- .../library/database/DatabaseHelper.java | 8 ++++++++ .../library/database/operation/StudentOA.java | 19 +++++++++++++++++++ .../library/database/operation/TeacherOA.java | 19 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/Library/src/main/java/com/cfive/classroom/library/database/DatabaseHelper.java b/Library/src/main/java/com/cfive/classroom/library/database/DatabaseHelper.java index bade514..7e86430 100644 --- a/Library/src/main/java/com/cfive/classroom/library/database/DatabaseHelper.java +++ b/Library/src/main/java/com/cfive/classroom/library/database/DatabaseHelper.java @@ -191,6 +191,10 @@ public class DatabaseHelper { return StudentOA.checkPasswd(stuID, passwd); } + public static boolean changePasswdInStudent(long stuID, String passwd) throws NoConfigException, SQLException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException { + return StudentOA.changePasswd(stuID, passwd); + } + public static Student insertIntoStudent(long stuID, String stuName, Gender gender, long classID, String passwd) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException { return StudentOA.insert(stuID, stuName, gender, classID, passwd); } @@ -215,6 +219,10 @@ public class DatabaseHelper { return TeacherOA.checkPasswd(tchID, passwd); } + public static boolean changePasswdInTeacher(long tchID, String passwd) throws NoConfigException, SQLException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException { + return TeacherOA.changePasswd(tchID, passwd); + } + public static Teacher insertIntoTeacher(long tchID, String tchName, Gender gender, int facID, String passwd) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException { return TeacherOA.insert(tchID, tchName, gender, facID, passwd); } diff --git a/Library/src/main/java/com/cfive/classroom/library/database/operation/StudentOA.java b/Library/src/main/java/com/cfive/classroom/library/database/operation/StudentOA.java index e9abdfe..3fba840 100644 --- a/Library/src/main/java/com/cfive/classroom/library/database/operation/StudentOA.java +++ b/Library/src/main/java/com/cfive/classroom/library/database/operation/StudentOA.java @@ -56,6 +56,25 @@ public class StudentOA { return pbkdf2Util.authenticate(passwd, student.getPassword(), student.getSalt()); } + public static boolean changePasswd(long stuID, String passwd) throws NoConfigException, SQLException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException { + Student student = select(stuID); + if (student == null) { + throw new DependenciesNotFoundException(); + } + PBKDF2Util pbkdf2Util = new PBKDF2Util(); + String salt = pbkdf2Util.generateSalt(); + String encryptedPassword = pbkdf2Util.getEncryptedPassword(passwd, salt); + String sql = "UPDATE student SET passwd=?,salt=? WHERE stuID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, encryptedPassword); + preparedStatement.setString(2, salt); + preparedStatement.setLong(3, stuID); + return preparedStatement.executeUpdate() == 1; + } + } + } + public static Student insert(long stuID, String stuName, Gender gender, long classID, String passwd) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException, NoSuchAlgorithmException, InvalidKeySpecException { if (isExists(stuID)) throw new AlreadyExistsException(); if (!ClassOA.isExists(classID)) throw new DependenciesNotFoundException(); diff --git a/Library/src/main/java/com/cfive/classroom/library/database/operation/TeacherOA.java b/Library/src/main/java/com/cfive/classroom/library/database/operation/TeacherOA.java index 8139ae0..41c7f2a 100644 --- a/Library/src/main/java/com/cfive/classroom/library/database/operation/TeacherOA.java +++ b/Library/src/main/java/com/cfive/classroom/library/database/operation/TeacherOA.java @@ -52,6 +52,25 @@ public class TeacherOA { return pbkdf2Util.authenticate(passwd, teacher.getPassword(), teacher.getSalt()); } + public static boolean changePasswd(long tchID, String passwd) throws NoConfigException, SQLException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException { + Teacher teacher = select(tchID); + if (teacher == null) { + throw new DependenciesNotFoundException(); + } + PBKDF2Util pbkdf2Util = new PBKDF2Util(); + String salt = pbkdf2Util.generateSalt(); + String encryptedPassword = pbkdf2Util.getEncryptedPassword(passwd, salt); + String sql = "UPDATE teacher SET passwd=?,salt=? WHERE tchID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, encryptedPassword); + preparedStatement.setString(2, salt); + preparedStatement.setLong(3, tchID); + return preparedStatement.executeUpdate() == 1; + } + } + } + public static Teacher insert(long tchID, String tchName, Gender gender, int facID, String passwd) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException, NoSuchAlgorithmException, InvalidKeySpecException { if (isExists(tchID)) throw new AlreadyExistsException(); if (!FacultyOA.isExists(facID)) throw new DependenciesNotFoundException();