`protected/extensions/eauth`
.`protected/extensions`
directory.`main.php`
you need to add: 'import'=>array( 'ext.eoauth.*', 'ext.eoauth.lib.*', 'ext.lightopenid.*', 'ext.eauth.services.*', ), 'components'=>array( 'loid' => array( 'class' => 'ext.lightopenid.loid', ), 'eauth' => array( 'class' => 'ext.eauth.EAuth', 'popup' => true, // Use the popup window instead of redirecting. 'services' => array( // You can change the providers and their classes. 'google' => array( 'class' => 'GoogleOpenIDService', ), 'yandex' => array( 'class' => 'YandexOpenIDService', ), 'twitter' => array( 'class' => 'TwitterOAuthService', 'key' => '...', 'secret' => '...', ), 'facebook' => array( 'class' => 'FacebookOAuthService', 'client_id' => '...', 'client_secret' => '...', ), 'vkontakte' => array( 'class' => 'VKontakteOAuthService', 'client_id' => '...', 'client_secret' => '...', ), 'mailru' => array( 'class' => 'MailruOAuthService', 'client_id' => '...', 'client_secret' => '...', ), ), ), ),
popup
and services
. The popup
parameter is responsible for using the popup window for authorization, instead of redirecting to the provider's site. The services
parameter is a list of providers supported by our application. For each provider, you can specify your own class based on the provider’s base class. To obtain keys for OAuth providers, you must register your application with the appropriate provider.`yiic webapp create`
, and add the ability to `yiic webapp create`
via Google and Yandex (we will not connect OAuth providers, so as not to mess with the keys). View the finished demo .ServiceUserIdentity
, which is responsible for the input using our extension. Class Code: <?php class ServiceUserIdentity extends UserIdentity { const ERROR_NOT_AUTHENTICATED = 3; /** * @var EAuthServiceBase the authorization service instance. */ protected $service; /** * Constructor. * @param EAuthServiceBase $service the authorization service instance. */ public function __construct($service) { $this->service = $service; } /** * Authenticates a user based on {@link username}. * This method is required by {@link IUserIdentity}. * @return boolean whether authentication succeeds. */ public function authenticate() { if ($this->service->isAuthenticated) { $this->username = $this->service->getAttribute('name'); $this->setState('id', $this->service->id); $this->setState('name', $this->username); $this->setState('service', $this->service->serviceName); $this->errorCode = self::ERROR_NONE; } else { $this->errorCode = self::ERROR_NOT_AUTHENTICATED; } return !$this->errorCode; } }
id
and name
. In addition, each provider has its own identifier, which is contained in the serviceName
property. In the ServiceUserIdentity
class ServiceUserIdentity
we store these attributes in the session (and in the cookie) of the current user.SiteContoller
`site/login`
. Add the following code to the beginning of the action: public function actionLogin() { $service = Yii::app()->request->getQuery('service'); if (isset($service)) { $authIdentity = Yii::app()->eauth->getIdentity($service); $authIdentity->redirectUrl = Yii::app()->user->returnUrl; $authIdentity->cancelUrl = $this->createAbsoluteUrl('site/login'); if ($authIdentity->authenticate()) { $identity = new ServiceUserIdentity($authIdentity); // if ($identity->authenticate()) { Yii::app()->user->login($identity); // popup $authIdentity->redirect(); } else { // popup cancelUrl $authIdentity->cancel(); } } // - , $this->redirect(array('site/login')); } // ... }
$_GET['service']
variable. If there is such a variable, then create an instance of the provider class and configure paths for redirecting and canceling authorization. Then we call the `$authIdentity->authenticate()`
method, which does all the magic for us. The `$authIdentity->redirect();`
and `$authIdentity->cancel();`
required for the popup window to close properly if it is used.`protected/views/site/login.php`
<h2>Do you already have an account on one of these sites? Click the logo to log in with it here:</h2> <?php Yii::app()->eauth->renderWidget(); ?>
`protected/extensions/eauth/views/auth.php`
`[theme_name]/views/EAuthWidget/auth.php`
.redirectUrl
page (in the example, this is Yii::app()->user->returnUrl
). If the user clicks No, thanks
, then the popup window will simply be closed.{{users}}
table: service
and identity
. The first field is the name of the authorization service (the serviceName
property). The second is the unique identifier of the user on this service ( id
property);Source: https://habr.com/ru/post/129804/
All Articles