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

added connection between groupe and user in jpa

parent 96a73739
No related branches found
No related tags found
No related merge requests found
......@@ -35,6 +35,7 @@ public class SecurityConfig {
.authorizeHttpRequests(auth-> auth
.requestMatchers(HttpMethod.POST, "api/auth/**").permitAll()
.requestMatchers(HttpMethod.POST, "api/user/**").permitAll()
.requestMatchers(HttpMethod.GET, "api/groups/**").permitAll()
.requestMatchers(HttpMethod.GET, "swagger-ui/**").permitAll()
.requestMatchers(HttpMethod.GET, "/v3/api-docs/**").permitAll()
.anyRequest().authenticated()
......
......@@ -9,12 +9,16 @@ import ntnu.idatt2016.v233.SmartMat.service.user.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
/**
* The user controller is responsible for handling requests related to users.
* It uses the user service to handle the requests.
* @author Birk
* @version 1.2
* @since 20.04.2023
*/
@AllArgsConstructor
@RestController
@RequestMapping("/api/user")
......@@ -73,5 +77,20 @@ public class UserController {
return ResponseEntity.ok(newUser);
}
/**
* Get a user from the database.
* @param username The username of the user to be fetched.
* @return The user with the given username.
*/
@GetMapping("/get/{username}")
public ResponseEntity<User> getUser(@PathVariable String username) {
return userService.getUserFromUsername(username)
.map(user -> {
user.setPassword(null);
return ResponseEntity.ok(user);
})
.orElseGet(() -> ResponseEntity.notFound().build());
}
}
\ No newline at end of file
package ntnu.idatt2016.v233.SmartMat.entity.group;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import java.util.List;
/**
* Group is an entity class representing a group in the system.
......@@ -36,4 +38,9 @@ public class Group {
@Column(name = "group_name")
String groupName;
@OneToMany(mappedBy = "group")
@JsonIgnoreProperties({"password", "group"})
private List<User> users;
}
package ntnu.idatt2016.v233.SmartMat.entity.user;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonIncludeProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
......@@ -54,6 +57,12 @@ public class User implements UserDetails {
@Enumerated(EnumType.STRING)
private Authority authority;
@ManyToOne
@JoinColumn(name = "group_id")
@JsonIgnoreProperties("users")
private Group group;
/**
* used when created jwts and validating user authority
* @return the users authority
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment