@@ -41,25 +41,23 @@ public function getRoleClass()
41
41
*/
42
42
public function roles (): BelongsToMany
43
43
{
44
- $ model_has_roles = config ('permission.table_names.model_has_roles ' );
45
-
46
- return $ this ->morphToMany (
44
+ $ relation = $ this ->morphToMany (
47
45
config ('permission.models.role ' ),
48
46
'model ' ,
49
- $ model_has_roles ,
47
+ config ( ' permission.table_names. model_has_roles' ) ,
50
48
config ('permission.column_names.model_morph_key ' ),
51
49
PermissionRegistrar::$ pivotRole
52
- )
53
- ->where (function ($ q ) use ($ model_has_roles ) {
54
- $ q ->when (PermissionRegistrar::$ teams , function ($ q ) use ($ model_has_roles ) {
55
- $ teamId = app (PermissionRegistrar::class)->getPermissionsTeamId ();
56
- $ q ->where ($ model_has_roles .'. ' .PermissionRegistrar::$ teamsKey , $ teamId )
57
- ->where (function ($ q ) use ($ teamId ) {
58
- $ teamField = config ('permission.table_names.roles ' ).'. ' .PermissionRegistrar::$ teamsKey ;
59
- $ q ->whereNull ($ teamField )->orWhere ($ teamField , $ teamId );
60
- });
50
+ );
51
+
52
+ if (! PermissionRegistrar::$ teams ) {
53
+ return $ relation ;
54
+ }
55
+
56
+ return $ relation ->wherePivot (PermissionRegistrar::$ teamsKey , getPermissionsTeamId ())
57
+ ->where (function ($ q ) {
58
+ $ teamField = config ('permission.table_names.roles ' ).'. ' .PermissionRegistrar::$ teamsKey ;
59
+ $ q ->whereNull ($ teamField )->orWhere ($ teamField , getPermissionsTeamId ());
61
60
});
62
- });
63
61
}
64
62
65
63
/**
@@ -98,21 +96,6 @@ public function scopeRole(Builder $query, $roles, $guard = null): Builder
98
96
});
99
97
}
100
98
101
- /**
102
- * Add teams pivot if teams are enabled
103
- *
104
- * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
105
- */
106
- protected function getRolesRelation ()
107
- {
108
- $ relation = $ this ->roles ();
109
- if (PermissionRegistrar::$ teams && ! is_a ($ this , Permission::class)) {
110
- $ relation ->wherePivot (PermissionRegistrar::$ teamsKey , app (PermissionRegistrar::class)->getPermissionsTeamId ());
111
- }
112
-
113
- return $ relation ;
114
- }
115
-
116
99
/**
117
100
* Assign the given role to the model.
118
101
*
@@ -122,33 +105,30 @@ protected function getRolesRelation()
122
105
*/
123
106
public function assignRole (...$ roles )
124
107
{
125
- $ roleClass = $ this ->getRoleClass ();
126
108
$ roles = collect ($ roles )
127
109
->flatten ()
128
- ->map (function ($ role ) {
110
+ ->reduce (function ($ array , $ role ) {
129
111
if (empty ($ role )) {
130
- return false ;
112
+ return $ array ;
113
+ }
114
+
115
+ $ role = $ this ->getStoredRole ($ role );
116
+ if (! $ role instanceof Role) {
117
+ return $ array ;
131
118
}
132
119
133
- return $ this ->getStoredRole ($ role );
134
- })
135
- ->filter (function ($ role ) {
136
- return $ role instanceof Role;
137
- })
138
- ->each (function ($ role ) {
139
120
$ this ->ensureModelSharesGuard ($ role );
140
- })
141
- ->map (function ($ role ) {
142
- return [$ role ->getKeyName () => $ role ->getKey (), 'values ' => PermissionRegistrar::$ teams && ! is_a ($ this , Permission::class) ?
143
- [PermissionRegistrar::$ teamsKey => app (PermissionRegistrar::class)->getPermissionsTeamId ()] : [],
144
- ];
145
- })
146
- ->pluck ('values ' , (new $ roleClass ())->getKeyName ())->toArray ();
121
+
122
+ $ array [$ role ->getKey ()] = PermissionRegistrar::$ teams && ! is_a ($ this , Permission::class) ?
123
+ [PermissionRegistrar::$ teamsKey => getPermissionsTeamId ()] : [];
124
+
125
+ return $ array ;
126
+ }, []);
147
127
148
128
$ model = $ this ->getModel ();
149
129
150
130
if ($ model ->exists ) {
151
- $ this ->getRolesRelation ()->sync ($ roles , false );
131
+ $ this ->roles ()->sync ($ roles , false );
152
132
$ model ->load ('roles ' );
153
133
} else {
154
134
$ class = \get_class ($ model );
@@ -178,7 +158,7 @@ function ($object) use ($roles, $model) {
178
158
*/
179
159
public function removeRole ($ role )
180
160
{
181
- $ this ->getRolesRelation ()->detach ($ this ->getStoredRole ($ role ));
161
+ $ this ->roles ()->detach ($ this ->getStoredRole ($ role ));
182
162
183
163
$ this ->load ('roles ' );
184
164
@@ -198,7 +178,7 @@ public function removeRole($role)
198
178
*/
199
179
public function syncRoles (...$ roles )
200
180
{
201
- $ this ->getRolesRelation ()->detach ();
181
+ $ this ->roles ()->detach ();
202
182
203
183
return $ this ->assignRole ($ roles );
204
184
}
0 commit comments