forked from osopromadze/Spring-Boot-Blog-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserPrincipal.java
More file actions
121 lines (94 loc) · 2.64 KB
/
UserPrincipal.java
File metadata and controls
121 lines (94 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.sopromadze.blogapi.security;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sopromadze.blogapi.model.user.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class UserPrincipal implements UserDetails {
private static final long serialVersionUID = 1L;
private Long id;
private String firstName;
private String lastName;
private String username;
@JsonIgnore
private String email;
@JsonIgnore
private String password;
private Collection<? extends GrantedAuthority> authorities;
public UserPrincipal(Long id, String firstName, String lastName, String username, String email, String password,
Collection<? extends GrantedAuthority> authorities) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.username = username;
this.email = email;
this.password = password;
if (authorities == null) {
this.authorities = null;
} else {
this.authorities = new ArrayList<>(authorities);
}
}
public static UserPrincipal create(User user) {
List<GrantedAuthority> authorities = user.getRoles().stream()
.map(role -> new SimpleGrantedAuthority(role.getName().name())).collect(Collectors.toList());
return new UserPrincipal(user.getId(), user.getFirstName(), user.getLastName(), user.getUsername(),
user.getEmail(), user.getPassword(), authorities);
}
public Long getId() {
return id;
}
public String getEmail() {
return email;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return authorities == null ? null : new ArrayList<>(authorities);
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
public boolean equals(Object object) {
if (this == object)
return true;
if (object == null || getClass() != object.getClass())
return false;
UserPrincipal that = (UserPrincipal) object;
return Objects.equals(id, that.id);
}
public int hashCode() {
return Objects.hash(id);
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}