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;
|
||||
}
|
||||
|
||||
}
|
||||
84
app/Auth/XenForoUser.php
Normal file
84
app/Auth/XenForoUser.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use App\Services\XenforoService;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class XenForoUser implements Authenticatable {
|
||||
|
||||
public ?array $permissions = null;
|
||||
private XenforoService $services;
|
||||
|
||||
public function __construct(public readonly object $data) {
|
||||
$this->services = app(XenforoService::class);
|
||||
}
|
||||
|
||||
public function __get(string $name): mixed {
|
||||
return $this->data->$name ?? null;
|
||||
}
|
||||
|
||||
public function getAuthIdentifierName(): string
|
||||
{
|
||||
return 'user_id';
|
||||
}
|
||||
|
||||
public function getAuthIdentifier(): mixed
|
||||
{
|
||||
return $this->data->user_id;
|
||||
}
|
||||
|
||||
public function getAuthPasswordName(): string
|
||||
{
|
||||
return 'password';
|
||||
}
|
||||
|
||||
public function getAuthPassword(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function getRememberToken(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function setRememberToken($value): void
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public function getRememberTokenName(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get XenForo avatar if it exist.
|
||||
*
|
||||
* @param string $xfSize
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getAvatarUrl( string $xfSize = 'm' ): ?string
|
||||
{
|
||||
$userId = $this->data->user_id;
|
||||
$avatarDate = $this->data->avatar_date;
|
||||
|
||||
if( $avatarDate ){
|
||||
$group = floor($userId / 1000);
|
||||
return config('app.forum_url') . "/data/avatars/{$xfSize}/{$group}/{$userId}.jpg?{$avatarDate}";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function can(string $permissionGroup, string $permissionName): bool
|
||||
{
|
||||
if( !$this->permissions ){
|
||||
$this->permissions = $this->services->getPermissions($this->data->user_id, $this->data->permission_combination_id);
|
||||
}
|
||||
|
||||
return ($this->permissions[$permissionGroup][$permissionName] ?? 0) === true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user