PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/yiisoft/yii2-authclient/widgets/AuthChoice.php

https://gitlab.com/itlboy/yii2-starter-installed
PHP | 285 lines | 135 code | 24 blank | 126 comment | 16 complexity | 8e53d966a4b3d75f7d96e4d05a900b1c MD5 | raw file
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\authclient\widgets;
  8. use yii\base\InvalidConfigException;
  9. use yii\base\Widget;
  10. use Yii;
  11. use yii\helpers\Json;
  12. use yii\helpers\Url;
  13. use yii\helpers\Html;
  14. use yii\authclient\ClientInterface;
  15. /**
  16. * AuthChoice prints buttons for authentication via various auth clients.
  17. * It opens a popup window for the client authentication process.
  18. * By default this widget relies on presence of [[\yii\authclient\Collection]] among application components
  19. * to get auth clients information.
  20. *
  21. * Example:
  22. *
  23. * ```php
  24. * <?= yii\authclient\widgets\AuthChoice::widget([
  25. * 'baseAuthUrl' => ['site/auth']
  26. * ]); ?>
  27. * ```
  28. *
  29. * You can customize the widget appearance by using [[begin()]] and [[end()]] syntax
  30. * along with using method [[clientLink()]] or [[createClientUrl()]].
  31. * For example:
  32. *
  33. * ```php
  34. * <?php
  35. * use yii\authclient\widgets\AuthChoice;
  36. * ?>
  37. * <?php $authAuthChoice = AuthChoice::begin([
  38. * 'baseAuthUrl' => ['site/auth']
  39. * ]); ?>
  40. * <ul>
  41. * <?php foreach ($authAuthChoice->getClients() as $client): ?>
  42. * <li><?= $authAuthChoice->clientLink($client) ?></li>
  43. * <?php endforeach; ?>
  44. * </ul>
  45. * <?php AuthChoice::end(); ?>
  46. * ```
  47. *
  48. * This widget supports following keys for [[ClientInterface::getViewOptions()]] result:
  49. * - popupWidth - integer width of the popup window in pixels.
  50. * - popupHeight - integer height of the popup window in pixels.
  51. * - widget - configuration for the widget, which should be used to render a client link;
  52. * such widget should be a subclass of [[AuthChoiceItem]].
  53. *
  54. * @see \yii\authclient\AuthAction
  55. *
  56. * @property array $baseAuthUrl Base auth URL configuration. This property is read-only.
  57. * @property ClientInterface[] $clients Auth providers. This property is read-only.
  58. *
  59. * @author Paul Klimov <klimov.paul@gmail.com>
  60. * @since 2.0
  61. */
  62. class AuthChoice extends Widget
  63. {
  64. /**
  65. * @var string name of the auth client collection application component.
  66. * This component will be used to fetch services value if it is not set.
  67. */
  68. public $clientCollection = 'authClientCollection';
  69. /**
  70. * @var string name of the GET param , which should be used to passed auth client id to URL
  71. * defined by [[baseAuthUrl]].
  72. */
  73. public $clientIdGetParamName = 'authclient';
  74. /**
  75. * @var array the HTML attributes that should be rendered in the div HTML tag representing the container element.
  76. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  77. */
  78. public $options = [];
  79. /**
  80. * @var array additional options to be passed to the underlying JS plugin.
  81. */
  82. public $clientOptions = [];
  83. /**
  84. * @var boolean indicates if popup window should be used instead of direct links.
  85. */
  86. public $popupMode = true;
  87. /**
  88. * @var boolean indicates if widget content, should be rendered automatically.
  89. * Note: this value automatically set to 'false' at the first call of [[createClientUrl()]]
  90. */
  91. public $autoRender = true;
  92. /**
  93. * @var array configuration for the external clients base authentication URL.
  94. */
  95. private $_baseAuthUrl;
  96. /**
  97. * @var ClientInterface[] auth providers list.
  98. */
  99. private $_clients;
  100. /**
  101. * @param ClientInterface[] $clients auth providers
  102. */
  103. public function setClients(array $clients)
  104. {
  105. $this->_clients = $clients;
  106. }
  107. /**
  108. * @return ClientInterface[] auth providers
  109. */
  110. public function getClients()
  111. {
  112. if ($this->_clients === null) {
  113. $this->_clients = $this->defaultClients();
  114. }
  115. return $this->_clients;
  116. }
  117. /**
  118. * @param array $baseAuthUrl base auth URL configuration.
  119. */
  120. public function setBaseAuthUrl(array $baseAuthUrl)
  121. {
  122. $this->_baseAuthUrl = $baseAuthUrl;
  123. }
  124. /**
  125. * @return array base auth URL configuration.
  126. */
  127. public function getBaseAuthUrl()
  128. {
  129. if (!is_array($this->_baseAuthUrl)) {
  130. $this->_baseAuthUrl = $this->defaultBaseAuthUrl();
  131. }
  132. return $this->_baseAuthUrl;
  133. }
  134. /**
  135. * Returns default auth clients list.
  136. * @return ClientInterface[] auth clients list.
  137. */
  138. protected function defaultClients()
  139. {
  140. /* @var $collection \yii\authclient\Collection */
  141. $collection = Yii::$app->get($this->clientCollection);
  142. return $collection->getClients();
  143. }
  144. /**
  145. * Composes default base auth URL configuration.
  146. * @return array base auth URL configuration.
  147. */
  148. protected function defaultBaseAuthUrl()
  149. {
  150. $baseAuthUrl = [
  151. Yii::$app->controller->getRoute()
  152. ];
  153. $params = $_GET;
  154. unset($params[$this->clientIdGetParamName]);
  155. $baseAuthUrl = array_merge($baseAuthUrl, $params);
  156. return $baseAuthUrl;
  157. }
  158. /**
  159. * Outputs client auth link.
  160. * @param ClientInterface $client external auth client instance.
  161. * @param string $text link text, if not set - default value will be generated.
  162. * @param array $htmlOptions link HTML options.
  163. * @return string generated HTML.
  164. * @throws InvalidConfigException on wrong configuration.
  165. */
  166. public function clientLink($client, $text = null, array $htmlOptions = [])
  167. {
  168. $viewOptions = $client->getViewOptions();
  169. if (empty($viewOptions['widget'])) {
  170. if ($text === null) {
  171. $text = Html::tag('span', '', ['class' => 'auth-icon ' . $client->getName()]);
  172. }
  173. if (!isset($htmlOptions['class'])) {
  174. $htmlOptions['class'] = $client->getName();
  175. }
  176. if (!isset($htmlOptions['title'])) {
  177. $htmlOptions['title'] = $client->getTitle();
  178. }
  179. Html::addCssClass($htmlOptions, ['widget' => 'auth-link']);
  180. if ($this->popupMode) {
  181. if (isset($viewOptions['popupWidth'])) {
  182. $htmlOptions['data-popup-width'] = $viewOptions['popupWidth'];
  183. }
  184. if (isset($viewOptions['popupHeight'])) {
  185. $htmlOptions['data-popup-height'] = $viewOptions['popupHeight'];
  186. }
  187. }
  188. return Html::a($text, $this->createClientUrl($client), $htmlOptions);
  189. }
  190. $widgetConfig = $viewOptions['widget'];
  191. if (!isset($widgetConfig['class'])) {
  192. throw new InvalidConfigException('Widget config "class" parameter is missing');
  193. }
  194. /* @var $widgetClass Widget */
  195. $widgetClass = $widgetConfig['class'];
  196. if (!(is_subclass_of($widgetClass, AuthChoiceItem::className()))) {
  197. throw new InvalidConfigException('Item widget class must be subclass of "' . AuthChoiceItem::className() . '"');
  198. }
  199. unset($widgetConfig['class']);
  200. $widgetConfig['client'] = $client;
  201. $widgetConfig['authChoice'] = $this;
  202. return $widgetClass::widget($widgetConfig);
  203. }
  204. /**
  205. * Composes client auth URL.
  206. * @param ClientInterface $client external auth client instance.
  207. * @return string auth URL.
  208. */
  209. public function createClientUrl($client)
  210. {
  211. $this->autoRender = false;
  212. $url = $this->getBaseAuthUrl();
  213. $url[$this->clientIdGetParamName] = $client->getId();
  214. return Url::to($url);
  215. }
  216. /**
  217. * Renders the main content, which includes all external services links.
  218. * @return string generated HTML.
  219. */
  220. protected function renderMainContent()
  221. {
  222. $items = [];
  223. foreach ($this->getClients() as $externalService) {
  224. $items[] = Html::tag('li', $this->clientLink($externalService));
  225. }
  226. return Html::tag('ul', implode('', $items), ['class' => 'auth-clients']);
  227. }
  228. /**
  229. * Initializes the widget.
  230. */
  231. public function init()
  232. {
  233. $view = Yii::$app->getView();
  234. if ($this->popupMode) {
  235. AuthChoiceAsset::register($view);
  236. if (empty($this->clientOptions)) {
  237. $options = '';
  238. } else {
  239. $options = Json::htmlEncode($this->clientOptions);
  240. }
  241. $view->registerJs("\$('#" . $this->getId() . "').authchoice({$options});");
  242. } else {
  243. AuthChoiceStyleAsset::register($view);
  244. }
  245. $this->options['id'] = $this->getId();
  246. echo Html::beginTag('div', $this->options);
  247. }
  248. /**
  249. * Runs the widget.
  250. * @return string rendered HTML.
  251. */
  252. public function run()
  253. {
  254. $content = '';
  255. if ($this->autoRender) {
  256. $content .= $this->renderMainContent();
  257. }
  258. $content .= Html::endTag('div');
  259. return $content;
  260. }
  261. }