
: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.



== Usage

This domain service is useful both for xref:testing:integtestsupport:about.adoc[integration testing] and while running xref:testing:fixtures:about.adoc[fixture scripts].

For example, to integration test a workflow system, whereby objects are moved from one user to another, it is helpful to switch the effective user to verify that the task was assigned correctly.

Or, this fixture script uses the `SudoService` to set up `ToDoItem` objects:

[source,java]
----
protected void execute(final ExecutionContext ec) {
    ...
    sudoService.call(
        InteractionContext.switchUser(UserMemento.ofName("joe")),
        () -> wrap(toDoItem).completed()
    );
    ...
}
----


== Disabling permission checks

Sometimes it's useful to be able to "switch off" security checks, for example when running in initial seed scripts or fixtures that use the xref:refguide:applib:index/services/wrapper/WrapperFactory.adoc[WrapperFactory].
This can be done using any user that has the special xref:refguide:applib:index/services/sudo/SudoService.adoc#ACCESS_ALL_ROLE[ACCESS_ALL_ROLE] role.

For example:

[source,java]
----
protected void execute(final ExecutionContext ec) {
    ...
    sudoService.call(InteractionContext.switchUser(
            UserMemento.ofNameAndRoles("seed-user", SudoService.ACCESS_ALL_ROLE),
            () -> { /* ... */ }
    );
    ...
}
----




== See also

* xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[]
* xref:refguide:applib:index/services/iactnlayer/InteractionService.adoc[InteractionService]
* xref:refguide:applib:index/services/wrapper/WrapperFactory.adoc[WrapperFactory]

