Initial commit
This commit is contained in:
77
app/Auth/XenForoGuard.php
Normal file
77
app/Auth/XenForoGuard.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class XenForoGuard implements Guard
|
||||
{
|
||||
private ?XenForoUser $user = null;
|
||||
|
||||
public function __construct(private readonly Request $request) {}
|
||||
|
||||
public function check(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
public function guest(): bool
|
||||
{
|
||||
return ! $this->check();
|
||||
}
|
||||
|
||||
public function id(): mixed
|
||||
{
|
||||
return $this->user()?->getAuthIdentifier();
|
||||
}
|
||||
|
||||
public function hasUser(): bool
|
||||
{
|
||||
return $this->user !== null;
|
||||
}
|
||||
|
||||
public function user(): ?XenForoUser
|
||||
{
|
||||
if ($this->hasUser())
|
||||
return $this->user;
|
||||
|
||||
$sessionId = $this->request->cookie('xf_session');
|
||||
if(!$sessionId)
|
||||
return null;
|
||||
|
||||
$xfSession = \DB::connection('xenforo')
|
||||
->table('session')
|
||||
->where('session_id', $sessionId)
|
||||
->value('session_data');
|
||||
|
||||
if(!$xfSession)
|
||||
return null;
|
||||
|
||||
$sessionData = unserialize($xfSession);
|
||||
|
||||
if (!$sessionData || !isset($sessionData['userId']) || !$sessionData['userId'])
|
||||
return null;
|
||||
|
||||
$xfUser = \DB::connection('xenforo')
|
||||
->table('user')
|
||||
->where('user_id', $sessionData['userId'])
|
||||
->first();
|
||||
|
||||
if(!$xfUser)
|
||||
return null;
|
||||
|
||||
return $this->user = new XenForoUser($xfUser);
|
||||
}
|
||||
|
||||
public function validate(array $credentials = []): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setUser(mixed $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user