0 follower

Final Class Yiisoft\Yii\AuthClient\Widget\AuthChoice

InheritanceYiisoft\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 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 \Yiisoft\Yii\AuthClient\Widget\begin() and end() syntax along with using method clientLink() or 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 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 Yiisoft\Yii\AuthClient\Widget\AuthChoiceItem.

See also Yiisoft\Yii\AuthClient\AuthAction.

Protected Methods

Hide inherited methods

Method Description Defined By
renderMainContent() Renders the main content, which includes all external services links. Yiisoft\Yii\AuthClient\Widget\AuthChoice

Method Details

Hide inherited methods

__construct() public method

public __construct( Yiisoft\Yii\AuthClient\Collection $clientCollection, \Yiisoft\Router\UrlGeneratorInterface $urlGenerator, \Yiisoft\View\WebView $webView, \Yiisoft\Assets\AssetManager $assetManager ): mixed
$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();
}

            
absoluteButtons() public method

Note: No popup window and no route

public absoluteButtons( \Psr\Http\Message\ServerRequestInterface $request, array $provider, string $name ): string
$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 '';
}

            
authRoute() public method

public authRoute( string $authRoute ): self
$authRoute string

                public function authRoute(string $authRoute): self
{
    $this->authRoute = $authRoute;
    return $this;
}

            
authRoutedButtons() public method

Note: Popup window with {$authRoute} e.g. 'auth/authclient'

public authRoutedButtons( string $authRoute, array $provider, string $name ): string
$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 '';
}

            
clientLink() public method

Outputs client auth link.

public clientLink( Yiisoft\Yii\AuthClient\OAuth2 $client, string $text null, array $htmlOptions = [] ): string
$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

createClientUrl() public method

Composes client auth URL.

public createClientUrl( Yiisoft\Yii\AuthClient\AuthClientInterface $client ): string
$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);
}

            
getClient() public method

public getClient( string $name ): Yiisoft\Yii\AuthClient\OAuth2
$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;
}

            
getClients() public method

public getClients( ): array

                public function getClients(): array
{
    return $this->clients;
}

            
getId() public method

public getId( ): string

                public function getId(): string
{
    return 'yii-auth-client';
}

            
init() public method

Initializes the widget.

public init( ): void

                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();
}

            
render() public method

Runs the widget.

public render( ): string
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;
}

            
renderMainContent() protected method

Renders the main content, which includes all external services links.

protected renderMainContent( ): string
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();
}

            
setClients() public method

public setClients( Yiisoft\Yii\AuthClient\OAuth2[] $clients ): void
$clients Yiisoft\Yii\AuthClient\OAuth2[]

                public function setClients(array $clients): void
{
    $this->clients = $clients;
}