Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
melancn committed Aug 30, 2018
1 parent 486d37e commit 2938e7c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
3 changes: 2 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
});
Expand Down
23 changes: 12 additions & 11 deletions src/CnuAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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' => ''
];

Expand Down
36 changes: 36 additions & 0 deletions src/Listener/ActivateCnuUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace cnuer\Auth\Cnu\Listener;

use Flarum\Event\UserWillBeSaved;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Settings\SettingsRepositoryInterface;

class ActivateCnuUser
{
/**
* @var SettingsRepository
*/
protected $settings;

public function __construct(SettingsRepositoryInterface $settings)
{
$this->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();
}
}

0 comments on commit 2938e7c

Please sign in to comment.