【问题标题】:Issue with getting user id in Yii application在 Yii 应用程序中获取用户 ID 的问题
【发布时间】:2014-11-26 18:02:53
【问题描述】:

我有一个正在生产中运行的站点,它运行良好,我正在对代码进行一些更改,因此我将其复制到本地主机。该站点有一个用户管理系统,需要登录。用户登录后,他们将被重定向到 mysite.com/admin/index。

正在进行用户验证,并且 UserIdentity 类中的身份验证类返回错误代码为 0。但是,当用户转到 WebUser 类时,当我检查 $this->id 时它返回一个空值。另外,Yi::app()->user->id 也不存在。

以下是我的代码的相关部分:

在main.php中

'components'=>array(
        'user'=>array(
            // enable cookie-based authentication
            // 'allowAutoLogin'=>true,
            'class' => 'WebUser',
            'loginUrl'=>array('site/login'),
        ),
        // uncomment the following to enable URLs in path-format
        'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName'=>false,
            'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),
        ),

在 UserIdentity.php 中

private $_group;
private $_id;

public function getId(){
  return $this->_id;
}

public function authenticate($seeker = false, $queryid = false){
  //Retrieve seeker Login

  $record=User::model()->findByAttributes(array('login_name'=>$this->username, 'status' => 0));

  //Invalid username
  if($record===null){
    $this->errorCode=self::ERROR_USERNAME_INVALID;
  }
  //Invalid password
  else if($record->password !== $record->encryptPassword($this->password) && $record->password !== $record->encryptPassword($this->password, true)){
    $this->errorCode=self::ERROR_PASSWORD_INVALID;
  }
  //Valid login
  else{
    //Use new encryption
    if($record->password === $record->encryptPassword($this->password, true))
      $record->resetPassword($this->password);
    $this->_id       = $record->id;
    $this->_group    = $record->group;
    $this->errorCode = self::ERROR_NONE;
  }
  if($this->errorCode == self::ERROR_NONE){
    $auth=Yii::app()->authManager;
    try{ $role=$auth->createRole($this->_group); } catch(Exception $e){}

    if(!$auth->isAssigned($this->_group,$this->_id)){
      if($auth->assign($this->_group,$this->_id)){
        Yii::app()->authManager->save();
      }
    }
  }

  return !$this->errorCode;
}

在我的 SiteController.php 中,当我在登录后检查用户 ID 时,它返回正确的值,但在用户被重定向到 admin/index 并且我检查用户 ID 的值后,它没有显示任何值。

有人可以建议我缺少什么吗?

【问题讨论】:

    标签: php yii


    【解决方案1】:

    下面的代码保存 url 状态,
    请求admin/edit/3 -> 重定向到login page -> 重定向到admin/edit/3
    请求login page -> 重定向admin/index

    $this->redirect(Yii::app()->getUser()->getReturnUrl('admin/index'));
    

    【讨论】:

    • 没明白你的意思。
    • 你留在site.com/admin/edit/3并且你的会话已经过期,按f5,成功登录后你回到site.com/admin/edit/3而不是admin/index
    • 现在,当我在站点/登录时,我看到了登录表单,它会将我重定向到验证时的管理员/索引。转到 site/admin/edit/3 只会给我一个 404。
    • admin/edit/3 只是一个与admin/index不同的私人页面示例
    • 我还是不明白。
    【解决方案2】:

    如果仅根据您的代码做出决定,其他都不重要,则看起来像这样的验证

    Yii::app()->user->returnUrl == '/index.php'
    

    总是错误的。您可以通过在调试器中在此品脱处停止代码来简单地调试它,或者只需输入“dump'n'stop code”:)

    var_dump(
        Yii::app()->user->returnUrl == '/index.php',
        Yii::app()->user->returnUrl
    );
    die();
    

    在这行之前

    $this->redirect(Yii::app()->user->returnUrl == '/index.php'? '/admin/index': Yii::app()->user->returnUrl);
    

    【讨论】:

      猜你喜欢
      • 2018-11-06
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2016-01-30
      • 2016-01-09
      • 2022-07-06
      • 1970-01-01
      相关资源
      最近更新 更多