onunix 2012-06-05 16:16:05 12932次浏览 23条回复 0 0 0

我使用srbac的过程: 安装好后,因为我有个表叫Abc,然后我创建了角色a,其有个task,为Abc Manager,Abc Manager有权限 Create abc

然后我修改abcconroller的 class AbcController extends CController 改为 class AbcController extends SBaseController

这个过程应该没有什么错误吧,然后访问:admin页面,出现错误

属性 "AbcController.menu" 未被定义.

D:\skill\finance\Yii\protected\views\layouts\column2.php(14)

02 <div class="span-19">
03     <div id="content">
04         <?php echo $content; ?>
05     </div><!-- content -->
06 </div>
07 <div class="span-5 last">
08     <div id="sidebar">
09     <?php
10         $this->beginWidget('zii.widgets.CPortlet', array(
11             'title'=>'Operations',
12         ));
13         $this->widget('zii.widgets.CMenu', array(
14             'items'=>$this->menu,
15             'htmlOptions'=>array('class'=>'operations'),
16         ));
17         $this->endWidget();
18     ?>
19     </div><!-- sidebar -->
20 </div>
21 <?php $this->endContent(); ?>

请问我的操作过程错在那里呢?

  • 回复于 2012-06-05 16:51 举报

    SBaseController或者AbcController中没有menu属性

  • 回复于 2012-06-05 16:55 举报

    自动生成的代码,怎么会有问题呢?要手动修改?那怎么修改呢

  • 回复于 2012-06-05 17:19 举报
    $this->widget('zii.widgets.CMenu', array(
    14    'items'=>$this->menu,
    

    这个$this->menu是什么呀?是这里出问题了

  • 回复于 2012-06-05 19:58 举报

    在你的SBaseController或者AbcController中添加public $menu=array();

  • 回复于 2012-06-06 10:02 举报

    似乎是我哪个地方做得不对,按照你的在SBaseController中添加public $menu=array();
    又出了个新的错,怎么可能出这么多错呢?

    CException
    属性 "AbcController.breadcrumbs" 未被定义.

    D:\skill\finance\Yii\protected\views\Abc\admin.php(2)
    
    01 <?php
    02 $this->breadcrumbs=array(
    03     'Abcs'=>array('index'),
    04     'Manage',
    05 );
    06 
    07 $this->menu=array(
    08     array('label'=>'List Abc', 'url'=>array('index')),
    09     array('label'=>'Create Abc', 'url'=>array('create')),
    10 );
    11 
    12 Yii::app()->clientScript->registerScript('search', "
    13 $('.search-button').click(function(){
    14     $('.search-form').toggle();
    
  • 回复于 2012-06-06 10:28 举报

    改为 class AbcController extends Controller 试试看

  • 回复于 2012-06-06 11:04 举报

    你在Controller.php所在的目录下面把Controller.php改为MyController.php(里面的类名也要改哦!),类名称那里改为class MyController extends SBaseController

  • 回复于 2012-06-06 12:44 举报

    你在controller里加上
    public $menu=array();

    public $breadcrumbs=array();
    而controller 继承SBaseController ,SBaseController 继承ccontroller
    其它的控制器都继承controller
    这样就可以了

  • 回复于 2012-06-06 15:47 举报

    定位出是因为userid为空,
    我打印出的:

    2012/06/06 16:01:22 [trace] [admin] userid is 
    in D:\skill\finance\Yii\protected\modules\srbac\components\SDbAuthManager.php (32)
    in D:\skill\finance\Yii\protected\modules\srbac\controllers\SBaseController.php (84)
    in D:\skill\finance\Yii\index.php (13)
    2012/06/06 16:01:22 [trace] [system.db.CDbCommand] Querying SQL: SELECT name, type, description, t1.bizrule, t1.data, t2.bizrule AS bizrule2, t2.data AS data2 FROM items t1, assignments t2 WHERE name=itemname AND userid=:userid
    in D:\skill\finance\Yii\protected\modules\srbac\components\SDbAuthManager.php (35)
    

    userid为什么为空呢?它应该是在哪里赋值的呢?

    class SDbAuthManager extends CDbAuthManager {
    
      /**
       * Performs access check for the specified user.
       * @param string the name of the operation that need access check
       * @param mixed the user ID. This should can be either an integer and a string representing
       * the unique identifier of a user. See {@link IWebUser::getId}.
       * @param array name-value pairs that would be passed to biz rules associated
       * with the tasks and roles assigned to the user.
       * @return boolean whether the operations can be performed by the user.
       */
      public function checkAccess($itemName, $userId, $params=array()) {
        if (!empty($this->defaultRoles) && in_array($itemName,$this->defaultRoles)) {
          return true;
        }
        
        $sql = "SELECT name, type, description, t1.bizrule, t1.data, t2.bizrule AS bizrule2, t2.data AS data2 FROM {$this->itemTable} t1, {$this->assignmentTable} t2 WHERE name=itemname AND userid=:userid";
        $command = $this->db->createCommand($sql);
        $command->bindValue(':userid', $userId);
        Yii::trace('userid is ',$userId);
        // check directly assigned items
        $names = array();
        foreach ($command->queryAll() as $row) {
           Yii::trace('Checking permission "' . $row['name'] . '"', 'system.web.auth.CDbAuthManager');
          if ($this->executeBizRule($row['bizrule2'], $params, unserialize($row['data2']))
            && $this->executeBizRule($row['bizrule'], $params, unserialize($row['data']))) {
            if (strtolower($row['name']) === strtolower($itemName)) {
              return true;
            }
            $names[] = $row['name'];
          }
        }
    
  • 回复于 2012-06-06 15:56 举报

    我加的打印语句:Yii::trace('userid is ',$userId);应该没有错吧,就应该这样打印吧,但是日志中没有打印出来userid。

  • 回复于 2012-06-06 16:00 举报

    userid的获取是在哪个文件里面呢?

  • 回复于 2012-06-06 16:44 举报

    var_dump();
    exit()
    die("信息")

  • 回复于 2012-06-06 16:55 举报

    我前面的打印语句写错了:Yii::trace('userid is ',$userId); 。错误地写成,了,实际userid是有值的,我看了下,它的值竟然是username,就错在这里了。问题就出在这里了,应该是userid的,怎么值会变成username了。

    $userId应该为1,不应该为admin。

    我看了下mainl.php

    'srbac' => array(
    'userclass'=>'User', //default: User
    'userid'=>'user_ID', //default: userid
    'username'=>'username', //default:username
    

    似乎也没有错,错在哪里呢?

  • 回复于 2012-06-06 17:13 举报

    问题确实就在这了
    我写成

    public function checkAccess($itemName, $userId, $params=array()) {
        if (!empty($this->defaultRoles) && in_array($itemName,$this->defaultRoles)) {
          return true;
        }
        
        $sql = "SELECT name, type, description, t1.bizrule, t1.data, t2.bizrule AS bizrule2, t2.data AS data2 FROM {$this->itemTable} t1, {$this->assignmentTable} t2 WHERE name=itemname AND userid=:userid";
        $command = $this->db->createCommand($sql);
         $userId = 1;//关键是这里,
        $command->bindValue(':userid', $userId);
        Yii::trace('itemName is '.$itemName);
        Yii::trace('userid is '.$userId);
       
        
        // check directly assigned items
        $names = array();
        foreach ($command->queryAll() as $row) {
           Yii::trace('Checking permission "' . $row['name'] . '"', 'system.web.auth.CDbAuthManager');
          if ($this->executeBizRule($row['bizrule2'], $params, unserialize($row['data2']))
            && $this->executeBizRule($row['bizrule'], $params, unserialize($row['data']))) {
            if (strtolower($row['name']) === strtolower($itemName)) {
              return true;
            }
            $names[] = $row['name'];
          }
        }
    

    就OK了,为什么userid的值错误了呢,userid不应该为 'admin‘,应该为1

  • 回复于 2012-06-06 17:17 举报

    如何继续查userid不正确的根源呢?请教下各位,我不知道该如何继续查下去了。

  • 回复于 2012-06-06 17:26 举报

    dsfgfasdfadfsaff

  • 回复于 2012-06-06 17:29 举报

    在UserIdentity文件里,重写getId()方法
    让它返回你的userid就行了

  • 回复于 2012-06-06 17:43 举报

    直接写个 exit('Test');

  • 回复于 2012-06-06 17:47 举报

    是的,应该这样,因为用户名和用户ID放在User表中,自动生成了model\User.php和UserController.php,我应该在model\User.php中增加一个方法 getIDByUsername(),然后在UserIdentity文件里,调用model\User.php中的 getIDByUsername(),思路是否应该这样?

    还是应该:

    在UserController.php里面也增加一个 getIDByUsername(),方法,然后在UserIdentity文件里getId()方法中,调用UserController.php里的getIDByUsername()方法?

  • 回复于 2012-06-07 08:53 举报

    不用的,直接在UserIdentity
    public $userid;

    public function authenticate()
    {
        $username=strtolower($this->username);
        $user=Member::model()->find('LOWER(username)=?',array($username));
        if($user===NULL)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if(!$user->validatePassword($this->password))
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else 
        {
            $this->userid=$user->uid;//加上这句
            $this->username=$user->username;//加上这句
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }
    public function getId()   //重写getId()方法
    {
        return $this->id;
    }
    

    这样就行了

您需要登录后才可以回复。登录 | 立即注册