Closed juanpscotto closed 4 years ago
make sure to use the getPermissions()
method which will ensure the return type is an array.
$this->getPermissions()
The error is in the EloquentRole.class.
Would you please provide us with more detail and the code you are using?
class Role extends EloquentRole
{
/**
* {@inheritDoc}
*/
protected $table = 'roles';
/**
* {@inheritDoc}
*/
protected $fillable = [
'name',
'slug',
'permissions',
'description'
];
}
$role = new Role();
$role->slug = 'root';
$role->name = 'Super Admin';
$role->save();
$role->addPermission('update_sales');
$role->save();
Just pushed out a new patch to address this bug. run composer update
and everything should be working on your end.
@suwardany
Thanks. I've notice the same happens with updatePermission()
on PermissibleTrait.class
.
Also. If throws the same error on this function:
class StrictPermissions implements PermissionsInterface
{
use PermissionsTrait;
/**
* {@inheritdoc}
*/
protected function createPreparedPermissions(): array
{
$prepared = [];
if (! empty($this->secondaryPermissions)) {
foreach ($this->secondaryPermissions as $permissions) {
$this->preparePermissions($prepared, $permissions);
}
}
if (! empty($this->permissions)) {
$this->preparePermissions($prepared, $this->permissions);
}
return $prepared;
}
}
Thanks @juanpscotto .. i just pushed an update to the master branch (still untagged) .. can you give it a try?
Don't forget the updatePermission
and removePermission
on PermissibleTrait.class
Getting the same error here:
protected function createPreparedPermissions(): array
{
$prepared = [];
if (! empty($this->getSecondaryPermissions())) {
foreach ($this->getSecondaryPermissions() as $permissions) {
$this->preparePermissions($prepared, $permissions);
}
}
if (! empty($this->getPermissions())) {
$permissions = [];
$this->preparePermissions($permissions, $this->getPermissions());
$prepared = array_merge($prepared, $permissions);
}
return $prepared;
}
I've solved this like this:
protected function createPreparedPermissions(): array
{
$prepared = [];
if (! empty($this->getSecondaryPermissions())) {
foreach ($this->getSecondaryPermissions() as $permissions) {
if(!empty($permissions)) { // added this validation
$this->preparePermissions($prepared, $permissions);
}
}
}
if (! empty($this->getPermissions())) {
$permissions = [];
$this->preparePermissions($permissions, $this->getPermissions());
$prepared = array_merge($prepared, $permissions);
}
return $prepared;
}
pushed to master as well!
Thanks man! the updatePermission and removePermission methods are fixed now. It only remains the createPreparedPermissions, method:
Argument 2 passed to Cartalyst\Sentinel\Permissions\StandardPermissions::preparePermissions() must be of the type array, null given, called in /var/www/html/flux-erp/vendor/cartalyst/sentinel/src/Permissions/StandardPermissions.php on line 36
aye aye .. give it another try now @juanpscotto
Thanks man for your patience!. I've updated the dev-master branch.
Getting the same error when the roles.permissions is set to null. Only works when is an empty array '[]' or '{}' I've tried to use the StandardPermissions.php or the StrictPermissions.php with the same result.
Argument 2 passed to Cartalyst\Sentinel\Permissions\StandardPermissions::preparePermissions() must be of the type array, null given, called in /var/www/html/flux-erp/vendor/cartalyst/sentinel/src/Permissions/StandardPermissions.php on line 36
Argument 2 passed to Cartalyst\Sentinel\Permissions\StrictPermissions::preparePermissions() must be of the type array, null given, called in /var/www/html/flux-erp/vendor/cartalyst/sentinel/src/Permissions/StrictPermissions.php on line 36
Thanks man!
hmm .. what exactly triggers that one?
When I called:
public function hasAccess($permissions): bool
{
if (is_string($permissions)) {
$permissions = func_get_args();
}
$prepared = $this->getPreparedPermissions();
foreach ($permissions as $permission) {
if (! $this->checkPermission($prepared, $permission)) {
return false;
}
}
return true;
}
$user = \Sentinel::getUser();
$user->hasAccess('permission');
The flow is like this $user->hasAccess (PermissionsTrait.php) --> $prepared = $this->getPreparedPermissions(); (PermissionsTrait.php) --> createPreparedPermissions() (StandardPermissions.php or StrictPermissions.php)
I'm unable to reproduce that on my end, what are the exact user/roles and their permissions on your end? working with empty arrays or nulls as permissions works fine whether that is on a user, a role or on both.
Sorry sorry. Yes you are right. All is working fine now. I was overriding createPermissions on my User.php model. Thanks a lot!!
No worries @juanpscotto .. i've released a new patch tag.
Awesome!
Your Environment
Expected behaviour
Please describe what you are expecting to see happening.
Add permissions to roles with the addPermission Method
Actual behaviour
Please describe what is actually happening.
It throws an error:
Steps to reproduce
If your issue requires any specific steps to reproduce, please outline them here.