singleloginsystem/app/Services/Keka/KekaIntegrationService.php
= c4e2cfc4bb Feat: Modularize Keka HR integration with services
- Introduced `KekaAccessGuard`, `KekaConfigResolver`, `KekaIntegrationService`, `KekaTokenService`, and `KekaUrlBuilder` to streamline Keka HR functionality.
- Refactored `KekaController` to delegate key operations to modular services, improving maintainability.
- Updated dashboard UI to ensure alignment with the new service-based architecture.
- Enhanced error handling, logging, and code reusability for Keka integration workflows.
2026-06-12 11:55:12 +00:00

106 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Keka;
use App\Models\User;
use Illuminate\Http\{RedirectResponse, Request, Response};
use Illuminate\Support\Facades\Log;
class KekaIntegrationService
{
public function __construct(
private readonly KekaConfigResolver $configResolver,
private readonly KekaUrlBuilder $urlBuilder,
private readonly KekaTokenService $tokenService,
) {}
public function getExternalLoginUrl(User $user): string
{
$app = $this->configResolver->getApp();
$kekaUrl = $this->configResolver->getKekaUrl($app);
$provider = $this->configResolver->getProvider($app);
$redirectUrl = $this->urlBuilder->buildExternalLoginUrl($kekaUrl, $provider);
Log::info('Redirecting user to Keka external login page.', [
'user_id' => $user->id,
'keka_url' => $kekaUrl,
'redirect_url' => $redirectUrl,
]);
return $redirectUrl;
}
public function handleCallback(User $user, Request $request): Response|RedirectResponse
{
$code = $request->input('code');
if (empty($code)) {
return redirect()
->route('dashboard.user')
->with('keka_error', 'Keka connection failed: Missing code.');
}
$app = $this->configResolver->getApp();
$kekaUrl = $this->configResolver->getKekaUrl($app);
$callbackPath = $this->configResolver->getCallbackPath($app);
$callbackUrl = $this->urlBuilder->buildCallbackUrl($kekaUrl, $callbackPath);
$this->tokenService->store($user, (string) $code);
Log::info('Keka tokens stored successfully. Passing OIDC response to Keka.', [
'user_id' => $user->id,
'callback_url' => $callbackUrl,
'method' => $request->method(),
]);
if ($request->isMethod('post')) {
return response($this->renderAutoSubmitForm($callbackUrl, $request->all()));
}
$queryString = http_build_query($request->query());
return redirect("{$callbackUrl}?{$queryString}");
}
private function renderAutoSubmitForm(string $callbackUrl, array $inputsData): string
{
$inputs = '';
foreach ($inputsData as $key => $value) {
$safeKey = htmlspecialchars((string) $key, ENT_QUOTES, 'UTF-8');
$safeValue = htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
$inputs .= "<input type=\"hidden\" name=\"{$safeKey}\" value=\"{$safeValue}\" />\n";
}
return <<<HTML
<!DOCTYPE html>
<html>
<head>
<title>Forwarding to Keka...</title>
</head>
<body onload="document.forms[0].submit()">
<form method="post" action="{$callbackUrl}">
{$inputs}
<noscript>
<p>JavaScript is required to connect to Keka. Click the button below to continue.</p>
<input type="submit" value="Continue" />
</noscript>
</form>
</body>
</html>
HTML;
}
public function disconnect(User $user): void
{
$this->tokenService->revoke($user);
Log::info('Keka integration disconnected for user.', [
'user_id' => $user->id,
]);
}
}