added social connection action

This commit is contained in:
Antonio Ramirez
2016-12-09 16:41:25 +01:00
parent f6dcb21205
commit 3fe09e3eb3
9 changed files with 400 additions and 155 deletions

View File

@ -12,20 +12,39 @@ class ProfileController extends Controller
{
protected $profileQuery;
/**
* ProfileController constructor.
*
* @param string $id
* @param Module $module
* @param ProfileQuery $profileQuery
* @param array $config
*/
public function __construct($id, Module $module, ProfileQuery $profileQuery, array $config)
{
$this->profileQuery = $profileQuery;
parent::__construct($id, $module, $config);
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
['allow' => true, 'actions' => ['index'], 'roles' => ['@']],
['allow' => true, 'actions' => ['show'], 'roles' => ['?', '@']],
[
'allow' => true,
'actions' => ['index'],
'roles' => ['@']
],
[
'allow' => true,
'actions' => ['show'],
'roles' => ['?', '@']
],
],
],
];

View File

@ -0,0 +1,147 @@
<?php
namespace Da\User\Controller;
use Da\User\Contracts\AuthClientInterface;
use Da\User\Event\FormEvent;
use Da\User\Event\UserEvent;
use Da\User\Form\LoginForm;
use Da\User\Query\SocialNetworkAccountQuery;
use Da\User\Service\SocialNetworkAccountConnectService;
use Da\User\Service\SocialNetworkAuthenticateService;
use Da\User\Traits\ContainerTrait;
use yii\authclient\AuthAction;
use yii\authclient\ClientInterface;
use yii\base\Module;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\Controller;
use Yii;
class SecurityController extends Controller
{
use ContainerTrait;
protected $socialNetworkAccountQuery;
/**
* SecurityController constructor.
*
* @param string $id
* @param Module $module
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery
* @param array $config
*/
public function __construct(
$id,
Module $module,
SocialNetworkAccountQuery $socialNetworkAccountQuery,
array $config
) {
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
parent::__construct($id, $module, $config);
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['login', 'auth', 'blocked'],
'roles' => ['?']
],
[
'allow' => true,
'actions' => ['login', 'auth', 'logout'],
'roles' => ['@']
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'auth' => [
'class' => AuthAction::className(),
// if user is not logged in, will try to log him in, otherwise
// will try to connect social account to user.
'successCallback' => Yii::$app->user->isGuest
? [$this, 'authenticate']
: [$this, 'connect'],
],
];
}
public function actionLogin()
{
if (!Yii::$app->user->getIsGuest()) {
return $this->goHome();
}
/** @var LoginForm $form */
$form = $this->make(LoginForm::class);
/** @var FormEvent $event */
$event = $this->make(FormEvent::class, [$form]);
if ($form->load(Yii::$app->request->post())) {
$this->trigger(FormEvent::EVENT_BEFORE_LOGIN, $event);
if ($form->login()) {
$this->trigger(FormEvent::EVENT_AFTER_LOGIN, $event);
return $this->goBack();
}
}
return $this->render(
'login',
[
'model' => $form,
'module' => $this->module,
]
);
}
public function actionLogout()
{
$event = $this->make(UserEvent::class, [Yii::$app->getUser()->getIdentity()]);
$this->trigger(UserEvent::EVENT_BEFORE_LOGOUT, $event);
if (Yii::$app->getUser()->logout()) {
$this->trigger(UserEvent::EVENT_AFTER_LOGOUT, $event);
}
return $this->goHome();
}
public function authenticate(AuthClientInterface $client)
{
$this->make(SocialNetworkAuthenticateService::class, [$this, $this->action, $client])->run();
}
public function connect(AuthClientInterface $client) {
if (Yii::$app->user->isGuest) {
Yii::$app->session->setFlash('danger', Yii::t('user', 'Something went wrong'));
return;
}
$this->make(SocialNetworkAccountConnectService::class, [$this, $client])->run();
}
}

View File

@ -10,6 +10,8 @@ class FormEvent extends Event
const EVENT_AFTER_REQUEST = 'afterRequest';
const EVENT_BEFORE_RESEND = 'beforeResend';
const EVENT_AFTER_RESEND = 'afterResend';
const EVENT_BEFORE_LOGIN = 'beforeLogin';
const EVENT_AFTER_LOGIN = 'afterLogin';
protected $form;

View File

