Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions docs/architecture/media-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -709,13 +709,14 @@ the stored row only, so it answers while requests are turned off. See
Whether an account may request, whether its requests need approval, and how
many it may make resolve in layers: the account's own settings, then its
access group's (`request_group_limits`), then the server-wide request
settings. A layer set to inherit defers to the next; admins are never capped
by a group. An account is blocked in one of two places only: requests turned
off server-wide, or the requests switch on the account or its access group
(`requests_allowed`). The older `blocked` limit and approval modes on an
account are still honored when an API client writes them, but the migration
that added group limits moved existing ones onto the account's switch and no
editor offers them.
settings. A layer set to inherit defers to the next. Admin accounts skip
their group and are never capped by a quota from any layer, so their usage is
not counted; approval and blocking still apply to them. An account is blocked
in one of two places only: requests turned off server-wide, or the requests
switch on the account or its access group (`requests_allowed`). The older
`blocked` limit and approval modes on an account are still honored when an API
client writes them, but the migration that added group limits moved existing
ones onto the account's switch and no editor offers them.

A household manager can also block one profile from requests while the account
and its other profiles keep them (`requests_allowed` on the v2 profile, stored
Expand Down
5 changes: 3 additions & 2 deletions internal/backup/credentials_authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ func inviteePolicies(ctx context.Context, tx pgx.Tx, tables []secretTable, live

// requestPolicy reads, in tx's database, the request policy of user, whose
// requests switch is allowed: the server's request settings, its access
// group's limits, and its own (requests.LayeredPolicy). A table an older
// group's limits, and its own, with no quota for an admin
// (requests.LayeredPolicy). A table an older
// backup does not have counts as unset.
func requestPolicy(ctx context.Context, tx pgx.Tx, user *models.User, allowed bool) (requests.EffectivePolicy, error) {
settings := requests.Settings{GlobalMaxRequests: 5, GlobalWindowDays: 7}
Expand Down Expand Up @@ -465,7 +466,7 @@ func requestPolicy(ctx context.Context, tx pgx.Tx, user *models.User, allowed bo
group = &g
}
}
return requests.LayeredPolicy(settings, allowed, group, own), nil
return requests.LayeredPolicy(settings, allowed, group, own, user.Role == models.RoleAdmin), nil
}

// optionalRow scans one row of sql into dest in a savepoint: no row, or a
Expand Down
18 changes: 11 additions & 7 deletions internal/requests/group_limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/jackc/pgx/v5"

"github.com/Silo-Server/silo-server/internal/access"
"github.com/Silo-Server/silo-server/internal/models"
)

// Who may request, and on what terms, resolves in layers: the account's own
// limits, then its access group's, then the server-wide settings. Blocking is
// not a limit: an account is blocked by the requests switch on the account or
// its access group (access.EffectiveUserPolicy.RequestsAllowed), or by
// requests being off server-wide. The older "blocked" limit and approval
// limits, then its access group's, then the server-wide settings. A server
// admin skips its group and is never capped by a quota from any layer.
// Blocking is not a limit: an account is blocked by the requests switch on the
// account or its access group (access.EffectiveUserPolicy.RequestsAllowed), or
// by requests being off server-wide. The older "blocked" limit and approval
// modes on an account are still honored when written, but no editor offers
// them.

Expand Down Expand Up @@ -195,11 +197,13 @@ func normalizeGroupLimit(in GroupLimit) (GroupLimit, error) {
}

// requestAccess is what the account and its access group say about
// requesting: whether the account may request at all, and the group's
// limits when the group applies.
// requesting: whether the account may request at all, the group's limits
// when the group applies, and whether it is a server admin, which no request
// quota caps.
type requestAccess struct {
allowed bool
group *GroupLimit
admin bool
}

