fixes + add events documentation

This commit is contained in:
Antonio Ramirez
2017-06-13 00:01:30 +02:00
parent 9f4a29941c
commit 692358adb6
6 changed files with 107 additions and 8 deletions

View File

@ -43,8 +43,8 @@ Here is an example of setting an event for the `RecoveryController`:
<?php <?php
// events.php file // events.php file
use Da\Controller\RecoveryController; use Da\User\Controller\RecoveryController;
use Da\Event\FormEvent; use Da\User\Event\FormEvent;
use yii\base\Event; use yii\base\Event;
Event::on(RecoveryController::class, FormEvent::EVENT_BEFORE_REQUEST, function (FormEvent $event) { Event::on(RecoveryController::class, FormEvent::EVENT_BEFORE_REQUEST, function (FormEvent $event) {

View File

@ -25,8 +25,8 @@ Check the following code as an example:
<?php <?php
// events.php file // events.php file
use Da\Controller\RecoveryController; use Da\User\Controller\RecoveryController;
use Da\Event\ResetPasswordEvent; use Da\User\Event\ResetPasswordEvent;
use yii\base\Event; use yii\base\Event;
Event::on( Event::on(
@ -52,3 +52,5 @@ Event::on(
}); });
``` ```
© [2amigos](http://www.2amigos.us/) 2013-2017

View File

@ -0,0 +1,48 @@
Social Network Auth Events
==========================
The following is the list of the social network authentication events and where they happen:
On Controllers
--------------
- **SecurityController**
- **SocialNetworkAuthEvent::EVENT_BEFORE_CONNECT**: Occurs before a user's social network has been connected
- **SocialNetworkAuthEvent::EVENT_AFTER_CONNECT**: Occurs after a user's social network has been connected
- **SocialNetworkAuthEvent::EVENT_BEFORE_AUTHENTICATE**: Occurs before a user is authenticated via social network
- **SocialNetworkAuthEvent::EVENT_AFTER_AUTHENTICATE**: Occurs after a a user is authenticated via social network
How to Work With Social Network Auth Events
-------------------------------------------
This event when triggered will contain a `Da\User\Model\SocialNetworkAccount` model instance and also the client used
to authenticate throughout a social network. For example:
```php
<?php
// events.php file
use Da\User\Controller\SecurityController;
use Da\User\Event\SocialNetworkAuthEvent;
use yii\base\Event;
Event::on(
SecurityController::class,
SocialNetworkAuthEvent::EVENT_BEFORE_CONNECT,
function (SocialNetworkAuthEvent $event) {
$client = $event->getClient(); // $client is one of the Da\User\AuthClient\ clients
$account = $event->getAccount(); // $account is a Da\User\Model\SocialNetworkAccount
// ... your logic here
});
```
> For further information about how to authenticate via social networks and the available clients, please visit the
> [guide](../helpful-guides/social-network-authentication.md)
© [2amigos](http://www.2amigos.us/) 2013-2017

View File

@ -0,0 +1,47 @@
Social Network Connect Events
=============================
The following is the list of the social network connection events and where they happen:
On Controllers
--------------
- **RegistrationController**
- **SocialNetworkConnectEvent::EVENT_BEFORE_CONNECT**: Occurs before a user's social network has been connected
- **SocialNetworkConnectEvent::EVENT_AFTER_CONNECT**: Occurs after a user's social network has been connected
- **SettingsController**
- **SocialNetworkConnectEvent::EVENT_BEFORE_DISCONNECT**: Occurs before a user is disconnecting a social network
- **SocialNetworkConnectEvent::EVENT_AFTER_DISCONNECT**: Occurs after a a user is disconnecting a social network
How to Work With Social Network Connect Events
----------------------------------------------
This event when triggered will contain a `Da\User\Model\User` model instance and also a
`Da\User\Model\SocialNetworkAccount` instance. For example:
```php
<?php
// events.php file
use Da\User\Controller\RegistrationController;
use Da\User\Event\SocialNetworkConnectEvent;
use yii\base\Event;
Event::on(
RegistrationController::class,
SocialNetworkConnectEvent::EVENT_BEFORE_CONNECT,
function (SocialNetworkConnectEvent $event) {
$user = $event->getUser(); // $token is a Da\User\Model\User instance
$account = $event->getAccount(); // $account is a Da\User\Model\SocialNetworkAccount
// ... your logic here
});
```
© [2amigos](http://www.2amigos.us/) 2013-2017

View File

@ -68,9 +68,9 @@ Here is an example of setting an event for the `AdminController` and the `User`
<?php <?php
// events.php file // events.php file
use Da\Controller\AdminController; use Da\User\Controller\AdminController;
use Da\Event\UserEvent; use Da\User\Event\UserEvent;
use Da\Model\User; use Da\User\Model\User;
use yii\base\Event; use yii\base\Event;
// This will happen at the controller's level // This will happen at the controller's level

View File

@ -64,9 +64,11 @@ class SocialNetworkAuthenticateService implements ServiceInterface
return false; return false;
} }
} }
$event = Yii::createObject(SocialNetworkAuthEvent::class, [$this->client]);
$event = Yii::createObject(SocialNetworkAuthEvent::class, [$account, $this->client]);
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_BEFORE_AUTHENTICATE, $event); $this->controller->trigger(SocialNetworkAuthEvent::EVENT_BEFORE_AUTHENTICATE, $event);
if ($account->user instanceof User) { if ($account->user instanceof User) {
if ($account->user->getIsBlocked()) { if ($account->user->getIsBlocked()) {
Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Your account has been blocked.')); Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Your account has been blocked.'));