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