Skip to content

Commit

Permalink
Merge pull request #35 from cathrkri/feature/password-hashing
Browse files Browse the repository at this point in the history
Wrote javadoc to User
  • Loading branch information
robinsp authored Mar 3, 2026
2 parents db8bb74 + 8a43cfa commit 654e852
Showing 1 changed file with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ntnu.sytemutvikling.team6.models.user;

import ntnu.sytemutvikling.team6.security.PasswordHasher;

import java.util.UUID;

// TODO: Enhetstesting mangler
import ntnu.sytemutvikling.team6.security.PasswordHasher;

/**
* Represents a user.
* Represents a user in the system.
*
* <p>A user has a unique identifier, personal information, a hashed password, a role,
* settings and an inbox.
*
* <p>The password is never stored ad plain text. It is hashed using {@link PasswordHasher}
*
* @author Robin Strand Prestmo
*/
Expand All @@ -23,7 +25,7 @@ public class User {
private final Inbox inbox;

/**
* Creates a new user
* Creates a new user.
*
* @param id gives the user a unique identifier with UUID
* @param name the name of the user
Expand All @@ -32,7 +34,7 @@ public class User {
* @param role users role
* @param settings the user´s settings
* @param inbox the user´s inbox
*
* @throws IllegalArgumentException if any required argument is invalid.
*/
public User(UUID id,
String name,
Expand All @@ -50,7 +52,8 @@ public User(UUID id,
}

if (email == null || email.isBlank() || !email.contains("@") || !email.contains(".")) {
throw new IllegalArgumentException("Email cannot be null or blank, and must contain '@' and '.'");
throw new IllegalArgumentException("Email cannot be null or blank,"
+ " and must contain '@' and '.'");
}

if (role == null) {
Expand Down Expand Up @@ -102,26 +105,52 @@ public Inbox getInbox() {

// Add Setters

/**
* Updates the users name.
*
* @param name the new name
* @throws IllegalArgumentException if the name is null or blank
*/
public void setName(String name) {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be null or blank.");
}
this.name = name;
}

/**
* Updates the users password.
*
* <p>The password is hashed before being stored.
*
* @param password the new password
*/
public void setPassword(String password) {
this.passwordHash = passwordHasher.getHashPassword(password);
}

/**
* Updates the users email.
*
* @param email the new email
* @throws IllegalArgumentException if the email is null, blank, or does not contain '@' or '.'
*/
public void setEmail(String email) {
if (email == null || email.isBlank() || !email.contains("@") || !email.contains(".")) {
throw new IllegalArgumentException("Email cannot be null or blank, and must contains '@' and '.'");
throw new IllegalArgumentException("Email cannot be null or blank,"
+ " and must contains '@' and '.'");
}
this.email = email;
}

// Other methods

/**
* Checks if the provided password matches the stored password.
*
* @param password the password to verify
* @return true if the password is correct, false otherwise
*/
public boolean checkPassword(String password) {
return passwordHasher.isValidPassword(password, passwordHash);
}
Expand Down

0 comments on commit 654e852

Please sign in to comment.