AdminUserForm.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 fecadmin\models\AdminUser;
  11. /**
  12. * @author Terry Zhao <2358269014@qq.com>
  13. * @since 1.0
  14. */
  15. class AdminUserForm extends AdminUser {
  16. private $_admin_user;
  17. public function rules()
  18. {
  19. $parent_rules = parent::rules();
  20. $current_rules = [
  21. ['username', 'filter', 'filter' => 'trim'],
  22. ['username', 'required'],
  23. ['username', 'validateUsername'],
  24. ['username', 'string', 'min' => 2, 'max' => 20],
  25. ['email', 'filter', 'filter' => 'trim'],
  26. // ['email', 'required'],
  27. // ['email', 'email'],
  28. // ['email', 'string', 'max' => 255],
  29. // ['email', 'validateEmail'],
  30. ['code', 'required'],
  31. ['code', 'filter', 'filter' => 'trim'],
  32. ['code', 'validateCode'],
  33. // ['code', 'string', 'min' => 5, 'max' => 5],
  34. // ['role', 'required'],
  35. ['person', 'filter', 'filter' => 'trim'],
  36. // ['person', 'required'],
  37. // ['email', 'unique', 'targetClass' => '\fecadmin\models\AdminUser', 'message' => 'This email address has already been taken.'],
  38. // ['password', 'required'],
  39. // ['password', 'string', 'min' => 6],
  40. ['password', 'validatePasswordFormat'],
  41. ];
  42. return array_merge($parent_rules,$current_rules) ;
  43. }
  44. public function validateUsername($attribute, $params){
  45. //$user = User::findByUsername($this->username)
  46. if($this->id){
  47. $one = AdminUser::find()->where(" id != ".$this->id." AND username = '".$this->username."' ")
  48. ->one();
  49. if($one['id']){
  50. $this->addError($attribute,"this username is exist!");
  51. }
  52. }else{
  53. $one = AdminUser::find()->where(" username = '".$this->username."' ")
  54. ->one();
  55. if($one['id']){
  56. $this->addError($attribute,"this username is exist!");
  57. }
  58. }
  59. }
  60. public function validateCode($attribute, $params){
  61. //$user = User::findByUsername($this->username)
  62. if($this->id){
  63. $one = AdminUser::find()->where(" id != ".$this->id." AND code = '".$this->code."' ")
  64. ->one();
  65. if($one['id']){
  66. $this->addError($attribute,"this code is exist!");
  67. }
  68. }else{
  69. $one = AdminUser::find()->where(" code = '".$this->code."' ")
  70. ->one();
  71. if($one['id']){
  72. $this->addError($attribute,"this code is exist!");
  73. }
  74. }
  75. }
  76. public function validateEmail($attribute, $params){
  77. //$user = User::findByUsername($this->username)
  78. if($this->id){
  79. $one = AdminUser::find()->where(" id != ".$this->id." AND email = '".$this->email."' ")
  80. ->one();
  81. if($one['id']){
  82. $this->addError($attribute,"this email is exist!");
  83. }
  84. }else{
  85. $one = AdminUser::find()->where(" email = '".$this->email."' ")
  86. ->one();
  87. if($one['id']){
  88. $this->addError($attribute,"this email is exist!");
  89. }
  90. }
  91. }
  92. public function validatePasswordFormat($attribute, $params){
  93. if($this->id){
  94. if($this->password && strlen($this->password) <= 6){
  95. $this->addError($attribute,"password must >=6");
  96. }
  97. }else{
  98. if($this->password && strlen($this->password) >= 6){
  99. }else{
  100. $this->addError($attribute,"password must >=6");
  101. }
  102. }
  103. }
  104. public function setPassword($password)
  105. {
  106. if($this->password){
  107. $this->password_hash = \Yii::$app->security->generatePasswordHash($password);
  108. $this->password = '';
  109. }
  110. }
  111. # 重写保存方法
  112. public function save($runValidation = true, $attributeNames = NULL){
  113. if($this->id){
  114. $this->updated_at_datetime = date("Y-m-d H:i:s");
  115. }else{
  116. $this->created_at_datetime = date("Y-m-d H:i:s");
  117. $this->updated_at_datetime = date("Y-m-d H:i:s");
  118. }
  119. # 如果auth_key为空,则重置
  120. if(!$this->auth_key){
  121. $this->generateAuthKey();
  122. }
  123. # 如果access_token为空,则重置
  124. if(!$this->access_token){
  125. $this->generateAccessToken();
  126. }
  127. # 设置password
  128. $this->setPassword($this->password);
  129. parent::save($runValidation,$attributeNames);
  130. }
  131. }