|
- <?php
-
- namespace common\models;
-
- use Yii;
- use yii\base\NotSupportedException;
- use yii\web\IdentityInterface;
-
- /**
- * User model
- *
- * @property integer $ID
- * @property integer $MAIN_CORP_ID cs运营主体
- * @property integer $MAIN_CORP_ID2 车队系统运营主体
- * @property string $USER_NAME
- * @property string $USER_PASSWORD
- * @property integer $ORG_ID
- * @property integer $TOP_ORG_ID
- * @property string $OPERA_ORG_ID
- * @property string $USER_SIGN
- * @property integer $USER_TYPE
- * @property string|array $MENU_PERMISSION
- * @property integer $USER_ROLE
- * @property integer $USER_ROLE2
- * @property string $TRUE_NAME
- * @property integer $STATUS
- * @property integer $CANCEL_FLAG
- * @property integer $CREATE_USER_ID
- * @property string $CREATE_TIME
- * @property integer $UPDATE_USER_ID
- * @property string $UPDATE_TIME
- * @property string $PHONE_no
- *
- */
- class User extends zModel implements IdentityInterface
- {
- const SUPER_ADMIN_FO = 20; //车系统超级管理员
- const NO_ACCESS_FO = 21; //车系统无权限角色的ID
-
- const CANCEL_FLAG_DELETED = 1;
- const CANCEL_FLAG_VISIBLE = 0;
- const STATUS_ACTIVE = 0;
- const STATUS_DELETE = 1;
-
- public $USER_PASSWORD1;
- public $USER_PASSWORD2;
- public $auth_key = true;
-
- public $password_hash;
- public $password_reset_token;
-
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%base_user}}';
- }
-
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- // TimestampBehavior::className(),
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- ['CANCEL_FLAG', 'default', 'value' => self::CANCEL_FLAG_VISIBLE],
- [['USER_NAME', 'TRUE_NAME', 'ORG_ID'], 'required', 'message' => '请填写{attribute}'],
- [['USER_NAME'], 'unique', 'message' => '帐号名已存在'],
- [['PHONE_no', 'USER_ROLE2'], 'required'],
- [['USER_PASSWORD'], 'required', 'message' => '请输入密码', 'on' => 'create'],
- [['USER_PASSWORD1'], 'required', 'message' => '请输入旧密码', 'on' => 'password'],
- [['USER_PASSWORD2'], 'required', 'message' => '请确认密码', 'on' => 'password'],
- [['USER_PASSWORD'], 'required', 'message' => '请输入密码', 'on' => 'password'],
- [['USER_PASSWORD'], 'string', 'length' => [6, 34]],
- [['USER_PASSWORD', 'USER_NAME', 'TRUE_NAME'], 'trim'],
- [['PHONE_no'], 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请填写正确的手机号'],
- [['USER_PASSWORD'], 'validateChangePassword', 'on'=> 'password']
-
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function scenarios()
- {
- // $scenarios = parent::scenarios();
- $scenarios['create'] = [
- 'USER_NAME', 'TRUE_NAME', 'USER_PASSWORD', 'ORG_ID', 'MAIN_CORP_ID', 'MAIN_CORP_ID2', 'USER_ROLE2', 'MENU_PERMISSON', 'STATUS', 'PHONE_no'
- ];
- $scenarios['update'] = [
- 'USER_NAME', 'TRUE_NAME', 'USER_PASSWORD', 'ORG_ID', 'USER_ROLE2', 'MENU_PERMISSON', 'STATUS', 'PHONE_no'
- ];
- $scenarios['password'] = [
- 'USER_PASSWORD','USER_PASSWORD1','USER_PASSWORD2',
- ];
- $scenarios['default'] = array_keys($this->getAttributes());
- $scenarios['update-role'] = ['TRUE_NAME', 'USER_ROLE2', 'MENU_PERMISSION'];
- return $scenarios;
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ID' => 'ID',
- 'MAIN_CORP_ID' => '运营主体',
- 'MAIN_CORP_ID2' => '运营主体',
- 'USER_NAME' => '账号名',
- 'USER_PASSWORD' => '密码',
- 'USER_PASSWORD1' => '原始密码',
- 'USER_PASSWORD2' => '新密码',
- 'ORG_ID' => '公司', //'直属组织机构ID,BASE_ORGANIZATION.ORG_ID,非0',
- 'TOP_ORG_ID' => '', //'产品所属顶级机构ID,BASE_ORGANIZATION.ORG_ID 是否需要支付 需要支付置0',
- 'OPERA_ORG_ID' => 'Opera Org ID',
- 'USER_SIGN' => '用户识别代码',
- 'USER_TYPE' => '用户类型', //'0:全权限 1:仅直通巴士 2.穿梭巴士 3.组合线路 4:直通+穿梭 5.直通+组合 6.穿梭+组合',
- 'MENU_PERMISSION' => '菜单权限列表',
- 'USER_ROLE' => '角色', //CS系统角色
- 'USER_ROLE2' => '角色', //车系统
- 'TRUE_NAME' => '姓名',
- 'STATUS' => '状态',
- 'CANCEL_FLAG' => '已删除',
- 'CREATE_USER_ID' => '创建用户',
- 'CREATE_TIME' => '创建时间',
- 'UPDATE_USER_ID' => '更新用户',
- 'UPDATE_TIME' => '记录最后更新时间',
- 'PHONE_no' => '联系电话',
- ];
- }
-
- /**
- * @inheritdoc
- */
- public static function findIdentity($id)
- {
- $user = static::findOne(['id' => $id]);
- // 'status' => self::STATUS_ACTIVE
- $menus = explode('|', $user->MENU_PERMISSION);
- $url = [];
- foreach ($menus as $menu) {
- $tmp = explode(',', $menu);
- $url = array_merge($tmp, $url);
- }
- $user->MENU_PERMISSION = $url;
- return $user;
- }
-
- /**
- * @inheritdoc
- */
- public static function findIdentityByAccessToken($token, $type = null)
- {
- throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
- }
-
- /**
- * Finds user by username
- *
- * @param string $username
- * @return static|null
- */
- public static function findByUsername($username)
- {
- return static::findOne(['USER_NAME' => $username, 'CANCEL_FLAG' => self::CANCEL_FLAG_VISIBLE]);
- }
-
- /**
- * Finds user by password reset token
- *
- * @param string $token password reset token
- * @return static|null
- */
- public static function findByPasswordResetToken($token)
- {
- if (!static::isPasswordResetTokenValid($token)) {
- return null;
- }
-
- return static::findOne([
- 'password_reset_token' => $token,
- // 'status' => self::STATUS_ACTIVE,
- ]);
- }
-
- /**
- * Finds out if password reset token is valid
- *
- * @param string $token password reset token
- * @return bool
- */
- public static function isPasswordResetTokenValid($token)
- {
- if (empty($token)) {
- return false;
- }
-
- $timestamp = (int)substr($token, strrpos($token, '_') + 1);
- $expire = Yii::$app->params['user.passwordResetTokenExpire'];
- return $timestamp + $expire >= time();
- }
-
- /**
- * CS运营主体
- * @return \yii\db\ActiveQuery
- */
- public function getMainCorp()
- {
- return $this->hasOne(BaseMainCorporation::className(), ['id' => 'MAIN_CORP_ID']);
- }
-
- /**
- * 车系统运营主体
- * @return \yii\db\ActiveQuery
- */
- public function getMainCorp2()
- {
- return $this->hasOne(BaseMainCorporation::className(), ['id' => 'MAIN_CORP_ID2']);
- }
-
- /**
- * 车系统运营主体
- * @return \yii\db\ActiveQuery
- */
- public function getUserRole2()
- {
- return $this->hasOne(BaseUserAuth::className(), ['id' => 'USER_ROLE2']);
- }
-
- /**
- * @inheritdoc
- */
- public function getId()
- {
- return $this->getPrimaryKey();
- }
-
- /**
- * @inheritdoc
- */
- public function getAuthKey()
- {
- return $this->auth_key;
- }
-
- /**
- * @inheritdoc
- */
- public function validateAuthKey($authKey)
- {
- //公司base_user目前没有用auth_key
- return true;
- // return $this->getAuthKey() === $authKey;
- }
-
- /**
- * Validates password
- *
- * @param string $password password to validate
- * @return bool if password provided is valid for current user
- */
- public function validatePassword($password)
- {
- return $this->USER_PASSWORD == md5($password);
- // return Yii::$app->security->validatePassword($password, $this->password_hash);
- }
-
- /**
- * Generates password hash from password and sets it to the model
- *
- * @param string $password
- */
- public function setPassword($password)
- {
- $this->password_hash = Yii::$app->security->generatePasswordHash($password);
- }
-
- /**
- * Generates "remember me" authentication key
- */
- public function generateAuthKey()
- {
- $this->auth_key = Yii::$app->security->generateRandomString();
- }
-
- /**
- * Generates new password reset token
- */
- public function generatePasswordResetToken()
- {
- $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
- }
-
- /**
- * Removes password reset token
- */
- public function removePasswordResetToken()
- {
- $this->password_reset_token = null;
- }
-
- public function getPermission()
- {
- $user = Yii::$app->user->identity;
- /* @var $user User */
- return $user->MENU_PERMISSION;
- }
-
-
- //修改密码,验证
- public function validateChangePassword()
- {
- if ($this->login_user->USER_PASSWORD != md5($this->USER_PASSWORD1)) {
- $this->addError('USER_PASSWORD1','原始密码不正确');
- return false;
- }
- if ($this->USER_PASSWORD !== $this->USER_PASSWORD2) {
- $this->addError('USER_PASSWORD','两次密码不一致');
- $this->addError('USER_PASSWORD2','两次密码不一致');
- return false;
- }
-
- return true;
- }
-
- /**
- * User: wangxj
- *
- * cs登录后,车队系统不需要登录
- * @return bool
- *
- */
- public static function cs1Login()
- {
- //登录检测,不合理
- $cookies = Yii::$app->getRequest()->getCookies();
- if ($cookies->has('xm_data')) {
- $user = User::findIdentity($cookies->getValue('user_id'));
- if ($user !== null) {
- Yii::$app->user->login($user);
- return true;
- }
- }
- return false;
- }
-
- /**
- * User: qius
- * Editor: wangxj
- *
- * 获取左上角系统列表
- *
- * @return array
- */
- public function getMainCorpArray()
- {
- $model_main_corp = new \backend\modules\zzcs\models\BaseMainCorporation();
- $all_corp_array = $model_main_corp->getAllCorporation();
- $corp_array = array();
- foreach ($all_corp_array["all_corporation"] as $main_corp_info) {
- $corp_array[$main_corp_info["id"]] = $main_corp_info;
- }
- $av_main_corp = array();
- $av_main_corp[] = $this->getSystemUrlFromMainCorp($corp_array[$this->MAIN_CORP_ID]);
- if ($this->MAIN_CORP_ID != $this->MAIN_CORP_ID2) {
- $av_main_corp[] = $this->getSystemUrlFromMainCorp($corp_array[$this->MAIN_CORP_ID2]);
- }
- return $av_main_corp;
- }
-
- protected function getSystemUrlFromMainCorp($main_corp_info)
- {
- $base_url = "http://" . CS1_DOMAIN;
- $base_url1 = "http://fo." . DOMAIN;
- // $base_url2 = "http://fx.". DOMAIN;
- $system_array = array();
- $system_array["disp_name"] = $main_corp_info["full_name"];
- if ($main_corp_info["corp_type"] == 1) {
- $system_array["system_name"] = Yii::$app->params['systemVersion']['CS'];
- $system_array["system_url"] = $base_url . "/menu/menu";
- } else {
- $system_array["system_name"] = Yii::$app->params['systemVersion']['FO'];
- $system_array["system_url"] = $base_url1 . "/motorcade";
- }
- return $system_array;
- }
-
- /**
- * User: wangxj
- * 车系统
- * 根据user_role2更新当前用户对象权限属性(menu_permission,并不会保存)
- *
- */
- public function syncPermissionFo()
- {
- if (is_array($this->USER_ROLE2)) {
- $roleAuth = BaseUserAuth::find()->where(['id' => $this->USER_ROLE2])->all();
- $this->USER_ROLE2 = implode(',', $this->USER_ROLE2);
- } else {
- $roleAuth = BaseUserAuth::find()->where(['id in (' . $this->USER_ROLE2 . ')'])->all();
-
- }
- if (!empty($roleAuth)) {
- $roles = [];
- foreach ($roleAuth as $item) {
- /* @var $item \common\models\BaseUserAuth */
- $tmp = explode(',', $item->role_list);
- $roles = array_unique(array_merge($roles, $tmp));
- }
- $this->MENU_PERMISSION = implode(',', $roles);
- } else {
- $this->MENU_PERMISSION = '';
- }
-
- }
- }
|