This repository has been archived by the owner on Sep 5, 2018. It is now read-only.
/
CurrentUser.java
160 lines (131 loc) · 4.04 KB
/
CurrentUser.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package util.security;
import static util.security.SessionParameters.LOCALE;
import static util.security.SessionParameters.USERNAME;
import java.util.Locale;
import java.util.UUID;
import models.User;
import org.apache.commons.lang3.LocaleUtils;
import play.cache.Cache;
import play.i18n.Lang;
import play.libs.F.Option;
import play.mvc.Http.Context;
import play.mvc.Http.Session;
import services.UserService;
public final class CurrentUser {
private static final String SESSION_KEY = "SESSION_ID";
private CurrentUser() {
throw new AssertionError();
}
// --- USER ---
public static boolean isAuthenticated() {
return getCurrentSession().isDefined()
&& (getSessionParameter(USERNAME) != null);
}
public static void setUser(User user) {
setSessionParameter(USERNAME, user.username);
Cache.set(USERNAME.getKey(), user);
if (user.preferredLocale != null) {
setPreferredLocale(user.preferredLocale);
}
}
public static Option<User> currentUser() {
if (!isAuthenticated()) {
return Option.None();
}
// Fetch current user.
Option<User> optUser;
User user = (User) Cache.get(USERNAME.getKey());
if (user == null) {
optUser = UserService.findByUsername(getSessionParameter(USERNAME));
if (optUser.isDefined()) {
Cache.set(USERNAME.getKey(), optUser.get());
}
} else {
optUser = Option.Some(user);
}
// If current user was not found, session cookie might be outdated.
// Clear session.
if (!optUser.isDefined()) {
logout();
}
return optUser;
}
// --- LOCALE ---
public static void setPreferredLocale(Locale locale) {
if (locale == null) {
removeSessionParameter(LOCALE);
} else {
setSessionParameter(LOCALE, locale.toString());
Context.current().changeLang(locale.getLanguage());
}
}
public static Option<Locale> preferredLocale() {
Locale locale = null;
if (isAuthenticated() && currentUser().isDefined()) {
User user = currentUser().get();
locale = user.preferredLocale;
}
if ((locale == null) && sessionContainsParameter(LOCALE)) {
locale = LocaleUtils.toLocale(getSessionParameter(LOCALE));
}
if ((locale == null) && (Context.current != null)) {
locale = Context.current().lang().toLocale();
}
return Option.Some(locale);
}
public static Option<Lang> preferredLang() {
if (preferredLocale().isDefined()) {
return Option.Some(new Lang(new play.api.i18n.Lang(
preferredLocale().get().getLanguage(), preferredLocale()
.get().getCountry())));
}
return Option.None();
}
// --- UTIL ---
public static void logout() {
if (getCurrentSession().isDefined()) {
Session session = getCurrentSession().get();
if (isAuthenticated()) {
removeSessionParameter(USERNAME);
}
session.clear();
}
Cache.remove(USERNAME.getKey());
}
static String sessionId() {
if (!getCurrentSession().isDefined()) {
return null;
}
if (getCurrentSession().get().containsKey(SESSION_KEY)) {
return getCurrentSession().get().get(SESSION_KEY);
}
// Create new session.
getCurrentSession().get().clear();
String sessionId = UUID.randomUUID().toString();
getCurrentSession().get().put(SESSION_KEY, sessionId);
return sessionId();
}
private static String getSessionParameter(SessionParameters parameter) {
return getCurrentSession().isDefined() ? getCurrentSession().get().get(
parameter.getKey()) : null;
}
private static void setSessionParameter(SessionParameters parameter,
String value) {
if (getCurrentSession().isDefined()) {
getCurrentSession().get().put(parameter.getKey(), value);
}
}
private static void removeSessionParameter(SessionParameters parameter) {
if (getCurrentSession().isDefined()) {
getCurrentSession().get().remove(parameter.getKey());
}
}
private static boolean sessionContainsParameter(SessionParameters parameter) {
return getCurrentSession().isDefined() ? getCurrentSession().get()
.containsKey(parameter.getKey()) : false;
}
private static Option<Session> getCurrentSession() {
return Context.current.get() == null ? Option.<Session> None() : Option
.Some(Context.current().session());
}
}