mirror of
https://github.com/usememos/memos.git
synced 2026-11-21 03:19:12 +01:00
4f46523758
An active application ADMIN now passes every memo-local authorization check on named operations: authorship, audience, Space membership and participation, attachment and reaction ownership. Structural validity still applies, an archived admin holds no privilege, and collection listings keep the audience predicate so feeds never surface other users' private memos. - core/access: admin bypass in the read policy; CanManageMemo and CanManageAttachment as the shared author-level predicates. - store: drivers read the actor's role alongside its lifecycle state in the mutation transaction; validators accept an admin where they required the author or a member; shared attachment binding and removal predicates. - api: handlers use the shared predicates; admins may place memos in any existing Space and see a memo's Space by name. - web: one canManageMemo helper replaces the ad hoc creator checks. - docs: multi-spaces design revised in place; share RPC comments updated.
113 lines
3.7 KiB
Go
113 lines
3.7 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/usememos/memos/store"
|
|
)
|
|
|
|
func (d *DB) DeleteAttachmentsWithPolicy(ctx context.Context, policy *store.AttachmentDeletionPolicy, attachmentIDs []int32) error {
|
|
if policy == nil || policy.ActorUserID <= 0 {
|
|
return store.ErrMemoPermissionDenied
|
|
}
|
|
tx, err := d.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to begin attachment delete transaction")
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
actor, err := requirePostgresActiveMemoActor(ctx, tx, policy.ActorUserID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
attachments, err := listPostgresAttachmentSnapshots(ctx, tx, attachmentIDs)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to read attachment delete targets")
|
|
}
|
|
memoIDs, err := store.ValidateAttachmentMutationTargets(policy.ActorUserID, actor.Admin, attachmentIDs, attachments)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := store.ValidateAttachmentDeletionMemoSnapshots(memoIDs, policy.ExpectedMemoContents); err != nil {
|
|
return err
|
|
}
|
|
if err := authorizePostgresAttachmentMutation(ctx, tx, policy.ActorUserID, actor.Admin, memoIDs, policy.ExpectedMemoContents); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, attachmentID := range attachmentIDs {
|
|
result, err := tx.ExecContext(ctx, "DELETE FROM attachment WHERE id = $1", attachmentID)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to delete attachment")
|
|
}
|
|
if rows, err := result.RowsAffected(); err != nil {
|
|
return errors.Wrap(err, "failed to count deleted attachment")
|
|
} else if rows != 1 {
|
|
return store.ErrMemoMutationConflict
|
|
}
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return errors.Wrap(err, "failed to commit attachment deletion")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func listPostgresAttachmentSnapshots(ctx context.Context, tx *sql.Tx, attachmentIDs []int32) ([]*store.Attachment, error) {
|
|
attachments := make([]*store.Attachment, 0, len(attachmentIDs))
|
|
seen := make(map[int32]struct{}, len(attachmentIDs))
|
|
for _, batch := range deleteUserBatches(attachmentIDs, deleteUserBatchSize) {
|
|
clause, args := deleteUserInClause(1, batch)
|
|
if err := appendDeleteUserAttachments(ctx, tx, `SELECT id, uid, creator_id, memo_id, storage_type, reference, payload
|
|
FROM attachment WHERE id IN `+clause+` ORDER BY id`, args, seen, &attachments); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return attachments, nil
|
|
}
|
|
|
|
func authorizePostgresAttachmentMutation(
|
|
ctx context.Context,
|
|
tx *sql.Tx,
|
|
actorUserID int32,
|
|
actorIsAdmin bool,
|
|
memoIDs []int32,
|
|
expectedMemoContents map[int32]string,
|
|
) error {
|
|
writePolicy := &store.MemoWritePolicy{ActorUserID: actorUserID}
|
|
for _, memoID := range memoIDs {
|
|
snapshot := &store.MemoWriteSnapshot{ActorIsAdmin: actorIsAdmin}
|
|
var memoSpace sql.NullInt64
|
|
var content string
|
|
if err := tx.QueryRowContext(ctx, `SELECT creator_id, row_status, space_id, visibility, content
|
|
FROM memo WHERE id = $1`, memoID).Scan(
|
|
&snapshot.CreatorID, &snapshot.RowStatus, &memoSpace, &snapshot.Visibility, &content,
|
|
); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return store.ErrMemoMutationConflict
|
|
}
|
|
return errors.Wrap(err, "failed to read attachment memo")
|
|
}
|
|
snapshot.SpaceID = store.NullInt32Pointer(memoSpace)
|
|
if snapshot.SpaceID != nil {
|
|
var err error
|
|
snapshot.SourceSpaceExists, snapshot.SourceMemberActive, err = readPostgresMemoSpaceState(ctx, tx, *snapshot.SpaceID, actorUserID)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to read attachment memo space state")
|
|
}
|
|
}
|
|
if err := store.ValidateMemoWriteSnapshot(writePolicy, nil, snapshot); err != nil {
|
|
return err
|
|
}
|
|
if expectedMemoContents != nil {
|
|
expectedContent, ok := expectedMemoContents[memoID]
|
|
if !ok || content != expectedContent {
|
|
return store.ErrMemoMutationConflict
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|