engelsystem/includes/helper/oauth_helper.php

116 lines
3.7 KiB
PHP
Raw Normal View History

2021-11-23 11:59:53 +01:00
<?php
namespace Engelsystem\Events\Listener;
use Engelsystem\Config\Config;
2022-11-09 00:02:30 +01:00
use Engelsystem\Models\AngelType;
2021-11-23 11:59:53 +01:00
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Psr\Log\LoggerInterface;
class OAuth2
{
/** @var array */
2022-12-03 00:57:04 +01:00
protected array $config;
2021-11-23 11:59:53 +01:00
/** @var LoggerInterface */
2022-12-03 00:57:04 +01:00
protected LoggerInterface $log;
2021-11-23 11:59:53 +01:00
/**
* @param Config $config
* @param LoggerInterface $log
*/
public function __construct(Config $config, LoggerInterface $log)
{
$this->config = $config->get('oauth');
$this->log = $log;
}
/**
* @param string $event
2022-12-03 00:57:04 +01:00
* @param string $provider OAuth provider name
2021-11-23 11:59:53 +01:00
* @param Collection $data OAuth userdata
*/
2022-12-03 00:57:04 +01:00
public function login(string $event, string $provider, Collection $data): void
2021-11-23 11:59:53 +01:00
{
$ssoTeams = $this->getSsoTeams($provider);
$user = auth()->user();
2022-12-03 00:57:04 +01:00
$currentUserAngeltypes = $user->userAngelTypes;
2021-11-23 11:59:53 +01:00
$userGroups = $data->get(($this->config[$provider] ?? [])['groups'] ?? 'groups', []);
foreach ($userGroups as $groupName) {
if (!isset($ssoTeams[$groupName])) {
continue;
}
$team = $ssoTeams[$groupName];
2022-12-03 00:57:04 +01:00
$angelType = AngelType::find($team['id']);
/** @var AngelType $userAngeltype */
$userAngeltype = $currentUserAngeltypes->where('pivot.angel_type_id', $team['id'])->first();
2021-11-23 11:59:53 +01:00
$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,
2022-12-03 00:57:04 +01:00
'angeltype' => $angelType->name,
2022-11-09 00:02:30 +01:00
'confirmed' => $confirmed ? 'yes' : 'no',
2021-11-23 11:59:53 +01:00
'supporter' => $supporter ? 'yes' : 'no',
]
);
2022-12-03 00:57:04 +01:00
$user->userAngelTypes()->attach($angelType, ['supporter' => $supporter, 'confirm_user_id' => $confirmed]);
2021-11-23 11:59:53 +01:00
continue;
}
if (!$supporter) {
continue;
}
2022-12-03 00:57:04 +01:00
if ($userAngeltype->pivot->supporter != $supporter) {
$userAngeltype->pivot->supporter = $supporter;
$userAngeltype->pivot->save();
2021-11-23 11:59:53 +01:00
$this->log->info(
'SSO {provider}: Set supporter state for angeltype {angeltype}',
[
'provider' => $provider,
2022-12-03 00:57:04 +01:00
'angeltype' => $userAngeltype->pivot->angelType->name,
2021-11-23 11:59:53 +01:00
]
);
}
2022-12-03 00:57:04 +01:00
if (!$userAngeltype->pivot->confirm_user_id) {
$userAngeltype->pivot->confirmUser()->associate($user);
$userAngeltype->pivot->save();
2021-11-23 11:59:53 +01:00
$this->log->info(
'SSO {provider}: Set confirmed state for angeltype {angeltype}',
[
'provider' => $provider,
2022-12-03 00:57:04 +01:00
'angeltype' => $userAngeltype->pivot->angelType->name,
2021-11-23 11:59:53 +01:00
]
);
}
}
}
public function getSsoTeams(string $provider): array
{
$config = $this->config[$provider] ?? [];
$teams = [];
foreach ($config['teams'] ?? [] as $ssoName => $conf) {
$conf = Arr::wrap($conf);
$teamId = $conf['id'] ?? $conf[0];
$isSupporter = $conf['supporter'] ?? false;
$teams[$ssoName] = ['id' => $teamId, 'supporter' => $isSupporter];
}
return $teams;
}
}