From de1588843aabdf2c1394c32e7761961f8da9ba77 Mon Sep 17 00:00:00 2001 From: Ralf Haferkamp Date: Thu, 29 Sep 2022 12:12:54 +0200 Subject: [PATCH] Fix notifications for group grantees For looking up group members we need to pass an authenticated reva context via the notification interface. We use the share owner's context that we generated when stating the share target. Fixes: #4703 --- .../notifications/pkg/channels/channels.go | 19 +++++++++---------- services/notifications/pkg/service/service.go | 8 ++++---- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/services/notifications/pkg/channels/channels.go b/services/notifications/pkg/channels/channels.go index 1003c4598..59928fbc2 100644 --- a/services/notifications/pkg/channels/channels.go +++ b/services/notifications/pkg/channels/channels.go @@ -20,9 +20,9 @@ import ( // Channel defines the methods of a communication channel. type Channel interface { // SendMessage sends a message to users. - SendMessage(userIDs []string, msg, subject, senderDisplayName string) error + SendMessage(ctx context.Context, userIDs []string, msg, subject, senderDisplayName string) error // SendMessageToGroup sends a message to a group. - SendMessageToGroup(groupdID *groups.GroupId, msg, subject, senderDisplayName string) error + SendMessageToGroup(ctx context.Context, groupdID *groups.GroupId, msg, subject, senderDisplayName string) error } // NewMailChannel instantiates a new mail communication channel. @@ -101,12 +101,12 @@ func (m Mail) getMailClient() (*mail.SMTPClient, error) { } // SendMessage sends a message to all given users. -func (m Mail) SendMessage(userIDs []string, msg, subject, senderDisplayName string) error { +func (m Mail) SendMessage(ctx context.Context, userIDs []string, msg, subject, senderDisplayName string) error { if m.conf.Notifications.SMTP.Host == "" { return nil } - to, err := m.getReceiverAddresses(userIDs) + to, err := m.getReceiverAddresses(ctx, userIDs) if err != nil { return err } @@ -129,9 +129,8 @@ func (m Mail) SendMessage(userIDs []string, msg, subject, senderDisplayName stri } // SendMessageToGroup sends a message to all members of the given group. -func (m Mail) SendMessageToGroup(groupID *groups.GroupId, msg, subject, senderDisplayName string) error { - // TODO We need an authenticated context here... - res, err := m.gatewayClient.GetGroup(context.Background(), &groups.GetGroupRequest{GroupId: groupID}) +func (m Mail) SendMessageToGroup(ctx context.Context, groupID *groups.GroupId, msg, subject, senderDisplayName string) error { + res, err := m.gatewayClient.GetGroup(ctx, &groups.GetGroupRequest{GroupId: groupID}) if err != nil { return err } @@ -144,15 +143,15 @@ func (m Mail) SendMessageToGroup(groupID *groups.GroupId, msg, subject, senderDi members = append(members, id.OpaqueId) } - return m.SendMessage(members, msg, subject, senderDisplayName) + return m.SendMessage(ctx, members, msg, subject, senderDisplayName) } -func (m Mail) getReceiverAddresses(receivers []string) ([]string, error) { +func (m Mail) getReceiverAddresses(ctx context.Context, receivers []string) ([]string, error) { addresses := make([]string, 0, len(receivers)) for _, id := range receivers { // Authenticate is too costly but at the moment our only option to get the user. // We don't have an authenticated context so calling `GetUser` doesn't work. - res, err := m.gatewayClient.Authenticate(context.Background(), &gateway.AuthenticateRequest{ + res, err := m.gatewayClient.Authenticate(ctx, &gateway.AuthenticateRequest{ Type: "machine", ClientId: "userid:" + id, ClientSecret: m.conf.Notifications.MachineAuthAPIKey, diff --git a/services/notifications/pkg/service/service.go b/services/notifications/pkg/service/service.go index 126382c96..c34c884dc 100644 --- a/services/notifications/pkg/service/service.go +++ b/services/notifications/pkg/service/service.go @@ -209,9 +209,9 @@ func (s eventsNotifier) handleSpaceShared(e events.SpaceShared) { emailSubject := fmt.Sprintf("%s invited you to join %s", sharerUserResponse.GetUser().DisplayName, md.GetInfo().GetSpace().Name) if e.GranteeUserID != nil { - err = s.channel.SendMessage([]string{e.GranteeUserID.OpaqueId}, msg, emailSubject, sharerDisplayName) + err = s.channel.SendMessage(ownerCtx, []string{e.GranteeUserID.OpaqueId}, msg, emailSubject, sharerDisplayName) } else if e.GranteeGroupID != nil { - err = s.channel.SendMessageToGroup(e.GranteeGroupID, msg, emailSubject, sharerDisplayName) + err = s.channel.SendMessageToGroup(ownerCtx, e.GranteeGroupID, msg, emailSubject, sharerDisplayName) } if err != nil { s.logger.Error(). @@ -347,9 +347,9 @@ func (s eventsNotifier) handleShareCreated(e events.ShareCreated) { emailSubject := fmt.Sprintf("%s shared %s with you", sharerUserResponse.GetUser().DisplayName, md.GetInfo().Name) if e.GranteeUserID != nil { - err = s.channel.SendMessage([]string{e.GranteeUserID.OpaqueId}, msg, emailSubject, sharerDisplayName) + err = s.channel.SendMessage(ownerCtx, []string{e.GranteeUserID.OpaqueId}, msg, emailSubject, sharerDisplayName) } else if e.GranteeGroupID != nil { - err = s.channel.SendMessageToGroup(e.GranteeGroupID, msg, emailSubject, sharerDisplayName) + err = s.channel.SendMessageToGroup(ownerCtx, e.GranteeGroupID, msg, emailSubject, sharerDisplayName) } if err != nil { s.logger.Error().