diff --git a/changelog/unreleased/emit-space-membership-expired-event.md b/changelog/unreleased/emit-space-membership-expired-event.md new file mode 100644 index 0000000000..93a9f1df94 --- /dev/null +++ b/changelog/unreleased/emit-space-membership-expired-event.md @@ -0,0 +1,4 @@ +Enhancement: Emit SpaceMembershipExpired event + +https://github.com/cs3org/reva/pull/5051 +https://github.com/owncloud/ocis/issues/10919 diff --git a/pkg/storage/utils/decomposedfs/spaces.go b/pkg/storage/utils/decomposedfs/spaces.go index 0a369a61f9..a029c45599 100644 --- a/pkg/storage/utils/decomposedfs/spaces.go +++ b/pkg/storage/utils/decomposedfs/spaces.go @@ -22,6 +22,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/cs3org/reva/v2/pkg/events" "math" "os" "path/filepath" @@ -905,7 +906,10 @@ func (fs *Decomposedfs) StorageSpaceFromNode(ctx context.Context, n *node.Node, // This way we don't have to have a cron job checking the grants in regular intervals. // The tradeof obviously is that this code is here. if isGrantExpired(g) { - if err := n.DeleteGrant(ctx, g, true); err != nil { + var errDeleteGrant, errIndexRemove error + + errDeleteGrant = n.DeleteGrant(ctx, g, true) + if errDeleteGrant != nil { sublog.Error().Err(err).Str("grantee", id). Msg("failed to delete expired space grant") } @@ -914,19 +918,44 @@ func (fs *Decomposedfs) StorageSpaceFromNode(ctx context.Context, n *node.Node, switch { case g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_USER: // remove from user index - if err := fs.userSpaceIndex.Remove(g.Grantee.GetUserId().GetOpaqueId(), n.SpaceID); err != nil { + errIndexRemove = fs.userSpaceIndex.Remove(g.Grantee.GetUserId().GetOpaqueId(), n.SpaceID) + if errIndexRemove != nil { sublog.Error().Err(err).Str("grantee", id). Msg("failed to delete expired user space index") } case g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_GROUP: // remove from group index - if err := fs.groupSpaceIndex.Remove(g.Grantee.GetGroupId().GetOpaqueId(), n.SpaceID); err != nil { + errIndexRemove = fs.groupSpaceIndex.Remove(g.Grantee.GetGroupId().GetOpaqueId(), n.SpaceID) + if errIndexRemove != nil { sublog.Error().Err(err).Str("grantee", id). Msg("failed to delete expired group space index") } } } + // publish SpaceMembershipExpired event + if errDeleteGrant == nil && errIndexRemove == nil { + ev := events.SpaceMembershipExpired{ + SpaceOwner: n.SpaceOwnerOrManager(ctx), + SpaceID: &provider.StorageSpaceId{OpaqueId: n.SpaceID}, + SpaceName: sname, + ExpiredAt: time.Unix(int64(g.Expiration.Seconds), int64(g.Expiration.Nanos)), + Timestamp: &types.Timestamp{ + Seconds: uint64(time.Now().Unix()), + }, + } + switch { + case g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_USER: + ev.GranteeUserID = g.Grantee.GetUserId() + case g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_GROUP: + ev.GranteeGroupID = g.Grantee.GetGroupId() + } + err = events.Publish(ctx, fs.stream, ev) + if err != nil { + sublog.Error().Err(err).Msg("error publishing SpaceMembershipExpired event") + } + } + continue } grantExpiration[id] = g.Expiration