mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-27 10:34:42 -05:00
feat(userlog): notify users about received ocm shares
Users never got a notification when a federated instance shared something with them or removed that share again. The ocmcore service publishes OCMCoreShareCreated and OCMCoreShareDelete, but only when it has an events config, and the ocm service never passed one. We now pass the events config to ocmcore, and userlog turns both events into the regular "Resource shared" / "Resource unshared" notifications for the grantee. They honor the same in-app settings as local shares. The sharer is a federated user, so we look it up with GetAcceptedUser in the name of the grantee. That call has to go out without the service account token, otherwise the invite manager filters by the service account and finds nothing. If the lookup fails, e.g. because the federated connection was deleted, the notification says "Someone at <provider domain> shared <resource> with you" instead of showing an empty name. The federated id itself is "<uuid>@<issuer url>", which tells the reader nothing. Clients only fill in the known rich parameters, so we fill in the provider in the rich message ourselves. The resource lives on the remote instance. The notification points to the received share id for both events and carries the resource name without an id, so clients render it like a local share but don't build a link to a resource that does not exist here. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
1 parent
90e03ba234
commit
3320159dca
8 files changed
+291
-2
No files matched your search
@@ -176,6 +176,15 @@ func OCMConfigFromStruct(cfg *config.Config, logger log.Logger) map[string]any {
|
||||
"file": cfg.OCMCore.Drivers.JSON.File,
|
||||
},
|
||||
},
|
||||
"events": map[string]any{
|
||||
"natsaddress": cfg.Events.Endpoint,
|
||||
"natsclusterid": cfg.Events.Cluster,
|
||||
"enabletls": cfg.Events.EnableTLS,
|
||||
"tlsinsecure": cfg.Events.TLSInsecure,
|
||||
"tlsrootcacertificate": cfg.Events.TLSRootCACertificate,
|
||||
"authusername": cfg.Events.AuthUsername,
|
||||
"authpassword": cfg.Events.AuthPassword,
|
||||
},
|
||||
},
|
||||
"storageprovider": map[string]any{
|
||||
"driver": "ocmreceived",
|
||||
|
||||
@@ -50,6 +50,10 @@ var _registeredEvents = []events.Unmarshaller{
|
||||
events.ShareRemoved{},
|
||||
events.ShareExpired{},
|
||||
|
||||
// ocm share related
|
||||
events.OCMCoreShareCreated{},
|
||||
events.OCMCoreShareDelete{},
|
||||
|
||||
// misc
|
||||
ocEvents.ResourceMention{},
|
||||
}
|
||||
|
||||
@@ -47,9 +47,9 @@ func (ulf userlogFilter) filterUsersBySettings(ctx context.Context, users []stri
|
||||
var settingId string
|
||||
// map type to settings key
|
||||
switch event.Event.(type) {
|
||||
case events.ShareCreated:
|
||||
case events.ShareCreated, events.OCMCoreShareCreated:
|
||||
settingId = defaults.SettingUUIDProfileEventShareCreated
|
||||
case events.ShareRemoved:
|
||||
case events.ShareRemoved, events.OCMCoreShareDelete:
|
||||
settingId = defaults.SettingUUIDProfileEventShareRemoved
|
||||
case events.ShareExpired:
|
||||
settingId = defaults.SettingUUIDProfileEventShareExpired
|
||||
|
||||
@@ -160,6 +160,14 @@ func (s *Service) processEvent(event events.Event) {
|
||||
users, err = utils.ResolveID(ctx, e.GranteeUserID, e.GranteeGroupID, gwc)
|
||||
case events.ShareExpired:
|
||||
users, err = utils.ResolveID(ctx, e.GranteeUserID, e.GranteeGroupID, gwc)
|
||||
|
||||
// ocm share related, the grantee is always a local user
|
||||
case events.OCMCoreShareCreated:
|
||||
executant = e.Sharer
|
||||
users = append(users, e.GranteeUserID.GetOpaqueId())
|
||||
case events.OCMCoreShareDelete:
|
||||
executant = e.Sharer
|
||||
users = append(users, e.Grantee.GetOpaqueId())
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -12,9 +12,13 @@ import (
|
||||
|
||||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
|
||||
invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1"
|
||||
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
|
||||
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
|
||||
storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
|
||||
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
|
||||
"github.com/opencloud-eu/reva/v3/pkg/events"
|
||||
ocmuser "github.com/opencloud-eu/reva/v3/pkg/ocm/user"
|
||||
"github.com/opencloud-eu/reva/v3/pkg/rgrpc/todo/pool"
|
||||
"github.com/opencloud-eu/reva/v3/pkg/storagespace"
|
||||
"github.com/opencloud-eu/reva/v3/pkg/utils"
|
||||
@@ -118,6 +122,12 @@ func (c *Converter) ConvertEvent(eventid string, event any) (OC10Notification, e
|
||||
case events.ShareRemoved:
|
||||
return c.shareMessage(eventid, ShareRemoved, ev.Executant, ev.ItemID, ev.ShareID, ev.Timestamp)
|
||||
|
||||
// ocm share related
|
||||
case events.OCMCoreShareCreated:
|
||||
return c.ocmShareMessage(eventid, ShareCreated, ShareCreatedByProvider, ev.Sharer, ev.GranteeUserID, ev.ShareID, ev.ResourceName, utils.TSToTime(ev.CTime))
|
||||
case events.OCMCoreShareDelete:
|
||||
return c.ocmShareMessage(eventid, ShareRemoved, ShareRemovedByProvider, ev.Sharer, ev.Grantee, ev.ShareID, ev.ResourceName, utils.TSToTime(ev.CTime))
|
||||
|
||||
// misc
|
||||
case ocEvents.ResourceMention:
|
||||
return c.resourceMention(eventid, Mention, ev.Executant, ev.Ref.GetResourceId(), ev.Timestamp)
|
||||
@@ -308,6 +318,85 @@ func (c *Converter) shareMessage(eventid string, nt NotificationTemplate, execut
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ocmShareMessage converts an event about a share received from a federated instance. The
|
||||
// resource lives on the remote instance, so we only know its name from the event. The resource
|
||||
// details carry no id, clients would turn it into a link to a local resource.
|
||||
//
|
||||
// If the sharer cannot be resolved we use the providerNT template, which names the provider
|
||||
// domain of the sharer instead of the user.
|
||||
func (c *Converter) ocmShareMessage(eventid string, nt, providerNT NotificationTemplate, sharer *user.UserId, grantee *user.UserId, shareid string, resourcename string, ts time.Time) (OC10Notification, error) {
|
||||
usr := c.getOCMUser(sharer, grantee)
|
||||
provider := sharer.GetIdp()
|
||||
if usr == nil {
|
||||
nt = providerNT
|
||||
}
|
||||
|
||||
subj, subjraw, msg, msgraw, err := composeMessage(nt, c.locale, c.defaultLanguage, c.translationPath, map[string]any{
|
||||
"username": usr.GetDisplayName(),
|
||||
"resourcename": resourcename,
|
||||
"provider": provider,
|
||||
})
|
||||
if err != nil {
|
||||
return OC10Notification{}, err
|
||||
}
|
||||
// clients only know how to fill in the rich parameters, so we fill in the provider ourselves
|
||||
msgraw = strings.ReplaceAll(msgraw, "{provider}", provider)
|
||||
|
||||
details := generateDetails(usr, nil, nil, &collaboration.ShareId{OpaqueId: shareid})
|
||||
details["resource"] = map[string]string{"name": resourcename}
|
||||
|
||||
return OC10Notification{
|
||||
EventID: eventid,
|
||||
Service: c.serviceName,
|
||||
UserName: usr.GetUsername(),
|
||||
Timestamp: ts.Format(time.RFC3339Nano),
|
||||
ResourceID: shareid,
|
||||
ResourceType: _resourceTypeShare,
|
||||
Subject: subj,
|
||||
SubjectRaw: subjraw,
|
||||
Message: msg,
|
||||
MessageRaw: msgraw,
|
||||
MessageDetails: details,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// getOCMUser looks up a federated user the grantee has accepted an invite from. It returns nil
|
||||
// if the user cannot be resolved, e.g. because the federated connection was deleted. The
|
||||
// federated id is no help then, it is the remote user id plus the issuer of the remote IdP,
|
||||
// e.g. "<uuid>@https://cloud.example.org".
|
||||
func (c *Converter) getOCMUser(sharer *user.UserId, grantee *user.UserId) *user.User {
|
||||
key := ocmuser.FormatOCMUser(sharer)
|
||||
if u, ok := c.users[key]; ok {
|
||||
return u
|
||||
}
|
||||
|
||||
gwc, err := c.gatewaySelector.Next()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAcceptedUser uses the user from the context to find the accepted user. The service
|
||||
// account would never match, so we call it without a token and pass the grantee as a filter.
|
||||
filter, err := utils.MarshalProtoV1ToJSON(grantee)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
res, err := gwc.GetAcceptedUser(context.Background(), &invitepb.GetAcceptedUserRequest{
|
||||
RemoteUserId: sharer,
|
||||
Opaque: &typespb.Opaque{
|
||||
Map: map[string]*typespb.OpaqueEntry{
|
||||
"user-filter": {Decoder: "json", Value: filter},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil || res.GetStatus().GetCode() != rpc.Code_CODE_OK || res.GetRemoteUser() == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
c.users[key] = res.GetRemoteUser()
|
||||
return res.GetRemoteUser()
|
||||
}
|
||||
|
||||
func (c *Converter) virusMessage(eventid string, nt NotificationTemplate, executant *user.User, rid *storageprovider.ResourceId, filename string, virus string, ts time.Time) (OC10Notification, error) {
|
||||
subj, subjraw, msg, msgraw, err := composeMessage(nt, c.locale, c.defaultLanguage, c.translationPath, map[string]any{
|
||||
"resourcename": filename,
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package service_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
|
||||
invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1"
|
||||
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
|
||||
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/opencloud-eu/reva/v3/pkg/events"
|
||||
"github.com/opencloud-eu/reva/v3/pkg/rgrpc/todo/pool"
|
||||
"github.com/opencloud-eu/reva/v3/pkg/utils"
|
||||
cs3mocks "github.com/opencloud-eu/reva/v3/tests/cs3mocks/mocks"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/opencloud-eu/opencloud/services/userlog/pkg/service"
|
||||
)
|
||||
|
||||
var _ = Describe("Converter", func() {
|
||||
var (
|
||||
gatewayClient *cs3mocks.GatewayAPIClient
|
||||
converter *service.Converter
|
||||
|
||||
// what ocmd makes of the sender "<uuid>@<issuer>@<provider domain>" of an incoming share
|
||||
sharer = &user.UserId{
|
||||
Type: user.UserType_USER_TYPE_FEDERATED,
|
||||
OpaqueId: "4c510ada-c86b-4815-8820-42cdf82c3d51@https://remote.example.org",
|
||||
Idp: "remote.example.org",
|
||||
}
|
||||
grantee = &user.UserId{OpaqueId: "f7fbf8c8-139b-4376-b307-cf0a8c2d0d9c", Idp: "https://local.example.org"}
|
||||
ctime = &types.Timestamp{Seconds: 1700000000}
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
pool.RemoveSelector("GatewaySelector" + "eu.opencloud.api.gateway")
|
||||
gatewayClient = &cs3mocks.GatewayAPIClient{}
|
||||
selector := pool.GetSelector[gateway.GatewayAPIClient](
|
||||
"GatewaySelector",
|
||||
"eu.opencloud.api.gateway",
|
||||
func(cc grpc.ClientConnInterface) gateway.GatewayAPIClient {
|
||||
return gatewayClient
|
||||
},
|
||||
)
|
||||
converter = service.NewConverter(context.Background(), "en", selector, "userlog", "", "en")
|
||||
})
|
||||
|
||||
Describe("ocm shares", func() {
|
||||
It("converts a received share and resolves the sharer for the grantee", func() {
|
||||
gatewayClient.On("GetAcceptedUser", mock.Anything, mock.MatchedBy(func(req *invitepb.GetAcceptedUserRequest) bool {
|
||||
var filter user.UserId
|
||||
if err := utils.UnmarshalJSONToProtoV1(req.GetOpaque().GetMap()["user-filter"].GetValue(), &filter); err != nil {
|
||||
return false
|
||||
}
|
||||
return req.GetRemoteUserId().GetOpaqueId() == sharer.GetOpaqueId() && filter.GetOpaqueId() == grantee.GetOpaqueId()
|
||||
})).Return(&invitepb.GetAcceptedUserResponse{
|
||||
Status: &rpc.Status{Code: rpc.Code_CODE_OK},
|
||||
RemoteUser: &user.User{Id: sharer, Username: "einstein", DisplayName: "Albert Einstein"},
|
||||
}, nil)
|
||||
|
||||
n, err := converter.ConvertEvent("eventid", events.OCMCoreShareCreated{
|
||||
ShareID: "shareid",
|
||||
Sharer: sharer,
|
||||
GranteeUserID: grantee,
|
||||
ItemID: "remote-item-id",
|
||||
ResourceName: "relativity",
|
||||
CTime: ctime,
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(n.Subject).To(Equal("Resource shared"))
|
||||
Expect(n.Message).To(Equal("Albert Einstein shared relativity with you"))
|
||||
Expect(n.UserName).To(Equal("einstein"))
|
||||
Expect(n.ResourceID).To(Equal("shareid"))
|
||||
Expect(n.ResourceType).To(Equal("share"))
|
||||
Expect(n.MessageDetails["share"]).To(Equal(map[string]string{"id": "shareid"}))
|
||||
Expect(n.MessageDetails["resource"]).To(Equal(map[string]string{"name": "relativity"}))
|
||||
})
|
||||
|
||||
It("names the provider domain when the sharer cannot be resolved", func() {
|
||||
gatewayClient.On("GetAcceptedUser", mock.Anything, mock.Anything).Return(&invitepb.GetAcceptedUserResponse{
|
||||
Status: &rpc.Status{Code: rpc.Code_CODE_NOT_FOUND},
|
||||
}, nil)
|
||||
|
||||
n, err := converter.ConvertEvent("eventid", events.OCMCoreShareDelete{
|
||||
ShareID: "shareid",
|
||||
Sharer: sharer,
|
||||
Grantee: grantee,
|
||||
ResourceName: "relativity",
|
||||
CTime: ctime,
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(n.Subject).To(Equal("Resource unshared"))
|
||||
Expect(n.Message).To(Equal("Someone at remote.example.org unshared relativity with you"))
|
||||
Expect(n.MessageRaw).To(Equal("Someone at remote.example.org unshared {resource} with you"))
|
||||
Expect(n.ResourceID).To(Equal("shareid"))
|
||||
Expect(n.MessageDetails).ToNot(HaveKey("user"))
|
||||
Expect(n.MessageDetails["resource"]).To(Equal(map[string]string{"name": "relativity"}))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -49,6 +49,17 @@ var (
|
||||
Message: l10n.Template("{user} unshared {resource} with you"),
|
||||
}
|
||||
|
||||
// used for federated shares when the sharer cannot be resolved
|
||||
ShareCreatedByProvider = NotificationTemplate{
|
||||
Subject: l10n.Template("Resource shared"),
|
||||
Message: l10n.Template("Someone at {provider} shared {resource} with you"),
|
||||
}
|
||||
|
||||
ShareRemovedByProvider = NotificationTemplate{
|
||||
Subject: l10n.Template("Resource unshared"),
|
||||
Message: l10n.Template("Someone at {provider} unshared {resource} with you"),
|
||||
}
|
||||
|
||||
Mention = NotificationTemplate{
|
||||
Subject: l10n.Template("You have been mentioned"),
|
||||
Message: l10n.Template("{user} mentioned you in {resource}"),
|
||||
@@ -72,6 +83,7 @@ var _placeholders = map[string]string{
|
||||
"{resource}": "{{ .resourcename }}",
|
||||
"{virus}": "{{ .virusdescription }}",
|
||||
"{date}": "{{ .date }}",
|
||||
"{provider}": "{{ .provider }}",
|
||||
}
|
||||
|
||||
// NotificationTemplate is the data structure for the notifications
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
@ocm
|
||||
Feature: federated users get in-app notifications about shares
|
||||
As a federated user
|
||||
I want to be notified when a user of another instance shares a resource with me or removes the share
|
||||
So that I know which federated resources I have access to
|
||||
|
||||
Background:
|
||||
Given user "Alice" has been created with default attributes
|
||||
And using server "REMOTE"
|
||||
And user "Brian" has been created with default attributes
|
||||
And "Brian" has created the federation share invitation
|
||||
And using server "LOCAL"
|
||||
And "Alice" has accepted invitation
|
||||
And user "Alice" has created folder "folderToShare"
|
||||
|
||||
|
||||
Scenario: federated user gets a notification when a resource is shared
|
||||
When user "Alice" sends the following resource share invitation to federated user using the Graph API:
|
||||
| resource | folderToShare |
|
||||
| space | Personal |
|
||||
| sharee | Brian |
|
||||
| shareType | user |
|
||||
| permissionsRole | Viewer |
|
||||
Then the HTTP status code should be "200"
|
||||
And using server "REMOTE"
|
||||
And user "Brian" should get a notification with subject "Resource shared" and message:
|
||||
| message |
|
||||
| Alice Hansen shared folderToShare with you |
|
||||
|
||||
|
||||
Scenario: federated user gets a notification when a share is removed
|
||||
Given user "Alice" has sent the following resource share invitation to federated user:
|
||||
| resource | folderToShare |
|
||||
| space | Personal |
|
||||
| sharee | Brian |
|
||||
| shareType | user |
|
||||
| permissionsRole | Viewer |
|
||||
When user "Alice" removes the access of user "Brian" from resource "folderToShare" of space "Personal" using the Graph API
|
||||
Then the HTTP status code should be "204"
|
||||
And using server "REMOTE"
|
||||
And user "Brian" should get a notification with subject "Resource unshared" and message:
|
||||
| message |
|
||||
| Alice Hansen unshared folderToShare with you |
|
||||
|
||||
|
||||
Scenario: federated user does not get a notification for a removed share when the in-app notification is disabled
|
||||
Given using server "REMOTE"
|
||||
And user "Brian" has disabled notification for the following event using the settings API:
|
||||
| event | Share Removed |
|
||||
| notificationTypes | in-app |
|
||||
And using server "LOCAL"
|
||||
And user "Alice" has sent the following resource share invitation to federated user:
|
||||
| resource | folderToShare |
|
||||
| space | Personal |
|
||||
| sharee | Brian |
|
||||
| shareType | user |
|
||||
| permissionsRole | Viewer |
|
||||
When user "Alice" removes the access of user "Brian" from resource "folderToShare" of space "Personal" using the Graph API
|
||||
Then the HTTP status code should be "204"
|
||||
And using server "REMOTE"
|
||||
And user "Brian" should get a notification with subject "Resource shared" and message:
|
||||
| message |
|
||||
| Alice Hansen shared folderToShare with you |
|
||||
But user "Brian" should not have a notification related to resource "folderToShare" with subject "Resource unshared"
|
||||
Reference in new issue
Block a user