AdminUserResetPassword.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecadmin\models\AdminUser;
  10. use Yii;
  11. use fecadmin\models\AdminUser;
  12. use yii\base\Model;
  13. /**
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class AdminUserResetPassword extends Model{
  18. public $username;
  19. public $old_password;
  20. public $new_password;
  21. public $password_repeat;
  22. private $_admin_user;
  23. public function rules()
  24. {
  25. return [
  26. [['old_password', 'new_password','password_repeat'], 'required'],
  27. // ['username', 'validateLogin'],
  28. ['new_password', 'validateNewPassword'],
  29. ['old_password', 'validateOldPassword'],
  30. ];
  31. }
  32. public function getAdminUser(){
  33. if($this->_admin_user === null){
  34. $this->_admin_user = Yii::$app->user->identity;
  35. }
  36. return $this->_admin_user;
  37. }
  38. public function updatePassword(){
  39. $AdminUser = $this->getAdminUser();
  40. $AdminUser->setPassword($this->new_password);
  41. $AdminUser->save();
  42. }
  43. public function validateNewPassword($attribute,$params){
  44. if (!$this->hasErrors()) {
  45. if($this->new_password != $this->password_repeat){
  46. $this->addError($attribute, 'Password and PasswordRepeat is Inconsistent!');
  47. return;
  48. }
  49. }
  50. }
  51. public function validateOldPassword($attribute,$params){
  52. if (!$this->hasErrors()) {
  53. $username = $this->getAdminUser()->username;
  54. $AdminUser = AdminUser::findByUsername($username);
  55. if($AdminUser->validatePassword($this->old_password)){
  56. }else{
  57. $this->addError($attribute, 'old password is not right!');
  58. }
  59. }
  60. }
  61. }