|
22 | 22 | */ |
23 | 23 |
|
24 | 24 | import { isApiAccessDeniedError } from '@object-ui/data-objectstack'; |
| 25 | +import { errorCodeIs } from '@object-ui/types'; |
25 | 26 | import type { ApiError } from '@objectstack/spec/api'; |
26 | 27 |
|
27 | 28 | /** |
@@ -136,6 +137,43 @@ export function declaredUserMessage(err: unknown): string | null { |
136 | 137 | return null; |
137 | 138 | } |
138 | 139 |
|
| 140 | +/** |
| 141 | + * The ADR-0112 code a delegated administrator's write answers when it falls |
| 142 | + * outside the caller's OWN `adminScope` (the ledger row registered under |
| 143 | + * `@objectstack/plugin-security`, thrown as `DelegationScopeViolationError`). |
| 144 | + */ |
| 145 | +const DELEGATION_SCOPE_VIOLATION = 'DELEGATION_SCOPE_VIOLATION'; |
| 146 | + |
| 147 | +/** |
| 148 | + * The server's reason for a delegated-admin scope refusal, or `null` when the |
| 149 | + * refusal is anything else. |
| 150 | + * |
| 151 | + * A `403 DELEGATION_SCOPE_VIOLATION` names which boundary of the caller's own |
| 152 | + * delegated scope refused the write (a business unit outside the delegated |
| 153 | + * subtree, a permission set outside the allowlist, a target user outside the |
| 154 | + * subtree). That text describes only the caller's own scope, so it is safe to |
| 155 | + * show, and it tells the delegate what to fix (ruling on objectstack#26059, Q3). |
| 156 | + * Every other denial stays opaque: `PERMISSION_DENIED`, `FORBIDDEN`, a bare 403 |
| 157 | + * or a row-level-security refusal answers `null` here, and callers keep their |
| 158 | + * fixed localized line (ruling B′ on objectstack#24914, unchanged). |
| 159 | + * |
| 160 | + * Callers must ask this BEFORE {@link isPermissionError}: that predicate answers |
| 161 | + * true for any 403, so a scope refusal read after it is flattened into the |
| 162 | + * plain denial. The code is read from the same pair {@link isPermissionError} |
| 163 | + * reads (`code`, then `details.code`); the text is |
| 164 | + * {@link extractWriteErrorMessage}'s, without the `[Security] ` machine prefix. |
| 165 | + * A scope refusal that carries no usable text answers `null`, so the caller |
| 166 | + * still says something. |
| 167 | + */ |
| 168 | +export function delegationScopeMessage(err: unknown): string | null { |
| 169 | + if (!err || typeof err !== 'object') return null; |
| 170 | + const details = (err as { details?: unknown }).details; |
| 171 | + if (!errorCodeIs(err, DELEGATION_SCOPE_VIOLATION) && !errorCodeIs(details, DELEGATION_SCOPE_VIOLATION)) { |
| 172 | + return null; |
| 173 | + } |
| 174 | + return extractWriteErrorMessage(err); |
| 175 | +} |
| 176 | + |
139 | 177 | /** One field the server rejected, normalised for `form.setError`. */ |
140 | 178 | export interface WriteFieldError { |
141 | 179 | /** Field name, matching the form control's `name`. */ |
@@ -198,6 +236,10 @@ export function extractFieldErrors(err: unknown): WriteFieldError[] | null { |
198 | 236 | * `PERMISSION_DENIED` / `FORBIDDEN` / a row-level-security denial). Lets a |
199 | 237 | * caller show a friendly "you don't have permission" toast rather than dumping |
200 | 238 | * the raw server text. |
| 239 | + * |
| 240 | + * A delegated-admin scope refusal is a 403 too, so this answers true for it: |
| 241 | + * a caller that shows that refusal's reason asks |
| 242 | + * {@link delegationScopeMessage} first. |
201 | 243 | */ |
202 | 244 | export function isPermissionError(err: unknown): boolean { |
203 | 245 | const e = err as Record<string, any> | null | undefined; |
|
0 commit comments