Skip to content

Commit

Permalink
feat: Emit SpaceMembershipExpired event
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianbeier committed Jan 23, 2025
1 parent 3678ee9 commit de65b4f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog/unreleased/emit-space-membership-expired-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Enhancement: Emit SpaceMembershipExpired event

https://github.com/cs3org/reva/pull/5051
https://github.com/owncloud/ocis/issues/10919
35 changes: 32 additions & 3 deletions pkg/storage/utils/decomposedfs/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
ocsconv "github.com/cs3org/reva/v2/pkg/conversions"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/rgrpc/status"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
sdk "github.com/cs3org/reva/v2/pkg/sdk/common"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand Down

0 comments on commit de65b4f

Please sign in to comment.