/** * 静态方法, 单例统一访问入口 * @return object 返回对象的唯一实例 */ public static function getInstance() { if (is_null(self::$_instance) || !isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; }
/** * 根据clientId获得Client完整信息,在整个会话期内只有一个Client对象 * @param $token */ public function getAccessInfo2($token) { if ($this->hasCache2 === true && $this->accessToken !== NULL) { Yii::trace("cache 2 get token:" . $token, "miniyun.cache2"); return $this->accessToken; } $this->accessToken = MiniToken::getInstance()->getAccessInfo($token); return $this->accessToken; }
/** * Overrides OAuth2::getToken() */ protected function getToken($clientId, $scope, $deviceId) { //如是网页版,则把前一个Token删除,确保系统只有一个用户登录网页版 //如果是在pc客户端解锁页面的时候,不用删除设备 if (MiniHttp::clientIsBrowser()) { MiniToken::getInstance()->deleteByDeviceId($deviceId); } $tokenOauth = MiniToken::getInstance()->getToken4Login($clientId, $deviceId); if (!isset($tokenOauth)) { return false; } $token = array("access_token" => $tokenOauth["oauth_token"], "expires_in" => $this->getVariable('access_token_lifetime', OAUTH2_DEFAULT_ACCESS_TOKEN_LIFETIME), "scope" => $tokenOauth["scope"]); // Issue a refresh token also, if we support them if (in_array(OAUTH2_GRANT_TYPE_REFRESH_TOKEN, $this->getSupportedGrantTypes())) { $refreshToken = MiniRefreshToken::getInstance()->getToken($tokenOauth["oauth_token"]); $token["refresh_token"] = $refreshToken["refresh_token"]; } if ($tokenOauth["expires"] < time()) { $this->setVariable('_old_oauth_token', $tokenOauth["oauth_token"]); if (isset($refreshToken)) { $this->setVariable('_old_refresh_token', $refreshToken["refresh_token"]); } return false; } return $token; }
/** *根据ID删除设备 * @param $deviceId * @return boolean */ public function deleteDevice($deviceId) { //删除设备的Meta MiniUserDeviceMeta::getInstance()->deleteMeta($deviceId); //删除设备的Token MiniToken::getInstance()->deleteToken($deviceId); if ($this->hasCache === true) { //删除设备的cache $this->deleteCache($this->getCacheKey($deviceId)); } //把在线设备清单进行删除 MiniOnlineDevice::getInstance()->deleteOnlineDevice($deviceId); //把设备ID资源暂存到Options表中 $this->temporary2Option($deviceId); //删除设备自己,这里不能修改为sql模式,因为设备ID在删除的时候,自动将ID记录到了Options表中 $device = UserDevice::model()->findByPk($deviceId); if (isset($device)) { $device->delete(); } return true; }
/** * 把用户disabled * @param int $userId */ public function disableUser($userId) { $user = User::model()->findByPk($userId); if (isset($user)) { $user["user_status"] = 0; $user->save(); //清空cache $this->cleanCache($userId); //清理Token MiniToken::getInstance()->cleanByUserId($userId); } }
/** * 根据openId获得accessToken */ public function bindOpenId($appKey, $openId) { $user = MiniUser::getInstance()->getUserByOpenId($openId); if (!empty($user)) { $webDevice = MiniUserDevice::getInstance()->getWebDevice($user["id"]); if (!empty($webDevice)) { $token = MiniToken::getInstance()->getToken4Login($appKey, $webDevice["id"]); if (!empty($token)) { return array("success" => true, "access_token" => $token["oauth_token"]); } } } return array("success" => false); }