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(); } }