Skip to content

Commit

Permalink
Add UserModel getUser for AdminEvent (#47)
Browse files Browse the repository at this point in the history
* Add UserModel getUser for AdminEvent

* Rename user to authUser for AdminEvent bindings

* Update Script documentation with authUser
  • Loading branch information
philipsabri authored Jan 17, 2024
1 parent cccf89b commit 9cb5283
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The script event listener allows you to run JS event listeners in the same fashi
- `event`: the Event or AdminEvent
- `realm`: the RealmModel
- `user`: the UserModel (for `onEvent` only)
- `authUser`: the UserModel (for `onAdminEvent` only)
- `session`: the KeycloakSession
- `LOG`: a JBoss Logger

Expand Down Expand Up @@ -68,7 +69,9 @@ function onAdminEvent(event, representation) {
" on " +
event.resourceType +
" in realm " +
realm.name
realm.name +
" by user " +
authUser.username
);
}
```
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/phasetwo/keycloak/events/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public static UserModel getUser(KeycloakSession session, Event event) {
: null;
}

public static UserModel getAuthUser(KeycloakSession session, AdminEvent event) {
RealmModel realm = getRealm(session, event);
return (event.getAuthDetails().getUserId() != null)
? session.users().getUserById(realm, event.getAuthDetails().getUserId())
: null;
}

public static String toString(Event event) {
StringBuilder sb = new StringBuilder();
sb.append("type=");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void onEvent(AdminEvent event, boolean b) {
bindings -> {
bindings.put("event", event);
bindings.put("realm", Events.getRealm(session, event));
bindings.put("authUser", Events.getAuthUser(session, event));
bindings.put("session", session);
bindings.put("LOG", log);
});
Expand Down

0 comments on commit 9cb5283

Please sign in to comment.