AdminForm.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace backendApi\modules\v1\models;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\helpers\Tool;
  6. use common\libs\logging\operate\AdminOperate;
  7. /**
  8. * Login form
  9. */
  10. class AdminForm extends Model
  11. {
  12. public $id;
  13. public $adminName;
  14. public $realName;
  15. public $remark;
  16. public $isEnable;
  17. public $isModifyPassword;
  18. public $bindIp;
  19. public $oldPassword;
  20. public $password;
  21. public $surePassword;
  22. public $roleId;
  23. public $lang;
  24. public $countryId;
  25. public function init() {
  26. parent::init();
  27. $this->adminOperateLogger = new AdminOperate([
  28. 'fetchClass' => Admin::class,
  29. ]);
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['id', 'adminName', 'realName', 'oldPassword', 'password', 'surePassword', 'roleId', 'remark', 'isEnable', 'isModifyPassword', 'bindIp'], 'trim'],
  38. [['id', 'adminName', 'realName', 'roleId'], 'required'],
  39. [['password', 'surePassword'], 'required', 'on'=>['add', 'changePassword', 'noLoginModifyPassword']],
  40. [['adminName', 'oldPassword'], 'required', 'on'=>['noLoginModifyPassword']],
  41. [['adminName'], 'unique', 'targetClass'=>Admin::class, 'targetAttribute'=>'ADMIN_NAME', 'on'=>['add']],
  42. ['surePassword', 'compare', 'compareAttribute'=>'password', 'message' => \Yii::t('ctx', 'twoPasswordsMustSame')], // 两次密码必须一致
  43. ];
  44. }
  45. /**
  46. * 指定校验场景
  47. * @return array
  48. */
  49. public function scenarios()
  50. {
  51. $parentScenarios = parent::scenarios();
  52. $customScenarios = [
  53. 'add' => ['adminName', 'realName', 'password', 'surePassword', 'roleId', 'remark', 'isEnable', 'isModifyPassword', 'bindIp', 'countryId'],
  54. 'edit' => ['id', 'password', 'surePassword', 'roleId', 'realName', 'remark', 'isEnable', 'isModifyPassword', 'bindIp', 'countryId'],
  55. 'changePassword' => ['password', 'surePassword'],
  56. 'noLoginModifyPassword' => ['adminName', 'oldPassword', 'password', 'surePassword'],
  57. 'changeLanguage' => ['adminName', 'lang'],
  58. ];
  59. return array_merge($parentScenarios, $customScenarios);
  60. }
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => 'ID',
  65. 'adminName' => '管理员用户名',
  66. 'realName' => '管理员会员姓名',
  67. 'remark' => '备注',
  68. 'isEnable' => '是否启用',
  69. 'isModifyPassword' => '是否修改密码',
  70. 'bindIp' => '绑定IP',
  71. 'oldPassword' => '原密码',
  72. 'password' => '密码',
  73. 'surePassword' => '确认密码',
  74. 'roleId' => '角色',
  75. 'countryId' => '国家',
  76. ];
  77. }
  78. /**
  79. * 添加
  80. * @return Admin|null
  81. * @throws \yii\base\Exception
  82. */
  83. public function edit(){
  84. if(!$this->validate()){
  85. return null;
  86. }
  87. if($this->scenario == 'add'){
  88. $model = new Admin();
  89. $model->ADMIN_NAME = strtolower($this->adminName);
  90. $model->REAL_NAME = $this->realName;
  91. $model->ROLE_ID = $this->roleId;
  92. $model->REMARK = $this->remark;
  93. $model->IS_ENABLE = $this->isEnable ? $this->isEnable : 0;
  94. $model->IS_MODIFY_PASSWORD = $this->isModifyPassword ? $this->isModifyPassword : 0;
  95. $model->BIND_IP = $this->bindIp;
  96. $model->CREATE_ADMIN = \Yii::$app->user->id;
  97. $model->CREATED_AT = Date::nowTime();
  98. } elseif($this->scenario == 'edit') {
  99. $model = Admin::findOne(['ID'=>$this->id]);
  100. $this->adminOperateLogger->beforeUpdate($model);
  101. $model->REAL_NAME = $this->realName;
  102. $model->ROLE_ID = $this->roleId;
  103. $model->REMARK = $this->remark;
  104. $model->IS_ENABLE = $this->isEnable ? $this->isEnable : 0;
  105. $model->IS_MODIFY_PASSWORD = $this->isModifyPassword ? $this->isModifyPassword : 0;
  106. $model->BIND_IP = $this->bindIp;
  107. $model->UPDATE_ADMIN = \Yii::$app->user->id;
  108. $model->UPDATED_AT = Date::nowTime();
  109. } elseif($this->scenario == 'changePassword') {
  110. $model = Admin::findOne(['ID'=>\Yii::$app->user->id]);
  111. } elseif($this->scenario == 'noLoginModifyPassword') {
  112. $model = Admin::findOne(["ADMIN_NAME" => $this->adminName]);
  113. if (!$model) {
  114. $this->addError('noLoginModifyPassword', \Yii::t('ctx', 'memberNameDoesNotExist')); // 不存在的用户名
  115. return null;
  116. }
  117. if ($this->oldPassword === $this->password) {
  118. $this->addError('noLoginModifyPassword', \Yii::t('ctx', 'loginPasswordHasNotChanged')); // 登录密码没有发生改变
  119. return null;
  120. }
  121. if (!$model->validatePassword($this->oldPassword)) {
  122. $this->addError('noLoginModifyPassword', \Yii::t('ctx', 'oldLoginPasswordIncorrect')); // 原登录密码错误
  123. return null;
  124. }
  125. $model->IS_MODIFY_PASSWORD = 0;
  126. } else if ($this->scenario == 'changeLanguage') {
  127. $model = Admin::findOne(["ADMIN_NAME" => $this->adminName]);
  128. if (!$model) {
  129. $this->addError('noLoginModifyPassword', \Yii::t('ctx', 'memberNameDoesNotExist')); // 不存在的用户名
  130. return null;
  131. }
  132. $model->LANG = Tool::langConvert($this->lang);
  133. }else {
  134. $this->addError('id', \Yii::t('ctx', 'submissionDoesNotExist')); // 提交场景不存在
  135. return null;
  136. }
  137. // 给密码进行加密 ,如果需要添加验证密码安全验证时 ,可以在这个地方加
  138. if($this->password){
  139. if ($this->scenario == 'changePassword' || $this->scenario == 'noLoginModifyPassword'){
  140. if (strlen($this->password) < 8){
  141. $this->addError($this->scenario, \Yii::t('ctx', 'passwordLeastEightCharacters')); // 登录密码不能小于8位
  142. return null;
  143. }
  144. // $symbol = '!#$%^&*';
  145. $symbol = '`~!@#$^&*()=|{}\'\":;\',\\[\\].<>\/?~!@#¥……&*()——|{}【】‘;:”。,、?';
  146. $passwordRules = '/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)(?!['.$symbol.']+$)[\da-zA-z'.$symbol.']{8,}$/';
  147. $verificationResults = preg_match($passwordRules,$this->password);
  148. if (!$verificationResults){
  149. $this->addError($this->scenario, \Yii::t('ctx', 'passwordRule')); // 登录密码中需要包含数字、大写字母、小写字母、特殊字符至少两种
  150. return null;
  151. }
  152. }
  153. $model->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  154. }
  155. // 执行修改或添加 , 如果执行不成功就在这里报出错误
  156. if(!$model->save()){
  157. $this->addErrors($model->getErrors());
  158. return null;
  159. }
  160. // 执行完成后根据对应的类型写入日志
  161. if($this->scenario == 'add'){
  162. $this->adminOperateLogger->afterInsert($model)->clean()->save([
  163. 'optType' => '添加管理员',
  164. 'userName' => $model->ADMIN_NAME,
  165. 'remark' => $this->remark,
  166. ]);
  167. if (!AdminRole::isSuperAdmin($this->roleId)) {
  168. foreach ($this->countryId as $country) {
  169. $adminCountry = new AdminCountry();
  170. $adminCountry->ADMIN_ID = $model->ID;
  171. $adminCountry->COUNTRY_ID = $country;
  172. $adminCountry->save();
  173. }
  174. }
  175. } elseif($this->scenario == 'edit') {
  176. $this->adminOperateLogger->afterUpdate($model);
  177. $this->adminOperateLogger->clean()->save([
  178. 'optType' => '编辑管理员',
  179. 'userName' => $model->ADMIN_NAME,
  180. 'remark' => $this->remark,
  181. ]);
  182. (new AdminCountry())->deleteAll(['ADMIN_ID' => $model->ID]);
  183. if (!AdminRole::isSuperAdmin($this->roleId)) {
  184. // 国家
  185. foreach ($this->countryId as $country) {
  186. $adminCountry = new AdminCountry();
  187. $adminCountry->ADMIN_ID = $model->ID;
  188. $adminCountry->COUNTRY_ID = $country;
  189. $adminCountry->save();
  190. }
  191. }
  192. } elseif($this->scenario == 'noLoginModifyPassword') {
  193. $this->adminOperateLogger->clean()->save([
  194. 'optType' => '不登录重置密码',
  195. 'userName' => $model->ADMIN_NAME,
  196. 'adminName' => $model->ADMIN_NAME,
  197. 'remark' => '不登录重置密码',
  198. ]);
  199. } elseif($this->scenario == 'changeLanguage') {
  200. }else{
  201. $this->adminOperateLogger->clean()->save([
  202. 'optType' => '重置密码',
  203. 'userName' => Admin::getAdminNameById(\Yii::$app->user->id),
  204. 'remark' => $this->remark,
  205. ]);
  206. }
  207. return $model;
  208. }
  209. /**
  210. * 删除管理员前
  211. * @param $selected
  212. * @throws \Exception
  213. */
  214. public function beforeDelete($selected) {
  215. foreach ($selected as $value){
  216. if($value==\Yii::$app->user->id){
  217. throw new \Exception(\Yii::t('ctx', 'unableToDeleteOneSelf'));
  218. }
  219. }
  220. $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
  221. }
  222. /**
  223. * 删除管理员
  224. * @param $selected
  225. */
  226. public function delete($selected) {
  227. $this->adminOperateLogger->clean()->save([
  228. 'optType' => '删除管理员',
  229. ]);
  230. }
  231. }