我面临着限制用户在我正在开发的站点上使用他们自己的数据的问题。
目前,所有用户都可以访问所有其他用户的数据。
我在网上找到了这个片段:
public function defaultScope() {
return array(
'condition' => 'mob_num = '.YII::app()->user->getId(), // Customer can see only his orders
);
}
当我的列是一个整数时,这很好,但是如果它是一个字符串,它会给出以下错误:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause'. The SQL statement executed was: SELECT COUNT(*) FROM `mob_reg` `t` WHERE name = shayan
public function authenticate()
{
/*$users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin',
);*/
// $users= Auth::model()->findByPk("8951821861");
$users = Auth::model()->findByAttributes(array('company'=>$this->username));
if($users == NULL)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if ($users->name != $this->username)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if ($users->company != $this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else if($users->company=='naga')
{
$this->errorCode=self::ERROR_NONE;
$this->setState('roles', 'super');
$this->id=$users->company;
}
else {
$this->errorCode=self::ERROR_NONE;
$this->setState('roles', 'normal');
$this->id=$users->company;
}
return !$this->errorCode;
/* if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
elseif($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;*/
}
public function getid()
{
return $this->id;
}
发布于 2013-09-02 07:28:37
首先,也是最重要的,我不会使用defaultScope,因为这会阻止您以后可能想添加的其他用例,比如计算与当前用户有某种联系的其他人的统计信息,例如,我最常打电话给谁等。
因此,我认为您应该在查找数据时添加额外的限制,如下所示:
$orders = Orders::model()->findByAttributes(array('name' => user()->getid()));
如果您真的想坚持使用defaultScope,则需要正确引用名称,使用参数是可行的方法:
public function defaultScope()
{
return array(
'condition' => "name=?",
'params' => array(user()->getid()),
);
}
当然,您不希望将用户名存储在order表中,但这是另一回事。
https://stackoverflow.com/questions/18566632
复制相似问题