acl
This commit is contained in:
+23
-7
@@ -11,12 +11,16 @@ import (
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
"mediarr/permission"
|
||||
)
|
||||
|
||||
func (s *Service) Rename(ctx context.Context, documentID ident.UUID, name string) (*Document, error) {
|
||||
func (s *Service) Rename(ctx context.Context, actorID, documentID ident.UUID, name string) (*Document, error) {
|
||||
if _, err := s.getActiveDocument(ctx, documentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, documentID, permission.Write); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
normalized, nameKey, err := normalizeName(name)
|
||||
if err != nil {
|
||||
@@ -42,11 +46,14 @@ func (s *Service) Rename(ctx context.Context, documentID ident.UUID, name string
|
||||
return documentFromDB(updated), nil
|
||||
}
|
||||
|
||||
func (s *Service) Move(ctx context.Context, documentID, newParentID ident.UUID) (*Document, error) {
|
||||
func (s *Service) Move(ctx context.Context, actorID, documentID, newParentID ident.UUID) (*Document, error) {
|
||||
document, err := s.getActiveDocument(ctx, documentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, documentID, permission.Manage); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if document.ParentID == nil {
|
||||
return nil, ErrCannotMoveRoot
|
||||
}
|
||||
@@ -73,6 +80,9 @@ func (s *Service) Move(ctx context.Context, documentID, newParentID ident.UUID)
|
||||
if strings.HasPrefix(parent.Path, document.Path) {
|
||||
return nil, ErrInvalidMove
|
||||
}
|
||||
if err := s.can(ctx, actorID, newParentID, permission.Create); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newPath := parent.Path + hex.EncodeToString(document.ID[:]) + "/"
|
||||
newDepth := parent.Depth + 1
|
||||
@@ -126,11 +136,14 @@ func (s *Service) Move(ctx context.Context, documentID, newParentID ident.UUID)
|
||||
return documentFromDB(moved), nil
|
||||
}
|
||||
|
||||
func (s *Service) Trash(ctx context.Context, documentID, deletedBy ident.UUID) (*Document, error) {
|
||||
func (s *Service) Trash(ctx context.Context, actorID, documentID ident.UUID) (*Document, error) {
|
||||
document, err := s.getActiveDocument(ctx, documentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, documentID, permission.Delete); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
batch, err := ident.NewV7()
|
||||
if err != nil {
|
||||
@@ -144,7 +157,7 @@ func (s *Service) Trash(ctx context.Context, documentID, deletedBy ident.UUID) (
|
||||
Int64: now,
|
||||
Valid: true,
|
||||
},
|
||||
DeletedBy: &deletedBy,
|
||||
DeletedBy: &actorID,
|
||||
TrashBatch: batch.Bytes(),
|
||||
UpdatedAt: now,
|
||||
Prefix: document.Path,
|
||||
@@ -156,14 +169,17 @@ func (s *Service) Trash(ctx context.Context, documentID, deletedBy ident.UUID) (
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||
}
|
||||
|
||||
return s.Get(ctx, documentID)
|
||||
return s.getDocumentModel(ctx, documentID)
|
||||
}
|
||||
|
||||
func (s *Service) Restore(ctx context.Context, documentID ident.UUID) (*Document, error) {
|
||||
func (s *Service) Restore(ctx context.Context, actorID, documentID ident.UUID) (*Document, error) {
|
||||
document, err := s.getDocument(ctx, documentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, documentID, permission.Delete); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !document.DeletedAt.Valid {
|
||||
return documentFromDB(*document), nil
|
||||
}
|
||||
@@ -199,5 +215,5 @@ func (s *Service) Restore(ctx context.Context, documentID ident.UUID) (*Document
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||
}
|
||||
|
||||
return s.Get(ctx, documentID)
|
||||
return s.getDocumentModel(ctx, documentID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user