Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding configurable team resolver for permission team id #2790

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

adrenallen
Copy link

@adrenallen adrenallen commented Jan 19, 2025

This adds a configurable team resolver to the permission registrar class.

This allows users to implement their own storage for the team id. This is useful if you are setting a current_team_id on the user table similar to how jetstream works..

By implementing a resolver in this way, the middleware approach is optional as the resolver will always go through your logic via CLI, HTTP, or otherwise. Which means less explicit calls to setPermissionsTeamId.

We are defaulting to the DefaultTeamResolver so that we are backwards compatible with anyone that already has the config published.

Let me know if more tests are desired, I felt that the current tests did a good job covering the setting and getting of team permission id.

Example of a custom resolver:

App\Permissions\CustomerTeamResolver.php

class CustomTeamResolver implements PermissionsTeamResolver
{
    public function setPermissionsTeamId($id): void
    {
        if ($id instanceof \Illuminate\Database\Eloquent\Model) {
            $id = $id->getKey();
        }

        if (auth()->user()) {
            auth()->user()->current_team_id= $id;
            auth()->user()->save();
        }
        
    }

    public function getPermissionsTeamId() : int|string|null
    {
        return auth()->user()?->current_team_id;
    }
}

config/permission.php

...
'team_resolver' => \App\Permissions\CustomTeamResolver::class,
...

- Introduced a new `PermissionsTeamResolver` interface to standardize team ID handling for permissions.
- Implemented `DefaultPermissionsTeamResolver` class to manage team IDs.
- Updated `permission.php` configuration to include `team_resolver` setting.
- Modified `PermissionRegistrar` to utilize the new team resolver for setting and getting permissions team IDs.
fixed bug with team resolver in construct for permission registrar
@adrenallen
Copy link
Author

For context, this is how I accomplished this same functionality prior to this PR:

AppServiceProvider.php

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->beforeResolving(\Spatie\Permission\PermissionRegistrar::class, function () {
            $this->app->singleton(\Spatie\Permission\PermissionRegistrar::class, PermissionRegistrar::class);
        });
    }

PermissionRegistrar.php

<?php

namespace App\Providers;

class PermissionRegistrar extends \Spatie\Permission\PermissionRegistrar
{
    /**
     * Set the team id for teams/groups support, this id is used when querying permissions/roles
     *
     * @param  int|string|\Illuminate\Database\Eloquent\Model|null  $id
     */
    public function setPermissionsTeamId($id): void
    {
        auth()->user()->current_organization_id = $id;
        auth()->user()->save();
    }

    /**
     * @return int|string|null
     */
    public function getPermissionsTeamId()
    {
        return current_organization_id();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant