本文介绍了授予bjyauthorize权限以从CLI运行ZF2的mvc应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ZF2上有一个完全运行的mvc应用程序.我想从命令行运行一些操作.我已经正确设置了控制台路由和其他环境.当我从CLI运行我的应用程序时,出现了这样的权限被拒绝"异常:

I have a completely running mvc application on ZF2. I want to run some actions from command line. I have properly set up my console routes and other environments. When I run my app from CLI, I got Permission denied exception like this:

'You are not authorized to access GeneratePdf\Controller\GeneratePdf\GeneratePdf:generate-all' in /var/www/zf2-reporting/module/BjyAuthorize/src/BjyAuthorize/Guard/Controller.php‌​:172

我的数据库中已经有一些用户.如何使用这些凭据授权CLI用户运行操作?

I already have some user in my database. How can I use those credentials to authorize a CLI user to run Actions?

修改:

以下是bjyauthorize.global.php中提到的控制器的guards数组.

Following is the guards array in bjyauthorize.global.php for mentioned controller.

'guards' => array(
'BjyAuthorize\Guard\Controller' => array(array('controller' => 'GeneratePdf\Controller\GeneratePdf', 'roles' => array('admin', 'letters_admin'))

我也使用过ZfcUser.如何通过CLI传递用户登录凭据.或者,如果有任何方法可以使用cli中的用户会话.

I have used ZfcUser as well. How can I pass user login credentials from CLI. Or if there is any way to use user session from cli.

谢谢

推荐答案

我找到了解决方案.我无法为cli用户授予权限,但这是通过从CLI运行时禁用bjyAuthorize来完成的.

I found the solution. I am not able to give permission for cli user but it has done by disabling bjyAuthorize while running from CLI.

我找到了以下解决方案:如何在ZF2 CLI应用程序中使用BjyAuthorize?

I found solution on this: How to use BjyAuthorize in ZF2 CLI application?

以下是其他人找到此问题的说明:

Here is the explanation for others if they found this issue:

要在从cli运行时禁用bjyAuthorize,我们可以在application.config.php中执行以下操作.

To disable bjyAuthorize while running from cli, we can do like below in application.config.php.

最初不要在application.config.php数组中添加"BjyAuthorize"和"BjyProfiler".检查控制台,如果没有控制台访问权限,则将它们添加到$ config数组中.

Do not add "BjyAuthorize" and "BjyProfiler" in your application.config.php array initially. Check for console, if not console access then add them in $config array.

if (!Console::isConsole()) {
    array_unshift($config['modules'], 'BjyAuthorize');
    array_unshift($config['modules'], 'BjyProfiler');
}
return $config;

同样有必要在Application/Module.php的onBootstrap方法中检查控制台,如下所示

Also it is necessary to check Console in Application/Module.php's onBootstrap method like below

if (!Console::isConsole()) {
        $authorize = $sm->get('BjyAuthorize\Service\Authorize');
        $acl = $authorize->getAcl();
        $role = $authorize->getIdentity();
    }

最后但并非最不重要的一点,不要忘记导入控制台类:

Last but not least, do not forget to import Console class:

use Zend\Console\Console;

这篇关于授予bjyauthorize权限以从CLI运行ZF2的mvc应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:17
查看更多