Skip to content

Commit

Permalink
Merge pull request #18 from pastore99/13-provide-code-and-ui-for-rese…
Browse files Browse the repository at this point in the history
…tting-password

13 provide code and UI for resetting password
  • Loading branch information
panuozzo77 authored Dec 22, 2023
2 parents 496a569 + a8734fe commit 64968b4
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/main/java/model/DAO/DAOAPersonalInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
* The DAOAPersonalInfo class provides methods for creating a user's personal info registry in the database.
*/
public class DAOAPersonalInfo {

/**
* Creates a user's personal info registry in the database.
*
* @param id the ID of the user
* @param name the first name of the user
* @param surname the last name of the user
* @return {@code true} if the user's personal info registry is created successfully,
* {@code false} otherwise
*/
public boolean createRegistry(int id, String name, String surname) {
Connection connection = null;
PreparedStatement preparedStatementPersonalInfo = null;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/model/DAO/DAOConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import java.util.logging.Logger;

/**
* The DAOConnection class provides methods for establishing and releasing database connections.
* Initialize the data source by loading the database driver class and creating the data source object.
* @throws ClassNotFoundException if the database driver class is not found
*/
public class DAOConnection {
private static DataSource dataSource;
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/model/DAO/DAOLicense.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* The DAOLicense class provides methods for retrieving and activating licenses from a database.
*/
public class DAOLicense {

/**
* This method extracts License object data from a ResultSet
*
* @param resultSet ResultSet object from license table
* @return License object containing data from resultSet
* @throws SQLException if there is any SQL related error
*/
private static License extractLicenseFromResultSet(ResultSet resultSet) throws SQLException {
License license = new License();
license.setSequence(resultSet.getString("Sequence"));
Expand All @@ -18,6 +28,12 @@ private static License extractLicenseFromResultSet(ResultSet resultSet) throws S
return license;
}

/**
* Search for a license by its code in the database.
*
* @param code the license code to search for.
* @return the License if it is found, else null.
*/
public License getLicenseByCode(String code) {
String query = "SELECT * FROM license WHERE Sequence = ?";
Connection connection = null;
Expand Down Expand Up @@ -49,6 +65,12 @@ public License getLicenseByCode(String code) {
return null;
}

/**
* Activate a license and link it to a user.
*
* @param license the License object to activate.
* @param userId the ID of the user to link to the license.
*/
public void activate(License license, int userId) {
String updateQuery = "UPDATE TalkAID2.license SET active = TRUE, ID_User = ? WHERE Sequence = ?";
Connection connection = null;
Expand All @@ -73,6 +95,4 @@ public void activate(License license, int userId) {
}
}
}


}
37 changes: 37 additions & 0 deletions src/main/java/model/DAO/DAOUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* DAOUser is a class that provides methods for accessing the User table in the database.
*/
public class DAOUser {

/**
* Private helper method that takes a ResultSet object and constructs a User object from it.
*
* @param resultSet A ResultSet object containing a row from the User table.
* @return A User object constructed from the ResultSet.
* @throws SQLException If any database error occurs.
*/
private User getUserFromResultSet(ResultSet resultSet) throws SQLException {
User user = new User();

Expand All @@ -25,6 +35,12 @@ private User getUserFromResultSet(ResultSet resultSet) throws SQLException {
return user;
}

/**
* Checks if a given email is present in the User table.
*
* @param email The email to check.
* @return true if the email exists in the User table; false otherwise.
*/
public boolean checkIfEmailExists(String email) {
Connection connection = null;
PreparedStatement preparedStatement = null;
Expand Down Expand Up @@ -66,6 +82,14 @@ public boolean checkIfEmailExists(String email) {
return false;
}

/**
* Creates a new user in the User table.
*
* @param email The email of the user.
* @param password The password of the user.
* @param therapistId The ID of the therapist associated with the user.
* @return The ID of the newly created user, or -1 if an error occurs.
*/
public int createUser(String email, String password, int therapistId) {
Connection connection = null;
PreparedStatement preparedStatement = null;
Expand Down Expand Up @@ -113,6 +137,12 @@ public int createUser(String email, String password, int therapistId) {
return -1;
}

/**
* Retrieves a User from the User table based on an ID or an email.
*
* @param idOrEmail Either an Integer representing the User's ID or a String representing the User's email.
* @return The User object if found, or null if not found.
*/
public User getUserByIdOrEmail(Object idOrEmail) {
Connection connection = null;
PreparedStatement preparedStatement = null;
Expand Down Expand Up @@ -155,6 +185,13 @@ public User getUserByIdOrEmail(Object idOrEmail) {
return null; // or you may throw an exception here
}

/**
* Resets a user's password.
*
* @param email The email of the user.
* @param newPassword The new password to set for the user.
* @return true if the password was successfully updated; false otherwise.
*/
public boolean resetPassword(String email, String newPassword) {
Connection connection = null;
PreparedStatement preparedStatement = null;
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/model/service/email/EmailManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import java.io.InputStream;
import java.util.Properties;

/**
* This class provides functionality for sending email messages.
* It reads the configuration from a properties file named 'email.properties'.
*/
public class EmailManager {

private static final String EMAIL_PROPERTIES = "/email.properties";
Expand All @@ -15,10 +19,20 @@ public class EmailManager {

private Properties emailProps;

/**
* Constructor for the EmailManager class.
* It initializes the email properties by loading them from a properties file.
*/
public EmailManager() {
this.emailProps = loadEmailProperties();
}

/**
* This method sends an email to a given address, with a specified subject and body.
* @param toAddress the destination address of the email
* @param subject the subject of the email
* @param body the body of the email
*/
public void sendEmail(String toAddress, String subject, String body) {
Session session = getSession();
MimeMessage message = new MimeMessage(session);
Expand All @@ -32,6 +46,10 @@ public void sendEmail(String toAddress, String subject, String body) {
}
}

/**
* This method returns a session for email interactions, using the loaded properties and authenticating the user.
* @return a Session object
*/
private Session getSession() {
return Session.getDefaultInstance(emailProps, new javax.mail.Authenticator() {
@Override
Expand All @@ -41,6 +59,12 @@ protected PasswordAuthentication getPasswordAuthentication() {
});
}

/**
* This method sends a preconfigured email message, using a specified session.
* @param session the Session object
* @param message the MimeMessage object, holding the information about the message to send
* @throws MessagingException if the send operation fails
*/
private void sendEmail(Session session, MimeMessage message) throws MessagingException {
Transport transport = session.getTransport("smtp");
String host = HOST;
Expand All @@ -55,13 +79,25 @@ private void sendEmail(Session session, MimeMessage message) throws MessagingExc
transport.close();
}

/**
* This method generates a MimeMessage, filling it with the necessary information.
* @param message the MimeMessage object to fill with information
* @param toAddress the destination address of the email
* @param subject the subject of the email
* @param body the body of the email
* @throws MessagingException if the setting operation fails
*/
private void generateMessage(MimeMessage message, String toAddress, String subject, String body) throws MessagingException {
message.setFrom(new InternetAddress(emailProps.getProperty("email.string")));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
message.setSubject(subject);
message.setText(body);
}

/**
* This method loads the email properties from a resource file named 'email.properties'.
* @return a Properties object, holding the email properties
*/
private Properties loadEmailProperties() {
Properties props = new Properties();
try (InputStream input = EmailManager.class.getResourceAsStream(EMAIL_PROPERTIES)) {
Expand All @@ -77,4 +113,4 @@ private Properties loadEmailProperties() {
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
return props;
}
}
}

0 comments on commit 64968b4

Please sign in to comment.