add acl
This commit is contained in:
+123
@@ -0,0 +1,123 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
)
|
||||
|
||||
func (s *Service) Grant(ctx context.Context, params GrantParams) (*Entry, error) {
|
||||
if !params.Permissions.Valid() {
|
||||
return nil, fmt.Errorf("%w: %d", ErrInvalidPermissions, params.Permissions)
|
||||
}
|
||||
|
||||
if _, err := s.db.Queries.GetDocument(ctx, params.DocumentID); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, params.DocumentID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := s.loadSubject(ctx, params.SubjectID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id, err := ident.NewV7()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
entry, err := s.db.WriteQueries.UpsertACLEntry(ctx, database.UpsertACLEntryParams{
|
||||
ID: id,
|
||||
DocumentID: params.DocumentID,
|
||||
SubjectID: params.SubjectID,
|
||||
Permissions: int64(params.Permissions),
|
||||
Propagate: boolToInt(params.Propagate),
|
||||
ExpiresAt: timeToNull(params.ExpiresAt),
|
||||
GrantedBy: params.GrantedBy,
|
||||
CreatedAt: now.UnixMilli(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entryFromDB(entry), nil
|
||||
}
|
||||
|
||||
func (s *Service) Revoke(ctx context.Context, documentID, subjectID ident.UUID) error {
|
||||
_, err := s.db.WriteQueries.RevokeACLEntry(ctx, database.RevokeACLEntryParams{
|
||||
RevokedAt: sql.NullInt64{
|
||||
Int64: time.Now().UnixMilli(),
|
||||
Valid: true,
|
||||
},
|
||||
DocumentID: documentID,
|
||||
SubjectID: subjectID,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ErrEntryNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, documentID ident.UUID) ([]Entry, error) {
|
||||
rows, err := s.db.Queries.ListActiveACLEntriesForDocument(ctx, database.ListActiveACLEntriesForDocumentParams{
|
||||
DocumentID: documentID,
|
||||
Now: time.Now().UnixMilli(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entries := make([]Entry, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
entries = append(entries, *entryFromDB(row))
|
||||
}
|
||||
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func entryFromDB(entry database.AclEntry) *Entry {
|
||||
return &Entry{
|
||||
ID: entry.ID,
|
||||
DocumentID: entry.DocumentID,
|
||||
SubjectID: entry.SubjectID,
|
||||
Permissions: Permission(entry.Permissions),
|
||||
Propagate: entry.Propagate != 0,
|
||||
ExpiresAt: nullToTime(entry.ExpiresAt),
|
||||
GrantedBy: entry.GrantedBy,
|
||||
CreatedAt: time.UnixMilli(entry.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func boolToInt(value bool) int64 {
|
||||
if value {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func timeToNull(value *time.Time) sql.NullInt64 {
|
||||
if value == nil {
|
||||
return sql.NullInt64{}
|
||||
}
|
||||
return sql.NullInt64{Int64: value.UnixMilli(), Valid: true}
|
||||
}
|
||||
|
||||
func nullToTime(value sql.NullInt64) *time.Time {
|
||||
if !value.Valid {
|
||||
return nil
|
||||
}
|
||||
result := time.UnixMilli(value.Int64)
|
||||
return &result
|
||||
}
|
||||
Reference in New Issue
Block a user