From 6dedc4cc556736f10ae570cbe8f86e4fbfff0a21 Mon Sep 17 00:00:00 2001 From: PauBarahona22 Date: Wed, 9 Oct 2024 18:24:41 +0200 Subject: [PATCH] Create Student entity and repository with role addition in user --- .../udl/eps/softarch/demo/domain/Student.java | 25 +++++++++++++++++++ .../udl/eps/softarch/demo/domain/User.java | 3 +++ .../demo/repository/StudentRepository.java | 7 ++++++ 3 files changed, 35 insertions(+) create mode 100644 src/main/java/cat/udl/eps/softarch/demo/domain/Student.java create mode 100644 src/main/java/cat/udl/eps/softarch/demo/repository/StudentRepository.java diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Student.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Student.java new file mode 100644 index 0000000..366a56f --- /dev/null +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Student.java @@ -0,0 +1,25 @@ +package cat.udl.eps.softarch.demo.domain; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonValue; +import jakarta.persistence.Entity; +import jakarta.validation.constraints.NotEmpty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.AuthorityUtils; + +import java.util.Collection; + +@Entity +@Data +@EqualsAndHashCode(callSuper = true) +public class Student extends User { + + @NotEmpty + private String phoneNumber; + + @NotEmpty + private String name; + +} \ No newline at end of file diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/User.java b/src/main/java/cat/udl/eps/softarch/demo/domain/User.java index f6adb2e..1456d8b 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/User.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/User.java @@ -55,6 +55,9 @@ public void encodePassword() { @JsonValue(value = false) @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Collection getAuthorities() { + if (this instanceof Student) { + return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_STUDENT"); + } return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"); } diff --git a/src/main/java/cat/udl/eps/softarch/demo/repository/StudentRepository.java b/src/main/java/cat/udl/eps/softarch/demo/repository/StudentRepository.java new file mode 100644 index 0000000..057bde1 --- /dev/null +++ b/src/main/java/cat/udl/eps/softarch/demo/repository/StudentRepository.java @@ -0,0 +1,7 @@ +package cat.udl.eps.softarch.demo.repository; + +import cat.udl.eps.softarch.demo.domain.Student; +import org.springframework.data.repository.CrudRepository; + +public interface StudentRepository extends CrudRepository { +}