Skip to content
Snippets Groups Projects
Commit 6dc30e01 authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

Merge branch 'feature/58-user-entity-jpa' into 'main'

Feature/58 user entity jpa

See merge request idatt2106-v23-03/backend!27
parents 96cfcbad 09c2929d
No related branches found
No related tags found
No related merge requests found
Showing
with 169 additions and 46 deletions
...@@ -8,5 +8,5 @@ ...@@ -8,5 +8,5 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-20" project-jdk-type="JavaSDK" /> <component name="ProjectRootManager" version="2" languageLevel="JDK_19" project-jdk-name="openjdk-20" project-jdk-type="JavaSDK" />
</project> </project>
\ No newline at end of file
...@@ -24,9 +24,9 @@ import org.springframework.security.provisioning.InMemoryUserDetailsManager; ...@@ -24,9 +24,9 @@ import org.springframework.security.provisioning.InMemoryUserDetailsManager;
/** /**
* Configures the authentication for the application. * Configures the authentication for the application.
* @author Anders (young buck) * @author Anders and birk
* @version 1.0 * @version 1.1
* @since 04.04.2023 * @since 05.04.2023
*/ */
@Configuration @Configuration
public class AuthenticationConfig { public class AuthenticationConfig {
...@@ -55,18 +55,20 @@ public class AuthenticationConfig { ...@@ -55,18 +55,20 @@ public class AuthenticationConfig {
} }
/** /**
* Configures the authentication manager. * Configures the authentication manager for the application.
* * @param userDetailsService the user details service
* @param userDetailsService The UserDetailsService instance for managing user details. * @param passwordEncoder the password encoder
* @return An AuthenticationManager instance. * @return the authentication manager
*/ */
@Bean @Bean
public AuthenticationManager authManager(UserDetailsService userDetailsService) { AuthenticationManager authenticationManager (UserDetailsService userDetailsService, PasswordEncoder passwordEncoder){
var authProvider = new DaoAuthenticationProvider(); DaoAuthenticationProvider autprovider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService); autprovider.setUserDetailsService(userDetailsService);
return new ProviderManager(authProvider); autprovider.setPasswordEncoder(passwordEncoder);
return new ProviderManager(autprovider);
} }
/** /**
* Configures the JWKSource instance for handling RSA keys. * Configures the JWKSource instance for handling RSA keys.
* *
......
package ntnu.idatt2016.v233.SmartMat.controller; package ntnu.idatt2016.v233.SmartMat.controller;
import ntnu.idatt2016.v233.SmartMat.model.request.LoginRequest; import ntnu.idatt2016.v233.SmartMat.entity.request.LoginRequest;
import ntnu.idatt2016.v233.SmartMat.service.TokenService; import ntnu.idatt2016.v233.SmartMat.service.TokenService;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
......
package ntnu.idatt2016.v233.SmartMat.model; package ntnu.idatt2016.v233.SmartMat.entity;
/** /**
* This is a record class representing an allergy * This is a record class representing an allergy
......
package ntnu.idatt2016.v233.SmartMat.model; package ntnu.idatt2016.v233.SmartMat.entity;
/** /**
* Fridge is a record class representing a fridge in the system. * Fridge is a record class representing a fridge in the system.
......
package ntnu.idatt2016.v233.SmartMat.model; package ntnu.idatt2016.v233.SmartMat.entity;
/** /**
* Recipe is a record class representing a recipe in the system. * Recipe is a record class representing a recipe in the system.
......
package ntnu.idatt2016.v233.SmartMat.model; package ntnu.idatt2016.v233.SmartMat.entity;
/** /**
......
package ntnu.idatt2016.v233.SmartMat.model.product; package ntnu.idatt2016.v233.SmartMat.entity.product;
import lombok.Builder; import lombok.Builder;
......
package ntnu.idatt2016.v233.SmartMat.model.request; package ntnu.idatt2016.v233.SmartMat.entity.request;
/** /**
* LoginRequest is a record class representing a login request. * LoginRequest is a record class representing a login request.
......
package ntnu.idatt2016.v233.SmartMat.model.user; package ntnu.idatt2016.v233.SmartMat.entity.user;
/** /**
* Authority is a record class representing an authority in the system. * Authority is a enum representing an authority in the system.
* *
* @author Anders * @author Birk
* @version 1.0 * @version 2.0
* @since 04.04.2023 * @since 05.04.2023
* *
* @param username The username of the user. * @param username The username of the user.
* @param authority The authority of the user. * @param authority The authority of the user.
*/ */
public record Authority(String username, String authority) { public enum Authority{
USER,
ADMIN
} }
package ntnu.idatt2016.v233.SmartMat.model.user; package ntnu.idatt2016.v233.SmartMat.entity.user;
/** /**
* Profile is a record class representing a profile in the system. * Profile is a record class representing a profile in the system.
......
package ntnu.idatt2016.v233.SmartMat.entity.user;
import jakarta.persistence.*;
import lombok.Builder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.List;
/**
* User is a class representing a user in the system.
* It implements the UserDetails interface.
*
* @author Anders and Birk
* @version 2.0
* @since 05.04.2023
*
*/
@Entity(name = "users")
@Builder
public class User implements UserDetails {
@Id
@Column(name = "user_name")
private String username;
@Column(name = "password")
private String password;
@Column(name = "enabled")
private boolean enabled;
@Enumerated(EnumType.STRING)
private Authority authority;
/**
* used when created jwts and validating user authority
* @return the users authority
*/
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority(authority.name()));
}
/**
* getter for the user password used in userdetails service
* @return the users password
*/
@Override
public String getPassword() {
return this.password;
}
/**
* getter for the user username used in userdetails service
* @return the users username
*/
@Override
public String getUsername() {
return this.username;
}
/**
* not used
* @return true if the account is not expired
*/
@Override
public boolean isAccountNonExpired() {
return true;
}
/**
* not used
* @return true if the account is not locked
*/
@Override
public boolean isAccountNonLocked() {
return true;
}
/**
* not used
* @return true if the credentials are not expired
*/
@Override
public boolean isCredentialsNonExpired() {
return true;
}
/**
* used in authentication
* @return true if the user is enabled
*/
@Override
public boolean isEnabled() {
return this.enabled;
}
}
package ntnu.idatt2016.v233.SmartMat.model.user;
/**
* User is a record class representing a user in the system.
*
* @author Anders
* @version 1.0
* @since 04.04.2023
*
* @param username The username of the user.
* @param password The password of the user.
* @param enabled Whether the user is enabled.
*/
public record User(String username, String password,
boolean enabled, Profile profile) {
}
package ntnu.idatt2016.v233.SmartMat.repository; package ntnu.idatt2016.v233.SmartMat.repository;
import ntnu.idatt2016.v233.SmartMat.model.Allergy; import ntnu.idatt2016.v233.SmartMat.entity.Allergy;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
......
package ntnu.idatt2016.v233.SmartMat.repository; package ntnu.idatt2016.v233.SmartMat.repository;
import ntnu.idatt2016.v233.SmartMat.model.product.Product; import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
......
...@@ -3,7 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.repository; ...@@ -3,7 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.repository;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import ntnu.idatt2016.v233.SmartMat.model.Recipe; import ntnu.idatt2016.v233.SmartMat.entity.Recipe;
/** /**
* This interface defines the methods for the recipe repository * This interface defines the methods for the recipe repository
......
...@@ -3,7 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.repository; ...@@ -3,7 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.repository;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import ntnu.idatt2016.v233.SmartMat.model.ShoppingList; import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
/** /**
* This interface defines the methods for the shopping list repository * This interface defines the methods for the shopping list repository
......
package ntnu.idatt2016.v233.SmartMat.repository;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
/**
* repo for user entity
* uses mysql from server when ran
* uses h2 in memory database when testing
* @author birk
* @version 1.0
* @since 05.04.2023
*/
public interface UserRepository extends JpaRepository<User, Long>{
/**
* gets user from username out of database
* @param username username of user
* @return user
*/
Optional<User> findByUsername(String username);
}
package ntnu.idatt2016.v233.SmartMat.service; package ntnu.idatt2016.v233.SmartMat.service;
import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.model.product.Product;
import ntnu.idatt2016.v233.SmartMat.repository.ProductRepository; import ntnu.idatt2016.v233.SmartMat.repository.ProductRepository;
import ntnu.idatt2016.v233.SmartMat.util.ProductUtil; import ntnu.idatt2016.v233.SmartMat.util.ProductUtil;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
......
...@@ -3,7 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.service; ...@@ -3,7 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.service;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import ntnu.idatt2016.v233.SmartMat.model.ShoppingList; import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
import ntnu.idatt2016.v233.SmartMat.repository.ShoppingListRepository; import ntnu.idatt2016.v233.SmartMat.repository.ShoppingListRepository;
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment