1 | package finki.it.phoneluxbackend.entities;
|
---|
2 |
|
---|
3 | import com.fasterxml.jackson.annotation.JsonIgnore;
|
---|
4 | import finki.it.phoneluxbackend.data.UserRole;
|
---|
5 | import lombok.*;
|
---|
6 | import org.springframework.security.core.GrantedAuthority;
|
---|
7 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
---|
8 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
9 |
|
---|
10 | import javax.persistence.*;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | @Getter
|
---|
17 | @Setter
|
---|
18 | @ToString
|
---|
19 | @EqualsAndHashCode
|
---|
20 | @NoArgsConstructor
|
---|
21 | @AllArgsConstructor
|
---|
22 | @Entity(name = "User")
|
---|
23 | @Table(name = "users")
|
---|
24 | public class User implements UserDetails {
|
---|
25 |
|
---|
26 | @SequenceGenerator(
|
---|
27 | name = "user_sequence",
|
---|
28 | sequenceName = "user_sequence",
|
---|
29 | allocationSize = 1
|
---|
30 | )
|
---|
31 | @Id
|
---|
32 | @GeneratedValue(
|
---|
33 | strategy = GenerationType.SEQUENCE,
|
---|
34 | generator = "user_sequence"
|
---|
35 | )
|
---|
36 | private Long id;
|
---|
37 | private String firstName;
|
---|
38 | private String lastName;
|
---|
39 | private String email;
|
---|
40 | private String password;
|
---|
41 | @Enumerated(EnumType.STRING)
|
---|
42 | private UserRole userRole;
|
---|
43 | private Boolean locked = false;
|
---|
44 | private Boolean enabled = false;
|
---|
45 |
|
---|
46 | @ManyToMany
|
---|
47 | @JoinTable(
|
---|
48 | name = "users_favourite_offers",
|
---|
49 | joinColumns = @JoinColumn(name = "user_id"),
|
---|
50 | inverseJoinColumns = @JoinColumn(name = "offer_id")
|
---|
51 | )
|
---|
52 | @JsonIgnore
|
---|
53 | private List<PhoneOffer> favouriteOffers = new ArrayList<PhoneOffer>();
|
---|
54 |
|
---|
55 | private String specifications;
|
---|
56 |
|
---|
57 | public User(String firstName, String lastName, String email, String password, UserRole userRole) {
|
---|
58 | this.firstName = firstName;
|
---|
59 | this.lastName = lastName;
|
---|
60 | this.email = email;
|
---|
61 | this.password = password;
|
---|
62 | this.userRole = userRole;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public User(Long id, String firstName, UserRole userRole) {
|
---|
66 | this.id = id;
|
---|
67 | this.firstName = firstName;
|
---|
68 | this.userRole = userRole;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | public User(Long id, String firstName, String lastName, String email, UserRole userRole) {
|
---|
73 | this.id = id;
|
---|
74 | this.firstName = firstName;
|
---|
75 | this.lastName = lastName;
|
---|
76 | this.email = email;
|
---|
77 | this.userRole = userRole;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public User(String firstName, String lastName, String email, UserRole userRole) {
|
---|
81 | this.firstName = firstName;
|
---|
82 | this.lastName = lastName;
|
---|
83 | this.email = email;
|
---|
84 | this.userRole = userRole;
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public Collection<? extends GrantedAuthority> getAuthorities() {
|
---|
89 | SimpleGrantedAuthority authority = new SimpleGrantedAuthority(userRole.name());
|
---|
90 | return Collections.singletonList(authority);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public String getPassword() {
|
---|
95 | return password;
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public String getUsername() {
|
---|
100 | return email;
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public boolean isAccountNonExpired() {
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public boolean isAccountNonLocked() {
|
---|
110 | return !locked;
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public boolean isCredentialsNonExpired() {
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public boolean isEnabled() {
|
---|
120 | return enabled;
|
---|
121 | }
|
---|
122 | }
|
---|