mirror of
https://github.com/usememos/memos.git
synced 2026-11-22 11:59: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.
56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
stderrors "errors"
|
|
|
|
"github.com/usememos/memos/store"
|
|
)
|
|
|
|
func authorizeSQLiteMemoComment(ctx context.Context, tx dbExecutor, contextMemoID, actorUserID int32) error {
|
|
snapshot, err := loadSQLiteMemoParticipation(ctx, tx, contextMemoID, actorUserID)
|
|
if err != nil {
|
|
if stderrors.Is(err, sql.ErrNoRows) {
|
|
return store.ErrMemoSpaceNotWritable
|
|
}
|
|
return err
|
|
}
|
|
return store.ValidateMemoCommentAuthorization(snapshot)
|
|
}
|
|
|
|
// loadSQLiteMemoParticipation resolves the current actor, Space, membership,
|
|
// and memo state shared by comment and reaction participation.
|
|
func loadSQLiteMemoParticipation(ctx context.Context, tx dbExecutor, memoID, actorUserID int32) (*store.MemoCommentAuthorizationSnapshot, error) {
|
|
actor, err := readSQLiteMemoActor(ctx, tx, actorUserID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
snapshot := &store.MemoCommentAuthorizationSnapshot{ActorUserID: actorUserID, Actor: actor, ContextID: memoID}
|
|
|
|
var contextSpace sql.NullInt64
|
|
err = tx.QueryRowContext(ctx, `SELECT creator_id, row_status, visibility, space_id FROM memo WHERE id = ?`, memoID).Scan(
|
|
&snapshot.ContextCreatorID, &snapshot.ContextRowStatus, &snapshot.ContextVisibility, &contextSpace,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
snapshot.ContextSpaceID = store.NullInt32Pointer(contextSpace)
|
|
|
|
if snapshot.ContextSpaceID != nil {
|
|
var exists bool
|
|
if err := tx.QueryRowContext(ctx, "SELECT EXISTS(SELECT 1 FROM space WHERE id = ?)", *snapshot.ContextSpaceID).Scan(&exists); err == nil {
|
|
snapshot.ContextSpaceExists = exists
|
|
} else if !stderrors.Is(err, sql.ErrNoRows) {
|
|
return nil, err
|
|
}
|
|
if actor.Admin {
|
|
return snapshot, nil
|
|
}
|
|
if snapshot.ContextMemberActive, err = sqliteSpaceMemberActive(ctx, tx, *snapshot.ContextSpaceID, actorUserID); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return snapshot, nil
|
|
}
|