MemberIdentity.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace backend\models;
  3. use yii\base\Model;
  4. use yii\web\IdentityInterface;
  5. class MemberIdentity extends Model implements IdentityInterface
  6. {
  7. const USER_LOGIN_ID_COOKIE = "USER_LOGIN_ID_COOKIE";
  8. const USER_LOGIN_TOKEN_COOKIE = "USER_LOGIN_TOKEN_COOKIE";
  9. const USER_AVATAR_PATH_COOKIE = "USER_AVATAR_PATH_COOKIE";
  10. const ADMIN_LOGIN_ID_COOKIE = "ADMIN_LOGIN_ID_COOKIE";
  11. const ADMIN_LOGIN_TOKEN_COOKIE = "ADMIN_LOGIN_TOKEN_COOKIE";
  12. const IB_LOGIN_ID_COOKIE = "IB_LOGIN_ID_COOKIE";
  13. const IB_LOGIN_TOKEN_COOKIE = "IB_LOGIN_TOKEN_COOKIE";
  14. const MEMBER_TYPE_USER = 1;
  15. const MEMBER_TYPE_IB = 2;
  16. const MEMBER_TYPE_ADMIN = 99;
  17. public $id;
  18. public $type;
  19. public $is_enable;
  20. public $username;
  21. public $ip;
  22. public $logins;
  23. public $name;
  24. public $gender;
  25. public $id_no;
  26. public $birthday;
  27. public $address;
  28. public $mobile;
  29. public $random_code;
  30. public $random_code_time;
  31. public $avatar;
  32. public $ref_id;
  33. public $ref_path;
  34. public $in_time;
  35. public $ib_old_login_name;
  36. private $_main_login;
  37. private $_mt4user;
  38. /**
  39. * @inheritdoc
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['id', 'type', 'is_enable', 'username', 'ip', 'logins', 'name', 'gender', 'id_no', 'birthday',
  45. 'address', 'mobile', 'main_login', 'random_code', 'random_code_time', 'avatar', 'ref_id',
  46. 'ref_path', 'in_time', 'ib_old_login_name'], 'safe'],
  47. ];
  48. }
  49. /**
  50. * Finds an identity by the given ID.
  51. * @param string|int $id the ID to be looked for
  52. * @return IdentityInterface the identity object that matches the given ID.
  53. * Null should be returned if such an identity cannot be found
  54. * or the identity is not in an active state (disabled, deleted, etc.)
  55. */
  56. public static function findIdentity($id)
  57. {
  58. $api = new MemberApi();
  59. $result = $api->getMemberInfo($id);
  60. if ($result['code'] == 1) {
  61. $identity = new static();
  62. $identity->setAttributes($result['data']);
  63. return $identity;
  64. } else {
  65. return null;
  66. }
  67. }
  68. /**
  69. * Finds an identity by the given token.
  70. * @param mixed $token the token to be looked for
  71. * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
  72. * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
  73. * @return IdentityInterface the identity object that matches the given token.
  74. * Null should be returned if such an identity cannot be found
  75. * or the identity is not in an active state (disabled, deleted, etc.)
  76. */
  77. public static function findIdentityByAccessToken($token, $type = null)
  78. {
  79. return null;
  80. }
  81. /**
  82. * Returns an ID that can uniquely identify a user identity.
  83. * @return string|int an ID that uniquely identifies a user identity.
  84. */
  85. public function getId()
  86. {
  87. return $this->id;
  88. }
  89. /**
  90. * Returns a key that can be used to check the validity of a given identity ID.
  91. *
  92. * The key should be unique for each individual user, and should be persistent
  93. * so that it can be used to check the validity of the user identity.
  94. *
  95. * The space of such keys should be big enough to defeat potential identity attacks.
  96. *
  97. * This is required if [[User::enableAutoLogin]] is enabled.
  98. * @return string a key that is used to check the validity of a given identity ID.
  99. * @see validateAuthKey()
  100. */
  101. public function getAuthKey()
  102. {
  103. return '';
  104. }
  105. /**
  106. * Validates the given auth key.
  107. *
  108. * This is required if [[User::enableAutoLogin]] is enabled.
  109. * @param string $authKey the given auth key
  110. * @return bool whether the given auth key is valid.
  111. * @see getAuthKey()
  112. */
  113. public function validateAuthKey($authKey)
  114. {
  115. return $this->getAuthKey() === $authKey;
  116. }
  117. /**
  118. * @return mixed
  119. */
  120. public function getMt4user()
  121. {
  122. if ($this->_mt4user === null) {
  123. $api = new Mt4UserApi();
  124. $mainLogin = $this->getMain_login();
  125. if ($mainLogin != null) {
  126. $result = $api->getUserInfo($mainLogin);
  127. $this->_mt4user = !empty($result['data']) ? (array)$result['data'] : false;
  128. }
  129. }
  130. return $this->_mt4user;
  131. }
  132. public function switchMt4user($login)
  133. {
  134. $api = new MemberApi();
  135. $loginArr = explode(',', $this->logins);
  136. if (in_array($login, $loginArr)) {
  137. $result = $api->switchMt4user($this->id, $login);
  138. if ($result['code'] == 1) {
  139. // 重置当前mt4user
  140. $this->_mt4user = null;
  141. $this->setMain_login($login);
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. /**
  148. * @return mixed
  149. */
  150. public function getMain_login()
  151. {
  152. if ($this->_main_login == null) {
  153. $loginArr = explode(',', $this->logins);
  154. if (isset($loginArr[0])) {
  155. $this->_main_login = $loginArr[0];
  156. }
  157. }
  158. return $this->_main_login;
  159. }
  160. /**
  161. * @param mixed $main_login
  162. */
  163. public function setMain_login($main_login)
  164. {
  165. $this->_main_login = $main_login;
  166. }
  167. /**
  168. * 是否为管理员
  169. * @return bool
  170. */
  171. public function isHaveAdmin()
  172. {
  173. if (!empty($_COOKIE[self::ADMIN_LOGIN_ID_COOKIE])) {
  174. $id = (int) $_COOKIE[self::ADMIN_LOGIN_ID_COOKIE];
  175. $api = new MemberApi();
  176. $memberInfo = $api->getMemberInfo($id)['data'];
  177. if (!empty($memberInfo['type']) && $memberInfo['type'] == self::MEMBER_TYPE_ADMIN) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. }