vyuldashev / nova-permission

A Laravel Nova tool for Spatie's laravel-permission library
https://novapackages.com/packages/vyuldashev/nova-permission
419 stars 217 forks source link

Class name must be a valid object or a string #71

Open julianovmartins opened 5 years ago

julianovmartins commented 5 years ago

I followed the tutorial to add the package in Nova. When I try to access the menu I get the message.

I looked at the logs, and the error happens in the following files:

Log: Class name must be a valid object or a string {"userId":1,"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0)...

Permission: /vendor/vyuldashev/nova-permission/src/Permission.php:97) Role: /vyuldashev/nova-permission/src/Role.php:91

composer.json: "laravel/framework": "5.8.*", "vyuldashev/nova-permission": "^1.4", "spatie/laravel-permission": "^2.34",

admench commented 5 years ago

Also having the same issue here:

[2019-04-09 06:37:52] local.ERROR: Class name must be a valid object or a string 
{"userId":1,"exception":"[object] 
(Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): 
Class name must be a valid object or a string at /home/vagrant/code/WeAreFamily/member-app/vendor/vyuldashev/nova-permission/src/ForgetCachedPermissions.php:28)

vyuldashev/nova-permission v1.4.7 laravel/framework v5.8.10 spatie/laravel-permission 2.36.1

julianovmartins commented 5 years ago

I solved it by inserting into the permissions table in the database all permissions that I had placed in the code. When Spatie/permission does not find the role or permission in the database, it causes that exception.

admench commented 5 years ago

Hmm, looking through all of my registered permissions + roles in the database in comparison to the codebase, I do not see any mismatches. Thank you for your help but I do feel this is not the solution.

thomasjohnkane commented 5 years ago

I had to change line 28 in ForgetCachedPermissions.php to this because it wasn't finding the Nova resource for permissions:

$permissionKey = (Nova::resourceForModel(app(PermissionRegistrar::class)->getPermissionClass())) ? Nova::resourceForModel(app(PermissionRegistrar::class)->getPermissionClass())::uriKey() : 'permissions';

Alternatively, you can remove the ForgetCachedPermissions middleware in your nova config. I'm not sure what negative impact that has on the function of the package though.

admench commented 5 years ago

This has fixed it. I edited ForgetCachedPermissions.php as you described. Thank you @thomasjohnkane

Are you going to PR this? Or shall I?

admench commented 5 years ago

I also tried removing the middleware from the nova config, which solved the problem.

@vyuldashev could you chime in to recommend what the best solution would be to PR into this repo?

Thanks

admench commented 5 years ago

I have copied the Vyuldashev\NovaPermission\ForgetCachedPermissions class into my App\Services directory along with the modification to the file, and have referenced that class in the nova config, so that the remote server gets the change, as it is in version control now.

astalpaert commented 5 years ago

I have copied the Vyuldashev\NovaPermission\ForgetCachedPermissions class into my App\Services directory along with the modification to the file, and have referenced that class in the nova config, so that the remote server gets the change, as it is in version control now.

Both solutions are working fine, thanks!

Kovah commented 5 years ago

The issue is still present, just encountered this while setting up the package. The fix by @thomasjohnkane works but I'm not sure which impact this has on the correct behavior of the package.

Kovah commented 5 years ago

@vyuldashev I updated to:

and now the package is completely broken. The error now pops up even with the provided fixes, or using your package middleware or after removing the middleware at all. I keep getting "Class name must be a valid object or a string" error. Removing the package now...

rsiasat00 commented 5 years ago

The error that shows "Class name must be a valid object or a string" seems to have been caused by the following codes below:

As mentioned in the logs: Class name must be a valid object or a string at /vendor/vyuldashev/nova-permission/src/Role.php:90

Screen Shot 2019-11-04 at 10 37 05 AM

Here is what the Permission::getModel does:

image

@vyuldashev Maybe this information can help on how to resolve this issue.

rsiasat00 commented 5 years ago

The error that shows "Class name must be a valid object or a string" seems to have been caused by the following codes below:

As mentioned in the logs: Class name must be a valid object or a string at /vendor/vyuldashev/nova-permission/src/Role.php:90

Screen Shot 2019-11-04 at 10 37 05 AM

Here is what the Permission::getModel does:

