nova-permission icon indicating copy to clipboard operation
nova-permission copied to clipboard

Class name must be a valid object or a string

Open julianovmartins opened this issue 5 years ago • 29 comments

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",

julianovmartins avatar Mar 19 '19 17:03 julianovmartins

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

admench avatar Apr 09 '19 06:04 admench

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.

julianovmartins avatar Apr 09 '19 12:04 julianovmartins

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.

admench avatar Apr 09 '19 15:04 admench

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.

thomasjohnkane avatar Apr 11 '19 16:04 thomasjohnkane

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

Are you going to PR this? Or shall I?

admench avatar Apr 12 '19 08:04 admench

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 avatar Apr 12 '19 08:04 admench

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.

admench avatar Apr 12 '19 09:04 admench

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!

astalpaert avatar May 13 '19 09:05 astalpaert

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 avatar Aug 15 '19 12:08 Kovah

@vyuldashev I updated to:

  • Laravel 6.0.3
  • Spatie Permissions 3.0
  • nova-permission 2.1.0

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...

Kovah avatar Sep 24 '19 10:09 Kovah

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 <nova_project_directory>/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 avatar Nov 04 '19 02:11 rsiasat00

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 <nova_project_directory>/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.

rsiasat00 avatar Nov 04 '19 03:11 rsiasat00

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

vyuldashev avatar Nov 04 '19 17:11 vyuldashev

@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.

Elandlord avatar Nov 05 '19 13:11 Elandlord

Same error here. I have commented middleware.

The error appears when trying to attach with manytomany.

Example: public function attachAnyTag

Globerada avatar Jan 28 '20 10:01 Globerada

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.

shaffe-fr avatar Jan 29 '20 01:01 shaffe-fr

same issue. any fix?

SadeghPM avatar Feb 01 '20 12:02 SadeghPM

Another reasons for this error:

  • you missed to add the UserModel for the permissions as Nova Resource
  • you have an role or permission item in the DB without a guard or with an invalid guard

karl007 avatar Feb 24 '20 14:02 karl007

Thanks @karl007 Can you explain more about, please:

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

SadeghPM avatar Feb 25 '20 05:02 SadeghPM

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.

karl007 avatar Feb 25 '20 07:02 karl007

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

SadeghPM avatar Mar 01 '20 09:03 SadeghPM

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

shaffe-fr avatar Mar 01 '20 10:03 shaffe-fr

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

SadeghPM avatar Mar 04 '20 13:03 SadeghPM

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

ineghi avatar May 14 '20 12:05 ineghi

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.

dnwjn avatar Sep 15 '20 12:09 dnwjn

Try v2.11.1, please

vyuldashev avatar Feb 14 '21 02:02 vyuldashev

@vyuldashev Problem still persists with same error message

SadeghPM avatar Sep 13 '21 11:09 SadeghPM

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

RomkaLTU avatar Jul 29 '22 05:07 RomkaLTU

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

cwilby avatar Sep 03 '22 23:09 cwilby