Skip to content

Commit 3cea227

Browse files
authored
Add configurable team resolver for permission team id (#2790)
* Add Permissions Team Resolver Implementation - 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.
1 parent a629566 commit 3cea227

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

config/permission.php

+6
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@
122122

123123
'teams' => false,
124124

125+
126+
/*
127+
* The class to use to resolve the permissions team id
128+
*/
129+
'team_resolver' => \Spatie\Permission\DefaultTeamResolver::class,
130+
125131
/*
126132
* Passport Client Credentials Grant
127133
* When set to true the package will use Passports Client to check permissions
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Spatie\Permission\Contracts;
4+
5+
interface PermissionsTeamResolver
6+
{
7+
/**
8+
* @return int|string|null
9+
*/
10+
public function getPermissionsTeamId() : int|string|null;
11+
12+
/**
13+
* Set the team id for teams/groups support, this id is used when querying permissions/roles
14+
*
15+
* @param int|string|\Illuminate\Database\Eloquent\Model|null $id
16+
*/
17+
public function setPermissionsTeamId($id): void;
18+
}

src/DefaultTeamResolver.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Spatie\Permission;
4+
5+
use Spatie\Permission\Contracts\PermissionsTeamResolver;
6+
7+
class DefaultTeamResolver implements PermissionsTeamResolver
8+
{
9+
protected int|string|null $teamId = null;
10+
11+
/**
12+
* Set the team id for teams/groups support, this id is used when querying permissions/roles
13+
*
14+
* @param int|string|\Illuminate\Database\Eloquent\Model|null $id
15+
*/
16+
public function setPermissionsTeamId($id): void
17+
{
18+
if ($id instanceof \Illuminate\Database\Eloquent\Model) {
19+
$id = $id->getKey();
20+
}
21+
$this->teamId = $id;
22+
}
23+
24+
/**
25+
* @return int|string|null
26+
*/
27+
public function getPermissionsTeamId() : int|string|null
28+
{
29+
return $this->teamId;
30+
}
31+
}

src/PermissionRegistrar.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Database\Eloquent\Collection;
1111
use Illuminate\Database\Eloquent\Model;
1212
use Spatie\Permission\Contracts\Permission;
13+
use Spatie\Permission\Contracts\PermissionsTeamResolver;
1314
use Spatie\Permission\Contracts\Role;
1415

1516
class PermissionRegistrar
@@ -34,9 +35,9 @@ class PermissionRegistrar
3435

3536
public bool $teams;
3637

37-
public string $teamsKey;
38+
protected PermissionsTeamResolver $teamResolver;
3839

39-
protected string|int|null $teamId = null;
40+
public string $teamsKey;
4041

4142
public string $cacheKey;
4243

@@ -55,6 +56,7 @@ public function __construct(CacheManager $cacheManager)
5556
{
5657
$this->permissionClass = config('permission.models.permission');
5758
$this->roleClass = config('permission.models.role');
59+
$this->teamResolver = new (config('permission.team_resolver', DefaultTeamResolver::class));
5860

5961
$this->cacheManager = $cacheManager;
6062
$this->initializeCache();
@@ -101,18 +103,15 @@ protected function getCacheStoreFromConfig(): Repository
101103
*/
102104
public function setPermissionsTeamId($id): void
103105
{
104-
if ($id instanceof \Illuminate\Database\Eloquent\Model) {
105-
$id = $id->getKey();
106-
}
107-
$this->teamId = $id;
106+
$this->teamResolver->setPermissionsTeamId($id);
108107
}
109108

110109
/**
111110
* @return int|string|null
112111
*/
113112
public function getPermissionsTeamId()
114113
{
115-
return $this->teamId;
114+
return $this->teamResolver->getPermissionsTeamId();
116115
}
117116

118117
/**

0 commit comments

Comments
 (0)