collection.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package access
  2. import (
  3. "github.com/ente-io/museum/ente"
  4. "github.com/ente-io/stacktrace"
  5. "github.com/gin-gonic/gin"
  6. )
  7. type GetCollectionParams struct {
  8. CollectionID int64
  9. // userID of the user trying to fetch the controller
  10. ActorUserID int64
  11. // IncludeDeleted defaults to false. If false and user is trying to fetch deletion collection
  12. // then the request fails
  13. IncludeDeleted bool
  14. // VerifyOwner deafults to false. If the flag is set to true, the method will verify that the actor actually owns the collection
  15. VerifyOwner bool
  16. // todo: Add accessType in params for verifying read/write/can-upload/owner types of access
  17. }
  18. type GetCollectionResponse struct {
  19. Collection ente.Collection
  20. Role *ente.CollectionParticipantRole
  21. }
  22. func (c controllerImpl) GetCollection(ctx *gin.Context, req *GetCollectionParams) (*GetCollectionResponse, error) {
  23. collection, err := c.CollectionRepo.Get(req.CollectionID)
  24. role := ente.UNKNOWN
  25. if err != nil {
  26. return nil, stacktrace.Propagate(err, "")
  27. }
  28. // Perform permission related access check if user is not the owner of the collection
  29. if req.VerifyOwner && req.ActorUserID != collection.Owner.ID {
  30. return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "actor doesn't owns the collection")
  31. }
  32. if req.ActorUserID != collection.Owner.ID {
  33. shareeRole, err := c.CollectionRepo.GetCollectionShareeRole(req.CollectionID, req.ActorUserID)
  34. if err != nil {
  35. return nil, stacktrace.Propagate(err, "")
  36. }
  37. // Hide public URL info for non-collection owners
  38. collection.PublicURLs = nil
  39. role = *shareeRole
  40. } else {
  41. role = ente.OWNER
  42. }
  43. if !req.IncludeDeleted && collection.IsDeleted {
  44. return nil, stacktrace.Propagate(ente.ErrNotFound, "trying to access deleted collection")
  45. }
  46. return &GetCollectionResponse{
  47. Collection: collection,
  48. Role: &role,
  49. }, nil
  50. }