OAuth2 event: refactoring

This commit is contained in:
Igor Scheller 2022-12-13 01:03:46 +01:00 committed by Michael Weimann
parent 5b8805f884
commit 93b30dcbdf
2 changed files with 61 additions and 53 deletions

View File

@ -5,6 +5,7 @@ namespace Engelsystem\Events\Listener;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Authenticator;
use Engelsystem\Models\AngelType; use Engelsystem\Models\AngelType;
use Engelsystem\Models\User\User;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -38,9 +39,8 @@ class OAuth2
*/ */
public function login(string $event, string $provider, Collection $data): void public function login(string $event, string $provider, Collection $data): void
{ {
$ssoTeams = $this->getSsoTeams($provider);
$user = $this->auth->user(); $user = $this->auth->user();
$currentUserAngeltypes = $user->userAngelTypes; $ssoTeams = $this->getSsoTeams($provider);
$groupsKey = ($this->config[$provider] ?? [])['groups'] ?? 'groups'; $groupsKey = ($this->config[$provider] ?? [])['groups'] ?? 'groups';
$userGroups = $data->get($groupsKey, []); $userGroups = $data->get($groupsKey, []);
@ -49,57 +49,7 @@ class OAuth2
continue; continue;
} }
$team = $ssoTeams[$groupName]; $this->syncTeams($provider, $user, $ssoTeams[$groupName]);
$angelType = AngelType::find($team['id']);
/** @var AngelType $userAngeltype */
$userAngeltype = $currentUserAngeltypes->where('pivot.angel_type_id', $team['id'])->first();
$supporter = $team['supporter'];
$confirmed = $supporter ? $user->id : null;
if (!$userAngeltype) {
$this->log->info(
'SSO {provider}: Added to angeltype {angeltype}, confirmed: {confirmed}, supporter: {supporter}',
[
'provider' => $provider,
'angeltype' => $angelType->name,
'confirmed' => $confirmed ? 'yes' : 'no',
'supporter' => $supporter ? 'yes' : 'no',
]
);
$user->userAngelTypes()->attach($angelType, ['supporter' => $supporter, 'confirm_user_id' => $confirmed]);
continue;
}
if (!$supporter) {
continue;
}
if ($userAngeltype->pivot->supporter != $supporter) {
$userAngeltype->pivot->supporter = $supporter;
$userAngeltype->pivot->save();
$this->log->info(
'SSO {provider}: Set supporter state for angeltype {angeltype}',
[
'provider' => $provider,
'angeltype' => $userAngeltype->pivot->angelType->name,
]
);
}
if (!$userAngeltype->pivot->confirm_user_id) {
$userAngeltype->pivot->confirmUser()->associate($user);
$userAngeltype->pivot->save();
$this->log->info(
'SSO {provider}: Set confirmed state for angeltype {angeltype}',
[
'provider' => $provider,
'angeltype' => $userAngeltype->pivot->angelType->name,
]
);
}
} }
} }
@ -118,4 +68,59 @@ class OAuth2
return $teams; return $teams;
} }
protected function syncTeams(string $providerName, User $user, array $ssoTeam)
{
$currentUserAngeltypes = $user->userAngelTypes;
$angelType = AngelType::find($ssoTeam['id']);
/** @var AngelType $userAngeltype */
$userAngeltype = $currentUserAngeltypes->where('pivot.angel_type_id', $ssoTeam['id'])->first();
$supporter = $ssoTeam['supporter'];
$confirmed = $supporter ? $user->id : null;
if (!$userAngeltype) {
$this->log->info(
'SSO {provider}: Added to angeltype {angeltype}, confirmed: {confirmed}, supporter: {supporter}',
[
'provider' => $providerName,
'angeltype' => $angelType->name,
'confirmed' => $confirmed ? 'yes' : 'no',
'supporter' => $supporter ? 'yes' : 'no',
]
);
$user->userAngelTypes()->attach($angelType, ['supporter' => $supporter, 'confirm_user_id' => $confirmed]);
return;
}
if (!$supporter) {
return;
}
if ($userAngeltype->pivot->supporter != $supporter) {
$userAngeltype->pivot->supporter = $supporter;
$userAngeltype->pivot->save();
$this->log->info(
'SSO {provider}: Set supporter state for angeltype {angeltype}',
[
'provider' => $providerName,
'angeltype' => $userAngeltype->pivot->angelType->name,
]
);
}
if (!$userAngeltype->pivot->confirm_user_id) {
$userAngeltype->pivot->confirmUser()->associate($user);
$userAngeltype->pivot->save();
$this->log->info(
'SSO {provider}: Set confirmed state for angeltype {angeltype}',
[
'provider' => $providerName,
'angeltype' => $userAngeltype->pivot->angelType->name,
]
);
}
}
} }

View File

@ -32,6 +32,7 @@ class OAuth2Test extends TestCase
/** /**
* @covers \Engelsystem\Events\Listener\OAuth2::login * @covers \Engelsystem\Events\Listener\OAuth2::login
* @covers \Engelsystem\Events\Listener\OAuth2::syncTeams
* @covers \Engelsystem\Events\Listener\OAuth2::__construct * @covers \Engelsystem\Events\Listener\OAuth2::__construct
*/ */
public function testLogin() public function testLogin()
@ -84,6 +85,7 @@ class OAuth2Test extends TestCase
/** /**
* @covers \Engelsystem\Events\Listener\OAuth2::login * @covers \Engelsystem\Events\Listener\OAuth2::login
* @covers \Engelsystem\Events\Listener\OAuth2::syncTeams
*/ */
public function testLoginNoChanges() public function testLoginNoChanges()
{ {
@ -112,6 +114,7 @@ class OAuth2Test extends TestCase
/** /**
* @covers \Engelsystem\Events\Listener\OAuth2::login * @covers \Engelsystem\Events\Listener\OAuth2::login
* @covers \Engelsystem\Events\Listener\OAuth2::syncTeams
*/ */
public function testLoginChangeSupport() public function testLoginChangeSupport()
{ {