// viewerRequestAccess resolves an account's requests switch and its access
Expand All @@ -221,7 +225,7 @@ func (s *Service) viewerRequestAccess(ctx context.Context, userID int) (requestA
if err != nil {
return requestAccess{}, err
}
out := requestAccess{allowed: effective.RequestsAllowed}
out := requestAccess{allowed: effective.RequestsAllowed, admin: user.Role == models.RoleAdmin}
if access.GroupApplies(user) {
if store, ok := s.store.(GroupLimitStore); ok {
if out.group, err = store.GetGroupLimit(ctx, *user.AccessGroupID); err != nil {
Expand Down
32 changes: 32 additions & 0 deletions internal/requests/group_limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@ func TestEffectivePolicyLayersAccountGroupServer(t *testing.T) {
}
}

// Admins are never capped by a request quota, wherever the quota comes from,
// but an account-level block still applies to them.
func TestAdminsHaveUnlimitedRequests(t *testing.T) {
groupID := int64(1)
cases := []struct {
name string
limit *UserLimit
wantBlocked bool
}{
{name: "server and group quota"},
{name: "account quota", limit: &UserLimit{UserID: 1, LimitMode: LimitModeCustom, MaxRequests: intPtr(1), WindowDays: intPtr(1)}},
{name: "account block", limit: &UserLimit{UserID: 1, LimitMode: LimitModeBlocked}, wantBlocked: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
store := newFakeStore()
store.settings.GlobalMaxRequests = 0
store.groupLimits = map[int64]*GroupLimit{1: {GroupID: 1, LimitMode: LimitModeCustom, MaxRequests: intPtr(1), WindowDays: intPtr(1)}}
store.limit = tc.limit
svc := groupedService(store, &models.User{ID: 1, Role: models.RoleAdmin, AccessGroupID: &groupID})

policy, err := svc.EffectivePolicy(context.Background(), 1)
if err != nil {
t.Fatal(err)
}
if !policy.Unlimited || policy.Blocked != tc.wantBlocked {
t.Fatalf("admin policy = %+v, want unlimited, blocked=%v", policy, tc.wantBlocked)
}
})
}
}

// Every way to block shows up the same way: a detail page that says blocked.
func TestAccountSwitchBlocksRequestState(t *testing.T) {
store := newFakeStore()
Expand Down
11 changes: 8 additions & 3 deletions internal/requests/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1761,8 +1761,10 @@ func withPolicyCache(ctx context.Context) context.Context {
// LayeredPolicy is the request policy an account gets, without its usage:
// the server's settings, then its access group's limits, then its own, a
// layer set to inherit deferring to the next; allowed is the account's
// requests switch. Every reader of an account's request authority uses it.
func LayeredPolicy(settings Settings, allowed bool, group *GroupLimit, limit *UserLimit) EffectivePolicy {
// requests switch. A server admin is never capped by a quota from any layer,
// though a block still applies. Every reader of an account's request
// authority uses it.
func LayeredPolicy(settings Settings, allowed bool, group *GroupLimit, limit *UserLimit, admin bool) EffectivePolicy {
policy := EffectivePolicy{
RequestsEnabled: settings.RequestsEnabled,
MaxRequests: settings.GlobalMaxRequests,
Expand Down Expand Up @@ -1813,6 +1815,9 @@ func LayeredPolicy(settings Settings, allowed bool, group *GroupLimit, limit *Us
case ApprovalModeAuto:
policy.AutoApprove = true
}
if admin {
policy.Unlimited = true
}

return policy
}
Expand All @@ -1835,7 +1840,7 @@ func (s *Service) resolvePolicy(ctx context.Context, userID int) (EffectivePolic
if viewerAccess.group != nil {
group = viewerAccess.group
}
policy := LayeredPolicy(settings, viewerAccess.allowed, group, limit)
policy := LayeredPolicy(settings, viewerAccess.allowed, group, limit, viewerAccess.admin)
policy.WindowStart = s.now().AddDate(0, 0, -policy.WindowDays)
if !policy.Unlimited {
used, err := s.store.CountUserRequestsSince(ctx, userID, policy.WindowStart)
Expand Down
209 changes: 107 additions & 102 deletions web/src/components/admin/RequestLimitFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface RequestLimitFieldsProps {
disabled?: boolean;
/** One field under the other, for a narrow panel; side by side from `sm` otherwise. */
stacked?: boolean;
/** False hides the limit, for an admin account, which no request limit caps. */
showLimit?: boolean;
}

/** Space-separated ids for aria-describedby, or undefined when there are none. */
Expand All @@ -56,6 +58,7 @@ export function RequestLimitFields({
errors,
disabled,
stacked = false,
showLimit = true,
}: RequestLimitFieldsProps) {
const t = useT();
const approvalId = useId();
Expand Down Expand Up @@ -122,109 +125,111 @@ export function RequestLimitFields({
) : null}
</div>

<div className="min-w-0 space-y-2">
<Label htmlFor={limitId}>{t("web.admin.common.requestLimits.limit")}</Label>
<Select
value={draft.limit}
onValueChange={(limit) => setLimit(limit as RequestLimitChoice)}
disabled={disabled}
>
<SelectTrigger
id={limitId}
className="w-full"
aria-describedby={describedBy(limitHint !== undefined && limitHintId)}
>
<SelectValue />
</SelectTrigger>
<SelectContent className={TOUCH_TARGETS}>
<SelectItem value="inherit">{inheritLabel}</SelectItem>
<SelectItem value="custom">
{t("web.admin.common.requestLimits.limitCustom")}
</SelectItem>
<SelectItem value="unlimited">
{t("web.admin.common.requestLimits.limitNone")}
</SelectItem>
</SelectContent>
</Select>
{custom ? (
<div className="flex flex-wrap items-center gap-2 text-sm">
{t("web.admin.common.requestLimits.customLimit", {
max: () => (
<>
<Label htmlFor={maxId} className="sr-only">
{t("web.admin.common.requestLimits.maxRequestsLabel")}
</Label>
<Input
id={maxId}
type="number"
inputMode="numeric"
min={0}
step={1}
className="w-20"
value={draft.maxRequests}
onChange={(event) => editNumbers({ maxRequests: event.target.value })}
aria-invalid={errors.maxRequests ? true : undefined}
aria-describedby={describedBy(
Boolean(errors.maxRequests) && maxErrorId,
zero && zeroHintId,
)}
disabled={disabled}
/>
</>
),
window: () => (
<>
<Label htmlFor={windowId} className="sr-only">
{t("web.admin.common.requestLimits.windowDaysLabel")}
</Label>
<Input
id={windowId}
type="number"
inputMode="numeric"
min={1}
step={1}
className="w-20"
value={draft.windowDays}
onChange={(event) => editNumbers({ windowDays: event.target.value })}
aria-invalid={errors.windowDays ? true : undefined}
aria-describedby={describedBy(Boolean(errors.windowDays) && windowErrorId)}
disabled={disabled}
/>
</>
),
unit: (chunks) => <span className="text-muted-foreground">{chunks}</span>,
})}
</div>
) : null}
{limitHint !== undefined ? (
<p id={limitHintId} className="text-muted-foreground text-xs">
{limitHint}
</p>
) : null}
{zero ? (
<p id={zeroHintId} className="text-muted-foreground text-xs">
{t("web.admin.common.requestLimits.zeroHint", { subject })}
</p>
) : null}
{custom && errors.maxRequests ? (
<p
id={maxErrorId}
role={edited ? "alert" : undefined}
className="text-destructive text-xs"
{showLimit ? (
<div className="min-w-0 space-y-2">
<Label htmlFor={limitId}>{t("web.admin.common.requestLimits.limit")}</Label>
<Select
value={draft.limit}
onValueChange={(limit) => setLimit(limit as RequestLimitChoice)}
disabled={disabled}
>
{errors.maxRequests}
</p>
) : null}
{custom && errors.windowDays ? (
<p
id={windowErrorId}
role={edited ? "alert" : undefined}
className="text-destructive text-xs"
>
{errors.windowDays}
</p>
) : null}
</div>
<SelectTrigger
id={limitId}
className="w-full"
aria-describedby={describedBy(limitHint !== undefined && limitHintId)}
>
<SelectValue />
</SelectTrigger>
<SelectContent className={TOUCH_TARGETS}>
<SelectItem value="inherit">{inheritLabel}</SelectItem>
<SelectItem value="custom">
{t("web.admin.common.requestLimits.limitCustom")}
</SelectItem>
<SelectItem value="unlimited">
{t("web.admin.common.requestLimits.limitNone")}
</SelectItem>
</SelectContent>
</Select>
{custom ? (
<div className="flex flex-wrap items-center gap-2 text-sm">
{t("web.admin.common.requestLimits.customLimit", {
max: () => (
<>
<Label htmlFor={maxId} className="sr-only">
{t("web.admin.common.requestLimits.maxRequestsLabel")}
</Label>
<Input
id={maxId}
type="number"
inputMode="numeric"
min={0}
step={1}
className="w-20"
value={draft.maxRequests}
onChange={(event) => editNumbers({ maxRequests: event.target.value })}
aria-invalid={errors.maxRequests ? true : undefined}
aria-describedby={describedBy(
Boolean(errors.maxRequests) && maxErrorId,
zero && zeroHintId,
)}
disabled={disabled}
/>
</>
),
window: () => (
<>
<Label htmlFor={windowId} className="sr-only">
{t("web.admin.common.requestLimits.windowDaysLabel")}
</Label>
<Input
id={windowId}
type="number"
inputMode="numeric"
min={1}
step={1}
className="w-20"
value={draft.windowDays}
onChange={(event) => editNumbers({ windowDays: event.target.value })}
aria-invalid={errors.windowDays ? true : undefined}
aria-describedby={describedBy(Boolean(errors.windowDays) && windowErrorId)}
disabled={disabled}
/>
</>
),
unit: (chunks) => <span className="text-muted-foreground">{chunks}</span>,
})}
</div>
) : null}
{limitHint !== undefined ? (
<p id={limitHintId} className="text-muted-foreground text-xs">
{limitHint}
</p>
) : null}
{zero ? (
<p id={zeroHintId} className="text-muted-foreground text-xs">
{t("web.admin.common.requestLimits.zeroHint", { subject })}
</p>
) : null}
{custom && errors.maxRequests ? (
<p
id={maxErrorId}
role={edited ? "alert" : undefined}
className="text-destructive text-xs"
>
{errors.maxRequests}
</p>
) : null}
{custom && errors.windowDays ? (
<p
id={windowErrorId}
role={edited ? "alert" : undefined}
className="text-destructive text-xs"
>
{errors.windowDays}
</p>
) : null}
</div>
) : null}
</div>
);
}
Loading
Loading