You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

429 lines
12 KiB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\base\NotSupportedException;
  5. use yii\web\IdentityInterface;
  6. /**
  7. * User model
  8. *
  9. * @property integer $ID
  10. * @property integer $MAIN_CORP_ID cs运营主体
  11. * @property integer $MAIN_CORP_ID2 车队系统运营主体
  12. * @property string $USER_NAME
  13. * @property string $USER_PASSWORD
  14. * @property integer $ORG_ID
  15. * @property integer $TOP_ORG_ID
  16. * @property string $OPERA_ORG_ID
  17. * @property string $USER_SIGN
  18. * @property integer $USER_TYPE
  19. * @property string|array $MENU_PERMISSION
  20. * @property integer $USER_ROLE
  21. * @property integer $USER_ROLE2
  22. * @property string $TRUE_NAME
  23. * @property integer $STATUS
  24. * @property integer $CANCEL_FLAG
  25. * @property integer $CREATE_USER_ID
  26. * @property string $CREATE_TIME
  27. * @property integer $UPDATE_USER_ID
  28. * @property string $UPDATE_TIME
  29. * @property string $PHONE_no
  30. *
  31. */
  32. class User extends zModel implements IdentityInterface
  33. {
  34. const SUPER_ADMIN_FO = 20; //车系统超级管理员
  35. const NO_ACCESS_FO = 21; //车系统无权限角色的ID
  36. const CANCEL_FLAG_DELETED = 1;
  37. const CANCEL_FLAG_VISIBLE = 0;
  38. const STATUS_ACTIVE = 0;
  39. const STATUS_DELETE = 1;
  40. public $USER_PASSWORD1;
  41. public $USER_PASSWORD2;
  42. public $auth_key = true;
  43. public $password_hash;
  44. public $password_reset_token;
  45. /**
  46. * @inheritdoc
  47. */
  48. public static function tableName()
  49. {
  50. return '{{%base_user}}';
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function behaviors()
  56. {
  57. return [
  58. // TimestampBehavior::className(),
  59. ];
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function rules()
  65. {
  66. return [
  67. ['CANCEL_FLAG', 'default', 'value' => self::CANCEL_FLAG_VISIBLE],
  68. [['USER_NAME', 'TRUE_NAME', 'ORG_ID'], 'required', 'message' => '请填写{attribute}'],
  69. [['USER_NAME'], 'unique', 'message' => '帐号名已存在'],
  70. [['PHONE_no', 'USER_ROLE2'], 'required'],
  71. [['USER_PASSWORD'], 'required', 'message' => '请输入密码', 'on' => 'create'],
  72. [['USER_PASSWORD1'], 'required', 'message' => '请输入旧密码', 'on' => 'password'],
  73. [['USER_PASSWORD2'], 'required', 'message' => '请确认密码', 'on' => 'password'],
  74. [['USER_PASSWORD'], 'required', 'message' => '请输入密码', 'on' => 'password'],
  75. [['USER_PASSWORD'], 'string', 'length' => [6, 34]],
  76. [['USER_PASSWORD', 'USER_NAME', 'TRUE_NAME'], 'trim'],
  77. [['PHONE_no'], 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请填写正确的手机号'],
  78. [['USER_PASSWORD'], 'validateChangePassword', 'on'=> 'password']
  79. ];
  80. }
  81. /**
  82. * @inheritdoc
  83. */
  84. public function scenarios()
  85. {
  86. // $scenarios = parent::scenarios();
  87. $scenarios['create'] = [
  88. 'USER_NAME', 'TRUE_NAME', 'USER_PASSWORD', 'ORG_ID', 'MAIN_CORP_ID', 'MAIN_CORP_ID2', 'USER_ROLE2', 'MENU_PERMISSON', 'STATUS', 'PHONE_no'
  89. ];
  90. $scenarios['update'] = [
  91. 'USER_NAME', 'TRUE_NAME', 'USER_PASSWORD', 'ORG_ID', 'USER_ROLE2', 'MENU_PERMISSON', 'STATUS', 'PHONE_no'
  92. ];
  93. $scenarios['password'] = [
  94. 'USER_PASSWORD','USER_PASSWORD1','USER_PASSWORD2',
  95. ];
  96. $scenarios['default'] = array_keys($this->getAttributes());
  97. $scenarios['update-role'] = ['TRUE_NAME', 'USER_ROLE2', 'MENU_PERMISSION'];
  98. return $scenarios;
  99. }
  100. /**
  101. * @inheritdoc
  102. */
  103. public function attributeLabels()
  104. {
  105. return [
  106. 'ID' => 'ID',
  107. 'MAIN_CORP_ID' => '运营主体',
  108. 'MAIN_CORP_ID2' => '运营主体',
  109. 'USER_NAME' => '账号名',
  110. 'USER_PASSWORD' => '密码',
  111. 'USER_PASSWORD1' => '原始密码',
  112. 'USER_PASSWORD2' => '新密码',
  113. 'ORG_ID' => '公司', //'直属组织机构ID,BASE_ORGANIZATION.ORG_ID,非0',
  114. 'TOP_ORG_ID' => '', //'产品所属顶级机构ID,BASE_ORGANIZATION.ORG_ID 是否需要支付 需要支付置0',
  115. 'OPERA_ORG_ID' => 'Opera Org ID',
  116. 'USER_SIGN' => '用户识别代码',
  117. 'USER_TYPE' => '用户类型', //'0:全权限 1:仅直通巴士 2.穿梭巴士 3.组合线路 4:直通+穿梭 5.直通+组合 6.穿梭+组合',
  118. 'MENU_PERMISSION' => '菜单权限列表',
  119. 'USER_ROLE' => '角色', //CS系统角色
  120. 'USER_ROLE2' => '角色', //车系统
  121. 'TRUE_NAME' => '姓名',
  122. 'STATUS' => '状态',
  123. 'CANCEL_FLAG' => '已删除',
  124. 'CREATE_USER_ID' => '创建用户',
  125. 'CREATE_TIME' => '创建时间',
  126. 'UPDATE_USER_ID' => '更新用户',
  127. 'UPDATE_TIME' => '记录最后更新时间',
  128. 'PHONE_no' => '联系电话',
  129. ];
  130. }
  131. /**
  132. * @inheritdoc
  133. */
  134. public static function findIdentity($id)
  135. {
  136. $user = static::findOne(['id' => $id]);
  137. // 'status' => self::STATUS_ACTIVE
  138. $menus = explode('|', $user->MENU_PERMISSION);
  139. $url = [];
  140. foreach ($menus as $menu) {
  141. $tmp = explode(',', $menu);
  142. $url = array_merge($tmp, $url);
  143. }
  144. $user->MENU_PERMISSION = $url;
  145. return $user;
  146. }
  147. /**
  148. * @inheritdoc
  149. */
  150. public static function findIdentityByAccessToken($token, $type = null)
  151. {
  152. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  153. }
  154. /**
  155. * Finds user by username
  156. *
  157. * @param string $username
  158. * @return static|null
  159. */
  160. public static function findByUsername($username)
  161. {
  162. return static::findOne(['USER_NAME' => $username, 'CANCEL_FLAG' => self::CANCEL_FLAG_VISIBLE]);
  163. }
  164. /**
  165. * Finds user by password reset token
  166. *
  167. * @param string $token password reset token
  168. * @return static|null
  169. */
  170. public static function findByPasswordResetToken($token)
  171. {
  172. if (!static::isPasswordResetTokenValid($token)) {
  173. return null;
  174. }
  175. return static::findOne([
  176. 'password_reset_token' => $token,
  177. // 'status' => self::STATUS_ACTIVE,
  178. ]);
  179. }
  180. /**
  181. * Finds out if password reset token is valid
  182. *
  183. * @param string $token password reset token
  184. * @return bool
  185. */
  186. public static function isPasswordResetTokenValid($token)
  187. {
  188. if (empty($token)) {
  189. return false;
  190. }
  191. $timestamp = (int)substr($token, strrpos($token, '_') + 1);
  192. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  193. return $timestamp + $expire >= time();
  194. }
  195. /**
  196. * CS运营主体
  197. * @return \yii\db\ActiveQuery
  198. */
  199. public function getMainCorp()
  200. {
  201. return $this->hasOne(BaseMainCorporation::className(), ['id' => 'MAIN_CORP_ID']);
  202. }
  203. /**
  204. * 车系统运营主体
  205. * @return \yii\db\ActiveQuery
  206. */
  207. public function getMainCorp2()
  208. {
  209. return $this->hasOne(BaseMainCorporation::className(), ['id' => 'MAIN_CORP_ID2']);
  210. }
  211. /**
  212. * 车系统运营主体
  213. * @return \yii\db\ActiveQuery
  214. */
  215. public function getUserRole2()
  216. {
  217. return $this->hasOne(BaseUserAuth::className(), ['id' => 'USER_ROLE2']);
  218. }
  219. /**
  220. * @inheritdoc
  221. */
  222. public function getId()
  223. {
  224. return $this->getPrimaryKey();
  225. }
  226. /**
  227. * @inheritdoc
  228. */
  229. public function getAuthKey()
  230. {
  231. return $this->auth_key;
  232. }
  233. /**
  234. * @inheritdoc
  235. */
  236. public function validateAuthKey($authKey)
  237. {
  238. //公司base_user目前没有用auth_key
  239. return true;
  240. // return $this->getAuthKey() === $authKey;
  241. }
  242. /**
  243. * Validates password
  244. *
  245. * @param string $password password to validate
  246. * @return bool if password provided is valid for current user
  247. */
  248. public function validatePassword($password)
  249. {
  250. return $this->USER_PASSWORD == md5($password);
  251. // return Yii::$app->security->validatePassword($password, $this->password_hash);
  252. }
  253. /**
  254. * Generates password hash from password and sets it to the model
  255. *
  256. * @param string $password
  257. */
  258. public function setPassword($password)
  259. {
  260. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  261. }
  262. /**
  263. * Generates "remember me" authentication key
  264. */
  265. public function generateAuthKey()
  266. {
  267. $this->auth_key = Yii::$app->security->generateRandomString();
  268. }
  269. /**
  270. * Generates new password reset token
  271. */
  272. public function generatePasswordResetToken()
  273. {
  274. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  275. }
  276. /**
  277. * Removes password reset token
  278. */
  279. public function removePasswordResetToken()
  280. {
  281. $this->password_reset_token = null;
  282. }
  283. public function getPermission()
  284. {
  285. $user = Yii::$app->user->identity;
  286. /* @var $user User */
  287. return $user->MENU_PERMISSION;
  288. }
  289. //修改密码,验证
  290. public function validateChangePassword()
  291. {
  292. if ($this->login_user->USER_PASSWORD != md5($this->USER_PASSWORD1)) {
  293. $this->addError('USER_PASSWORD1','原始密码不正确');
  294. return false;
  295. }
  296. if ($this->USER_PASSWORD !== $this->USER_PASSWORD2) {
  297. $this->addError('USER_PASSWORD','两次密码不一致');
  298. $this->addError('USER_PASSWORD2','两次密码不一致');
  299. return false;
  300. }
  301. return true;
  302. }
  303. /**
  304. * User: wangxj
  305. *
  306. * cs登录后,车队系统不需要登录
  307. * @return bool
  308. *
  309. */
  310. public static function cs1Login()
  311. {
  312. //登录检测,不合理
  313. $cookies = Yii::$app->getRequest()->getCookies();
  314. if ($cookies->has('xm_data')) {
  315. $user = User::findIdentity($cookies->getValue('user_id'));
  316. if ($user !== null) {
  317. Yii::$app->user->login($user);
  318. return true;
  319. }
  320. }
  321. return false;
  322. }
  323. /**
  324. * User: qius
  325. * Editor: wangxj
  326. *
  327. * 获取左上角系统列表
  328. *
  329. * @return array
  330. */
  331. public function getMainCorpArray()
  332. {
  333. $model_main_corp = new \backend\modules\zzcs\models\BaseMainCorporation();
  334. $all_corp_array = $model_main_corp->getAllCorporation();
  335. $corp_array = array();
  336. foreach ($all_corp_array["all_corporation"] as $main_corp_info) {
  337. $corp_array[$main_corp_info["id"]] = $main_corp_info;
  338. }
  339. $av_main_corp = array();
  340. $av_main_corp[] = $this->getSystemUrlFromMainCorp($corp_array[$this->MAIN_CORP_ID]);
  341. if ($this->MAIN_CORP_ID != $this->MAIN_CORP_ID2) {
  342. $av_main_corp[] = $this->getSystemUrlFromMainCorp($corp_array[$this->MAIN_CORP_ID2]);
  343. }
  344. return $av_main_corp;
  345. }
  346. protected function getSystemUrlFromMainCorp($main_corp_info)
  347. {
  348. $base_url = "http://" . CS1_DOMAIN;
  349. $base_url1 = "http://fo." . DOMAIN;
  350. // $base_url2 = "http://fx.". DOMAIN;
  351. $system_array = array();
  352. $system_array["disp_name"] = $main_corp_info["full_name"];
  353. if ($main_corp_info["corp_type"] == 1) {
  354. $system_array["system_name"] = Yii::$app->params['systemVersion']['CS'];
  355. $system_array["system_url"] = $base_url . "/menu/menu";
  356. } else {
  357. $system_array["system_name"] = Yii::$app->params['systemVersion']['FO'];
  358. $system_array["system_url"] = $base_url1 . "/motorcade";
  359. }
  360. return $system_array;
  361. }
  362. /**
  363. * User: wangxj
  364. * 车系统
  365. * 根据user_role2更新当前用户对象权限属性(menu_permission,并不会保存)
  366. *
  367. */
  368. public function syncPermissionFo()
  369. {
  370. if (is_array($this->USER_ROLE2)) {
  371. $roleAuth = BaseUserAuth::find()->where(['id' => $this->USER_ROLE2])->all();
  372. $this->USER_ROLE2 = implode(',', $this->USER_ROLE2);
  373. } else {
  374. $roleAuth = BaseUserAuth::find()->where(['id in (' . $this->USER_ROLE2 . ')'])->all();
  375. }
  376. if (!empty($roleAuth)) {
  377. $roles = [];
  378. foreach ($roleAuth as $item) {
  379. /* @var $item \common\models\BaseUserAuth */
  380. $tmp = explode(',', $item->role_list);
  381. $roles = array_unique(array_merge($roles, $tmp));
  382. }
  383. $this->MENU_PERMISSION = implode(',', $roles);
  384. } else {
  385. $this->MENU_PERMISSION = '';
  386. }
  387. }
  388. }