<span><strong>YII2 实现修改密码功能</strong></span><br /><br /><strong><span>主要难点:</span></strong><br /><br /><span>1、密码加密</span><br /><span> YII2对密码加密生成的结果是不同的,即用相同的初始密码在不同时间得到的加密结果不同,所以我们不能用常用的方法去验证密码是否正确(将密码加密后与数据库中的密码相比较)。YII2有自己的加密以及密码验证流程。</span><br /><span> 加密 $hash = Yii::$app->getSecurity()->generatePasswordHash('123456']);</span><br /><span> 验证 Yii::$app->getSecurity()->validatePassword('123456', $hash),返回true或false</span><br />2、返回信息处理<br /> Yii::$app->session->setFlash('contact','旧密码错误');//设置 flash<br /> return $this->goBack(); //页面转跳 <br /> Yii::$app->session->getFlash('contact'); //获取 flash
<span>public</span> <span>function</span><span> actionPassword(){ </span><span>$model</span>=<span>new</span><span> PasswordForm; </span><span>$request</span> = YII::<span>$app</span>-><span>request; </span><span>if</span>(<span>$request</span>-><span>isPost){ </span><span>$p</span> = <span>$request</span>->post('PasswordForm'<span>); </span><span>$id</span> = YII::<span>$app</span>->user-><span>id; </span><span>$admin</span>= Admin::findIdentity(<span>$id</span><span>); </span><span>$password</span> = <span>$admin</span>-><span>password; </span><span>if</span>(Yii::<span>$app</span>->getSecurity()->validatePassword(<span>$p</span>['password'], <span>$password</span><span>)){ </span><span>if</span>(<span>$p</span>['pass1'] == <span>$p</span>['pass2'<span>]){ </span><span>$newPass</span> = Yii::<span>$app</span>->getSecurity()->generatePasswordHash(<span>$p</span>['pass1'<span>]); </span><span>$connection</span> = \Yii::<span>$app</span>-><span>db; </span><span>$r</span> = <span>$connection</span>->createCommand()->update('admin', ['password' => <span>$newPass</span>], 'id='.<span>$id</span>)-><span>execute(); </span><span>if</span>(<span>$r</span><span>){ Yii</span>::<span>$app</span>->user-><span>logout(); </span><span>return</span> <span>$this</span>-><span>goHome(); }</span><span>else</span><span>{ </span><span>return</span> <span>$this</span>-><span>goBack(); } } }</span><span>else</span><span>{ Yii</span>::<span>$app</span>->session->setFlash('contact','旧密码错误'<span>); </span><span>return</span> <span>$this</span>->redirect(<span>array</span>('site/password'<span>)); } }</span><span>else</span><span>{ </span><span>return</span> <span>$this</span>->render('password',['model'=><span>$model</span><span>]); } }</span>
Code optimization: The main optimization details are as follows
1. Move the main logic code in the controller into the model
2. Optimize database operations
In controller:
<span>public</span> <span>function</span><span> actionPassword(){ </span><span>$model</span>=<span>new</span><span> PasswordForm; </span><span>$request</span> = YII::<span>$app</span>-><span>request; </span><span>if</span>(<span>$request</span>->isPost && <span>$model</span>->load(Yii::<span>$app</span>->request->post()) && <span>$model</span>-><span>changePassword()){ Yii</span>::<span>$app</span>->user-><span>logout(); </span><span>return</span> <span>$this</span>-><span>goHome(); }</span><span>else</span><span>{ </span><span>return</span> <span>$this</span>->render('password',['model'=><span>$model</span><span>]); } }</span>
In model:
<span>public</span> <span>function</span><span> changePassword(){ </span><span>$id</span> = YII::<span>$app</span>->user-><span>id; </span><span>$admin</span>= Admin::findIdentity(<span>$id</span><span>); </span><span>$password</span> = <span>$admin</span>-><span>password; </span><span>if</span>(Yii::<span>$app</span>->getSecurity()->validatePassword(<span>$this</span>->password, <span>$password</span><span>)){ </span><span>if</span>(<span>$this</span>->pass1 == <span>$this</span>-><span>pass2){ </span><span>$newPass</span> = Yii::<span>$app</span>->getSecurity()->generatePasswordHash(<span>$this</span>-><span>pass1); </span><span>$admin</span>->password = <span>$newPass</span><span>; </span><span>if</span>(<span>$admin</span>-><span>save()){ </span><span>return</span> <span>true</span><span>; }</span><span>else</span><span>{ </span><span>return</span> <span>false</span><span>; } }</span><span>else</span><span>{ Yii</span>::<span>$app</span>->session->setFlash('contact','两次新密码不相等'<span>); </span><span>return</span> <span>false</span><span>; } }</span><span>else</span><span>{ Yii</span>::<span>$app</span>->session->setFlash('contact','旧密码错误'<span>); </span><span>return</span> <span>false</span><span>; } }</span>