Final Class Yiisoft\Yii\AuthClient\Widget\AuthChoice
| Inheritance | Yiisoft\Yii\AuthClient\Widget\AuthChoice » Yiisoft\Widget\Widget |
|---|
AuthChoice prints buttons for authentication via various auth clients.
It opens a popup window for the client authentication process. By default this widget relies on presence of {@see \Yiisoft\Yii\AuthClient\Collection} among application components to get auth clients information.
Example:
<?= AuthChoice::widget()->authRoute('site/auth'); ?>
You can customize the widget appearance by using {@see \Yiisoft\Yii\AuthClient\Widget\begin()} and {@see \Yiisoft\Yii\AuthClient\Widget\end()} syntax along with using method {@see \Yiisoft\Yii\AuthClient\Widget\clientLink()} or {@see \Yiisoft\Yii\AuthClient\Widget\createClientUrl()}. For example:
<?php
use Yiisoft\Yii\AuthClient\Widget\AuthChoice;
?>
<?php $authChoice = AuthChoice::begin([
'baseAuthUrl' => ['site/auth']
]); ?>
<ul>
<?php foreach ($authChoice->getClients() as $client): ?>
<li><?= $authChoice->clientLink($client) ?></li>
<?php endforeach; ?>
</ul>
<?php AuthChoice::end(); ?>
This widget supports following keys for {@see \Yiisoft\Yii\AuthClient\AuthClientInterface::getViewOptions()} result:
- popupWidth: int, width of the popup window in pixels.
- popupHeight: int, height of the popup window in pixels.
- widget: array, configuration for the widget, which should be used to render a client link; such widget should be a subclass of {@see \Yiisoft\Yii\AuthClient\Widget\AuthChoiceItem}.
See also Yiisoft\Yii\AuthClient\AuthAction.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Yii\AuthClient\Widget\AuthChoice | |
| absoluteButtons() | Note: No popup window and no route | Yiisoft\Yii\AuthClient\Widget\AuthChoice |
| authRoute() | Yiisoft\Yii\AuthClient\Widget\AuthChoice | |
| authRoutedButtons() | Note: Popup window with {$authRoute} e.g. 'auth/authclient' | Yiisoft\Yii\AuthClient\Widget\AuthChoice |
| clientLink() | Outputs client auth link. | Yiisoft\Yii\AuthClient\Widget\AuthChoice |
| createClientUrl() | Composes client auth URL. | Yiisoft\Yii\AuthClient\Widget\AuthChoice |
| getClient() | Yiisoft\Yii\AuthClient\Widget\AuthChoice | |
| getClients() | Yiisoft\Yii\AuthClient\Widget\AuthChoice | |
| getId() | Yiisoft\Yii\AuthClient\Widget\AuthChoice | |
| init() | Initializes the widget. | Yiisoft\Yii\AuthClient\Widget\AuthChoice |
| render() | Runs the widget. | Yiisoft\Yii\AuthClient\Widget\AuthChoice |
| setClients() | Yiisoft\Yii\AuthClient\Widget\AuthChoice |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| renderMainContent() | Renders the main content, which includes all external services links. | Yiisoft\Yii\AuthClient\Widget\AuthChoice |
Method Details
| public mixed __construct ( Yiisoft\Yii\AuthClient\Collection $clientCollection, \Yiisoft\Router\UrlGeneratorInterface $urlGenerator, \Yiisoft\View\WebView $webView, \Yiisoft\Assets\AssetManager $assetManager ) | ||
| $clientCollection | Yiisoft\Yii\AuthClient\Collection | |
| $urlGenerator | \Yiisoft\Router\UrlGeneratorInterface | |
| $webView | \Yiisoft\View\WebView | |
| $assetManager | \Yiisoft\Assets\AssetManager | |
public function __construct(
Collection $clientCollection,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly WebView $webView,
private readonly AssetManager $assetManager,
) {
$this->clients = $clientCollection->getClients();
$this->init();
}
Note: No popup window and no route
| public string absoluteButtons ( \Psr\Http\Message\ServerRequestInterface $request, array $provider, string $name ) | ||
| $request | \Psr\Http\Message\ServerRequestInterface | |
| $provider | array | |
| $name | string | |
public function absoluteButtons(ServerRequestInterface $request, array $provider, string $name): string
{
foreach ($this->getClients() as $client) {
if ($name === $client->getName()) {
if (strlen($client->getClientId()) > 0) {
$clientAuthUrl = $client->buildAuthUrl($request, (array) $provider['params']);
return A::tag()
->addClass($client->getButtonClass())
->content(' ' . ucfirst((string) $provider['buttonName']))
->href($clientAuthUrl)
->id('btn-' . $name)
->render();
}
}
}
return '';
}
| public self authRoute ( string $authRoute ) | ||
| $authRoute | string | |
public function authRoute(string $authRoute): self
{
$this->authRoute = $authRoute;
return $this;
}
Note: Popup window with {$authRoute} e.g. 'auth/authclient'
| public string authRoutedButtons ( string $authRoute, array $provider, string $name ) | ||
| $authRoute | string | |
| $provider | array | |
| $name | string | |
public function authRoutedButtons(string $authRoute, array $provider, string $name): string
{
foreach ($this->getClients() as $client) {
if ($name === $client->getName()) {
if (strlen($client->getClientId()) > 0) {
$viewOptions = $client->getViewOptions();
$height = (string) $viewOptions['popupHeight'];
$width = (string) $viewOptions['popupWidth'];
$this->authRoute($authRoute);
return $this->clientLink($client, ' ' . ucfirst((string) $provider['buttonName']), [
'onclick' => "window.open(this.href, 'authPopup', 'width=" . $width . ',height=' . $height . "'); return false;",
'class' => $client->getButtonClass() ,
]);
}
}
}
return '';
}
Outputs client auth link.
| public string clientLink ( Yiisoft\Yii\AuthClient\OAuth2 $client, string $text = null, array $htmlOptions = [] ) | ||
| $client | Yiisoft\Yii\AuthClient\OAuth2 |
Extending from an auth client instance. |
| $text | string |
Link text, if not set - default value will be generated. |
| $htmlOptions | array |
Link HTML options. |
| return | string |
Generated HTML. |
|---|---|---|
| throws | Yiisoft\Yii\AuthClient\Exception\InvalidConfigException |
on wrong configuration. |
| throws | \Yiisoft\Definitions\Exception\InvalidConfigException | |
public function clientLink(OAuth2 $client, string $text = null, array $htmlOptions = []): string
{
$viewOptions = $client->getViewOptions();
if (empty($viewOptions['widget'])) {
if ($text === null) {
$text = Html::tag('span', '', ['class' => 'auth-icon ' . $client->getName()])->render();
}
if (!isset($htmlOptions['class'])) {
$htmlOptions['class'] = $client->getName();
}
if (!isset($htmlOptions['title'])) {
$htmlOptions['title'] = $client->getTitle();
}
Html::addCssClass($htmlOptions, ['widget' => 'auth-link']);
if ($this->popupMode) {
if (isset($viewOptions['popupWidth'])) {
/**
* @var int $viewOptions['popupWidth']
* @var int $htmlOptions['data-popup-width']
*/
$htmlOptions['data-popup-width'] = $viewOptions['popupWidth'];
}
if (isset($viewOptions['popupHeight'])) {
/**
* @var int $viewOptions['popupHeight']
* @var int $htmlOptions['data-popup-height']
*/
$htmlOptions['data-popup-height'] = $viewOptions['popupHeight'];
}
}
return Html::a($text, $this->createClientUrl($client), $htmlOptions)->render();
}
$widgetConfig = (array)$viewOptions['widget'];
if (!isset($widgetConfig['class'])) {
throw new InvalidConfigException('Widget config "class" parameter is missing');
}
/* @var $widgetClass Widget */
$widgetClass = $widgetConfig['class'];
/**
* @psalm-suppress MixedArgument $widgetClass
*/
if (!is_subclass_of($widgetClass, AuthChoiceItem::class)) {
throw new InvalidConfigException('Item widget class must be subclass of "' . AuthChoiceItem::class . '"');
}
unset($widgetConfig['class']);
$widgetConfig['client'] = $client;
$widgetConfig['authChoice'] = $this;
return $widgetClass::widget($widgetConfig)->render();
}
Composes client auth URL.
| public string createClientUrl ( Yiisoft\Yii\AuthClient\AuthClientInterface $client ) | ||
| $client | Yiisoft\Yii\AuthClient\AuthClientInterface |
External auth client instance. |
| return | string |
Auth URL. |
|---|---|---|
public function createClientUrl($client): string
{
$this->autoRender = false;
$params = [];
$params[$this->clientIdGetParamName] = $client->getName();
return $this->urlGenerator->generate($this->authRoute, $params);
}
| public Yiisoft\Yii\AuthClient\OAuth2 getClient ( string $name ) | ||
| $name | string | |
public function getClient(string $name): OAuth2
{
$clients = array_filter(
$this->getClients(),
fn($client) => $client->getName() === $name
);
$client = end($clients);
if ($client === false) {
throw new InvalidConfigException("OAuth2 client with name '{$name}' not found.");
}
return $client;
}
Initializes the widget.
| public void init ( ) |
public function init(): void
{
if ($this->popupMode) {
$this->assetManager->register(AuthChoiceAsset::class);
if (empty($this->clientOptions)) {
$options = '';
} else {
$options = Json::htmlEncode($this->clientOptions);
}
$this->webView->registerJs("
const el = document.getElementById('" . $this->getId() . "');
if (el && typeof authchoice === 'function') {
authchoice(el, {$options});
}
");
} else {
$this->assetManager->register(AuthChoiceStyleAsset::class);
}
$this->options['id'] = $this->getId();
// This next line can cause header related issues
echo Html::tag('div', '', $this->options)->open();
}
Runs the widget.
| public string render ( ) | ||
| return | string |
Rendered HTML. |
|---|---|---|
| throws | \Yiisoft\Definitions\Exception\InvalidConfigException | |
#[\Override]
public function render(): string
{
$content = '';
if ($this->autoRender) {
$content .= $this->renderMainContent();
}
$content .= Html::tag('div')->close();
return $content;
}
Renders the main content, which includes all external services links.
| protected string renderMainContent ( ) | ||
| return | string |
Generated HTML. |
|---|---|---|
| throws | Yiisoft\Yii\AuthClient\Exception\InvalidConfigException | |
| throws | \Yiisoft\Definitions\Exception\InvalidConfigException | |
protected function renderMainContent(): string
{
$items = [];
/**
* @var OAuth2 $externalService
*/
foreach ($this->getClients() as $externalService) {
$items[] = Html::tag('li', $this->clientLink($externalService));
}
return Html::tag('ul', implode('', $items), ['class' => 'auth-clients'])->render();
}
| public void setClients ( Yiisoft\Yii\AuthClient\OAuth2[] $clients ) | ||
| $clients | Yiisoft\Yii\AuthClient\OAuth2[] | |
public function setClients(array $clients): void
{
$this->clients = $clients;
}
Signup or Login in order to comment.