Skip to content

Commit

Permalink
Create Student entity and repository with role addition in user
Browse files Browse the repository at this point in the history
  • Loading branch information
PauBarahona22 committed Oct 9, 2024
1 parent 18ca6df commit 6dedc4c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Student.java
Original file line number Diff line number Diff line change
@@ -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;

}
3 changes: 3 additions & 0 deletions src/main/java/cat/udl/eps/softarch/demo/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public void encodePassword() {
@JsonValue(value = false)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Collection<? extends GrantedAuthority> getAuthorities() {
if (this instanceof Student) {
return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_STUDENT");
}
return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Student, Long> {
}

0 comments on commit 6dedc4c

Please sign in to comment.