From 6dedc4cc556736f10ae570cbe8f86e4fbfff0a21 Mon Sep 17 00:00:00 2001 From: PauBarahona22 Date: Wed, 9 Oct 2024 18:24:41 +0200 Subject: [PATCH 1/3] 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 { +} From 0875a3ca2e6b3f9fbe08f21f618bac39f12d2d91 Mon Sep 17 00:00:00 2001 From: PauBarahona22 Date: Wed, 9 Oct 2024 18:39:24 +0200 Subject: [PATCH 2/3] Create Student entity role fix --- .../java/cat/udl/eps/softarch/demo/domain/Student.java | 7 ++++++- src/main/java/cat/udl/eps/softarch/demo/domain/User.java | 3 --- 2 files changed, 6 insertions(+), 4 deletions(-) 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 index 366a56f..ac258a6 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Student.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Student.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonValue; +import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import jakarta.validation.constraints.NotEmpty; import lombok.Data; @@ -21,5 +22,9 @@ public class Student extends User { @NotEmpty private String name; - + @Override + @ElementCollection + public Collection getAuthorities(){ + return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_CLIENT"); + } } \ 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 1456d8b..f6adb2e 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,9 +55,6 @@ 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"); } From 60d51cc39c4cccecceb0ea012a8b4e156e1d923e Mon Sep 17 00:00:00 2001 From: PauBarahona22 Date: Wed, 9 Oct 2024 18:50:07 +0200 Subject: [PATCH 3/3] Create Student entity role fix 2.0 --- src/main/java/cat/udl/eps/softarch/demo/domain/Student.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index ac258a6..3fdcba5 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Student.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Student.java @@ -25,6 +25,6 @@ public class Student extends User { @Override @ElementCollection public Collection getAuthorities(){ - return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_CLIENT"); + return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_STUDENT"); } } \ No newline at end of file