moacode / craft-fab-permissions

Restrict access to fields and tabs for particular user groups across your Craft elements.
Other
16 stars 9 forks source link

Console commands don't work #13

Closed punani3000 closed 4 years ago

punani3000 commented 4 years ago

Hi,

I found another bug: console commands don't work if the plugin is active. I get the following error:

Exception 'TypeError' with message 'Argument 3 passed to thejoshsmith\fabpermissions\services\Fab::canViewField() must be an instance of craft\web\User, instance of craft\console\User given, called in /www/site/vendor/thejoshsmith/craft-fab-permissions/src/services/Fields.php on line 32'

When I add the following in to line 32 in the file src/services/Fields.php console commands work fine: if(Craft::$app->request->getIsConsoleRequest()) return $fields;

Thanks, Dirk

moacode commented 4 years ago

Hi Dirk, does that happen with the latest version?

On Thu, 5 Dec 2019 at 11:49 PM, punani3000 notifications@github.com wrote:

Hi,

I found another bug: console commands don't work if the plugin is active. I get the following error:

Exception 'TypeError' with message 'Argument 3 passed to thejoshsmith\fabpermissions\services\Fab::canViewField() must be an instance of craft\web\User, instance of craft\console\User given, called in /www/site/vendor/thejoshsmith/craft-fab-permissions/src/services/Fields.php on line 32'

When I add the following in to line 32 in the file src/services/Fields.php console commands work fine: if(Craft::$app->request->getIsConsoleRequest()) return $fields;

Thanks, Dirk

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/thejoshsmith/craft-fab-permissions/issues/13?email_source=notifications&email_token=ABKCPKZKVUHSTSFZ4GY2RYLQXDMDTA5CNFSM4JVXUT32YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4H6JDBPQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKCPK6KHWV77N35LK3LZOLQXDMDTANCNFSM4JVXUT3Q .

moacode commented 4 years ago

Closing due to inactivity.

JoshCoady commented 3 years ago

I have this same issue. It is because when running on the console the object returned by Craft::$app->getUser() is craft\console\User and not craft\web\User. Unfortunately, there is not a common interface or ancestor except Component.

You could probably do something like this:

use craft\console\User as ConsoleUser;
use craft\web\User as WebUser;
...
     * @param ConsoleUser|WebUser $user User object
...
    public function canViewTab(FieldLayoutTab $tab, $user, $currentSite = null)
    {
        if( $user->getIsAdmin() ) return true;
        if( $user->getIsGuest() ) return false;

That being said, I am currently only running the console as admin and do not foresee needing to run as anything else. In that case, the easiest fix is to just move the fields service config to app.web.php so it is not used in the console.