Class yii\authclient\widgets\AuthChoice
Inheritance | yii\authclient\widgets\AuthChoice » yii\base\Widget |
---|---|
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-authclient/blob/master/widgets/AuthChoice.php |
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 yii\authclient\Collection among application components to get auth clients information.
Example:
<?= yii\authclient\widgets\AuthChoice::widget([
'baseAuthUrl' => ['site/auth']
]); ?>
You can customize the widget appearance by using begin() and end() syntax along with using method clientLink() or createClientUrl(). For example:
<?php
use yii\authclient\widgets\AuthChoice;
?>
<?php $authAuthChoice = AuthChoice::begin([
'baseAuthUrl' => ['site/auth']
]); ?>
<ul>
<?php foreach ($authAuthChoice->getClients() as $client): ?>
<li><?php $authAuthChoice->clientLink($client) ?></li>
<?php endforeach; ?>
</ul>
<?php AuthChoice::end(); ?>
This widget supports following keys for yii\authclient\ClientInterface::getViewOptions() result:
- popupWidth - integer width of the popup window in pixels.
- popupHeight - integer height of the popup window in pixels.
- widget - configuration for the widget, which should be used to render a client link; such widget should be a subclass of yii\authclient\widgets\AuthChoiceItem.
See also yii\authclient\AuthAction.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$autoRender | boolean | Indicates if widget content, should be rendered automatically. | yii\authclient\widgets\AuthChoice |
$baseAuthUrl | array | Base auth URL configuration. | yii\authclient\widgets\AuthChoice |
$clientCollection | string | Name of the auth client collection application component. | yii\authclient\widgets\AuthChoice |
$clientIdGetParamName | string | Name of the GET param , which should be used to passed auth client id to URL defined by $baseAuthUrl. | yii\authclient\widgets\AuthChoice |
$clientOptions | array | Additional options to be passed to the underlying JS plugin. | yii\authclient\widgets\AuthChoice |
$clients | yii\authclient\ClientInterface[] | Auth providers. | yii\authclient\widgets\AuthChoice |
$options | array | The HTML attributes that should be rendered in the div HTML tag representing the container element. | yii\authclient\widgets\AuthChoice |
$popupMode | boolean | Indicates if popup window should be used instead of direct links. | yii\authclient\widgets\AuthChoice |
Public Methods
Method | Description | Defined By |
---|---|---|
clientLink() | Outputs client auth link. | yii\authclient\widgets\AuthChoice |
createClientUrl() | Composes client auth URL. | yii\authclient\widgets\AuthChoice |
getBaseAuthUrl() | yii\authclient\widgets\AuthChoice | |
getClients() | yii\authclient\widgets\AuthChoice | |
init() | Initializes the widget. | yii\authclient\widgets\AuthChoice |
run() | Runs the widget. | yii\authclient\widgets\AuthChoice |
setBaseAuthUrl() | yii\authclient\widgets\AuthChoice | |
setClients() | yii\authclient\widgets\AuthChoice |
Protected Methods
Method | Description | Defined By |
---|---|---|
defaultBaseAuthUrl() | Composes default base auth URL configuration. | yii\authclient\widgets\AuthChoice |
defaultClients() | Returns default auth clients list. | yii\authclient\widgets\AuthChoice |
renderMainContent() | Renders the main content, which includes all external services links. | yii\authclient\widgets\AuthChoice |
Property Details
Indicates if widget content, should be rendered automatically. Note: this value automatically set to 'false' at the first call of createClientUrl()
Base auth URL configuration. This property is read-only.
Name of the auth client collection application component. This component will be used to fetch services value if it is not set.
Name of the GET param , which should be used to passed auth client id to URL defined by $baseAuthUrl.
Additional options to be passed to the underlying JS plugin.
Auth providers. This property is read-only.
The HTML attributes that should be rendered in the div HTML tag representing the container element.
See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
Indicates if popup window should be used instead of direct links.
Method Details
Outputs client auth link.
public void clientLink ( $client, $text = null, array $htmlOptions = [] ) | ||
$client | yii\authclient\ClientInterface |
External auth client instance. |
$text | string |
Link text, if not set - default value will be generated. |
$htmlOptions | array |
Link HTML options. |
throws | \yii\base\InvalidConfigException |
on wrong configuration. |
---|
public function clientLink($client, $text = null, array $htmlOptions = [])
{
$viewOptions = $client->getViewOptions();
if (empty($viewOptions['widget'])) {
if ($text === null) {
$text = Html::tag('span', '', ['class' => 'auth-icon ' . $client->getName()]);
$text .= Html::tag('span', $client->getTitle(), ['class' => 'auth-title']);
}
if (!array_key_exists('class', $htmlOptions)) {
$htmlOptions['class'] = $client->getName();
}
Html::addCssClass($htmlOptions, ['widget' => 'auth-link']);
if ($this->popupMode) {
if (isset($viewOptions['popupWidth'])) {
$htmlOptions['data-popup-width'] = $viewOptions['popupWidth'];
}
if (isset($viewOptions['popupHeight'])) {
$htmlOptions['data-popup-height'] = $viewOptions['popupHeight'];
}
}
echo Html::a($text, $this->createClientUrl($client), $htmlOptions);
} else {
$widgetConfig = $viewOptions['widget'];
if (!isset($widgetConfig['class'])) {
throw new InvalidConfigException('Widget config "class" parameter is missing');
}
/* @var $widgetClass Widget */
$widgetClass = $widgetConfig['class'];
if (!(is_subclass_of($widgetClass, AuthChoiceItem::className()))) {
throw new InvalidConfigException('Item widget class must be subclass of "' . AuthChoiceItem::className() . '"');
}
unset($widgetConfig['class']);
$widgetConfig['client'] = $client;
$widgetConfig['authChoice'] = $this;
echo $widgetClass::widget($widgetConfig);
}
}
Composes client auth URL.
public string createClientUrl ( $provider ) | ||
$provider | yii\authclient\ClientInterface |
External auth client instance. |
return | string |
Auth URL. |
---|
public function createClientUrl($provider)
{
$this->autoRender = false;
$url = $this->getBaseAuthUrl();
$url[$this->clientIdGetParamName] = $provider->getId();
return Url::to($url);
}
Composes default base auth URL configuration.
protected array defaultBaseAuthUrl ( ) | ||
return | array |
Base auth URL configuration. |
---|
protected function defaultBaseAuthUrl()
{
$baseAuthUrl = [
Yii::$app->controller->getRoute()
];
$params = $_GET;
unset($params[$this->clientIdGetParamName]);
$baseAuthUrl = array_merge($baseAuthUrl, $params);
return $baseAuthUrl;
}
Returns default auth clients list.
protected yii\authclient\ClientInterface[] defaultClients ( ) | ||
return | yii\authclient\ClientInterface[] |
Auth clients list. |
---|
protected function defaultClients()
{
/* @var $collection \yii\authclient\Collection */
$collection = Yii::$app->get($this->clientCollection);
return $collection->getClients();
}
public array getBaseAuthUrl ( ) | ||
return | array |
Base auth URL configuration. |
---|
public function getBaseAuthUrl()
{
if (!is_array($this->_baseAuthUrl)) {
$this->_baseAuthUrl = $this->defaultBaseAuthUrl();
}
return $this->_baseAuthUrl;
}
public yii\authclient\ClientInterface[] getClients ( ) | ||
return | yii\authclient\ClientInterface[] |
Auth providers |
---|
public function getClients()
{
if ($this->_clients === null) {
$this->_clients = $this->defaultClients();
}
return $this->_clients;
}
Initializes the widget.
public void init ( ) |
public function init()
{
$view = Yii::$app->getView();
if ($this->popupMode) {
AuthChoiceAsset::register($view);
if (empty($this->clientOptions)) {
$options = '';
} else {
$options = Json::htmlEncode($this->clientOptions);
}
$view->registerJs("\$('#" . $this->getId() . "').authchoice({$options});");
} else {
AuthChoiceStyleAsset::register($view);
}
$this->options['id'] = $this->getId();
echo Html::beginTag('div', $this->options);
}
Renders the main content, which includes all external services links.
protected void renderMainContent ( ) |
protected function renderMainContent()
{
echo Html::beginTag('ul', ['class' => 'auth-clients clear']);
foreach ($this->getClients() as $externalService) {
echo Html::beginTag('li', ['class' => 'auth-client']);
$this->clientLink($externalService);
echo Html::endTag('li');
}
echo Html::endTag('ul');
}
Runs the widget.
public void run ( ) |
public function run()
{
if ($this->autoRender) {
$this->renderMainContent();
}
echo Html::endTag('div');
}
public void setBaseAuthUrl ( array $baseAuthUrl ) | ||
$baseAuthUrl | array |
Base auth URL configuration. |
public function setBaseAuthUrl(array $baseAuthUrl)
{
$this->_baseAuthUrl = $baseAuthUrl;
}
public void setClients ( array $clients ) | ||
$clients | yii\authclient\ClientInterface[] |
Auth providers |
public function setClients(array $clients)
{
$this->_clients = $clients;
}