image

@vyuldashev Maybe this information can help on how to resolve this issue.

I've checked the codes for getPermissionClass that is being used by getModel and it returns

Spatie\Permission\Models\Permission::class

which is the one being used in $model. Copied the value of model and used it to return the getModel() function and the roles are now working.

return \Spatie\Permission\Models\Permission::class;

Not sure if it has any issues though.

vyuldashev commented 5 years ago

Are you on the latest version of Laravel and this package? Just made a fresh install and everything works without errors.

Elandlord commented 5 years ago

@vyuldashev Problem still persists on:

Laravel: 6.4.0 Spatie permissions: 3.4 Nova permissions: 2.2.0

Which all sould be the latest versions.

However, the problem only exists when I use my own Eloquent models for the permission package, and define them in the config/permission.php file. I've tried all the above solutions, but they didn't solve this problem unfortunately.

[2019-11-05 13:42:41] local.ERROR: Class name must be a valid object or a string {"userId":3,"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Class name must be a valid object or a string at /Users/ericlandheer/Programming/Laravel/ticketsystem/vendor/vyuldashev/nova-permission/src/Role.php:109)

[2019-11-05 13:42:43] local.ERROR: Class name must be a valid object or a string {"userId":3,"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Class name must be a valid object or a string at /Users/ericlandheer/Programming/Laravel/ticketsystem/vendor/vyuldashev/nova-permission/src/Permission.php:115) Are the errors I get, respectively for Role and Permissions when trying to access them in the Nova CMS.

Globerada commented 4 years ago

Same error here. I have commented middleware.

The error appears when trying to attach with manytomany.

Example: public function attachAnyTag

shaffe-fr commented 4 years ago

In my case, this problem happened because the permission resource wasn't available for the current user because of permissions policy. To solve the problem I replaced $permissionKey = Nova::resourceForModel(app(PermissionRegistrar::class)->getPermissionClass())::uriKey(); by $permissionKey = \App\Nova\Permission::uriKey();.

A smarter way to solve this might be to fallback to a Permission resource in the config (with \Vyuldashev\NovaPermission\Permission::class as default value) when resourceForModel returns null.

@vyuldashev, I can prepare a PR if you want.

SadeghPM commented 4 years ago

same issue. any fix?

karl007 commented 4 years ago

Another reasons for this error:

SadeghPM commented 4 years ago

Thanks @karl007 Can you explain more about, please:

you missed to add the UserModel for the permissions as Nova Resource

karl007 commented 4 years ago

Sorry.. If the Authenticate Model (normally the User) does not exists as Resource class. I my case I have another guard that has not the User Model as Authenticate and I missed to create a Resource of this Model.

SadeghPM commented 4 years ago

thanks , but i have User resource and this exception still exists.

shaffe-fr commented 4 years ago

@SadeghPM do you have a policy that makes the User resource unavailable for the current user?

SadeghPM commented 4 years ago

@shaffe-fr Yes, i have. Could this throw exception?

ineghi commented 4 years ago

Same problem here, in case of the current user has not access to NovaPermissionTool (through canSee).

dnwjn commented 4 years ago

I dove into this problem. By calling Nova::resourceForModel() Nova's resource collection is checked for the permission class, but it can't find it, because the class doesn't exist there.

I was able to work around this by adding the missing permission class to the collection. I did this by adding the following line to NovaServiceProvider's boot():

Nova::resources([\Vyuldashev\NovaPermission\Permission::class]);

Since I'm not sure if this is the permanent solution, I also found another way to do it. In ForgetCachedPermission.php, $permissionKey is retrieved by calling uriKey() on the model, which Nova can't find. As a replacement, I took the functionality of uriKey() and combined it with the call to the permission class. This way a key can still be generated from the permission class, now matter how it is configured.

I will create a PR for this shortly after posting this. I'm not sure if any of my possibilities are sufficient, but I just hope it contributes in any way.

vyuldashev commented 3 years ago

Try v2.11.1, please

SadeghPM commented 3 years ago

@vyuldashev Problem still persists with same error message

RomkaLTU commented 2 years ago

Same problem. Using lates version at the moment. 5.5, 3.1

cwilby commented 2 years ago

Had this issue because some of our roles do not have a guard_name value.