Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Version 1.0.0 (#7)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
herpaderpaldent authored Sep 22, 2018
1 parent db54ac8 commit 6beb575
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 66 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

50 changes: 0 additions & 50 deletions src/Action/Discourse/Users/Create.php

This file was deleted.

33 changes: 33 additions & 0 deletions src/Action/Discourse/Users/GetUserByCharacterId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 21.09.2018
* Time: 21:51
*/

namespace Herpaderpaldent\Seat\SeatDiscourse\Action\Discourse\Users;

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;

class GetUserByCharacterId
{
public function execute(int $id)
{
$client = new Client();
try {
$response = $client->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;
}
}

}
32 changes: 32 additions & 0 deletions src/Action/SeatDiscourse/GetChangelog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 22.09.2018
* Time: 10:51
*/

namespace Herpaderpaldent\Seat\SeatDiscourse\Action\SeatDiscourse;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Parsedown;

class GetChangelog
{
public function execute()
{
try {
$response = (new Client())
->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';
}
}

}
23 changes: 23 additions & 0 deletions src/Http/Controllers/SeatDiscourseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 22.09.2018
* Time: 10:47
*/

namespace Herpaderpaldent\Seat\SeatDiscourse\Http\Controllers;


use Herpaderpaldent\Seat\SeatDiscourse\Action\SeatDiscourse\GetChangelog;
use Seat\Web\Http\Controllers\Controller;

class SeatDiscourseController extends Controller
{
public function getAbout(GetChangelog $action)
{
$changelog = $action->execute();
return view('seatdiscourse::about', compact('changelog'));
}

}
9 changes: 8 additions & 1 deletion src/Http/Controllers/SsoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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')))) {
Expand Down
4 changes: 4 additions & 0 deletions src/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
'uses' => 'SsoController@redirect',
'as' => 'sso.forum',
]);
Route::get('discourse/about', [
'uses' => 'SeatDiscourseController@getAbout',
'as' => 'seatdiscourse.about',
]);
});
});
113 changes: 113 additions & 0 deletions src/Jobs/Logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 21.09.2018
* Time: 23:22
*/

namespace Herpaderpaldent\Seat\SeatDiscourse\Jobs;


use Seat\Web\Models\Group;
use Illuminate\Support\Facades\Redis;
use GuzzleHttp\Client;

class Logout extends SeatDiscourseJobBase
{
/**
* @var array
*/
protected $tags = ['logout'];

private $group;

private $client;

private $discourse_user_id;

/**
* @var int
*/
public $tries = 1;

/**
* ConversationOrchestrator constructor.
*
* @param \Seat\Web\Models\Group $group
*/
public function __construct(Group $group)
{

logger()->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()
{

}

}
60 changes: 60 additions & 0 deletions src/Jobs/SeatDiscourseJobBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 21.09.2018
* Time: 23:16
*/

namespace Herpaderpaldent\Seat\SeatDiscourse\Jobs;


use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

abstract class SeatDiscourseJobBase implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* @var array
*/
protected $tags = [];

/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 1;

/**
* Assign this job a tag so that Horizon can categorize and allow
* for specific tags to be monitored.
*
* If a job specifies the tags property, that is added.
*
* @return array
*/
public function tags(): array
{

$tags = ['seatdiscourse'];

if (property_exists($this, 'tags'))
return array_merge($this->tags, $tags);

return $tags;
}

/**
* Execute the job
*
* @return void
*/
public abstract function handle();

}
24 changes: 24 additions & 0 deletions src/Observers/RefreshTokenObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 22.09.2018
* Time: 10:33
*/

namespace Herpaderpaldent\Seat\SeatDiscourse\Observers;

use Herpaderpaldent\Seat\SeatDiscourse\Jobs\Logout;
use Seat\Eveapi\Models\RefreshToken;

class RefreshTokenObserver
{
public function deleting(RefreshToken $refresh_token)
{
logger()->debug('SoftDelete detected of '. $refresh_token->user->name);

dispatch(new Logout($refresh_token->user->group));

}

}
Loading

0 comments on commit 6beb575

Please sign in to comment.