From 82021dca5a9a3f9f74b3e5c469d802f9e7908f9a Mon Sep 17 00:00:00 2001 From: Ralf Haferkamp Date: Thu, 2 Feb 2023 12:56:11 +0100 Subject: [PATCH] graph: Fix event handling for /groups When running the graph service standalone with token auth we don't have a user in the context. Avoid nil point exception when issueing events in such a setup. --- services/graph/pkg/service/v0/groups.go | 38 +++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/services/graph/pkg/service/v0/groups.go b/services/graph/pkg/service/v0/groups.go index 90ee6c189..65bf24bfe 100644 --- a/services/graph/pkg/service/v0/groups.go +++ b/services/graph/pkg/service/v0/groups.go @@ -89,8 +89,13 @@ func (g Graph) PostGroup(w http.ResponseWriter, r *http.Request) { } if grp != nil && grp.Id != nil { - currentUser := revactx.ContextMustGetUser(r.Context()) - g.publishEvent(events.GroupCreated{Executant: currentUser.Id, GroupID: *grp.Id}) + e := events.GroupCreated{ + GroupID: grp.GetId(), + } + if currentUser, ok := revactx.ContextGetUser(r.Context()); ok { + e.Executant = currentUser.GetId() + } + g.publishEvent(e) } render.Status(r, http.StatusOK) // FIXME 201 should return 201 created render.JSON(w, r, grp) @@ -239,8 +244,13 @@ func (g Graph) DeleteGroup(w http.ResponseWriter, r *http.Request) { return } - currentUser := revactx.ContextMustGetUser(r.Context()) - g.publishEvent(events.GroupDeleted{Executant: currentUser.Id, GroupID: groupID}) + e := events.GroupDeleted{ + GroupID: groupID, + } + if currentUser, ok := revactx.ContextGetUser(r.Context()); ok { + e.Executant = currentUser.GetId() + } + g.publishEvent(e) render.Status(r, http.StatusNoContent) render.NoContent(w, r) } @@ -345,8 +355,14 @@ func (g Graph) PostGroupMember(w http.ResponseWriter, r *http.Request) { return } - currentUser := revactx.ContextMustGetUser(r.Context()) - g.publishEvent(events.GroupMemberAdded{Executant: currentUser.Id, GroupID: groupID, UserID: id}) + e := events.GroupMemberAdded{ + GroupID: groupID, + UserID: id, + } + if currentUser, ok := revactx.ContextGetUser(r.Context()); ok { + e.Executant = currentUser.GetId() + } + g.publishEvent(e) render.Status(r, http.StatusNoContent) render.NoContent(w, r) } @@ -396,8 +412,14 @@ func (g Graph) DeleteGroupMember(w http.ResponseWriter, r *http.Request) { } return } - currentUser := revactx.ContextMustGetUser(r.Context()) - g.publishEvent(events.GroupMemberRemoved{Executant: currentUser.Id, GroupID: groupID, UserID: memberID}) + e := events.GroupMemberRemoved{ + GroupID: groupID, + UserID: memberID, + } + if currentUser, ok := revactx.ContextGetUser(r.Context()); ok { + e.Executant = currentUser.GetId() + } + g.publishEvent(e) render.Status(r, http.StatusNoContent) render.NoContent(w, r) }