@ -8,6 +8,11 @@ use yii\base\Event;
class SocialNetworkAuthEvent extends Event
{
const EVENT_BEFORE_AUTHENTICATE = 'beforeAuthenticate';
const EVENT_AFTER_AUTHENTICATE = 'afterAuthenticate';
const EVENT_BEFORE_CONNECT = 'beforeConnect';
const EVENT_AFTER_CONNECT = 'afterConnect';
protected $client;
protected $account;

View File

@ -18,6 +18,8 @@ class UserEvent extends Event
const EVENT_AFTER_UNBLOCK = 'afterUnblock';
const EVENT_BEFORE_BLOCK = 'beforeBlock';
const EVENT_AFTER_BLOCK = 'afterBlock';
const EVENT_BEFORE_LOGOUT = 'beforeLogout';
const EVENT_AFTER_LOGOUT = 'afterLogout';
protected $user;

View File

@ -0,0 +1,91 @@
<?php
namespace Da\User\Service;
use Da\User\Contracts\AuthClientInterface;
use Da\User\Contracts\ServiceInterface;
use Da\User\Controller\SecurityController;
use Da\User\Event\SocialNetworkAuthEvent;
use Da\User\Model\SocialNetworkAccount;
use Da\User\Model\User;
use Da\User\Query\SocialNetworkAccountQuery;
use Da\User\Traits\ContainerTrait;
use Yii;
class SocialNetworkAccountConnectService implements ServiceInterface
{
use ContainerTrait;
protected $controller;
protected $client;
protected $socialNetworkAccountQuery;
/**
* SocialNetworkAccountUserLinkService constructor.
*
* @param SecurityController $controller
* @param AuthClientInterface $client
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery
*/
public function __construct(
SecurityController $controller,
AuthClientInterface $client,
SocialNetworkAccountQuery $socialNetworkAccountQuery
) {
$this->controller = $controller;
$this->client = $client;
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
}
public function run()
{
$account = $this->getSocialNetworkAccount();
$event = $this->make(SocialNetworkAuthEvent::class, [$account, $this->client]);
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_BEFORE_CONNECT, $event);
if ($account && $account->user === null) {
/** @var User $user */
$user = Yii::$app->user->identity;
$account->link('user', $user);
Yii::$app->session->setFlash('success', Yii::t('user', 'Your account has been connected'));
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_AFTER_CONNECT, $event);
return true;
} else {
Yii::$app->session->setFlash(
'danger',
Yii::t('user', 'This account has already been connected to another user')
);
}
return false;
}
protected function getSocialNetworkAccount()
{
$account = $this->socialNetworkAccountQuery->whereClient($this->client)->one();
if (null === $account) {
$data = $this->client->getUserAttributes();
$account = $this->make(
SocialNetworkAccount::class,
[
'provider' => $this->client->getId(),
'client_id' => $data['id'],
'data' => json_encode($data)
]
);
if ($account->save(false)) {
return $account;
}
}
return false;
}
}

View File

@ -1,85 +0,0 @@
<?php
namespace Da\User\Service;
use Da\User\Contracts\AuthClientInterface;
use Da\User\Contracts\ServiceInterface;
use Da\User\Model\SocialNetworkAccount;
use Da\User\Model\User;
use Da\User\Query\UserQuery;
use Yii;
class SocialNetworkAccountCreateService implements ServiceInterface
{
protected $client;
protected $query;
/**
* SocialNetworkAccountUserLinkService constructor.
*
* @param AuthClientInterface $client
* @param UserQuery $query
*/
public function __construct(
AuthClientInterface $client,
UserQuery $query
) {
$this->client = $client;
$this->query = $query;
}
/**
* @return object
*/
public function run()
{
$data = $this->client->getUserAttributes();
/** @var SocialNetworkAccount $account */
$account = Yii::createObject(
[
'class' => SocialNetworkAccount::class,
'provider' => $this->client->getId(),
'client_id' => $data['id'],
'data' => json_encode($data),
'username' => $this->client->getUserName(),
'email' => $this->client->getEmail()
]
);
if (($user = $this->getUser($account)) instanceof User) {
$account->user_id = $user->id;
}
$account->save(false);
return $account;
}
protected function getUser(SocialNetworkAccount $account)
{
$user = $this->query->whereEmail($account->email)->one();
if (null !== $user) {
return $user;
}
/** @var User $user */
$user = Yii::createObject(
'User',
[
'scenario' => 'connect',
'username' => $account->username,
'email' => $account->email
]
);
if (!$user->validate(['email'])) {
$user->email = null;
}
if (!$user->validate(['username'])) {
$user->username = null;
}
return Yii::$container->get(UserCreateService::class, [$user])->run() ? $user : false;
}
}

View File

