diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java index ceb23e7a1e23291edd300dd6c51f7d12506f0b8b..e3099ab28c7cc9bc45df8e3b00d0fd5bad226684 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java @@ -62,7 +62,6 @@ public class UserController { } User newUser = User.builder() - .authority(Authority.USER) .username(user.username()) .password(passwordEncoder.encode(user.password())) .email(user.email()) @@ -71,8 +70,8 @@ public class UserController { .dateOfBirth(user.birthDate()) .enabled(true) .build(); - userService.saveUser(newUser); + userService.addAuthorityToUser(newUser.getUsername(), Authority.USER); newUser.setPassword(null); return ResponseEntity.status(HttpStatus.CREATED).body(newUser); } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java index 567da5ed60d03c3c499178a34ce671c9e50b6ed5..032e2045a7da7cc82cfb03585d5a8342b8b78284 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java @@ -45,7 +45,7 @@ public class Group { String linkCode; @Column(name = "is_open") - boolean open; + Boolean open; @OneToMany @JoinColumn(name = "group_id") diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/AuthorityTable.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/AuthorityTable.java new file mode 100644 index 0000000000000000000000000000000000000000..a1446f49db8b2921bf4a4bcbdc7ddf22cac9392e --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/AuthorityTable.java @@ -0,0 +1,42 @@ +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); + } +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java index 01ebf01dd306d5e85a6f039b8177558458f5bb20..751ff5934bd603424b46b8c201132c4965faff9c 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java @@ -56,9 +56,6 @@ public class User implements UserDetails { @Column(name = "birthdate") private Date dateOfBirth; - @Enumerated(EnumType.STRING) - private Authority authority; - @OneToMany @JoinColumn(name = "username") @@ -84,6 +81,15 @@ 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; + + /** * adds an allergy to the user * @param allergy the allergy to add to the user @@ -124,7 +130,9 @@ public class User implements UserDetails { */ @Override public Collection<? extends GrantedAuthority> getAuthorities() { - return List.of(new SimpleGrantedAuthority(authority.name())); + return List.of(this.authorities.stream().map(AuthorityTable::getAuthority) + .map(Authority::toString).map(SimpleGrantedAuthority::new) + .toArray(GrantedAuthority[]::new)); } /** @@ -181,4 +189,11 @@ 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); + } + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/user/AuthoritesRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/user/AuthoritesRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..d2140f7cb3294e5d39bd41c02d0b024fcd919ceb --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/user/AuthoritesRepository.java @@ -0,0 +1,11 @@ +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> { +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java index 32e2b3684d8d0f348cd76f7de244b1b27c6661c4..63d196849336cd8439da664e340f1c72a7b7fe85 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java @@ -115,9 +115,9 @@ public class GroupService { Optional<Group> answer = groupRepository.findByGroupId(id); if (answer.isPresent()) { Group realGroup = answer.get(); - realGroup.setOpen(!realGroup.isOpen()); - System.out.println(realGroup.isOpen()); - return Optional.of(groupRepository.save(realGroup).isOpen()); + realGroup.setOpen(!realGroup.getOpen()); + System.out.println(realGroup.getOpen()); + return Optional.of(groupRepository.save(realGroup).getOpen()); } return Optional.empty(); } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/AuthoritesService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/AuthoritesService.java new file mode 100644 index 0000000000000000000000000000000000000000..887daed938288a73be6cfebb16fc46998cf72240 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/AuthoritesService.java @@ -0,0 +1,47 @@ +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); + } + +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java index 08db7eecab721cecc1291eacfcc824b141e5cd1f..6dc2d77222dbf744fa5f6a10f525b5416d0c5e84 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java @@ -1,8 +1,11 @@ package ntnu.idatt2016.v233.SmartMat.service.user; import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority; import ntnu.idatt2016.v233.SmartMat.entity.Recipe; +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 ntnu.idatt2016.v233.SmartMat.repository.user.UserRepository; import ntnu.idatt2016.v233.SmartMat.service.RecipeService; import org.springframework.security.core.userdetails.UsernameNotFoundException; @@ -25,6 +28,10 @@ public class UserService { private RecipeService recipeService; + private AuthoritesService authoritesService; + + private AuthoritesRepository authoritesRepository; + /** * gets user from username out of database @@ -118,4 +125,24 @@ public class UserService { recipeService.addUserToRecipe(tempRecipe, user); userRepository.save(user); } + + + /** + * 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); + } } diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/entity/user/UserTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/entity/user/UserTest.java index 54388372923421ca68d786fd4ade4292a8adfd58..30af02fb6b242a397b57a5bbb6a280d7db913b5a 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/entity/user/UserTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/entity/user/UserTest.java @@ -1,21 +1,27 @@ package ntnu.idatt2016.v233.SmartMat.entity.user; import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority; +import ntnu.idatt2016.v233.SmartMat.service.user.UserService; import org.junit.jupiter.api.Test; import org.springframework.security.core.authority.SimpleGrantedAuthority; import java.util.Collections; +import java.util.List; import static org.junit.jupiter.api.Assertions.*; class UserTest { + @Test void testGetAuthorities() { User user = User.builder() - .authority(Authority.USER) + .username("johndoe") + .authorities(List.of(new AuthorityTable(Authority.USER))) .build(); + + assertEquals(Collections.singletonList(new SimpleGrantedAuthority(Authority.USER.name())), user.getAuthorities()); } diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/UserRepositoryTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/UserRepositoryTest.java index 70053a4f299cdd15e9b46a18b1651f544dcf7c9d..f693c42223c69c0b0fd39b328812806cdfc6912e 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/UserRepositoryTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/UserRepositoryTest.java @@ -39,7 +39,6 @@ public class UserRepositoryTest { .firstName("Test") .lastName("User") .dateOfBirth(Date.valueOf("1990-01-01")) - .authority(Authority.USER) .build(); entityManager.persist(user); } @@ -54,7 +53,6 @@ public class UserRepositoryTest { .firstName("TestUSERNAME") .lastName("UserTEST") .dateOfBirth(Date.valueOf("1989-01-01")) - .authority(Authority.USER) .build(); userRepository.save(user); @@ -113,7 +111,6 @@ public class UserRepositoryTest { .firstName("New") .lastName("Name") .dateOfBirth(Date.valueOf("1995-01-01")) - .authority(Authority.ADMIN) .build(); userRepository.save(modifiedUser); @@ -163,7 +160,6 @@ public class UserRepositoryTest { .firstName("Test") .lastName("User") .dateOfBirth(Date.valueOf("1990-01-01")) - .authority(Authority.USER) .allergies(List.of(allergy)) .build(); diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/service/user/UserServiceTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/service/user/UserServiceTest.java index b5002f6aa51a95c9fefc7251e16e7283c766d0b8..deafc26ab4ac2cb8c452053afb047bff83e121af 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/service/user/UserServiceTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/service/user/UserServiceTest.java @@ -40,7 +40,6 @@ public class UserServiceTest { .firstName("Test") .lastName("User") .dateOfBirth(Date.valueOf("1990-01-01")) - .authority(Authority.USER) .build(); when(userRepository.findByUsername(user.getUsername())).thenReturn(Optional.of(user)); @@ -93,7 +92,6 @@ public class UserServiceTest { .firstName("Test") .lastName("User") .dateOfBirth(Date.valueOf("1990-01-01")) - .authority(Authority.USER) .build(); when(userRepository.findByUsername(user.getUsername())).thenReturn(Optional.of(user)); @@ -119,7 +117,6 @@ public class UserServiceTest { .firstName("Test") .lastName("User") .dateOfBirth(Date.valueOf("1990-01-01")) - .authority(Authority.USER) .build(); when(userRepository.findByUsername(user.getUsername())).thenReturn(Optional.empty()); @@ -141,7 +138,6 @@ public class UserServiceTest { .firstName("Test") .lastName("User") .dateOfBirth(Date.valueOf("1990-01-01")) - .authority(Authority.USER) .build(); when(userRepository.findByEmail(user.getEmail())).thenReturn(Optional.of(user)); @@ -166,7 +162,6 @@ public class UserServiceTest { .firstName("Test") .lastName("User") .dateOfBirth(Date.valueOf("1990-01-01")) - .authority(Authority.USER) .build(); when(userRepository.findByUsername(user.getUsername())).thenReturn(Optional.of(user));