如何调试此错误:未定义属性“CDbAuthManager.connectionId”

时间:2012-01-18 14:16:05

标签: php database yii

我正在使用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>";
        }

}

 ?>

我的印象是,当使用db版本的authManager,'class'=&gt;'CDbAuthManager'时,设置层次结构的唯一方法是通过yiic。但它没有在书中说出任何关于yiic的内容,只是说“现在运行init一次来创建RBAC层次结构”。对我来说,这意味着在控制器/操作对中键入这样的URL: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没有定义,但我已经定义了它并将其设置为有效的数据库连接。怎么不定义?

0 个答案:

没有答案