Add change password
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
package com.Samsung.TaskTraker_Server.Models;
|
package com.Samsung.TaskTraker_Server.Models;
|
||||||
|
import com.Samsung.TaskTraker_Server.Repository.DataBase;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class User {
|
public class User {
|
||||||
private final int ID;
|
private final int ID;
|
||||||
private final String login;
|
private final String login;
|
||||||
private final String password;
|
private String password;
|
||||||
|
|
||||||
public User(int ID, String login, String password) {
|
public User(int ID, String login, String password) {
|
||||||
this.ID = ID;
|
this.ID = ID;
|
||||||
@@ -24,6 +27,11 @@ public class User {
|
|||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPassword(String newPassword) throws SQLException {
|
||||||
|
DataBase.getInstance().UpdatePassword(newPassword, ID);
|
||||||
|
password = newPassword;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|||||||
@@ -91,6 +91,13 @@ public class DataBase {
|
|||||||
remove.executeUpdate();
|
remove.executeUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void UpdatePassword(String password, int userID) throws SQLException {
|
||||||
|
PreparedStatement update = connection.prepareStatement("UPDATE users SET password = ? WHERE id = ?");
|
||||||
|
update.setString(1, password);
|
||||||
|
update.setInt(2, userID);
|
||||||
|
update.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
public List<Task> getTaskList(User user) throws SQLException {
|
public List<Task> getTaskList(User user) throws SQLException {
|
||||||
PreparedStatement findTasks = connection.prepareStatement("SELECT * FROM tasks WHERE UserOwner = ?");
|
PreparedStatement findTasks = connection.prepareStatement("SELECT * FROM tasks WHERE UserOwner = ?");
|
||||||
findTasks.setInt(1, user.getID());
|
findTasks.setInt(1, user.getID());
|
||||||
|
|||||||
@@ -86,6 +86,26 @@ public class TaskTrakerServerApplication {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/user/ChangePassword")
|
||||||
|
public String editUserPassword(@RequestParam(name = "oldPassword", defaultValue = "") String oldPassword,
|
||||||
|
@RequestParam(name = "login", defaultValue = "") String login,
|
||||||
|
@RequestParam(name = "newPassword", defaultValue = "") String newPassword) {
|
||||||
|
if(oldPassword.isEmpty() || login.isEmpty() || newPassword.isEmpty())
|
||||||
|
return "{\n\t\"status\":\"error\"\n}";
|
||||||
|
|
||||||
|
try {
|
||||||
|
User user = UserRepository.getRepository().getUserByLogin(login);
|
||||||
|
if(user == null || !user.getPassword().equals(oldPassword))
|
||||||
|
return "{\n\t\"status\":\"error\"\n}";
|
||||||
|
|
||||||
|
user.setPassword(newPassword);
|
||||||
|
return "{\n\t\"status\":\"ok\"\n}";
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
return "{\n\t\"status\":\"error\"\n}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/user/task")
|
@GetMapping("/user/task")
|
||||||
public Task getTask(@RequestParam(value = "login", defaultValue = "null") String login,
|
public Task getTask(@RequestParam(value = "login", defaultValue = "null") String login,
|
||||||
@RequestParam(value = "password", defaultValue = "null") String pass,
|
@RequestParam(value = "password", defaultValue = "null") String pass,
|
||||||
|
|||||||
Reference in New Issue
Block a user