forked from mrigor87/CalorieManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthorizedUser.java
More file actions
56 lines (45 loc) · 1.54 KB
/
AuthorizedUser.java
File metadata and controls
56 lines (45 loc) · 1.54 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
package ru.javawebinar.topjava;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import ru.javawebinar.topjava.model.User;
import ru.javawebinar.topjava.to.UserTo;
import ru.javawebinar.topjava.util.UserUtil;
import static java.util.Objects.requireNonNull;
/**
* GKislin
* 06.03.2015.
*/
public class AuthorizedUser extends org.springframework.security.core.userdetails.User {
private static final long serialVersionUID = 1L;
private final UserTo userTo;
public AuthorizedUser(User user) {
super(user.getEmail(), user.getPassword(), user.isEnabled(), true, true, true, user.getRoles());
this.userTo = UserUtil.asTo(user);
}
public static AuthorizedUser safeGet() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null) {
return null;
}
Object principal = auth.getPrincipal();
return (principal instanceof AuthorizedUser) ? (AuthorizedUser) principal : null;
}
public static AuthorizedUser get() {
AuthorizedUser user = safeGet();
requireNonNull(user, "No authorized user found");
return user;
}
public static int id() {
return get().userTo.getId();
}
public static int getCaloriesPerDay() {
return get().userTo.getCaloriesPerDay();
}
public UserTo getUserTo() {
return userTo;
}
@Override
public String toString() {
return userTo.toString();
}
}