From 2938e7ca5fa2ee7413d4b9b1fc7d3bd6ae9d65c4 Mon Sep 17 00:00:00 2001 From: melancn Date: Thu, 30 Aug 2018 10:44:28 +0800 Subject: [PATCH] update --- bootstrap.php | 3 ++- src/CnuAuthController.php | 23 ++++++++++---------- src/Listener/ActivateCnuUser.php | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 src/Listener/ActivateCnuUser.php diff --git a/bootstrap.php b/bootstrap.php index 1d8c230..a781110 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -6,7 +6,8 @@ return function (Dispatcher $events) { $events->subscribe(Listener\AddClientAssets::class); $events->subscribe(Listener\AddCnuAuthRoute::class); - $events->subscribe(Listener\AddCnuUserGroup::class); + // $events->subscribe(Listener\AddCnuUserGroup::class); + $events->subscribe(Listener\ActivateCnuUser::class); $events->listen(ConfigureLocales::class, function (ConfigureLocales $event) { $event->loadLanguagePackFrom(__DIR__); }); diff --git a/src/CnuAuthController.php b/src/CnuAuthController.php index 5babf58..e060bf4 100644 --- a/src/CnuAuthController.php +++ b/src/CnuAuthController.php @@ -44,8 +44,8 @@ public function handle(Request $request) { $redirectUri = (string) $request->getAttribute('originalUri', $request->getUri())->withQuery(''); $server = new OauthCnu([ - 'identifier' => $this->settings->get('cnuer-auth-cnu.api_key'), - 'secret' => $this->settings->get('cnuer-auth-cnu.api_secret'), + 'api_key' => $this->settings->get('cnuer-auth-cnu.api_key'), + 'api_secret' => $this->settings->get('cnuer-auth-cnu.api_secret'), 'callback_uri' => $redirectUri ]); //'callback_uri' => $this->url->toRoute('auth/cnu') @@ -56,23 +56,24 @@ public function handle(Request $request) $oAuthCode = array_get($queryParams, 'code'); if (!$oAuthCode) { - $temporary = $server->gotoAuthUrl(); - - $session->set('temporary_credentials', serialize($temporary)); - $session->save(); + $server->gotoAuthUrl(); exit; } else { + // $session->set('temporary_credentials', serialize($temporary)); + // $session->save(); // get token - $oAuthToken = $server->getAccessToken($oAuthCode); - $info = $server->getUserInfo($oAuthToken); + $res = $server->getAccessToken($oAuthCode); + $oAuthToken = $res->access_token; + $res = $server->getUserInfo($oAuthToken); + $info = $res->data; } - $temporaryCredentials = unserialize($session->get('temporary_credentials')); + // $temporaryCredentials = unserialize($session->get('temporary_credentials')); - $identification = ['cnu_id' => $server->get('cnu_id')]; + $identification = ['cnu_id' => $info->user]; $suggestions = [ - 'username' => $server->get('uname'), + 'username' => $info->name, 'avatarUrl' => '' ]; diff --git a/src/Listener/ActivateCnuUser.php b/src/Listener/ActivateCnuUser.php new file mode 100644 index 0000000..77fdce0 --- /dev/null +++ b/src/Listener/ActivateCnuUser.php @@ -0,0 +1,36 @@ +settings = $settings; + } + + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(UserWillBeSaved::class, [$this, 'activateUser']); + } + + /** + * @param ConfigureForumRoutes $event + */ + public function activateUser(UserWillBeSaved $event) + { + if (empty($event->user->cnu_id)) return ; + $event->user->activate(); + } +}