| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace backend\models;
- use yii\base\Model;
- use yii\web\IdentityInterface;
- class MemberIdentity extends Model implements IdentityInterface
- {
-
- const USER_LOGIN_ID_COOKIE = "USER_LOGIN_ID_COOKIE";
- const USER_LOGIN_TOKEN_COOKIE = "USER_LOGIN_TOKEN_COOKIE";
- const USER_AVATAR_PATH_COOKIE = "USER_AVATAR_PATH_COOKIE";
- const ADMIN_LOGIN_ID_COOKIE = "ADMIN_LOGIN_ID_COOKIE";
- const ADMIN_LOGIN_TOKEN_COOKIE = "ADMIN_LOGIN_TOKEN_COOKIE";
- const IB_LOGIN_ID_COOKIE = "IB_LOGIN_ID_COOKIE";
- const IB_LOGIN_TOKEN_COOKIE = "IB_LOGIN_TOKEN_COOKIE";
- const MEMBER_TYPE_USER = 1;
- const MEMBER_TYPE_IB = 2;
- const MEMBER_TYPE_ADMIN = 99;
- public $id;
- public $type;
- public $is_enable;
- public $username;
- public $ip;
- public $logins;
- public $name;
- public $gender;
- public $id_no;
- public $birthday;
- public $address;
- public $mobile;
- public $random_code;
- public $random_code_time;
- public $avatar;
- public $ref_id;
- public $ref_path;
- public $in_time;
- public $ib_old_login_name;
- private $_main_login;
- private $_mt4user;
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id', 'type', 'is_enable', 'username', 'ip', 'logins', 'name', 'gender', 'id_no', 'birthday',
- 'address', 'mobile', 'main_login', 'random_code', 'random_code_time', 'avatar', 'ref_id',
- 'ref_path', 'in_time', 'ib_old_login_name'], 'safe'],
- ];
- }
- /**
- * Finds an identity by the given ID.
- * @param string|int $id the ID to be looked for
- * @return IdentityInterface the identity object that matches the given ID.
- * Null should be returned if such an identity cannot be found
- * or the identity is not in an active state (disabled, deleted, etc.)
- */
- public static function findIdentity($id)
- {
- $api = new MemberApi();
- $result = $api->getMemberInfo($id);
- if ($result['code'] == 1) {
- $identity = new static();
- $identity->setAttributes($result['data']);
- return $identity;
- } else {
- return null;
- }
- }
- /**
- * Finds an identity by the given token.
- * @param mixed $token the token to be looked for
- * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
- * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
- * @return IdentityInterface the identity object that matches the given token.
- * Null should be returned if such an identity cannot be found
- * or the identity is not in an active state (disabled, deleted, etc.)
- */
- public static function findIdentityByAccessToken($token, $type = null)
- {
- return null;
- }
- /**
- * Returns an ID that can uniquely identify a user identity.
- * @return string|int an ID that uniquely identifies a user identity.
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Returns a key that can be used to check the validity of a given identity ID.
- *
- * The key should be unique for each individual user, and should be persistent
- * so that it can be used to check the validity of the user identity.
- *
- * The space of such keys should be big enough to defeat potential identity attacks.
- *
- * This is required if [[User::enableAutoLogin]] is enabled.
- * @return string a key that is used to check the validity of a given identity ID.
- * @see validateAuthKey()
- */
- public function getAuthKey()
- {
- return '';
- }
- /**
- * Validates the given auth key.
- *
- * This is required if [[User::enableAutoLogin]] is enabled.
- * @param string $authKey the given auth key
- * @return bool whether the given auth key is valid.
- * @see getAuthKey()
- */
- public function validateAuthKey($authKey)
- {
- return $this->getAuthKey() === $authKey;
- }
- /**
- * @return mixed
- */
- public function getMt4user()
- {
- if ($this->_mt4user === null) {
- $api = new Mt4UserApi();
- $mainLogin = $this->getMain_login();
- if ($mainLogin != null) {
- $result = $api->getUserInfo($mainLogin);
- $this->_mt4user = !empty($result['data']) ? (array)$result['data'] : false;
- }
- }
- return $this->_mt4user;
- }
- public function switchMt4user($login)
- {
- $api = new MemberApi();
- $loginArr = explode(',', $this->logins);
- if (in_array($login, $loginArr)) {
- $result = $api->switchMt4user($this->id, $login);
- if ($result['code'] == 1) {
- // 重置当前mt4user
- $this->_mt4user = null;
- $this->setMain_login($login);
- return true;
- }
- }
- return false;
- }
- /**
- * @return mixed
- */
- public function getMain_login()
- {
- if ($this->_main_login == null) {
- $loginArr = explode(',', $this->logins);
- if (isset($loginArr[0])) {
- $this->_main_login = $loginArr[0];
- }
- }
- return $this->_main_login;
- }
- /**
- * @param mixed $main_login
- */
- public function setMain_login($main_login)
- {
- $this->_main_login = $main_login;
- }
-
- /**
- * 是否为管理员
- * @return bool
- */
- public function isHaveAdmin()
- {
- if (!empty($_COOKIE[self::ADMIN_LOGIN_ID_COOKIE])) {
- $id = (int) $_COOKIE[self::ADMIN_LOGIN_ID_COOKIE];
- $api = new MemberApi();
- $memberInfo = $api->getMemberInfo($id)['data'];
- if (!empty($memberInfo['type']) && $memberInfo['type'] == self::MEMBER_TYPE_ADMIN) {
- return true;
- }
- }
- return false;
- }
-
- }
|