我正在使用 yi 1.1 应用程序开发指南,并且正在尝试实现 RBAC(基于角色的访问控制)。我得到的错误是 CDbAuthManager 未定义,这很奇怪,因为我测试了数据库连接并且它有效,并且我在我的 main.php 配置文件中定义了数据库。这是代码 main.php:
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=rbac',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'myPassword',
'charset' => 'utf8',
),
'authManager'=>array(
'class'=>'CDbAuthManager',
'connectionId'=>'db',
),
这是 RbacController.php 文件:
<?php
class RbacController extends CController
{
public function filters()
{
return array(
'accessControl',
);
}
public function accessRules()
{
return array(
array(
'allow',
'actions'=>array('deletePost'),
'roles'=>array('deletePost'),
),
array(
'allow',
'actions'=>array('init', 'test'),
),
array('deny'),
);
}
public function actionInit()
{
$auth=Yii::app()->authManager;
$auth->createOperation('createPost', 'create a post');
$auth->createOperation('readPost', 'read a post');
$auth->createOperation('updatePost', 'update a post');
$auth->createOperation('deletePost', 'delete a post');
$bizRule='return Yii::app()->user->id==$params["post"]->authId;';
$task=$auth->createTask('updateOwnPost', 'update a post by author himself', $bizRule);
$task->addChild('updatePost');
$role=$auth->createRole('reader');
$role->addChild('readPost');
$role=$auth->createRole('author');
$role->addChild('readPost');
$role->addChild('createPost');
$role->addChild('updateOwnPost');
$role=$auth->createRole('editor');
$role->addChild('readPost');
$role->addChild('createPost');
$role=$auth->createRole('admin');
$role->addChild('editor');
$role->addChild('author');
$role->addChild('deletePost');
$auth->assign('reader', 'readerA');
$auth->assign('author', 'authorB');
$auth->assign('editor', 'editorC');
$auth->assign('admin', 'adminD');
echo 'Done';
}
public function actionDeletePost()
{
echo "Post Deleted";
}
public function actionTest()
{
$post= new stdClass();
$post->authId = 'authorB';
echo "Current Permissions:<br />";
echo "<ul>";
echo "<li>Create post: ".Yii::app()->user->checkAccess('createPost')."</li>";
echo "<li>Read post: ".Yii::app()->user->checkAccess('readPost')."</li>";
echo "<li>Update post: ".Yii::app()->user->checkAccess('updatePost')."</li>";
echo "<li>Delete post: ".Yii::app()->user->checkAccess('deletePost')."</li>";
echo "</ul>";
}
}
?>
我的印象是,当使用 authManager 的 db 版本时,'class'=>'CDbAuthManager',设置层次结构的唯一方法是通过 yiic。但是它在书中没有说任何关于 yiic 的事情,它只是说“现在运行一次 init 来创建 RBAC 层次结构”。对我来说,这意味着在 url 中输入 Controller /操作对,如下所示:http://localhost/rbac/index.php?r=Rbac/init . IDK,也许这就是我出错的地方。在网上搜索,并没有真正找到与此类似的问题。
编辑: 我只是尝试了另一种方法。这次我在/Users/scottmcpherson/Sites/phpsites/rbac/protected/commands/shell/RbacCommand.php 中创建了一个控制台应用程序命令 当我尝试在终端中运行它时,我得到了同样的错误:
exception 'CException' with message 'Property "CDbAuthManager.connectionId" is not defined.' in /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/base/CComponent.php:174
Stack trace:
#0 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/YiiBase.php(225): CComponent->__set('connectionId', 'db')
#1 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/base/CModule.php(371): YiiBase::createComponent(Array)
#2 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/base/CModule.php(86): CModule->getComponent('authManager')
#3 /Users/scottmcpherson/Sites/phpsites/rbac/protected/commands/shell/RbacCommand.php (24): CModule->__get('authManager')
#4 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/cli/commands/ShellCommand.p hp(147): RbacCommand->run(Array)
#5 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/cli/commands/ShellCommand.p hp(99): ShellCommand->runShell()
#6 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/console/CConsoleCommandRunner.php(63): ShellCommand->run(Array)
#7 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/console/CConsoleApplication .php(88): CConsoleCommandRunner->run(Array)
#8 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/base/CApplication.php(158): CConsoleApplication->processRequest()
#9 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/yiic.php(33): CApplication->run()
#10 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/yiic(15): require_once('/Users/scottmcp...')
#11 {main}
它说 connectionId 未定义,但我已经定义它并将其设置为有效的数据库连接。怎么不能定义呢?
最佳答案
这个错误有点含糊,我承认。这实际上是相反的问题。您正在尝试设置 connectionId,但错误告诉您它不能这样做,因为 connectionId 不是 CDbAuthManager 中的有效属性。我相信它实际上是:
'connectionID' => 'db',
'd' 也必须大写。
希望对您有所帮助,这确实不是最清楚的错误消息。
关于php - 如何调试此错误 : Property "CDbAuthManager.connectionId" is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8911606/