runet-id / api-client-bundle
此包已被弃用且不再维护。未建议替代包。
RUNET-ID API 客户端包
1.0.0
2017-02-22 11:22 UTC
Requires
- php: >=5.4.0
- runet-id/api-client: ^2.0
This package is not auto-updated.
Last update: 2020-08-21 20:28:15 UTC
README
安装
$ composer require runet-id/api-client-bundle:^1.0.0@alpha
最小配置
runet_id_api_client: container: default_credentials: default credentials: default: key: demokey secret: demosecret
描述
包的核心类是 RunetId\ApiClientBundle\ApiClientContainer
。它允许多个密钥同时工作并支持缓存(默认情况下,使用文件缓存到标准symphony缓存目录 %kernel.cache_dir%/runet_id_api_client
)。
通过配置 container:credentials
部分可以指定多个配置文件。在 default_credentials
中指定默认配置文件名(必填项)。
也可以在容器中通过 RunetId\ApiClientBundle\ApiClientContainer::setCurrentName($name)
设置“当前”配置文件(例如,使用 RequestListener
,如果配置文件的选择取决于对应用程序的请求参数)。如果未设置当前配置文件,则 RunetId\ApiClientBundle\ApiClientContainer::getCurrent()
方法返回默认配置文件。
建议始终使用 RunetId\ApiClientBundle\ApiClientContainer::getCurrent()
方法,因为它提供了最大的灵活性。
服务快速访问的别名(推荐)
services: api_container: "@runet_id.api_client.container" api: class: RunetId\ApiClientBundle\ApiCacheableClient factory: [ "@api_container", getCurrent ] # создаем глобальную переменную в twig # для быстрого доступа к апи из шаблонов twig: globals: api: "@api"
授权配置示例
连接 js
<script src="{{ asset('bundles/runetidapiclient/js/runet_id_api_client.js') }}"></script> <script> var runetIdApiClient = new RunetId; runetIdApiClient.init({ apiKey: '{{ api.options.key }}', backUrl: '{{ url('auth.token') }}' }); </script>
授权按钮代码
<button onclick="runetIdApiClient.login(); return false;"> Войти через –RUNET——ID– </button>
控制器示例
<?php namespace AppBundle\Controller; use RunetId\ApiClient\Exception\ApiException; use RunetId\ApiClient\Model\User; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; /** * @Route("/auth") */ class AuthController extends Controller { /** * @Route("/token", name="auth.token") * @param Request $request * @return Response * @throws HttpException */ public function tokenAction(Request $request) { $token = $request->query->get('token'); try { // содержит все данные о пользователе, полученные с RunetId $apiUser = $this->get('api')->user()->auth($token); } catch (ApiException $e) { throw new HttpException(403, $e->getMessage()); } // регистрируем пользователя на мероприятие со статусом "Участник" $this->get('api')->event()->register($apiUser->RunetId, User\Status::ROLE_PARTICIPANT); // здесь авторизуем пользователя средствами Symfony return new Response(' <script> window.onunload = function () { window.opener.location.reload(); }; setTimeout(window.close, 400); </script> '); } }