mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-23 04:00:23 +08:00
Widget "yii\authclient\widgets\Choice" fixed.
This commit is contained in:
@ -49,4 +49,9 @@ interface ClientInterface
|
|||||||
* @return array list of user attributes
|
* @return array list of user attributes
|
||||||
*/
|
*/
|
||||||
public function getUserAttributes();
|
public function getUserAttributes();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array view options in format: optionName => optionValue
|
||||||
|
*/
|
||||||
|
public function getViewOptions();
|
||||||
}
|
}
|
@ -39,6 +39,10 @@ trait ClientTrait
|
|||||||
* @var array authenticated user attributes.
|
* @var array authenticated user attributes.
|
||||||
*/
|
*/
|
||||||
private $_userAttributes;
|
private $_userAttributes;
|
||||||
|
/**
|
||||||
|
* @var array view options in format: optionName => optionValue
|
||||||
|
*/
|
||||||
|
private $_viewOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $id service id.
|
* @param string $id service id.
|
||||||
@ -111,11 +115,30 @@ trait ClientTrait
|
|||||||
/**
|
/**
|
||||||
* @param array $userAttributes list of user attributes
|
* @param array $userAttributes list of user attributes
|
||||||
*/
|
*/
|
||||||
public function setUserAttributes(array $userAttributes)
|
public function setUserAttributes($userAttributes)
|
||||||
{
|
{
|
||||||
$this->_userAttributes = $userAttributes;
|
$this->_userAttributes = $userAttributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $viewOptions view options in format: optionName => optionValue
|
||||||
|
*/
|
||||||
|
public function setViewOptions($viewOptions)
|
||||||
|
{
|
||||||
|
$this->_viewOptions = $viewOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array view options in format: optionName => optionValue
|
||||||
|
*/
|
||||||
|
public function getViewOptions()
|
||||||
|
{
|
||||||
|
if ($this->_viewOptions === null) {
|
||||||
|
$this->_viewOptions = $this->defaultViewOptions();
|
||||||
|
}
|
||||||
|
return $this->_viewOptions;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates service name.
|
* Generates service name.
|
||||||
* @return string service name.
|
* @return string service name.
|
||||||
@ -142,4 +165,14 @@ trait ClientTrait
|
|||||||
{
|
{
|
||||||
throw new NotSupportedException('Method "' . get_class($this) . '::' . __FUNCTION__ . '" not implemented.');
|
throw new NotSupportedException('Method "' . get_class($this) . '::' . __FUNCTION__ . '" not implemented.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the default [[viewOptions]] value.
|
||||||
|
* Particular client may override this method in order to provide specific default view options.
|
||||||
|
* @return array list of default [[viewOptions]]
|
||||||
|
*/
|
||||||
|
protected function defaultViewOptions()
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
}
|
}
|
@ -17,6 +17,9 @@ use yii\authclient\OpenId;
|
|||||||
*/
|
*/
|
||||||
class GoogleOpenId extends OpenId
|
class GoogleOpenId extends OpenId
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
parent::init();
|
parent::init();
|
||||||
@ -28,4 +31,15 @@ class GoogleOpenId extends OpenId
|
|||||||
'pref/language',
|
'pref/language',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
protected function defaultViewOptions()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'popupWidth' => 880,
|
||||||
|
'popupHeight' => 520,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
@ -17,6 +17,9 @@ use yii\authclient\OpenId;
|
|||||||
*/
|
*/
|
||||||
class YandexOpenId extends OpenId
|
class YandexOpenId extends OpenId
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
parent::init();
|
parent::init();
|
||||||
@ -26,4 +29,15 @@ class YandexOpenId extends OpenId
|
|||||||
'contact/email',
|
'contact/email',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
protected function defaultViewOptions()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'popupWidth' => 900,
|
||||||
|
'popupHeight' => 550,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
@ -10,12 +10,12 @@ namespace yii\authclient\widgets;
|
|||||||
use yii\base\Widget;
|
use yii\base\Widget;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\helpers\Html;
|
use yii\helpers\Html;
|
||||||
use yii\authclient\provider\ProviderInterface;
|
use yii\authclient\ClientInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Choice
|
* Class Choice
|
||||||
*
|
*
|
||||||
* @property ProviderInterface[] $providers auth providers list.
|
* @property ClientInterface[] $providers auth providers list.
|
||||||
* @property array $baseAuthUrl configuration for the external services base authentication URL.
|
* @property array $baseAuthUrl configuration for the external services base authentication URL.
|
||||||
*
|
*
|
||||||
* @author Paul Klimov <klimov.paul@gmail.com>
|
* @author Paul Klimov <klimov.paul@gmail.com>
|
||||||
@ -24,28 +24,28 @@ use yii\authclient\provider\ProviderInterface;
|
|||||||
class Choice extends Widget
|
class Choice extends Widget
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var ProviderInterface[] auth providers list.
|
* @var ClientInterface[] auth providers list.
|
||||||
*/
|
*/
|
||||||
private $_providers;
|
private $_clients;
|
||||||
/**
|
/**
|
||||||
* @var string name of the auth provider collection application component.
|
* @var string name of the auth client collection application component.
|
||||||
* This component will be used to fetch {@link services} value if it is not set.
|
* This component will be used to fetch {@link services} value if it is not set.
|
||||||
*/
|
*/
|
||||||
public $providerCollection;
|
public $clientCollection = 'auth';
|
||||||
/**
|
/**
|
||||||
* @var array configuration for the external services base authentication URL.
|
* @var array configuration for the external clients base authentication URL.
|
||||||
*/
|
*/
|
||||||
private $_baseAuthUrl;
|
private $_baseAuthUrl;
|
||||||
/**
|
/**
|
||||||
* @var string name of the GET param , which should be used to passed auth provider id to URL
|
* @var string name of the GET param , which should be used to passed auth client id to URL
|
||||||
* defined by {@link baseAuthUrl}.
|
* defined by {@link baseAuthUrl}.
|
||||||
*/
|
*/
|
||||||
public $providerIdGetParamName = 'provider';
|
public $clientIdGetParamName = 'client_id';
|
||||||
/**
|
/**
|
||||||
* @var array the HTML attributes that should be rendered in the div HTML tag representing the container element.
|
* @var array the HTML attributes that should be rendered in the div HTML tag representing the container element.
|
||||||
*/
|
*/
|
||||||
public $mainContainerHtmlOptions = [
|
public $mainContainerHtmlOptions = [
|
||||||
'class' => 'services'
|
'class' => 'auth-clients'
|
||||||
];
|
];
|
||||||
/**
|
/**
|
||||||
* @var boolean indicates if popup window should be used instead of direct links.
|
* @var boolean indicates if popup window should be used instead of direct links.
|
||||||
@ -58,22 +58,22 @@ class Choice extends Widget
|
|||||||
public $autoRender = true;
|
public $autoRender = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ProviderInterface[] $providers auth providers
|
* @param ClientInterface[] $clients auth providers
|
||||||
*/
|
*/
|
||||||
public function setProviders(array $providers)
|
public function setClients(array $clients)
|
||||||
{
|
{
|
||||||
$this->_providers = $providers;
|
$this->_clients = $clients;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ProviderInterface[] auth providers
|
* @return ClientInterface[] auth providers
|
||||||
*/
|
*/
|
||||||
public function getProviders()
|
public function getClients()
|
||||||
{
|
{
|
||||||
if ($this->_providers === null) {
|
if ($this->_clients === null) {
|
||||||
$this->_providers = $this->defaultProviders();
|
$this->_clients = $this->defaultClients();
|
||||||
}
|
}
|
||||||
return $this->_providers;
|
return $this->_clients;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -96,14 +96,14 @@ class Choice extends Widget
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns default auth providers list.
|
* Returns default auth clients list.
|
||||||
* @return ProviderInterface[] auth providers list.
|
* @return ClientInterface[] auth clients list.
|
||||||
*/
|
*/
|
||||||
protected function defaultProviders()
|
protected function defaultClients()
|
||||||
{
|
{
|
||||||
/** @var $collection \yii\authclient\provider\Collection */
|
/** @var $collection \yii\authclient\Collection */
|
||||||
$collection = Yii::$app->getComponent($this->providerCollection);
|
$collection = Yii::$app->getComponent($this->clientCollection);
|
||||||
return $collection->getProviders();
|
return $collection->getClients();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -116,47 +116,48 @@ class Choice extends Widget
|
|||||||
Yii::$app->controller->getRoute()
|
Yii::$app->controller->getRoute()
|
||||||
];
|
];
|
||||||
$params = $_GET;
|
$params = $_GET;
|
||||||
unset($params[$this->providerIdGetParamName]);
|
unset($params[$this->clientIdGetParamName]);
|
||||||
$baseAuthUrl = array_merge($baseAuthUrl, $params);
|
$baseAuthUrl = array_merge($baseAuthUrl, $params);
|
||||||
return $baseAuthUrl;
|
return $baseAuthUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Outputs external service auth link.
|
* Outputs external service auth link.
|
||||||
* @param ProviderInterface $service external auth service instance.
|
* @param ClientInterface $client external auth client instance.
|
||||||
* @param string $text link text, if not set - default value will be generated.
|
* @param string $text link text, if not set - default value will be generated.
|
||||||
* @param array $htmlOptions link HTML options.
|
* @param array $htmlOptions link HTML options.
|
||||||
*/
|
*/
|
||||||
public function providerLink($service, $text = null, array $htmlOptions = [])
|
public function providerLink($client, $text = null, array $htmlOptions = [])
|
||||||
{
|
{
|
||||||
if ($text === null) {
|
if ($text === null) {
|
||||||
$text = Html::tag('span', ['class' => 'auth-icon ' . $service->getName()], '');
|
$text = Html::tag('span', '', ['class' => 'auth-icon ' . $client->getName()]);
|
||||||
$text .= Html::tag('span', ['class' => 'auth-title'], $service->getTitle());
|
$text .= Html::tag('span', $client->getTitle(), ['class' => 'auth-title']);
|
||||||
}
|
}
|
||||||
if (!array_key_exists('class', $htmlOptions)) {
|
if (!array_key_exists('class', $htmlOptions)) {
|
||||||
$htmlOptions['class'] = 'auth-link ' . $service->getName();
|
$htmlOptions['class'] = 'auth-link ' . $client->getName();
|
||||||
}
|
}
|
||||||
if ($this->popupMode) {
|
if ($this->popupMode) {
|
||||||
if (isset($service->popupWidth)) {
|
$viewOptions = $client->getViewOptions();
|
||||||
$htmlOptions['data-popup-width'] = $service->popupWidth;
|
if (isset($viewOptions['popupWidth'])) {
|
||||||
|
$htmlOptions['data-popup-width'] = $viewOptions['popupWidth'];
|
||||||
}
|
}
|
||||||
if (isset($service->popupHeight)) {
|
if (isset($viewOptions['popupHeight'])) {
|
||||||
$htmlOptions['data-popup-height'] = $service->popupHeight;
|
$htmlOptions['data-popup-height'] = $viewOptions['popupHeight'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
echo Html::a($text, $this->createProviderUrl($service), $htmlOptions);
|
echo Html::a($text, $this->createProviderUrl($client), $htmlOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Composes external service auth URL.
|
* Composes external service auth URL.
|
||||||
* @param ProviderInterface $provider external auth service instance.
|
* @param ClientInterface $provider external auth service instance.
|
||||||
* @return string auth URL.
|
* @return string auth URL.
|
||||||
*/
|
*/
|
||||||
public function createProviderUrl($provider)
|
public function createProviderUrl($provider)
|
||||||
{
|
{
|
||||||
$this->autoRender = false;
|
$this->autoRender = false;
|
||||||
$url = $this->getBaseAuthUrl();
|
$url = $this->getBaseAuthUrl();
|
||||||
$url[$this->providerIdGetParamName] = $provider->getId();
|
$url[$this->clientIdGetParamName] = $provider->getId();
|
||||||
return Html::url($url);
|
return Html::url($url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,7 +167,7 @@ class Choice extends Widget
|
|||||||
protected function renderMainContent()
|
protected function renderMainContent()
|
||||||
{
|
{
|
||||||
echo Html::beginTag('ul', ['class' => 'auth-services clear']);
|
echo Html::beginTag('ul', ['class' => 'auth-services clear']);
|
||||||
foreach ($this->getProviders() as $externalService) {
|
foreach ($this->getClients() as $externalService) {
|
||||||
echo Html::beginTag('li', ['class' => 'auth-service']);
|
echo Html::beginTag('li', ['class' => 'auth-service']);
|
||||||
$this->providerLink($externalService);
|
$this->providerLink($externalService);
|
||||||
echo Html::endTag('li');
|
echo Html::endTag('li');
|
||||||
|
@ -26,33 +26,46 @@ class ClientTraitTest extends TestCase
|
|||||||
|
|
||||||
public function testSetGet()
|
public function testSetGet()
|
||||||
{
|
{
|
||||||
$provider = new Client();
|
$client = new Client();
|
||||||
|
|
||||||
$id = 'test_id';
|
$id = 'test_id';
|
||||||
$provider->setId($id);
|
$client->setId($id);
|
||||||
$this->assertEquals($id, $provider->getId(), 'Unable to setup id!');
|
$this->assertEquals($id, $client->getId(), 'Unable to setup id!');
|
||||||
|
|
||||||
$name = 'test_name';
|
$name = 'test_name';
|
||||||
$provider->setName($name);
|
$client->setName($name);
|
||||||
$this->assertEquals($name, $provider->getName(), 'Unable to setup name!');
|
$this->assertEquals($name, $client->getName(), 'Unable to setup name!');
|
||||||
|
|
||||||
$title = 'test_title';
|
$title = 'test_title';
|
||||||
$provider->setTitle($title);
|
$client->setTitle($title);
|
||||||
$this->assertEquals($title, $provider->getTitle(), 'Unable to setup title!');
|
$this->assertEquals($title, $client->getTitle(), 'Unable to setup title!');
|
||||||
|
|
||||||
|
$userAttributes = [
|
||||||
|
'attribute1' => 'value1',
|
||||||
|
'attribute2' => 'value2',
|
||||||
|
];
|
||||||
|
$client->setUserAttributes($userAttributes);
|
||||||
|
$this->assertEquals($userAttributes, $client->getUserAttributes(), 'Unable to setup user attributes!');
|
||||||
|
|
||||||
|
$viewOptions = [
|
||||||
|
'option1' => 'value1',
|
||||||
|
'option2' => 'value2',
|
||||||
|
];
|
||||||
|
$client->setViewOptions($viewOptions);
|
||||||
|
$this->assertEquals($viewOptions, $client->getViewOptions(), 'Unable to setup view options!');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetDescriptiveData()
|
public function testGetDefaults()
|
||||||
{
|
{
|
||||||
$provider = new Client();
|
$provider = new Client();
|
||||||
|
|
||||||
$this->assertNotEmpty($provider->getName(), 'Unable to get name!');
|
$this->assertNotEmpty($provider->getName(), 'Unable to get default name!');
|
||||||
$this->assertNotEmpty($provider->getTitle(), 'Unable to get title!');
|
$this->assertNotEmpty($provider->getTitle(), 'Unable to get default title!');
|
||||||
|
$this->assertNotNull($provider->getViewOptions(), 'Unable to get default view options!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Client extends Object implements ClientInterface
|
class Client extends Object implements ClientInterface
|
||||||
{
|
{
|
||||||
use ClientTrait;
|
use ClientTrait;
|
||||||
|
|
||||||
public function authenticate() {}
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user