{"id":51045288,"url":"https://github.com/whilesmartphp/eloquent-owner-access","last_synced_at":"2026-06-22T13:02:14.124Z","repository":{"id":363873782,"uuid":"1220645670","full_name":"whilesmartphp/eloquent-owner-access","owner":"whilesmartphp","description":null,"archived":false,"fork":false,"pushed_at":"2026-06-10T17:47:54.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"dev","last_synced_at":"2026-06-10T19:17:05.216Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/whilesmartphp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-25T06:25:58.000Z","updated_at":"2026-06-10T17:46:36.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/whilesmartphp/eloquent-owner-access","commit_stats":null,"previous_names":["whilesmartphp/eloquent-owner-access"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/whilesmartphp/eloquent-owner-access","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whilesmartphp%2Feloquent-owner-access","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whilesmartphp%2Feloquent-owner-access/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whilesmartphp%2Feloquent-owner-access/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whilesmartphp%2Feloquent-owner-access/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/whilesmartphp","download_url":"https://codeload.github.com/whilesmartphp/eloquent-owner-access/tar.gz/refs/heads/dev","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whilesmartphp%2Feloquent-owner-access/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34649822,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-22T02:00:06.391Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-06-22T13:02:12.832Z","updated_at":"2026-06-22T13:02:14.114Z","avatar_url":"https://github.com/whilesmartphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# eloquent-owner-access\n\nPluggable polymorphic owner authorization for Laravel packages with `owner_type` / `owner_id` columns.\n\nDomain packages (accounts, expenses, payments, invoices, products, customers, ...) expose CRUD endpoints scoped by a polymorphic owner. They should not have to know how the host app decides who owns what. This package gives them a single contract to consult and a default that preserves existing \"trust the client\" behavior, so adoption is incremental and safe.\n\n## Concept\n\nOne contract:\n\n```php\ninterface OwnerAuthorizer\n{\n    public function authorize(?Authenticatable $user, string $ownerType, mixed $ownerId): bool;\n\n    public function scope(\n        Builder $query,\n        ?Authenticatable $user,\n        string $ownerTypeColumn = 'owner_type',\n        string $ownerIdColumn = 'owner_id',\n    ): Builder;\n}\n```\n\n`authorize()` is per-record (called from `FormRequest::authorize()` for store/update and from controllers for show/destroy). `scope()` constrains index queries.\n\nThe default binding is `AllowAllAuthorizer` (everything goes through). Hosts that want strict tenancy bind their own implementation.\n\n## Installation\n\n```bash\ncomposer require whilesmart/eloquent-owner-access\n```\n\nThe service provider auto-registers via Laravel package discovery and `bindIf`s the default authorizer.\n\n## Usage in a package\n\nIn a `StoreXRequest`:\n\n```php\nuse Whilesmart\\OwnerAccess\\Concerns\\AuthorizesOwnerRequest;\n\nclass StoreAccountRequest extends FormRequest\n{\n    use AuthorizesOwnerRequest;\n\n    public function authorize(): bool\n    {\n        return $this-\u003eauthorizeOwnerInRequest();\n    }\n\n    public function rules(): array { /* ... */ }\n}\n```\n\nIn an `UpdateXRequest`:\n\n```php\npublic function authorize(): bool\n{\n    return $this-\u003eauthorizeOwnerOfBoundModel('account');\n}\n```\n\nIn the controller:\n\n```php\nuse Whilesmart\\OwnerAccess\\Concerns\\AuthorizesOwnerController;\n\nclass AccountController extends Controller\n{\n    use AuthorizesOwnerController;\n\n    public function index(Request $request)\n    {\n        $query = Account::query();\n        $query = $this-\u003escopeAccessibleOwners($query, $request-\u003euser());\n        // ...\n    }\n\n    public function show(Account $account, Request $request)\n    {\n        $this-\u003eauthorizeAccessTo($account, $request-\u003euser());\n        // ...\n    }\n\n    public function destroy(Account $account, Request $request)\n    {\n        $this-\u003eauthorizeAccessTo($account, $request-\u003euser());\n        $account-\u003edelete();\n        // ...\n    }\n}\n```\n\nUpdate is already covered by `UpdateXRequest::authorize()`, so no controller change is needed there.\n\n## Usage in a host app\n\nBy default everything is allowed. To enforce tenancy, write an authorizer that consults your tenancy model and bind it:\n\n```php\nnamespace App\\Authorization;\n\nuse Illuminate\\Contracts\\Auth\\Authenticatable;\nuse Illuminate\\Database\\Eloquent\\Builder;\nuse Whilesmart\\OwnerAccess\\Contracts\\OwnerAuthorizer;\n\nclass WorkspaceMemberAuthorizer implements OwnerAuthorizer\n{\n    public function authorize(?Authenticatable $user, string $ownerType, mixed $ownerId): bool\n    {\n        if ($user === null || $ownerType !== \\App\\Models\\Workspace::class) {\n            return false;\n        }\n\n        return $user-\u003eworkspaces()-\u003ewhereKey($ownerId)-\u003eexists();\n    }\n\n    public function scope(Builder $query, ?Authenticatable $user, string $ownerTypeColumn = 'owner_type', string $ownerIdColumn = 'owner_id'): Builder\n    {\n        if ($user === null) {\n            return $query-\u003ewhereRaw('0 = 1');\n        }\n\n        return $query-\u003ewhere($ownerTypeColumn, \\App\\Models\\Workspace::class)\n            -\u003ewhereIn($ownerIdColumn, $user-\u003eworkspaces()-\u003epluck('workspaces.id'));\n    }\n}\n```\n\nThen in `AppServiceProvider::register()`:\n\n```php\n$this-\u003eapp-\u003ebind(\n    \\Whilesmart\\OwnerAccess\\Contracts\\OwnerAuthorizer::class,\n    \\App\\Authorization\\WorkspaceMemberAuthorizer::class,\n);\n```\n\nThat single binding switches every consuming package over to strict mode at once.\n\n## Backwards compatibility\n\nThe provider uses `bindIf`, so any explicit binding the host registers (before or after) wins. Without an explicit binding, the default `AllowAllAuthorizer` keeps current behavior.\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## License\n\nMIT.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhilesmartphp%2Feloquent-owner-access","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwhilesmartphp%2Feloquent-owner-access","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhilesmartphp%2Feloquent-owner-access/lists"}