added social connection action
This commit is contained in:
147
lib/User/Controller/SecurityController.php
Normal file
147
lib/User/Controller/SecurityController.php
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user