refactor: wire WorkspaceScope into BelongsToWorkspace trait #62

Open
Charon wants to merge 1 commit from feat/clarify-workspace-scope-architecture into dev
Member

Summary

  • Wire WorkspaceScope into BelongsToWorkspace::bootBelongsToWorkspace() via addGlobalScope() so all queries on models using the trait are automatically filtered by workspace
  • Delegate getCurrentWorkspace() to Workspace::current() to eliminate duplicated resolution logic
  • Update scopeOwnedByCurrentWorkspace() and ownedByCurrentWorkspaceCached() to use Workspace::current() directly
  • Document the architecture relationship between WorkspaceScope (global scope) and BelongsToWorkspace (trait)

Fixes #5

Test plan

  • Existing WorkspaceSecurityTest tests pass (they already expect Account::query()->get() to be filtered by the global scope)
  • Existing WorkspaceCacheTest tests pass
  • forWorkspace() and acrossWorkspaces() macros work correctly (they call withoutGlobalScope which now has an effect)
  • Models with $workspaceContextRequired = false still opt out correctly
  • WorkspaceScope::withoutStrictMode() continues to work for test setup
## Summary - Wire `WorkspaceScope` into `BelongsToWorkspace::bootBelongsToWorkspace()` via `addGlobalScope()` so all queries on models using the trait are automatically filtered by workspace - Delegate `getCurrentWorkspace()` to `Workspace::current()` to eliminate duplicated resolution logic - Update `scopeOwnedByCurrentWorkspace()` and `ownedByCurrentWorkspaceCached()` to use `Workspace::current()` directly - Document the architecture relationship between WorkspaceScope (global scope) and BelongsToWorkspace (trait) Fixes #5 ## Test plan - [ ] Existing `WorkspaceSecurityTest` tests pass (they already expect `Account::query()->get()` to be filtered by the global scope) - [ ] Existing `WorkspaceCacheTest` tests pass - [ ] `forWorkspace()` and `acrossWorkspaces()` macros work correctly (they call `withoutGlobalScope` which now has an effect) - [ ] Models with `$workspaceContextRequired = false` still opt out correctly - [ ] `WorkspaceScope::withoutStrictMode()` continues to work for test setup
Charon added 1 commit 2026-03-24 13:22:14 +00:00
WorkspaceScope existed as a standalone Scope class but was never
registered as a global scope via BelongsToWorkspace. This meant
queries like Account::query()->get() would not be automatically
filtered by workspace, and the forWorkspace()/acrossWorkspaces()
macros (which call withoutGlobalScope) had no effect.

Changes:
- Add static::addGlobalScope(new WorkspaceScope) in bootBelongsToWorkspace()
- Delegate getCurrentWorkspace() to Workspace::current() (DRY)
- Update scopeOwnedByCurrentWorkspace() to use Workspace::current()
- Update ownedByCurrentWorkspaceCached() to use Workspace::current()
- Document architecture relationship between the two classes

Fixes #5

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin feat/clarify-workspace-scope-architecture:feat/clarify-workspace-scope-architecture
git checkout feat/clarify-workspace-scope-architecture

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git checkout dev
git merge --no-ff feat/clarify-workspace-scope-architecture
git checkout feat/clarify-workspace-scope-architecture
git rebase dev
git checkout dev
git merge --ff-only feat/clarify-workspace-scope-architecture
git checkout feat/clarify-workspace-scope-architecture
git rebase dev
git checkout dev
git merge --no-ff feat/clarify-workspace-scope-architecture
git checkout dev
git merge --squash feat/clarify-workspace-scope-architecture
git checkout dev
git merge --ff-only feat/clarify-workspace-scope-architecture
git checkout dev
git merge feat/clarify-workspace-scope-architecture
git push origin dev
Sign in to join this conversation.
No description provided.