@ -1,68 +0,0 @@
<?php
namespace Da\User\Service;
use Da\User\Contracts\AuthClientInterface;
use Da\User\Contracts\ServiceInterface;
use Da\User\Model\SocialNetworkAccount;
use Da\User\Model\User;
use Da\User\Query\SocialNetworkAccountQuery;
use Yii;
class SocialNetworkAccountUserLinkService implements ServiceInterface
{
protected $client;
protected $query;
/**
* SocialNetworkAccountUserLinkService constructor.
*
* @param AuthClientInterface $client
* @param SocialNetworkAccountQuery $query
*/
public function __construct(
AuthClientInterface $client,
SocialNetworkAccountQuery $query
) {
$this->client = $client;
$this->query = $query;
}
public function run()
{
$account = $this->getSocialNetworkAccount();
if ($account->user === null) {
/** @var User $user */
$user = Yii::$app->user->identity;
$account->link('user', $user);
return true;
}
return false;
}
protected function getSocialNetworkAccount()
{
$account = $this->query->whereClient($this->client)->one();
if (null === $account) {
$data = $this->client->getUserAttributes();
$account = Yii::createObject(
[
'class' => SocialNetworkAccount::class,
'provider' => $this->client->getId(),
'client_id' => $data['id'],
'data' => json_encode($data)
]
);
$account->save(false);
}
return $account;
}
}

View File

@ -0,0 +1,132 @@
<?php
namespace Da\User\Service;
use Da\User\Contracts\AuthClientInterface;
use Da\User\Contracts\ServiceInterface;
use Da\User\Controller\SecurityController;
use Da\User\Event\SocialNetworkAuthEvent;
use Da\User\Factory\MailFactory;
use Da\User\Model\SocialNetworkAccount;
use Da\User\Model\User;
use Da\User\Query\SocialNetworkAccountQuery;
use Da\User\Query\UserQuery;
use Yii;
use yii\authclient\AuthAction;
use yii\helpers\Url;
class SocialNetworkAuthenticateService implements ServiceInterface
{
protected $controller;
protected $authAction;
protected $client;
protected $socialNetworkAccountQuery;
protected $userQuery;
public function __construct(
SecurityController $controller,
AuthAction $authAction,
AuthClientInterface $client,
SocialNetworkAccountQuery $socialNetworkAccountQuery,
UserQuery $userQuery
) {
$this->controller = $controller;
$this->authAction = $authAction;
$this->client = $client;
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
$this->userQuery = $userQuery;
}
public function run()
{
$account = $this->socialNetworkAccountQuery->whereClient($this->client)->one();
if (!$this->controller->module->enableRegistration && ($account === null || $account->user === null)) {
Yii::$app->session->setFlash('danger', Yii::t('user', 'Registration on this website is disabled'));
$this->authAction->setSuccessUrl(Url::to(['/usr/security/login']));
return false;
}
if ($account === null) {
$account = $this->createAccount();
if (!$account) {
Yii::$app->session->setFlash('danger', Yii::t('user', 'Unable to create an account.'));
$this->authAction->setSuccessUrl(Url::to(['/usr/security/login']));
return false;
}
}
$event = Yii::createObject(SocialNetworkAuthEvent::class, [$this->client]);
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_BEFORE_AUTHENTICATE, $event);
if ($account->user instanceof User) {
if ($account->user->getIsBlocked()) {
Yii::$app->session->setFlash('danger', Yii::t('user', 'Your account has been blocked.'));
$this->authAction->setSuccessUrl(Url::to(['/user/security/login']));
} else {
Yii::$app->user->login($account->user, $this->controller->module->rememberLoginLifeSpan);
$this->authAction->setSuccessUrl(Yii::$app->getUser()->getReturnUrl());
}
} else {
$this->authAction->setSuccessUrl($account->getConnectionUrl());
}
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_AFTER_AUTHENTICATE, $event);
}
protected function createAccount()
{
$data = $this->client->getUserAttributes();
/** @var SocialNetworkAccount $account */
$account = $this->controller->make(
SocialNetworkAccount::class,
[
'provider' => $this->client->getId(),
'client_id' => $data['id'],
'data' => json_encode($data),
'username' => $this->client->getUserName(),
'email' => $this->client->getEmail()
]
);
if (($user = $this->getUser($account)) instanceof User) {
$account->user_id = $user->id;
$account->save(false);
return $account;
}
return false;
}
protected function getUser(SocialNetworkAccount $account)
{
$user = $this->userQuery->whereEmail($account->email)->one();
if (null !== $user) {
return $user;
}
/** @var User $user */
$user = $this->controller->make(
User::class,
[
'scenario' => 'connect',
'username' => $account->username,
'email' => $account->email
]
);
if (!$user->validate(['email'])) {
$user->email = null;
}
if (!$user->validate(['username'])) {
$user->username = null;
}
$mailService = MailFactory::makeWelcomeMailerService($user);
return $this->controller->make(UserCreateService::class, [$user, $mailService])->run() ? $user : false;
}
}