public class UserContext extends Object implements sirius.kernel.async.SubContext
An instance of this class is present in the CallContext
and takes care of
picking the right user manager to authenticate users or store / load them from a session.
This class also manages messages shown to the user.
Modifier and Type | Field and Description |
---|---|
static sirius.kernel.health.Log |
LOG
Contains the logger user used by the auth framework
|
static String |
MDC_SCOPE
The key used to store the current scope in the MDC
|
static String |
MDC_USER_ID
The key used to store the current user id in the MDC
|
static String |
MDC_USER_NAME
The key used to store the current user name in the MDC
|
Constructor and Description |
---|
UserContext() |
Modifier and Type | Method and Description |
---|---|
void |
addFieldError(String field,
String value)
Adds an error for a given field
|
void |
addFieldErrorMessage(String field,
String errorMessage)
Adds an error message for the given field
|
void |
addMessage(Message msg)
Adds a message to be shown to the user.
|
void |
attachUserToSession()
Binds the currently active user to the session.
|
void |
detach() |
void |
detachUserFromSession()
Removes the authentication and user identity from the session.
|
sirius.kernel.async.SubContext |
fork() |
static UserContext |
get()
Retrieves the current UserContext from the
CallContext . |
static ScopeInfo |
getCurrentScope()
Boilerplate method to quickly access the current scope.
|
static UserInfo |
getCurrentUser()
Boilerplate method to quickly access the current user.
|
String |
getFieldErrorMessage(String field)
Returns an error message for the given field
|
String |
getFieldValue(String field)
Returns the originally submitted field value even if it was rejected due to an error.
|
String |
getFieldValue(String field,
Object value)
Returns the originally submitted field value even if it was rejected due to an error.
|
Collection<String> |
getFieldValues(String field)
Returns all values submitted for the given field
|
static <H> H |
getHelper(Class<H> helperType)
Returns the helper of the given class for the current scope.
|
static <H> H |
getHelper(String name)
Returns the helper with the given name for the current scope.
|
List<Message> |
getMessages()
Returns all messages to be shown to the user.
|
ScopeInfo |
getScope()
Returns the currently active scope.
|
static UserSettings |
getSettings()
Returns the configuration with is specific to the current user.
|
UserInfo |
getUser()
Returns the current user.
|
UserInfo |
getUserForScope(ScopeInfo scope)
Returns the used which would be the current user if the space with the given id would be active.
|
UserManager |
getUserManager()
Determines and returns the current user manager.
|
List<Message> |
getUserSpecificMessages()
Returns all user specific messages without any globally or locally generated ones.
|
static void |
handle(Throwable e)
Handles the given exception by passing it to
Exceptions and by creating an
appropriate message for the user. |
boolean |
hasError(String field)
Determines if there is an error or error message for the given field
|
boolean |
isUserPresent()
Determines if the user is present.
|
static void |
message(Message msg)
Adds a message to the current UserContext.
|
void |
runAs(UserInfo user,
Runnable section)
Executes the given section as the given user.
|
void |
setCurrentScope(ScopeInfo scope)
Installs the given scope as current scope.
|
void |
setCurrentUser(UserInfo user)
Installs the given user as current user.
|
static void |
setErrorMessage(String field,
String errorMessage)
Adds an error message for the given field
|
static void |
setFieldError(String field,
Object value)
Adds a field error to the current UserContext.
|
String |
signalFieldError(String field)
Returns "has-error" if an error was added for the given field.
|
public static final String MDC_SCOPE
public static final String MDC_USER_ID
public static final String MDC_USER_NAME
public static final sirius.kernel.health.Log LOG
public static UserContext get()
CallContext
.public static UserInfo getCurrentUser()
getUser()
public static UserSettings getSettings()
This is boilerplate for UserContext.getCurrentUser().getConfig()
.
UserInfo.getSettings()
@Nonnull public static <H> H getHelper(@Nonnull Class<H> helperType)
NOTE: This helper is per ScopeInfo
not per UserInfo
! Therefore no user dependent data may be kept
in its state.
H
- the generic type of the helperhelperType
- the type of the helper to fetch@Nonnull public static <H> H getHelper(@Nonnull String name)
NOTE: This helper is per ScopeInfo
not per UserInfo
! Therefore no user dependent data may be kept
in its state.
H
- the generic type of the helpername
- the name of the helper to fetchpublic static ScopeInfo getCurrentScope()
getScope()
public static void handle(@Nullable Throwable e)
Exceptions
and by creating an
appropriate message for the user.e
- the exception to handle. If the given exception is null nothing will happen.public static void message(Message msg)
msg
- the message to addpublic static void setFieldError(String field, Object value)
field
- the field for which an error occurredvalue
- the value which was rejectedpublic void setCurrentScope(ScopeInfo scope)
For generic web requests, this is not necessary, as the scope is auto-detected.
scope
- the scope to setpublic void setCurrentUser(@Nullable UserInfo user)
For generic web requests, this is not necessary, as the user is auto-detected.
user
- the user to setpublic void runAs(@Nullable UserInfo user, @Nonnull Runnable section)
Restores the previously active user once the section is left.
user
- the user to installsection
- the section to execute as userpublic void addMessage(Message msg)
msg
- the message to be shown to the userpublic List<Message> getMessages()
public List<Message> getUserSpecificMessages()
public void addFieldError(String field, String value)
field
- the name of the fieldvalue
- the value which was supplied and rejectedpublic boolean hasError(String field)
field
- the field to check for errorspublic String signalFieldError(String field)
field
- the field to checkpublic String getFieldValue(String field, Object value)
field
- the name of the form fieldvalue
- the entity value (used if no error occurred)public String getFieldValue(String field)
field
- the name of the form fieldWebContext
otherwisepublic Collection<String> getFieldValues(String field)
field
- the name of the field which values should be extractedpublic static void setErrorMessage(String field, String errorMessage)
field
- name of the form fielderrorMessage
- value to be addedpublic void addFieldErrorMessage(String field, String errorMessage)
field
- name of the form fielderrorMessage
- value to be addedpublic String getFieldErrorMessage(String field)
field
- name of the form fieldpublic UserInfo getUser()
If no user is present yet, it tries to parse the current WebContext
and retireve the user from the
session.
public UserInfo getUserForScope(ScopeInfo scope)
You can use ScopeInfo.DEFAULT_SCOPE
to access the user of the default scope which will
most probably the administrative backend.
Note that this method will only check the session (UserManager.findUserForRequest(WebContext)
) and will
not try to perform a login via credentials as given in the current request.
scope
- the scope to fetch the user forUserInfo.NOBODY
if no user was foundpublic boolean isUserPresent()
This can be either direclty via a setCurrentUser(UserInfo)
or implicitely via getUser()
public void attachUserToSession()
This will make the authentication of a user persistent als long as the session remains
@Nonnull public UserManager getUserManager()
The user manager is determined by the current scope.
getCurrentScope()
,
ScopeDetector
public void detachUserFromSession()
This can be considered a logout.
public ScopeInfo getScope()
This is determined using the ScopeDetector
or it will always be the ScopeInfo.DEFAULT_SCOPE
if
no scope detector is present.
public sirius.kernel.async.SubContext fork()
fork
in interface sirius.kernel.async.SubContext
public void detach()
detach
in interface sirius.kernel.async.SubContext
Copyright © 2018. All rights reserved.