-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
It does not detect the model guard if you use it from the constructor #1923
Comments
laravel-permission/src/Models/Permission.php Lines 23 to 28 in 193e50e
For create a new permission YourCustomPermission::create(['name'=>'name','guard_name'=>'guard_name']); and now guard_name is setted
Maybe related #1793 could you create a minimal Laravel app that demonstrates the issue? Or maybe create a failing tests for us to look at? example |
The user class is a standard one, making use of the traits use Spatie\Permission\Traits\HasPermissions;
use Spatie\Permission\Traits\HasRoles; The guards that I have are web and admin. |
For create a new permission or role set |
Yes, this would work, but I had it on, it is that the guard admin is put if or if without passing it by parameter. In other words, when using that model, the default guard is admin and not web. |
It's no a bug, it's a expected behaivor, i don't understand why you wanna do that, but class YourCustomPermission extends SpatiePermission
{
public function __construct(array $attributes = [])
{
$attributes['guard_name'] = config('xxx.guard');
parent::__construct($attributes);
}
public static function create(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('xxx.guard');;
$permission = static::getPermission(['name' => $attributes['name'], 'guard_name' => $attributes['guard_name']]);
if ($permission) {
throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
}
return static::query()->create($attributes);
}
} |
If you want to close this bug, or mark it as an improvement, but ideally, it is to obtain the guard, either by attribute or constructor |
Ideally this code will work, since config cannot be used in an attribute. class AdminPermission extends SpatiePermission
{
public function __construct(array $attributes = [])
{
$this->guard_name = $attributes['guard_name '] ?? config('xxx.guard');
parent::__construct($attributes);
}
} What I want to use, is this class, to create the admin permissions, without passing any additional parameters. I have also tried this, but it doesn't work either class AdminPermission extends SpatiePermission
{
public function __construct(array $attributes = [])
{
$this->guard_name = $attributes['guard_name '] ?? config('xxx.guard');
$attributes['guard_name'] = $this->guard_name;
parent::__construct($attributes);
}
} config('xxx.guard') is admin, but in bbdd is stored with web |
There is, you forget class YourCustomPermission extends SpatiePermission
{
public function __construct(array $attributes = [])
{
$attributes['guard_name'] = config('xxx.guard');
parent::__construct($attributes);
}
public static function create(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('xxx.guard');;
$permission = static::getPermission(['name' => $attributes['name'], 'guard_name' => $attributes['guard_name']]);
if ($permission) {
throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
}
return static::query()->create($attributes);
}
} |
1.- In this line,
2.- In laravel-permission/src/Guard.php Line 17 in 193e50e
3.- Therefore execute this line, but since the laravel-permission/src/Guard.php Line 30 in 193e50e
4.- Since the collection is empty, it returns the default guard that is usually web laravel-permission/src/Guard.php Line 74 in 193e50e
So, wouldn't it be possible for it to also take into account what is initialized in the constructor? If it is not possible, close the ticket or mark it as an improvement for some later update of the package. While following your advice, I have added this method and works. class YourCustomPermission extends SpatiePermission
{
public static function create(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('xxx.guard');
return parent::create($attributes);
}
} EDIT: laravel-permission/src/Guard.php Line 30 in 193e50e
To $guardName = (new \ReflectionClass($class))->getDefaultProperties()['guard_name'] ?? (new $class)->guard_name ?? null; |
It's the same |
It's not the same, with I'm going to close this, if you want to add it or do something else, go ahead, at the moment I have the code that works, although it is duplicated (constructor and create method) I put what I was doing: |
I test it class YourCustomPermission extends SpatiePermission
{
public $guard_name = 'custom_guard';
}
dd(\Spatie\Permission\Guard::getDefaultName(\App\Models\YourCustomPermission ::class));
// displays "custom_guard" |
The code that you have given me that works, because the attribute is in the class, but I use the class YourCustomPermission extends Permission
{
public function __construct()
{
$this->guard_name = config('xxx.guard');
}
}
dd(\Spatie\Permission\Guard::getDefaultName(\App\Models\YourCustomPermission::class));
// displays "web" and with $guardName = (new \ReflectionClass($class))->getDefaultProperties()['guard_name'] ?? (new $class)->guard_name ?? null;
dd(\Spatie\Permission\Guard::getDefaultName(\App\Models\YourCustomPermission::class));
// displays "custom_guard" |
In my scenario I need that User Model can be associate with roles for multiple guards, to accomplish that I just define namespace App\Models;
Class User extends UserModel
{
// ...
protected array $guard_name = ['api', 'web'];
// ...
} With that, I can assign model to multiple guards. |
I was doing several tests, and I have discovered if you use multiple guards and try to tell it to be a specific guard from the constructor it doesn't work, but it does work if it is set as an attribute.
This does not work:
This works:
Reviewing the project code, I have seen that in this line comes the problem, it only obtains the attributes of the class, but not those that are initialized in the constructor:
laravel-permission/src/Guard.php
Line 30 in 193e50e
Therefore, if you have the default web guard, and another that is admin, everything you create will be created with the web guard, and when you try to assign it to someone from the admin guard, it will tell you that the permission or role does not exist
The text was updated successfully, but these errors were encountered: