Repository navigation
Conversation
…es that access is not permitted
| + ("_AUTH" if kwargs.get("resource").endswith("_FOLDER") else "_RESOURCE_AUTHORIZATION") | ||
| ]._build_workspace_permission(resource_id_key="target")(r, **kwargs) | ||
| ], | ||
| compare=CompareConstants.AND, |
There was a problem hiding this comment.
Here's my review of the diff:
1. Likely Bug — _build_workspace_permission is dropped for non-folder resources (4th item)
Before:
PermissionConstants[kwargs.get("resource").replace("_FOLDER", "")]
._build_workspace_permission(resource_id_key="target")(r, **kwargs)After:
PermissionConstants[
kwargs.get("resource")
+ ("_AUTH" if kwargs.get("resource").endswith("_FOLDER") else "_RESOURCE_AUTHORIZATION")
]._build_workspace_permission(resource_id_key="target")(r, **kwargs)This one actually keeps _build_workspace_permission(...), so it's fine — but note the semantic difference from the original: originally .replace("_FOLDER", "") was applied to build a base key like FOO before _build_workspace_permission. Now for folders you append _AUTH, and for non-folders _RESOURCE_AUTHORIZATION. Confirm this is intentional; the suffix mapping has changed.
2. kwargs.get("resource") can be None
str.endswith is called directly on kwargs.get("resource"). If resource is missing from kwargs, this raises AttributeError: 'NoneType' object has no attribute 'endswith'. The original code would fail differently ("RESOURCE_" + None → TypeError), but since these are lambdas evaluated at match time, prefer defensive handling or ensure resource is always present in kwargs.
3. Inconsistent / redundant string building
The ternary:
kwargs.get("resource") + ("_AUTH" if ... else "_RESOURCE_AUTHORIZATION")is repeated 12 times. This is a strong signal to extract a helper, e.g.:
def _perm_key(resource: str) -> str:
suffix = "_AUTH" if resource.endswith("_FOLDER") else "_RESOURCE_AUTHORIZATION"
return resource + suffixThen use lambda r, kwargs: PermissionConstants[_perm_key(kwargs["resource"])]. This removes duplication and future drift risk.
4. Semantic change worth double-checking
Originally, for a folder resource the base key was built by stripping _FOLDER, then appending _AUTH/_RESOURCE_AUTHORIZATION at call site. Now the logic is: keep resource as-is, and append _AUTH (folder) or _RESOURCE_AUTHORIZATION (non-folder). Make sure PermissionConstants actually contains keys of the form <RESOURCE>_AUTH (e.g. DOCUMENT_FOLDER_FOLDER_AUTH? or DOCUMENT_FOLDER_AUTH?) — a mismatch will raise KeyError at runtime, only when that code path executes (hard to catch in tests).
5. First lambda loses the RESOURCE_ prefix for non-folders
Before: "RESOURCE_" + resource.replace(...) + "_AUTH"
After: resource + "_RESOURCE_AUTHORIZATION" for non-folders.
The original prefixed RESOURCE_; the new version does not. So for a non-folder resource foo, old key = RESOURCE_foo_AUTH, new key = foo_RESOURCE_AUTHORIZATION. Unless PermissionConstants has both forms (or the old code was actually wrong), this is a behavior change. Verify the constant naming convention.
6. Repeated .get("resource") calls inside one expression
Each line calls kwargs.get("resource") twice. Minor, but capture once via a helper (see #3) for clarity and consistency.
7. No test coverage evidence
Because these lambdas are evaluated lazily and only for specific roles/resources, silent KeyErrors would not surface without exercising each branch (folder vs non-folder × each permission type). Recommend parameterized tests covering both suffixes.
Summary of actionable items
- Add a null-safety guard / assume-contract assertion for
kwargs["resource"]. - Extract
_perm_key(resource)helper and reuse everywhere. - Confirm the intended constant naming (
RESOURCE_<X>_AUTHvs<X>_RESOURCE_AUTHORIZATION) — the first lambda'sRESOURCE_prefix disappeared. - Add unit tests for folder and non-folder resource cases across all four permission lambdas.
| + ("_AUTH" if kwargs.get("resource").endswith("_FOLDER") else "_RESOURCE_AUTHORIZATION") | ||
| ]._build_workspace_permission(resource_id_key="target")(r, **kwargs) | ||
| ], | ||
| compare=CompareConstants.AND, |
There was a problem hiding this comment.
Code Review: Permission Resolution Changes
Summary
The change replaces .replace("_FOLDER", "") with an if/else that builds a permission key based on whether the resource ends with _FOLDER. It also fixes a prior bug where the old code (in most call sites) was building keys like RESOURCE_<X> without _AUTH, and in one case was appending "_AUTH" twice or missing it entirely.
However, several irregularities remain.
Issues
1. Behavioral duplication & high copy-paste risk
The exact same ternary expression is repeated 12 times. Any future change to permission naming must be updated in every location — easy to miss one and introduce a subtle auth bug.
kwargs.get("resource") + ("_AUTH" if kwargs.get("resource").endswith("_FOLDER") else "_RESOURCE_AUTHORIZATION")➡️ Extract into a helper:
def _resolve_permission_key(resource: str) -> str:
suffix = "_AUTH" if resource.endswith("_FOLDER") else "_RESOURCE_AUTHORIZATION"
return f"{resource}{suffix}"Then each lambda becomes:
lambda r, kwargs: PermissionConstants[_resolve_permission_key(kwargs["resource"])]2. Inconsistent first branch (EXTENDS_ADMIN) across classes
In WorkspaceResourceUserPermissionView and Page, the first lambda still wraps the value in a list:
[PermissionConstants[...]]…while the sibling branches return a bare PermissionConstants[...]. This is likely intentional (list = multiple OR'd permissions), but the reviewer should confirm this remains correct for both _FOLDER and non-folder resources. With the new logic, non-folder resources now get _RESOURCE_AUTHORIZATION here too, whereas before they produced RESOURCE_<X>_AUTH (with an incorrectly double-prefixed RESOURCE_). Verify that this is semantically what's desired.
3. Silent failure if resource is missing
kwargs.get("resource") returns None → .endswith → AttributeError.
Here resource comes from the URL path so it "shouldn't" be missing, but:
- Use
kwargs["resource"](fail loudly withKeyError) rather than.get()(which hides bugs until the attribute access fails), or - Add an explicit guard returning
403.
4. Inconsistent naming between folder and non-folder paths
- Folder:
<RESOURCE>_AUTH - Non-folder:
<RESOURCE>_RESOURCE_AUTHORIZATION
Double-check PermissionConstants actually defines both shapes for every possible resource value. If a resource is neither a folder nor has a corresponding *_RESOURCE_AUTHORIZATION entry, this will raise KeyError at request time. A defensive .get(...) plus explicit error response would surface the problem cleanly.
5. Pre-existing issue: calling _build_workspace_permission(...)(r, **kwargs) inside a lambda
lambda r, kwargs: PermissionConstants[...]._build_workspace_permission(resource_id_key="target")(r, **kwargs)This invokes the built check at decoration-time evaluation instead of returning a callable (compare with the sibling lambdas that return the permission object). If ViewPermission expects the lambda to return a permission/handler, this is inconsistent and may either execute an authorization check inadvertently during evaluation or return the wrong type. Worth confirming with the framework — this looks suspicious in both the before and after versions.
6. Diff context truncation
The third hunk (Page) appears to end mid-expression at compare=CompareConstants.AND,. Confirm the file is syntactically complete after the patch; a truncated diff can hide a broken bracket in the real file.
Minor / Style
- Repeated
kwargs.get("resource")calls per lambda — compute once:res = kwargs["resource"]
- The naming convention (
_AUTHvs_RESOURCE_AUTHORIZATION) is opaque to readers. Add a short comment or rename constants so the mapping rules are self-documenting.
Recommended refactor
_FOLDER_SUFFIX = "_AUTH"
_RESOURCE_SUFFIX = "_RESOURCE_AUTHORIZATION"
def resolve_perm_constants(resource: str) -> PermissionConstants:
suffix = _FOLDER_SUFFIX if resource.endswith("_FOLDER") else _RESOURCE_SUFFIX
return PermissionConstants[f"{resource}{suffix}"]Usage:
lambda r, kwargs: resolve_perm_constants(kwargs["resource"])This removes duplication, keeps behavior identical, and makes future changes a one-line edit.
Verdict
The change appears to fix a real naming bug (previously RESOURCE_ prefix was mis-added and _AUTH could be dropped), but it introduces significant duplication and should be refactored into a single helper before merging. Also verify items #2 (list vs scalar return), #5 (_build_workspace_permission(...) being called eagerly), and #6 (complete syntax) before release.
fix: the resource authorization page for the knowledge folder indicates that access is not permitted