-
Notifications
You must be signed in to change notification settings - Fork 682
/
ProfileManager.java
131 lines (117 loc) · 4.57 KB
/
ProfileManager.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
package org.pac4j.core.profile;
import org.pac4j.core.context.Pac4jConstants;
import org.pac4j.core.context.WebContext;
import java.util.*;
/**
* <p>This class is a generic way to manage the current user profile(s), i.e. the one(s) of the current authenticated user.</p>
* <p>It may be partially re-implemented for specific needs / frameworks.</p>
*
* @author Jerome Leleu
* @since 1.8.0
*/
public class ProfileManager {
protected final WebContext context;
public ProfileManager(final WebContext context) {
this.context = context;
}
/**
* Retrieve the first user profile if it exists.
*
* @param readFromSession if the user profile must be read from session
* @return the user profile
*/
public Optional<UserProfile> get(final boolean readFromSession) {
final LinkedHashMap<String, UserProfile> profiles = retrieveAll(readFromSession);
if (profiles.size() == 0) {
return Optional.empty();
} else {
return Optional.of(profiles.values().iterator().next());
}
}
/**
* Retrieve all user profiles.
*
* @param readFromSession if the user profiles must be read from session
* @return the user profiles.
*/
public List<UserProfile> getAll(final boolean readFromSession) {
final LinkedHashMap<String, UserProfile> profiles = retrieveAll(readFromSession);
if (profiles == null) {
return Collections.unmodifiableList(new ArrayList<>());
} else {
final List<UserProfile> listProfiles = new ArrayList<>();
for (final String key : profiles.keySet()) {
listProfiles.add(profiles.get(key));
}
return Collections.unmodifiableList(listProfiles);
}
}
private LinkedHashMap<String, UserProfile> retrieveAll(final boolean readFromSession) {
LinkedHashMap<String, UserProfile> profiles = null;
final Object objSession = this.context.getRequestAttribute(Pac4jConstants.USER_PROFILES);
if (objSession != null && objSession instanceof LinkedHashMap) {
profiles = (LinkedHashMap<String, UserProfile>) objSession;
}
if ((profiles == null || profiles.size() == 0) && readFromSession) {
final Object objRequest = this.context.getSessionAttribute(Pac4jConstants.USER_PROFILES);
if (objRequest != null && objRequest instanceof LinkedHashMap) {
profiles = (LinkedHashMap<String, UserProfile>) objRequest;
}
}
if (profiles == null) {
return new LinkedHashMap<>();
} else {
return profiles;
}
}
/**
* Remove the current user profile(s).
*
* @param removeFromSession if the user profile(s) must be removed from session
*/
public void remove(final boolean removeFromSession) {
if (removeFromSession) {
this.context.setSessionAttribute(Pac4jConstants.USER_PROFILES, new LinkedHashMap<String, UserProfile>());
}
this.context.setRequestAttribute(Pac4jConstants.USER_PROFILES, new LinkedHashMap<String, UserProfile>());
}
/**
* Save the given user profile (replace the current one if multi profiles are not supported, add it otherwise).
*
* @param saveInSession if the user profile must be saved in session
* @param profile a given user profile
* @param multiProfile whether multiple profiles are supported
*/
public void save(final boolean saveInSession, final UserProfile profile, final boolean multiProfile) {
final LinkedHashMap<String, UserProfile> profiles;
String clientName = profile.getClientName();
if (clientName == null) {
clientName = "DEFAULT";
}
if (multiProfile) {
profiles = retrieveAll(saveInSession);
profiles.remove(clientName);
} else {
profiles = new LinkedHashMap<>();
}
profiles.put(clientName, profile);
if (saveInSession) {
this.context.setSessionAttribute(Pac4jConstants.USER_PROFILES, profiles);
}
this.context.setRequestAttribute(Pac4jConstants.USER_PROFILES, profiles);
}
/**
* Perform a logout by removing the current user profile(s) from the session as well.
*/
public void logout() {
remove(true);
}
/**
* Tests if the current user is authenticated (meaning a user profile exists).
*
* @return whether the current user is authenticated
*/
public boolean isAuthenticated() {
return get(true).isPresent();
}
}