200 lines
4.7 KiB
Go
200 lines
4.7 KiB
Go
package document
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"mediarr/database"
|
|
"mediarr/ident"
|
|
"mediarr/permission"
|
|
)
|
|
|
|
type Authorizer interface {
|
|
Can(ctx context.Context, principalID, documentID ident.UUID, required permission.Permission) (bool, error)
|
|
IsActive(ctx context.Context, principalID ident.UUID) (bool, error)
|
|
}
|
|
|
|
type Service struct {
|
|
db database.DatabaseClient
|
|
authorizer Authorizer
|
|
}
|
|
|
|
func NewService(db database.DatabaseClient, authorizer Authorizer) *Service {
|
|
return &Service{
|
|
db: db,
|
|
authorizer: authorizer,
|
|
}
|
|
}
|
|
|
|
func (s *Service) withTx(ctx context.Context, fn func(*database.Queries) error) error {
|
|
tx, err := s.db.WriteDB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
_ = tx.Rollback()
|
|
}()
|
|
|
|
if err := fn(s.db.WriteQueries.WithTx(tx)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Commit()
|
|
}
|
|
|
|
func (s *Service) getDocument(ctx context.Context, id ident.UUID) (*database.Document, error) {
|
|
document, err := s.db.Queries.GetDocument(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, id)
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return &document, nil
|
|
}
|
|
|
|
func (s *Service) getActiveDocument(ctx context.Context, id ident.UUID) (*database.Document, error) {
|
|
document, err := s.getDocument(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if document.DeletedAt.Valid {
|
|
return nil, fmt.Errorf("%w: %s", ErrDocumentDeleted, id)
|
|
}
|
|
|
|
return document, nil
|
|
}
|
|
|
|
func (s *Service) getDocumentModel(ctx context.Context, id ident.UUID) (*Document, error) {
|
|
document, err := s.getDocument(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return documentFromDB(*document), nil
|
|
}
|
|
|
|
func (s *Service) requireActive(ctx context.Context, principalID ident.UUID) error {
|
|
active, err := s.authorizer.IsActive(ctx, principalID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !active {
|
|
return fmt.Errorf("%w: principal inactif %s", ErrPermissionDenied, principalID)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) allowed(ctx context.Context, principalID, documentID ident.UUID, required permission.Permission) (bool, error) {
|
|
return s.authorizer.Can(ctx, principalID, documentID, required)
|
|
}
|
|
|
|
func (s *Service) can(ctx context.Context, principalID, documentID ident.UUID, required permission.Permission) error {
|
|
allowed, err := s.allowed(ctx, principalID, documentID, required)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !allowed {
|
|
return fmt.Errorf("%w: %s sur %s", ErrPermissionDenied, principalID, documentID)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func normalizeName(name string) (string, string, error) {
|
|
normalized := strings.TrimSpace(name)
|
|
if normalized == "" || len(normalized) > 255 || strings.Contains(normalized, "/") || strings.ContainsRune(normalized, 0) {
|
|
return "", "", fmt.Errorf("%w: %q", ErrInvalidName, name)
|
|
}
|
|
|
|
return normalized, strings.ToLower(normalized), nil
|
|
}
|
|
|
|
func normalizeVisibility(visibility string) (string, error) {
|
|
if visibility == "" {
|
|
return VisibilityPrivate, nil
|
|
}
|
|
|
|
switch visibility {
|
|
case VisibilityPrivate, VisibilityCluster, VisibilityFederated, VisibilityPublic:
|
|
return visibility, nil
|
|
default:
|
|
return "", fmt.Errorf("%w: %q", ErrInvalidVisibility, visibility)
|
|
}
|
|
}
|
|
|
|
func metadataValue(metadata string) string {
|
|
if metadata == "" {
|
|
return "{}"
|
|
}
|
|
return metadata
|
|
}
|
|
|
|
func aclInheritValue(value *bool) int64 {
|
|
if value == nil || *value {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func isContainerKind(kind string) bool {
|
|
return kind == KindLibrary || kind == KindFolder
|
|
}
|
|
|
|
func isUniqueViolation(err error) bool {
|
|
return err != nil && strings.Contains(strings.ToLower(err.Error()), "unique constraint failed")
|
|
}
|
|
|
|
func documentFromDB(document database.Document) *Document {
|
|
visibility := ""
|
|
if document.Visibility.Valid {
|
|
visibility = document.Visibility.String
|
|
}
|
|
|
|
var position *string
|
|
if document.Position.Valid {
|
|
value := document.Position.String
|
|
position = &value
|
|
}
|
|
|
|
var deletedAt *int64
|
|
if document.DeletedAt.Valid {
|
|
value := document.DeletedAt.Int64
|
|
deletedAt = &value
|
|
}
|
|
|
|
return &Document{
|
|
ID: document.ID,
|
|
Kind: document.Kind,
|
|
OwnerID: document.OwnerID,
|
|
CreatedBy: document.CreatedBy,
|
|
ParentID: document.ParentID,
|
|
Name: document.Name,
|
|
NameKey: document.NameKey,
|
|
Path: document.Path,
|
|
Depth: document.Depth,
|
|
Position: position,
|
|
HeadVersionID: document.HeadVersionID,
|
|
Metadata: document.Metadata,
|
|
PolicyID: document.PolicyID,
|
|
Visibility: visibility,
|
|
ACLInherit: document.AclInherit != 0,
|
|
Rev: document.Rev,
|
|
CreatedAt: document.CreatedAt,
|
|
UpdatedAt: document.UpdatedAt,
|
|
DeletedAt: deletedAt,
|
|
DeletedBy: document.DeletedBy,
|
|
TrashBatch: document.TrashBatch,
|
|
}
|
|
}
|
|
|
|
func nowMillis() int64 {
|
|
return time.Now().UnixMilli()
|
|
}
|