github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/decomposedfs/events.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package decomposedfs
    20  
    21  import (
    22  	"context"
    23  
    24  	user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    25  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    26  	"github.com/cs3org/reva/v2/pkg/appctx"
    27  	revactx "github.com/cs3org/reva/v2/pkg/ctx"
    28  	"github.com/cs3org/reva/v2/pkg/events"
    29  	"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/node"
    30  	"github.com/cs3org/reva/v2/pkg/utils"
    31  )
    32  
    33  func (fs *Decomposedfs) publishEvent(ctx context.Context, evf func() (any, error)) {
    34  	log := appctx.GetLogger(ctx)
    35  	if fs.stream == nil {
    36  		log.Error().Msg("Failed to publish event, stream is undefined")
    37  		return
    38  	}
    39  	ev, err := evf()
    40  	if err != nil || ev == nil {
    41  		log.Error().Err(err).Msg("Failed to crete the event")
    42  		return
    43  	}
    44  	if err := events.Publish(ctx, fs.stream, ev); err != nil {
    45  		log.Error().Err(err).Msg("Failed to publish event")
    46  	}
    47  }
    48  
    49  func (fs *Decomposedfs) moveEvent(ctx context.Context, oldRef, newRef *provider.Reference, oldNode, newNode *node.Node, orp, nrp *provider.ResourcePermissions) func() (any, error) {
    50  	return func() (any, error) {
    51  		executant, _ := revactx.ContextGetUser(ctx)
    52  		ev := events.ItemMoved{
    53  			SpaceOwner:        newNode.Owner(),
    54  			Executant:         executant.GetId(),
    55  			Ref:               newRef,
    56  			OldReference:      oldRef,
    57  			Timestamp:         utils.TSNow(),
    58  			ImpersonatingUser: extractImpersonator(executant),
    59  		}
    60  		log := appctx.GetLogger(ctx)
    61  		if nref, err := fs.refFromNode(ctx, newNode, newRef.GetResourceId().GetStorageId(), nrp); err == nil {
    62  			ev.Ref = nref
    63  		} else {
    64  			log.Error().Err(err).Msg("Failed to get destination reference")
    65  		}
    66  
    67  		if oref, err := fs.refFromNode(ctx, oldNode, oldRef.GetResourceId().GetStorageId(), orp); err == nil {
    68  			ev.OldReference = oref
    69  		} else {
    70  			log.Error().Err(err).Msg("Failed to get source reference")
    71  		}
    72  
    73  		return ev, nil
    74  	}
    75  }
    76  
    77  func extractImpersonator(u *user.User) *user.User {
    78  	var impersonator user.User
    79  	if err := utils.ReadJSONFromOpaque(u.Opaque, "impersonating-user", &impersonator); err != nil {
    80  		return nil
    81  	}
    82  	return &impersonator
    83  }