如何使用Yii强制登录? [英] How to force a login using YII?

查看:107
本文介绍了如何使用Yii强制登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要通过一个OTP来验证他的帐户的用户。进行身份验证,他填补了他的OTP,并验证手机号码。一旦通过验证,我想迫使他登录。即因此,他不还是得手动登录。因此,对于这种形式,我主要有:

I have a user that needs to authenticate his account via an OTP. TO authenticate, he fills in his OTP and it verifies the cellphone number. Once authenticated, I want to force him to log in. I.e. So he doesn't still have to manually login. So, for this form, I basically have:

if($model->validate())
{
   // Force Login
   $this->redirect(array('/site/index'));    
}

只使用他的证件之一(即他的手机号码),我需要强制他登录。我会怎么做呢?

Using only one of his credentials (namely his cellphone number), I need to force him to login. How would I do that?

推荐答案

该程序的文档中解释了 CWebUser

The procedure is explained in the documentation for CWebUser:


  1. 用户提供所需的身份验证信息。

  2. 一个身份实例与用户提供的信息创建的。

  3. 呼叫IUserIdentity ::验证,以检查身份证是有效的。

  4. 如果有效,调用 CWebUser ::登录登录用户,并且将用户重定向浏览器 RETURNURL

  1. The user provides information needed for authentication.
  2. An identity instance is created with the user-provided information.
  3. Call IUserIdentity::authenticate to check if the identity is valid.
  4. If valid, call CWebUser::login to login the user, and Redirect the user browser to returnUrl.

您应该已经有一个身份类,它实现 IUserIdentity 。通常这是从 CUserIdentity A类派生,这需要用户名和建设的密码。有自由在这里有很大程度:您可以决定不从 CUserIdentity 获得并使用验证你希望的任何方式自己的自定义身份类,或者你可以派生自 CUserIdentity 并重写验证方法与OTP进行身份验证。

You should already have an identity class that implements IUserIdentity. Usually this is a class deriving from CUserIdentity, which takes a username and a password on construction. There is a great degree of freedom here: you can decide not to derive from CUserIdentity and use your own custom identity class that authenticates any way you wish, or you can derive from CUserIdentity and override the authenticate method to authenticate with the OTP.

然后该过程将是精确地按照上述

The procedure would then be exactly as above:

$identity = new MyIdentity(...); // you decide what params it takes
if ($identity->authenticate()) {
    $user = Yii::app()->user;
    $user->login($identity);
    $this->redirect($user->returnUrl);
}

这篇关于如何使用Yii强制登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