我面临着在我正在开发的网站上将用户限制为他们自己的数据的问题。
目前所有用户都可以访问所有其他用户的数据。
我在网上找到了这个片段:
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;
}