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

removed authorty tabel, and fixed the auth issue

parent e0653dfa
No related branches found
No related tags found
No related merge requests found
Showing
with 9 additions and 214 deletions
......@@ -75,10 +75,9 @@ public class UserController {
.lastName(user.lastName())
.dateOfBirth(user.birthDate())
.enabled(true)
.authorities(new ArrayList<>())
.authority(Authority.USER)
.build();
userService.saveUser(newUser);
userService.addAuthorityToUser(newUser.getUsername(), Authority.USER);
newUser.setPassword(null);
return ResponseEntity.status(HttpStatus.CREATED).body(newUser);
}
......
package ntnu.idatt2016.v233.SmartMat.entity.user;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority;
import java.util.ArrayList;
import java.util.List;
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class AuthorityTable {
public AuthorityTable(Authority authority) {
this.authority = authority;
}
@Id
private Authority authority;
@ManyToMany(mappedBy = "authorities")
@JsonIgnoreProperties({"authorities", "password"})
private List<User> users;
/**
* Adds a user to the authority
* @param user user to add
*/
public void addUser(User user){
if(users == null)
users = new ArrayList<>();
users.add(user);
}
}
......@@ -79,13 +79,9 @@ public class User implements UserDetails {
private List<Recipe> recipes;
@ManyToMany
@JoinTable(
name = "user_authority",
joinColumns = @JoinColumn(name = "username"),
inverseJoinColumns = @JoinColumn(name = "authority"))
@JsonIgnoreProperties({"users"})
private List<AuthorityTable> authorities = new ArrayList<>();
@Column(name = "authority")
private Authority authority;
/**
......@@ -128,9 +124,7 @@ public class User implements UserDetails {
*/
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(this.authorities.stream().map(AuthorityTable::getAuthority)
.map(Authority::toString).map(SimpleGrantedAuthority::new)
.toArray(GrantedAuthority[]::new));
return List.of(new SimpleGrantedAuthority(this.authority.toString()));
}
/**
......@@ -187,11 +181,5 @@ public class User implements UserDetails {
return this.enabled;
}
public void addAuthority(AuthorityTable authority){
if (this.authorities == null) {
this.authorities = new ArrayList<>();
}
this.authorities.add(authority);
}
}
package ntnu.idatt2016.v233.SmartMat.repository.user;
import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority;
import ntnu.idatt2016.v233.SmartMat.entity.user.AuthorityTable;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* AuthoritesRepository is a repository for Authorites.
*/
public interface AuthoritesRepository extends JpaRepository<AuthorityTable, Authority> {
}
package ntnu.idatt2016.v233.SmartMat.service.user;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority;
import ntnu.idatt2016.v233.SmartMat.entity.user.AuthorityTable;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import ntnu.idatt2016.v233.SmartMat.repository.user.AuthoritesRepository;
import org.springframework.stereotype.Service;
import java.util.Optional;
/**
* AuthoritesService is a service for Authorites.
* It is used to get an authority by the authority.
* If the authority does not exist, it will be created.
* @author Birk
* @version 1.0
* @since 25.04.2023
*/
@Service
@AllArgsConstructor
public class AuthoritesService {
private final AuthoritesRepository authoritesRepository;
/**
* Gets an authority by the authority.
* @param auth The authority to get
* @return The authority if it exists, otherwise it will be created and returned
*/
public AuthorityTable getAuthorityByAuth(Authority auth){
Optional<AuthorityTable> temp = authoritesRepository.findById(auth);
return temp.orElseGet(() -> authoritesRepository.save(new AuthorityTable(auth)));
}
/**
* Adds a user to an authority.
* @param auth The authority to add the user to
* @param user The user to add to the authority
*/
public void addUserToAuthoriy(AuthorityTable auth, User user){
auth.addUser(user);
}
}
......@@ -4,10 +4,8 @@ import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority;
import ntnu.idatt2016.v233.SmartMat.entity.Recipe;
import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy;
import ntnu.idatt2016.v233.SmartMat.entity.user.AuthorityTable;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import ntnu.idatt2016.v233.SmartMat.repository.AllergyRepository;
import ntnu.idatt2016.v233.SmartMat.repository.user.AuthoritesRepository;
import ntnu.idatt2016.v233.SmartMat.repository.user.UserRepository;
import ntnu.idatt2016.v233.SmartMat.service.RecipeService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
......@@ -32,10 +30,6 @@ public class UserService {
private RecipeService recipeService;
private AuthoritesService authoritesService;
private AuthoritesRepository authoritesRepository;
AllergyRepository allergyRepository;
/**
......@@ -132,24 +126,6 @@ public class UserService {
}
/**
* Adds authority to user
* @param username username of user
* @param authority authority to add
* @return user with added authority
*/
public User addAuthorityToUser(String username, Authority authority){
User user = userRepository.findByUsername(username)
.orElseThrow(()-> new UsernameNotFoundException("did not find user"));
AuthorityTable auth = authoritesService.getAuthorityByAuth(authority);
user.addAuthority(auth);
auth.addUser(user);
authoritesRepository.save(auth);
return userRepository.save(user);
}
/**
* Adds allergy to user
......
......@@ -17,7 +17,7 @@ class UserTest {
void testGetAuthorities() {
User user = User.builder()
.username("johndoe")
.authorities(List.of(new AuthorityTable(Authority.USER)))
.authority(Authority.USER)
.build();
......
......@@ -39,7 +39,7 @@ public class UserRepositoryTest {
.email("testuser@example.com")
.firstName("Test")
.lastName("User")
.authorities(new ArrayList<>())
.authority(Authority.USER)
.dateOfBirth(Date.valueOf("1990-01-01"))
.build();
entityManager.persist(user);
......@@ -54,7 +54,7 @@ public class UserRepositoryTest {
.email("testuser@example.no")
.firstName("TestUSERNAME")
.lastName("UserTEST")
.authorities(new ArrayList<>())
.authority(Authority.USER)
.dateOfBirth(Date.valueOf("1989-01-01"))
.build();
userRepository.save(user);
......@@ -113,7 +113,7 @@ public class UserRepositoryTest {
.email("newemail@example.com")
.firstName("New")
.lastName("Name")
.authorities(new ArrayList<>())
.authority(Authority.USER)
.dateOfBirth(Date.valueOf("1995-01-01"))
.build();
userRepository.save(modifiedUser);
......
package ntnu.idatt2016.v233.SmartMat.service.user;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Optional;
import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority;
import ntnu.idatt2016.v233.SmartMat.entity.user.AuthorityTable;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import ntnu.idatt2016.v233.SmartMat.repository.user.AuthoritesRepository;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
class AuthoritesServiceTest {
@InjectMocks
private AuthoritesService authoritesService;
@Mock
private AuthoritesRepository authoritesRepository;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void testGetAuthorityByAuth() {
Authority authorityType = Authority.USER;
AuthorityTable authorityTable = new AuthorityTable(authorityType);
when(authoritesRepository.findById(authorityType)).thenReturn(Optional.of(authorityTable));
AuthorityTable result = authoritesService.getAuthorityByAuth(authorityType);
assertEquals(authorityType, result.getAuthority());
}
@Test
void testGetAuthorityByAuthCreateNew() {
Authority authorityType = Authority.ADMIN;
when(authoritesRepository.findById(authorityType)).thenReturn(Optional.empty());
when(authoritesRepository.save(any(AuthorityTable.class))).thenAnswer(invocation -> invocation.getArgument(0));
AuthorityTable result = authoritesService.getAuthorityByAuth(authorityType);
assertEquals(authorityType, result.getAuthority());
}
@Test
void testAddUserToAuthority() {
Authority authorityType = Authority.USER;
AuthorityTable authorityTable = new AuthorityTable(authorityType);
User user = new User();
user.setUsername("testUser");
authoritesService.addUserToAuthoriy(authorityTable, user);
assertNotNull(authorityTable.getUsers());
assertTrue(authorityTable.getUsers().contains(user));
}
}
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