= UserService
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Allows the domain object to obtain the identity of the user interacting with said object.

If xref:refguide:applib:index/services/sudo/SudoService.adoc[SudoService] has been used to temporarily override the user and/or roles, then this service will report the overridden values instead. This is within the context of a thread.

In addition, if impersonation has been invoked through the xref:refguide:applib:index/services/user/ImpersonateMenu.adoc[ImpersonateMenu] , then this service will report the impersonated user, with the companion xref:refguide:applib:index/services/user/ImpersonatedUserHolder.adoc[ImpersonatedUserHolder] taking responsibilty for remembering the impersonated user over multiple (http) requests, eg using an http session. It's important to note that under these circumstances the user reported by this service (the "effective" user) will _not_ be the same as the user held in the xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] , as obtained by _InteractionLayerTracker#currentInteractionContext() InteractionLayerTracker_ (the "real" user).

== API

[source,java]
.UserService.java
----
class UserService {
  public static final String NOBODY;     // <.>
  Optional<UserMemento> currentUser()     // <.>
  boolean isCurrentUserWithSystemPrivileges()     // <.>
  UserMemento getUser()     // <.>
  UserMemento currentUserElseFail()     // <.>
  Optional<String> currentUserName()     // <.>
  String currentUserNameElseNobody()     // <.>
  boolean isImpersonating()     // <.>
  boolean supportsImpersonation()     // <.>
  void impersonateUser(String userName, List<String> roles, String multiTenancyToken)     // <.>
  void stopImpersonating()     // <.>
}
----

<.> xref:#NOBODY[NOBODY]
+
--
Default returned from _#currentUserNameElseNobody()_ .
--
<.> xref:#currentUser_[currentUser()]
+
--
Returns the details about the current user, either the "effective" user (if being _#impersonateUser(String, List, String) impersonated_ ) otherwise the "real" user (as obtained from the xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] of the current thread).
--
<.> xref:#isCurrentUserWithSystemPrivileges_[isCurrentUserWithSystemPrivileges()]
+
--
Whether the current user is the _system user_ (as obtained from the xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] of the current thread).
--
<.> xref:#getUser_[getUser()]
+
--
Gets the details about the _#currentUser()_ current user, if any (and returning `null` if there is none).
--
<.> xref:#currentUserElseFail_[currentUserElseFail()]
+
--
Gets the details about the _#currentUser()_ current user, throwing an exception if there is none.
--
<.> xref:#currentUserName_[currentUserName()]
+
--
Optionally gets the _#currentUser() current user_ 's name, obtained from xref:refguide:applib:index/services/user/UserMemento.adoc[UserMemento] .
--
<.> xref:#currentUserNameElseNobody_[currentUserNameElseNobody()]
+
--
Returns either the current user's name or else _#NOBODY_ .
--
<.> xref:#isImpersonating_[isImpersonating()]
+
--
Whether or not the user currently reported (in _#currentUser()_ and similar) is actually an impersonated user.
--
<.> xref:#supportsImpersonation_[supportsImpersonation()]
+
--
Whether impersonation is available for this request.
--
<.> xref:#impersonateUser_String_List_String[impersonateUser(String, List, String)]
+
--
Allows implementations to override the current user with another user.
--
<.> xref:#stopImpersonating_[stopImpersonating()]
+
--
For implementations that support impersonation, this is to programmatically stop impersonating a user
--

== Members

[#NOBODY]
=== NOBODY

Default returned from _#currentUserNameElseNobody()_ .

[#currentUser_]
=== currentUser()

Returns the details about the current user, either the "effective" user (if being _#impersonateUser(String, List, String) impersonated_ ) otherwise the "real" user (as obtained from the xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] of the current thread).

[#isCurrentUserWithSystemPrivileges_]
=== isCurrentUserWithSystemPrivileges()

Whether the current user is the _system user_ (as obtained from the xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] of the current thread).

[#getUser_]
=== getUser()

Gets the details about the _#currentUser()_ current user, if any (and returning `null` if there is none).

[#currentUserElseFail_]
=== currentUserElseFail()

Gets the details about the _#currentUser()_ current user, throwing an exception if there is none.

[#currentUserName_]
=== currentUserName()

Optionally gets the _#currentUser() current user_ 's name, obtained from xref:refguide:applib:index/services/user/UserMemento.adoc[UserMemento] .

[#currentUserNameElseNobody_]
=== currentUserNameElseNobody()

Returns either the current user's name or else _#NOBODY_ .

[#isImpersonating_]
=== isImpersonating()

Whether or not the user currently reported (in _#currentUser()_ and similar) is actually an impersonated user.

[#supportsImpersonation_]
=== supportsImpersonation()

Whether impersonation is available for this request.

The typical implementation uses an HTTP session, which is not guaranteed to be available for all viewers. Specifically, the Wicket viewer _does_ use HTTP sessions and therefore supports impersonation, but the RestfulObjects viewer does _not_ . This means that the result of this call varies on a request-by-request basis.

[#impersonateUser_String_List_String]
=== impersonateUser(String, List, String)

Allows implementations to override the current user with another user.

If this service (for this request) does not _#supportsImpersonation() support impersonation_ , then the request is just ignored.

IMPORTANT: this is intended for non-production environments only, where it can be invaluable (from a support perspective) to be able to quickly use the application "as if" logged in as another user.

[#stopImpersonating_]
=== stopImpersonating()

For implementations that support impersonation, this is to programmatically stop impersonating a user

If this service (for this request) does not _#supportsImpersonation() support impersonation_ , then the request is just ignored.

Intended to be called at some point after _#impersonateUser(String, List, String)_ would have been called.

include::hooks/UserService_010-implementation.adoc[]

include::hooks/UserService_020-examples-and-usage.adoc[]
