diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3f9a3c6 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,14 @@ +# Version 1.0.0 +This is the first release with all its functionality. Since version 0.9.2 `SeAT-Discourse` was very stable and did not need a lot of refactoring or bug-fixing. However, i still had some ToDo's noted. The next few update still have some functional refactoring however functionally it does not change. Also some documentation will be added, whenever i find time and motivation to do so. + +These are the things changed with this version: + +* Logging out Job introduced +** This Job will be dispatched if a `refresh_token` in an `user group` is deleted. +* Refactoring of unneeded actions +* Introduction of the about-page to inform you if a new version is available. +* Introduction of changelog. +* Disallow login if `user group` is missing a `refresh_token`. + +I was looking into automatically logging out members whenever they receive a new role. Unfortunately this is not achieved easily without events dispatched whenever someone receives a role. This would need change in `SeAT` (vanilla), which i am not able to push by myself. This might be added to a later moment. + diff --git a/src/Action/Discourse/Users/Create.php b/src/Action/Discourse/Users/Create.php deleted file mode 100644 index 2a06392..0000000 --- a/src/Action/Discourse/Users/Create.php +++ /dev/null @@ -1,50 +0,0 @@ -request('POST', getenv('DISCOURSE_URL') . '/users.json', [ - 'form_params' => [ - 'api_key' => getenv('DISCOURSE_API_KEY'), - 'api_username' => getenv('DISCOURSE_API_USERNAME'), - 'name' => $group->main_character->name, - 'email' => $group->email, - 'password' => md5(microtime()), - 'username' => studly_case($group->main_character->name), - 'active' => true, - 'approved' => true - ], - ]); - - if (200 === $response->getStatusCode()) { - - return "User " . studly_case($group->main_character->name) . " successfully created." ; - - } - - abort(500, "Something went wrong at creating user"); - } catch (GuzzleException $e) { - return $e; - - } - - - } -} \ No newline at end of file diff --git a/src/Action/Discourse/Users/GetUserByCharacterId.php b/src/Action/Discourse/Users/GetUserByCharacterId.php new file mode 100644 index 0000000..7868d4a --- /dev/null +++ b/src/Action/Discourse/Users/GetUserByCharacterId.php @@ -0,0 +1,33 @@ +request('GET', getenv('DISCOURSE_URL').'/users/by-external/'. $id .'.json', [ + 'query' => [ + 'api_key' => getenv('DISCOURSE_API_KEY'), + 'api_username' => getenv('DISCOURSE_API_USERNAME') + ], + ]); + + return collect(json_decode($response->getBody())); + } catch (GuzzleException $e) { + return $e; + } + } + +} diff --git a/src/Action/SeatDiscourse/GetChangelog.php b/src/Action/SeatDiscourse/GetChangelog.php new file mode 100644 index 0000000..6ff38aa --- /dev/null +++ b/src/Action/SeatDiscourse/GetChangelog.php @@ -0,0 +1,32 @@ +request('GET', "https://raw.githubusercontent.com/herpaderpaldent/seat-discourse/master/CHANGELOG.md"); + if ($response->getStatusCode() != 200) { + return 'Error while fetching changelog'; + } + $parser = new Parsedown(); + return $parser->parse($response->getBody()); + } catch (RequestException $e) { + return 'Error while fetching changelog'; + } + } + +} \ No newline at end of file diff --git a/src/Http/Controllers/SeatDiscourseController.php b/src/Http/Controllers/SeatDiscourseController.php new file mode 100644 index 0000000..6332632 --- /dev/null +++ b/src/Http/Controllers/SeatDiscourseController.php @@ -0,0 +1,23 @@ +execute(); + return view('seatdiscourse::about', compact('changelog')); + } + +} \ No newline at end of file diff --git a/src/Http/Controllers/SsoController.php b/src/Http/Controllers/SsoController.php index f63d806..917f773 100644 --- a/src/Http/Controllers/SsoController.php +++ b/src/Http/Controllers/SsoController.php @@ -159,9 +159,12 @@ public function castBooleansToString($property) /** * Process the SSO login request from Discourse * - * @param Request $request + * @param Request $request + * + * @param \Herpaderpaldent\Seat\SeatDiscourse\Action\Discourse\Groups\Sync $sync * * @return mixed + * @throws \Cviebrock\DiscoursePHP\Exception\PayloadException */ public function login(Request $request, Sync $sync) { @@ -173,6 +176,10 @@ public function login(Request $request, Sync $sync) $this->user = $request->user(); + foreach ($this->user->group->users as $user){ + if (is_null($user->refresh_token)) + return redirect()->route('profile.view')->with('error','One of your characters is missing its refresh token. Please login with him again'); + } if (! ($this->sso->validatePayload($payload = $request->get('sso'), $request->get('sig')))) { diff --git a/src/Http/routes.php b/src/Http/routes.php index 72954a0..3e75ea9 100644 --- a/src/Http/routes.php +++ b/src/Http/routes.php @@ -20,5 +20,9 @@ 'uses' => 'SsoController@redirect', 'as' => 'sso.forum', ]); + Route::get('discourse/about', [ + 'uses' => 'SeatDiscourseController@getAbout', + 'as' => 'seatdiscourse.about', + ]); }); }); \ No newline at end of file diff --git a/src/Jobs/Logout.php b/src/Jobs/Logout.php new file mode 100644 index 0000000..da2594d --- /dev/null +++ b/src/Jobs/Logout.php @@ -0,0 +1,113 @@ +debug('Initialising SeAT discourse logout job for ' . $group->main_character->name); + + $this->group = $group; + + array_push($this->tags, 'main_character_id:' . $group->main_character_id); + + $this->client = new Client(); + + } + + public function handle() + { + Redis::funnel('seat-discourse:jobs.group_logout_' . $this->group->main_character_id)->limit(1)->then(function () + { + $this->beforeStart(); + + try { + $response = $this->client->request('POST', getenv('DISCOURSE_URL').'/admin/users/' . $this->discourse_user_id . '/log_out', [ + 'form_params' => [ + 'api_key' => getenv('DISCOURSE_API_KEY'), + 'api_username' => getenv('DISCOURSE_API_USERNAME') + ], + ]); + + logger()->debug(json_decode($response->getBody())); + + $this->onFinish(); + + + } catch (\Throwable $exception) { + + $this->onFail($exception); + + } + + }, function () + { + logger()->warning('A logout job is already running for ' . $this->group->main_character->name . ' Removing the job from the queue.'); + + $this->delete(); + }); + + + } + + public function beforeStart() + { + $response = $this->client->request('GET', getenv('DISCOURSE_URL').'/users/by-external/'. $this->group->main_character_id .'.json', [ + 'query' => [ + 'api_key' => getenv('DISCOURSE_API_KEY'), + 'api_username' => getenv('DISCOURSE_API_USERNAME') + ], + ]); + + $user = collect(json_decode($response->getBody())); + + $this->discourse_user_id = $user['user']->id; + + } + + public function onFail($exception) + { + + report($exception); + } + + public function onFinish() + { + + } + +} \ No newline at end of file diff --git a/src/Jobs/SeatDiscourseJobBase.php b/src/Jobs/SeatDiscourseJobBase.php new file mode 100644 index 0000000..27cbb2e --- /dev/null +++ b/src/Jobs/SeatDiscourseJobBase.php @@ -0,0 +1,60 @@ +tags, $tags); + + return $tags; + } + + /** + * Execute the job + * + * @return void + */ + public abstract function handle(); + +} \ No newline at end of file diff --git a/src/Observers/RefreshTokenObserver.php b/src/Observers/RefreshTokenObserver.php new file mode 100644 index 0000000..5a4d130 --- /dev/null +++ b/src/Observers/RefreshTokenObserver.php @@ -0,0 +1,24 @@ +debug('SoftDelete detected of '. $refresh_token->user->name); + + dispatch(new Logout($refresh_token->user->group)); + + } + +} \ No newline at end of file diff --git a/src/SeatDiscourseServiceProvider.php b/src/SeatDiscourseServiceProvider.php index 31a17d5..b585c77 100644 --- a/src/SeatDiscourseServiceProvider.php +++ b/src/SeatDiscourseServiceProvider.php @@ -3,10 +3,9 @@ namespace Herpaderpaldent\Seat\SeatDiscourse; use Herpaderpaldent\Seat\SeatDiscourse\Commands\SyncRolesWithDiscourse; -use Herpaderpaldent\Seat\SeatDiscourse\Events\Register; +use Herpaderpaldent\Seat\SeatDiscourse\Observers\RefreshTokenObserver; use Illuminate\Support\ServiceProvider; -use Illuminate\Contracts\Routing\Registrar as Router; -use Illuminate\Auth\Events\Registered as RegisterEvent; +use Seat\Eveapi\Models\RefreshToken; class SeatDiscourseServiceProvider extends ServiceProvider @@ -20,7 +19,9 @@ public function boot() { $this->addCommands(); $this->addRoutes(); - $this->addEvents(); + $this->addViews(); + + RefreshToken::observe(RefreshTokenObserver::class); } @@ -31,17 +32,11 @@ public function boot() */ public function register() { - $this->mergeConfigFrom( - __DIR__ . '/config/seatdiscourse.sidebar.php', 'package.sidebar'); - } - - private function addEvents() - { - - // Internal Authentication Events - //$this->app->events->listen(RegisterEvent::class, Register::class); + $this->mergeConfigFrom(__DIR__ . '/config/seatdiscourse.sidebar.php', 'package.sidebar'); + $this->mergeConfigFrom(__DIR__ . '/config/seatdiscourse.config.php', 'seatdiscourse.config'); } + private function addCommands() { @@ -49,8 +44,13 @@ private function addCommands() SyncRolesWithDiscourse::class, ]); + } + private function addViews() + { + $this->loadViewsFrom(__DIR__ . '/resources/views', 'seatdiscourse'); } + private function addRoutes() { if (!$this->app->routesAreCached()) { diff --git a/src/config/seatdiscourse.config.php b/src/config/seatdiscourse.config.php new file mode 100644 index 0000000..53f1622 --- /dev/null +++ b/src/config/seatdiscourse.config.php @@ -0,0 +1,12 @@ + '1.0.0' +]; + +//TODO: Update Version \ No newline at end of file diff --git a/src/config/seatdiscourse.sidebar.php b/src/config/seatdiscourse.sidebar.php index f01a7f8..663a4a5 100644 --- a/src/config/seatdiscourse.sidebar.php +++ b/src/config/seatdiscourse.sidebar.php @@ -8,9 +8,21 @@ return [ 'seatdiscorse' => [ - 'name' => 'SeAT Discourse Forum', + 'name' => 'SeAT Discourse', 'icon' => 'fa-comments-o', 'route_segment' => 'sso', - 'route' => 'sso.forum' + 'entries' => [ + [ + 'name' => 'Forum', + 'icon' => 'fa-commenting-o', + 'route' => 'sso.forum' + ], + [ + 'name' => 'About', + 'icon' => 'fa-info-circle', + 'route' => 'seatdiscourse.about' + ] + ] + ] ]; \ No newline at end of file diff --git a/src/resources/views/about.blade.php b/src/resources/views/about.blade.php new file mode 100644 index 0000000..0d43b29 --- /dev/null +++ b/src/resources/views/about.blade.php @@ -0,0 +1,67 @@ +@extends('web::layouts.grids.4-4-4') + +@section('title', 'Seat Discourse') +@section('page_header', 'Seat Discourse') +@section('page_description', 'About') + +@section('left') + +
Since SeAT 3.0 beta has launched SeAT Discourse has been downloaded almost 126 times. I am very content that my package is being used and supports you and your members.
+ +As you might know, SeAT
, SeAT-Groups
and SeAT-Discourse
are OpenSource Projects which are available free of charge. However, programming takes up a lot of time which keeps me away from the game.
If you like SeAT-Discourse
, i highly appreciate ISK Donations to {!! img('character', 95725047, 64, ['class' => 'img-circle eve-icon small-icon']) !!} Herpaderp Aldent
If you find something is not working as expected, please don't hesitate and contact me. Either use SeAT-Slack or submit an issue on Github
+ +