请教srbac使用过程中的问题 [ 新手入门 ]
我使用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(); ?>
请问我的操作过程错在那里呢?
共 23 条回复
-
似乎是我哪个地方做得不对,按照你的在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();
-
定位出是因为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']; } }
-
我前面的打印语句写错了:
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
似乎也没有错,错在哪里呢?
-
问题确实就在这了
我写成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
-
是的,应该这样,因为用户名和用户ID放在User表中,自动生成了model\User.php和UserController.php,我应该在model\User.php中增加一个方法 getIDByUsername(),然后在UserIdentity文件里,调用model\User.php中的 getIDByUsername(),思路是否应该这样?
还是应该:
在UserController.php里面也增加一个 getIDByUsername(),方法,然后在UserIdentity文件里getId()方法中,调用UserController.php里的getIDByUsername()方法?
-
不用的,直接在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; }
这样就行了
onunix china
最后登录:1970-01-01
在线时长:0小时0分
- 粉丝2
- 金钱1145
- 威望0
- 积分1145