73 lines
2 KiB
PHP
73 lines
2 KiB
PHP
<?php
|
|
namespace Misuzu\Auth;
|
|
|
|
use Misuzu\Auth\SessionInfo;
|
|
use Misuzu\Forum\ForumCategoryInfo;
|
|
use Misuzu\Perms\IPermissionResult;
|
|
use Misuzu\Perms\Permissions;
|
|
use Misuzu\Users\UserInfo;
|
|
|
|
class AuthInfo {
|
|
public private(set) AuthTokenInfo $tokenInfo;
|
|
public private(set) ?UserInfo $userInfo;
|
|
public private(set) ?SessionInfo $sessionInfo;
|
|
public private(set) ?UserInfo $realUserInfo;
|
|
public private(set) array $perms;
|
|
|
|
public function __construct(
|
|
private Permissions $permissions
|
|
) {
|
|
$this->removeInfo();
|
|
}
|
|
|
|
public function setInfo(
|
|
AuthTokenInfo $tokenInfo,
|
|
?UserInfo $userInfo = null,
|
|
?SessionInfo $sessionInfo = null,
|
|
?UserInfo $realUserInfo = null
|
|
): void {
|
|
$this->tokenInfo = $tokenInfo;
|
|
$this->userInfo = $userInfo;
|
|
$this->sessionInfo = $sessionInfo;
|
|
$this->realUserInfo = $realUserInfo;
|
|
$this->perms = [];
|
|
}
|
|
|
|
public function removeInfo(): void {
|
|
$this->setInfo(AuthTokenInfo::empty());
|
|
}
|
|
|
|
public bool $isLoggedIn {
|
|
get => $this->userInfo !== null;
|
|
}
|
|
|
|
public ?string $userId {
|
|
get => $this->userInfo?->id;
|
|
}
|
|
|
|
public ?string $sessionId {
|
|
get => $this->sessionInfo?->id;
|
|
}
|
|
|
|
public bool $isImpersonating {
|
|
get => $this->realUserInfo !== null;
|
|
}
|
|
|
|
public ?string $realUserId {
|
|
get => $this->realUserInfo?->id;
|
|
}
|
|
|
|
public function getPerms(
|
|
string $category,
|
|
ForumCategoryInfo|string|null $forumCategoryInfo = null
|
|
): IPermissionResult {
|
|
$cacheKey = $category;
|
|
if($forumCategoryInfo !== null)
|
|
$cacheKey .= '|' . ($forumCategoryInfo instanceof ForumCategoryInfo ? $forumCategoryInfo->id : $forumCategoryInfo);
|
|
|
|
if(array_key_exists($cacheKey, $this->perms))
|
|
return $this->perms[$cacheKey];
|
|
|
|
return $this->perms[$cacheKey] = $this->permissions->getPermissions($category, $this->userInfo, $forumCategoryInfo);
|
|
}
|
|
}
|