This commit is contained in:
2024-01-15 16:32:24 +02:00
parent 8959798cd3
commit f70815c335
+31 -13
View File
@@ -25,20 +25,25 @@ readonly class AuthService
public function currentUserId(): ?string public function currentUserId(): ?string
{ {
if (app()->runningInConsole()) { if (app()->runningInConsole()) {
return config("lucent.systemUserId"); return config("lucent.systemUserId");
} elseif(request()->segment(1) !== "lucent") {
return config("lucent.systemUserId");
} else { } else {
return $this->session->get("user.id") || config("lucent.systemUserId"); return $this->session->get("user.id");
} }
} }
public function currentUserRoles(): array public
function currentUserRoles(): array
{ {
return $this->session->get("user.roles") ?? []; return $this->session->get("user.roles") ?? [];
} }
public function isLoggedIn(): bool public
function isLoggedIn(): bool
{ {
return !empty($this->currentUserId()); return !empty($this->currentUserId());
} }
@@ -46,7 +51,8 @@ readonly class AuthService
/** /**
* @throws LucentException * @throws LucentException
*/ */
public function login(string $email, string $token): void public
function login(string $email, string $token): void
{ {
$user = $this->userRepo->findByEmail(new Email($email)); $user = $this->userRepo->findByEmail(new Email($email));
@@ -74,7 +80,11 @@ readonly class AuthService
$this->session->put(["user" => $user->get()->safe()]); $this->session->put(["user" => $user->get()->safe()]);
} }
public function refreshSession(){ public
function refreshSession()
{
$user = $this->userRepo->findById($this->currentUserId()); $user = $this->userRepo->findById($this->currentUserId());
if ($user->isEmpty()) { if ($user->isEmpty()) {
@@ -90,7 +100,8 @@ readonly class AuthService
} }
public function create(string $name, string $email, array $roles): User public
function create(string $name, string $email, array $roles): User
{ {
$user = new User( $user = new User(
id: (string)Str::uuid(), id: (string)Str::uuid(),
@@ -111,7 +122,8 @@ readonly class AuthService
/** /**
* @throws LucentException * @throws LucentException
*/ */
public function sendLoginEmail(string $email): void public
function sendLoginEmail(string $email): void
{ {
$emailAddress = (new Email($email)); $emailAddress = (new Email($email));
$user = $this->userRepo->findByEmail($emailAddress); $user = $this->userRepo->findByEmail($emailAddress);
@@ -140,7 +152,8 @@ readonly class AuthService
/** /**
* @throws LucentException * @throws LucentException
*/ */
public function changeRoles(string $userId, array $roles): void public
function changeRoles(string $userId, array $roles): void
{ {
$user = $this->userRepo->findById($userId); $user = $this->userRepo->findById($userId);
@@ -157,7 +170,8 @@ readonly class AuthService
/** /**
* @throws LucentException * @throws LucentException
*/ */
public function updateName(string $name): void public
function updateName(string $name): void
{ {
$name = (new Name($name)); $name = (new Name($name));
$this->userRepo->updateName($this->currentUserId(), $name); $this->userRepo->updateName($this->currentUserId(), $name);
@@ -168,7 +182,8 @@ readonly class AuthService
/** /**
* @throws LucentException * @throws LucentException
*/ */
public function updateEmail(string $email): void public
function updateEmail(string $email): void
{ {
$email = (new Email($email)); $email = (new Email($email));
$user = $this->userRepo->findByEmail($email); $user = $this->userRepo->findByEmail($email);
@@ -185,7 +200,8 @@ readonly class AuthService
/** /**
* @throws LucentException * @throws LucentException
*/ */
public function invite( public
function invite(
string $name, string $name,
string $email, string $email,
array $roles array $roles
@@ -199,7 +215,8 @@ readonly class AuthService
/** /**
* @throws LucentException * @throws LucentException
*/ */
public function registerAdmin( public
function registerAdmin(
string $name, string $name,
string $email string $email
): User ): User
@@ -209,7 +226,8 @@ readonly class AuthService
return $user; return $user;
} }
public function validateRoles(array $roles): array public
function validateRoles(array $roles): array
{ {
return collect($roles) return collect($roles)
->filter(fn(string $role) => in_array($role, $this->channelService->channel->roles)) ->filter(fn(string $role) => in_array($role, $this->channelService->channel->roles))