add all controllers logic
This commit is contained in:
@@ -4,6 +4,14 @@ namespace App\Services\Auth;
|
||||
|
||||
class RegistrationCaptchaService
|
||||
{
|
||||
private function cacheKey(): string
|
||||
{
|
||||
$ip = (string) (request()?->ip() ?? 'unknown');
|
||||
$ua = (string) (request()?->header('User-Agent') ?? '');
|
||||
$hash = substr(sha1($ua), 0, 12);
|
||||
return 'captcha:' . $ip . ':' . $hash;
|
||||
}
|
||||
|
||||
public function generate(?int $length = null): string
|
||||
{
|
||||
$length = $length ?? random_int(4, 8);
|
||||
@@ -14,14 +22,14 @@ class RegistrationCaptchaService
|
||||
$captchaText .= $characters[random_int(0, strlen($characters) - 1)];
|
||||
}
|
||||
|
||||
session()->put('captcha_answer', $captchaText);
|
||||
cache()->put($this->cacheKey(), $captchaText, now()->addMinutes(10));
|
||||
|
||||
return $captchaText;
|
||||
}
|
||||
|
||||
public function verify(string $input): bool
|
||||
{
|
||||
$expected = (string) session()->get('captcha_answer', '');
|
||||
$expected = (string) cache()->get($this->cacheKey(), '');
|
||||
if ($expected === '') {
|
||||
return false;
|
||||
}
|
||||
@@ -31,6 +39,6 @@ class RegistrationCaptchaService
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
session()->forget('captcha_answer');
|
||||
cache()->forget($this->cacheKey());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use Illuminate\Session\Store;
|
||||
|
||||
class SessionActivityService
|
||||
{
|
||||
public function __construct(private Store $session)
|
||||
{
|
||||
}
|
||||
|
||||
public function hasLastActivity(): bool
|
||||
{
|
||||
return $this->session->has('last_activity');
|
||||
}
|
||||
|
||||
public function getLastActivity(): ?int
|
||||
{
|
||||
$value = $this->session->get('last_activity');
|
||||
return $value !== null ? (int) $value : null;
|
||||
}
|
||||
|
||||
public function setLastActivity(int $timestamp): void
|
||||
{
|
||||
$this->session->put('last_activity', $timestamp);
|
||||
}
|
||||
|
||||
public function clearLastActivity(): void
|
||||
{
|
||||
$this->session->forget('last_activity');
|
||||
}
|
||||
|
||||
public function invalidate(): void
|
||||
{
|
||||
$this->session->invalidate();
|
||||
$this->session->regenerateToken();
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use App\Config\SessionTimeout;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
|
||||
class SessionTimeoutConfigService
|
||||
{
|
||||
public function config(): array
|
||||
{
|
||||
return [
|
||||
'timeout' => SessionTimeout::TIMEOUT_DURATION,
|
||||
'warning_time' => SessionTimeout::WARNING_THRESHOLD,
|
||||
'check_interval' => SessionTimeout::CHECK_INTERVAL,
|
||||
'logout_url' => URL::to('/auth/logout'),
|
||||
'keep_alive_url' => route('session.timeout.ping'),
|
||||
'check_url' => route('session.timeout.check'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use App\Config\SessionTimeout;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
|
||||
class SessionTimeoutService
|
||||
{
|
||||
public function __construct(
|
||||
private SessionActivityService $activityService
|
||||
) {
|
||||
}
|
||||
|
||||
public function checkTimeout(): array
|
||||
{
|
||||
if (!$this->activityService->hasLastActivity()) {
|
||||
return $this->expireSession();
|
||||
}
|
||||
|
||||
$lastActivity = (int) $this->activityService->getLastActivity();
|
||||
$elapsed = time() - $lastActivity;
|
||||
|
||||
if ($elapsed > SessionTimeout::TIMEOUT_DURATION) {
|
||||
return $this->expireSession();
|
||||
}
|
||||
|
||||
if ($elapsed > SessionTimeout::WARNING_THRESHOLD) {
|
||||
return [
|
||||
'status' => 'warning',
|
||||
'time_remaining' => SessionTimeout::TIMEOUT_DURATION - $elapsed,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => 'active',
|
||||
'time_remaining' => SessionTimeout::TIMEOUT_DURATION - $elapsed,
|
||||
];
|
||||
}
|
||||
|
||||
public function pingActivity(): array
|
||||
{
|
||||
if (
|
||||
!$this->activityService->hasLastActivity()
|
||||
|| (time() - (int) $this->activityService->getLastActivity() > SessionTimeout::TIMEOUT_DURATION)
|
||||
) {
|
||||
return $this->expireSession();
|
||||
}
|
||||
|
||||
$this->activityService->setLastActivity(time());
|
||||
|
||||
return [
|
||||
'status' => 'active',
|
||||
'time_remaining' => SessionTimeout::TIMEOUT_DURATION,
|
||||
];
|
||||
}
|
||||
|
||||
private function expireSession(): array
|
||||
{
|
||||
Log::info('Session expired due to inactivity.');
|
||||
$this->activityService->clearLastActivity();
|
||||
$this->activityService->invalidate();
|
||||
|
||||
return [
|
||||
'status' => 'expired',
|
||||
'redirect' => URL::to('/login'),
|
||||
'message' => 'Your session has expired due to inactivity.',
|
||||
'time_remaining' => 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class UserRoleService
|
||||
{
|
||||
public function getRoleIds(int $userId): array
|
||||
{
|
||||
return DB::table('user_roles')
|
||||
->where('user_id', $userId)
|
||||
->whereNull('deleted_at')
|
||||
->pluck('role_id')
|
||||
->map(fn ($id) => (int) $id)
|
||||
->all();
|
||||
}
|
||||
|
||||
public function hasPermissionForCrud(array $roleIds, string $permissionName, string $crudAction = 'read'): bool
|
||||
{
|
||||
if (empty($roleIds)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$rows = DB::table('role_permissions as rp')
|
||||
->join('permissions as p', 'p.id', '=', 'rp.permission_id')
|
||||
->whereIn('rp.role_id', $roleIds)
|
||||
->where('p.name', $permissionName)
|
||||
->select('rp.can_create', 'rp.can_read', 'rp.can_update', 'rp.can_delete', 'rp.can_manage')
|
||||
->get();
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$manage = (bool) ($row->can_manage ?? false);
|
||||
if ($manage) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($crudAction === 'create' && !empty($row->can_create)) {
|
||||
return true;
|
||||
}
|
||||
if ($crudAction === 'read' && !empty($row->can_read)) {
|
||||
return true;
|
||||
}
|
||||
if ($crudAction === 'update' && !empty($row->can_update)) {
|
||||
return true;
|
||||
}
|
||||
if ($crudAction === 'delete' && !empty($row->can_delete)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user