-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added injectable class to provide the requesting user of a REST call
- Loading branch information
Showing
3 changed files
with
71 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...rest/ojcms-rest-server/src/main/java/de/adito/ojcms/rest/security/UserRequestContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package de.adito.ojcms.rest.security; | ||
|
||
import de.adito.ojcms.rest.security.user.OJUser; | ||
|
||
import javax.enterprise.context.RequestScoped; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Provides the user context of a REST request. | ||
* | ||
* @author Simon Danner, 11.05.2020 | ||
*/ | ||
@RequestScoped | ||
public class UserRequestContext | ||
{ | ||
private OJUser requestingUser; | ||
|
||
/** | ||
* The requesting user of the currently active REST call. | ||
* | ||
* @param <USER> the generic type of the user of the application | ||
* @return the requesting user | ||
* @throws IllegalStateException if no user has been set for the call | ||
*/ | ||
public <USER extends OJUser> USER getRequestingUser() | ||
{ | ||
if (requestingUser == null) | ||
throw new IllegalStateException("No user set for active request!"); | ||
|
||
//noinspection unchecked | ||
return (USER) requestingUser; | ||
} | ||
|
||
/** | ||
* Sets the requesting user for the currently active REST call. | ||
* | ||
* @param pRequestingUser the requesting user to set for the call | ||
*/ | ||
void setAuthenticatedUserForRequest(OJUser pRequestingUser) | ||
{ | ||
requestingUser = Objects.requireNonNull(pRequestingUser); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters