| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace backendApi\modules\v1\models;
- use common\components\Model;
- use common\helpers\Date;
- use common\helpers\Tool;
- use common\libs\logging\operate\AdminOperate;
- /**
- * Login form
- */
- class AdminForm extends Model
- {
- public $id;
- public $adminName;
- public $realName;
- public $remark;
- public $isEnable;
- public $isModifyPassword;
- public $bindIp;
- public $oldPassword;
- public $password;
- public $surePassword;
- public $roleId;
- public $lang;
- public $countryId;
- public function init() {
- parent::init();
- $this->adminOperateLogger = new AdminOperate([
- 'fetchClass' => Admin::class,
- ]);
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id', 'adminName', 'realName', 'oldPassword', 'password', 'surePassword', 'roleId', 'remark', 'isEnable', 'isModifyPassword', 'bindIp'], 'trim'],
- [['id', 'adminName', 'realName', 'roleId'], 'required'],
- [['password', 'surePassword'], 'required', 'on'=>['add', 'changePassword', 'noLoginModifyPassword']],
- [['adminName', 'oldPassword'], 'required', 'on'=>['noLoginModifyPassword']],
- [['adminName'], 'unique', 'targetClass'=>Admin::class, 'targetAttribute'=>'ADMIN_NAME', 'on'=>['add']],
- ['surePassword', 'compare', 'compareAttribute'=>'password', 'message' => \Yii::t('ctx', 'twoPasswordsMustSame')], // 两次密码必须一致
- ];
- }
- /**
- * 指定校验场景
- * @return array
- */
- public function scenarios()
- {
- $parentScenarios = parent::scenarios();
- $customScenarios = [
- 'add' => ['adminName', 'realName', 'password', 'surePassword', 'roleId', 'remark', 'isEnable', 'isModifyPassword', 'bindIp', 'countryId'],
- 'edit' => ['id', 'password', 'surePassword', 'roleId', 'realName', 'remark', 'isEnable', 'isModifyPassword', 'bindIp', 'countryId'],
- 'changePassword' => ['password', 'surePassword'],
- 'noLoginModifyPassword' => ['adminName', 'oldPassword', 'password', 'surePassword'],
- 'changeLanguage' => ['adminName', 'lang'],
- ];
- return array_merge($parentScenarios, $customScenarios);
- }
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'adminName' => '管理员用户名',
- 'realName' => '管理员会员姓名',
- 'remark' => '备注',
- 'isEnable' => '是否启用',
- 'isModifyPassword' => '是否修改密码',
- 'bindIp' => '绑定IP',
- 'oldPassword' => '原密码',
- 'password' => '密码',
- 'surePassword' => '确认密码',
- 'roleId' => '角色',
- 'countryId' => '国家',
- ];
- }
- /**
- * 添加
- * @return Admin|null
- * @throws \yii\base\Exception
- */
- public function edit(){
- if(!$this->validate()){
- return null;
- }
- if($this->scenario == 'add'){
- $model = new Admin();
- $model->ADMIN_NAME = strtolower($this->adminName);
- $model->REAL_NAME = $this->realName;
- $model->ROLE_ID = $this->roleId;
- $model->REMARK = $this->remark;
- $model->IS_ENABLE = $this->isEnable ? $this->isEnable : 0;
- $model->IS_MODIFY_PASSWORD = $this->isModifyPassword ? $this->isModifyPassword : 0;
- $model->BIND_IP = $this->bindIp;
- $model->CREATE_ADMIN = \Yii::$app->user->id;
- $model->CREATED_AT = Date::nowTime();
- } elseif($this->scenario == 'edit') {
- $model = Admin::findOne(['ID'=>$this->id]);
- $this->adminOperateLogger->beforeUpdate($model);
- $model->REAL_NAME = $this->realName;
- $model->ROLE_ID = $this->roleId;
- $model->REMARK = $this->remark;
- $model->IS_ENABLE = $this->isEnable ? $this->isEnable : 0;
- $model->IS_MODIFY_PASSWORD = $this->isModifyPassword ? $this->isModifyPassword : 0;
- $model->BIND_IP = $this->bindIp;
- $model->UPDATE_ADMIN = \Yii::$app->user->id;
- $model->UPDATED_AT = Date::nowTime();
- } elseif($this->scenario == 'changePassword') {
- $model = Admin::findOne(['ID'=>\Yii::$app->user->id]);
- } elseif($this->scenario == 'noLoginModifyPassword') {
- $model = Admin::findOne(["ADMIN_NAME" => $this->adminName]);
- if (!$model) {
- $this->addError('noLoginModifyPassword', \Yii::t('ctx', 'memberNameDoesNotExist')); // 不存在的用户名
- return null;
- }
- if ($this->oldPassword === $this->password) {
- $this->addError('noLoginModifyPassword', \Yii::t('ctx', 'loginPasswordHasNotChanged')); // 登录密码没有发生改变
- return null;
- }
- if (!$model->validatePassword($this->oldPassword)) {
- $this->addError('noLoginModifyPassword', \Yii::t('ctx', 'oldLoginPasswordIncorrect')); // 原登录密码错误
- return null;
- }
- $model->IS_MODIFY_PASSWORD = 0;
- } else if ($this->scenario == 'changeLanguage') {
- $model = Admin::findOne(["ADMIN_NAME" => $this->adminName]);
- if (!$model) {
- $this->addError('noLoginModifyPassword', \Yii::t('ctx', 'memberNameDoesNotExist')); // 不存在的用户名
- return null;
- }
- $model->LANG = Tool::langConvert($this->lang);
- }else {
- $this->addError('id', \Yii::t('ctx', 'submissionDoesNotExist')); // 提交场景不存在
- return null;
- }
- // 给密码进行加密 ,如果需要添加验证密码安全验证时 ,可以在这个地方加
- if($this->password){
- if ($this->scenario == 'changePassword' || $this->scenario == 'noLoginModifyPassword'){
- if (strlen($this->password) < 8){
- $this->addError($this->scenario, \Yii::t('ctx', 'passwordLeastEightCharacters')); // 登录密码不能小于8位
- return null;
- }
- // $symbol = '!#$%^&*';
- $symbol = '`~!@#$^&*()=|{}\'\":;\',\\[\\].<>\/?~!@#¥……&*()——|{}【】‘;:”。,、?';
- $passwordRules = '/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)(?!['.$symbol.']+$)[\da-zA-z'.$symbol.']{8,}$/';
- $verificationResults = preg_match($passwordRules,$this->password);
- if (!$verificationResults){
- $this->addError($this->scenario, \Yii::t('ctx', 'passwordRule')); // 登录密码中需要包含数字、大写字母、小写字母、特殊字符至少两种
- return null;
- }
- }
- $model->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
- }
- // 执行修改或添加 , 如果执行不成功就在这里报出错误
- if(!$model->save()){
- $this->addErrors($model->getErrors());
- return null;
- }
- // 执行完成后根据对应的类型写入日志
- if($this->scenario == 'add'){
- $this->adminOperateLogger->afterInsert($model)->clean()->save([
- 'optType' => '添加管理员',
- 'userName' => $model->ADMIN_NAME,
- 'remark' => $this->remark,
- ]);
- if (!AdminRole::isSuperAdmin($this->roleId)) {
- foreach ($this->countryId as $country) {
- $adminCountry = new AdminCountry();
- $adminCountry->ADMIN_ID = $model->ID;
- $adminCountry->COUNTRY_ID = $country;
- $adminCountry->save();
- }
- }
- } elseif($this->scenario == 'edit') {
- $this->adminOperateLogger->afterUpdate($model);
- $this->adminOperateLogger->clean()->save([
- 'optType' => '编辑管理员',
- 'userName' => $model->ADMIN_NAME,
- 'remark' => $this->remark,
- ]);
- (new AdminCountry())->deleteAll(['ADMIN_ID' => $model->ID]);
- if (!AdminRole::isSuperAdmin($this->roleId)) {
- // 国家
- foreach ($this->countryId as $country) {
- $adminCountry = new AdminCountry();
- $adminCountry->ADMIN_ID = $model->ID;
- $adminCountry->COUNTRY_ID = $country;
- $adminCountry->save();
- }
- }
- } elseif($this->scenario == 'noLoginModifyPassword') {
- $this->adminOperateLogger->clean()->save([
- 'optType' => '不登录重置密码',
- 'userName' => $model->ADMIN_NAME,
- 'adminName' => $model->ADMIN_NAME,
- 'remark' => '不登录重置密码',
- ]);
- } elseif($this->scenario == 'changeLanguage') {
- }else{
- $this->adminOperateLogger->clean()->save([
- 'optType' => '重置密码',
- 'userName' => Admin::getAdminNameById(\Yii::$app->user->id),
- 'remark' => $this->remark,
- ]);
- }
- return $model;
- }
- /**
- * 删除管理员前
- * @param $selected
- * @throws \Exception
- */
- public function beforeDelete($selected) {
- foreach ($selected as $value){
- if($value==\Yii::$app->user->id){
- throw new \Exception(\Yii::t('ctx', 'unableToDeleteOneSelf'));
- }
- }
- $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
- }
- /**
- * 删除管理员
- * @param $selected
- */
- public function delete($selected) {
- $this->adminOperateLogger->clean()->save([
- 'optType' => '删除管理员',
- ]);
- }
- }
|