2017-05-10 156 views
0

我開發了一個使用yii2的應用程序。然後我需要在afterLogin上驗證我的用戶以更改她/他的密碼,因爲我的用戶模型中默認爲'123456'。 如果仍然123456,我想做一個模式引導來通知我的用戶更改密碼。Yii2 - 登錄後更改密碼用戶

這樣

public function actionCreate(){ 
    $model = new User(); 
    $model->scenario = 'create'; 

    if ($model->load(Yii::$app->request->post())) { 
     $model->status = $model->status == 1 ? 10 : 0; 
     $model->setPassword('123456'); 
     .... 
} 

這樣的代碼,在配置/ web.php就像這樣:

'user' => [ 
     'identityClass' => 'app\models\User', 
     'on afterLogin' => ['app\events\AfterLoginEvent', 'handlePasswordForFirstTime'], 
     // 'enableAutoLogin' => true, 
    ], 

在事件,就像這樣:

namespace app\events; 
use Yii; 
use app\models\User; 

class AfterLoginEvent{ 
public static function handlePasswordForFirstTime($event){ 
    $user = new User(); 
    /*How to check if password still 123456*/ 
    } 
} 

感謝所有。

回答

0

只是驗證密碼

if ($user || $user->validatePassword('123456')) { 
    // password is still '123456' 
} else { 
    // user has changed password 
} 
+0

無效參數 - 警予\基地\ InvalidParamException 哈希無效。 –

+0

http://stackoverflow.com/questions/25910302/yii2-exception-in-validatepassword – Thu

相關問題