|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\controllers\auth;
|
|
|
|
|
|
|
|
use common\auth\Identity;
|
|
|
|
use core\services\auth\NetworkService;
|
|
|
|
use Yii;
|
|
|
|
use yii\authclient\ClientInterface;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
use yii\authclient\AuthAction;
|
|
|
|
use yii\web\Controller;
|
|
|
|
|
|
|
|
class NetworkController extends Controller
|
|
|
|
{
|
|
|
|
private $_service;
|
|
|
|
|
|
|
|
public function __construct($id, $module, NetworkService $service, $config = [])
|
|
|
|
{
|
|
|
|
parent::__construct($id, $module, $config);
|
|
|
|
$this->_service = $service;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actions()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'auth' => [
|
|
|
|
'class' => AuthAction::class,
|
|
|
|
'successCallback' => [$this, 'onAuthSuccess'],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAuthSuccess(ClientInterface $client): void
|
|
|
|
{
|
|
|
|
$network = $client->getId();
|
|
|
|
$attributes = $client->getUserAttributes();
|
|
|
|
$identity = ArrayHelper::getValue($attributes, 'id');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$user = $this->_service->auth($network, $identity);
|
|
|
|
Yii::$app->user->login(new Identity($user), Yii::$app->params['user.rememberMeDuration']);
|
|
|
|
} catch (\DomainException $e) {
|
|
|
|
Yii::$app->errorHandler->logException($e);
|
|
|
|
Yii::$app->session->setFlash('error', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|