2023-10-02 23:10:49 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Account;
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Illuminate\Contracts\Session\Session;
|
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Lucent\Channel\ChannelService;
|
|
|
|
|
use Lucent\LucentException;
|
|
|
|
|
use Lucent\Mail\LoginMail;
|
|
|
|
|
|
|
|
|
|
readonly class AuthService
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
private ChannelService $channelService,
|
|
|
|
|
private UserRepo $userRepo,
|
|
|
|
|
public Session $session,
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 13:07:09 +03:00
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
public function currentUserId(): ?string
|
|
|
|
|
{
|
2023-10-06 13:07:09 +03:00
|
|
|
if(app()->runningInConsole()){
|
|
|
|
|
return config("lucent.systemUserId");
|
|
|
|
|
}else{
|
|
|
|
|
return $this->session->get("user.id");
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isLoggedIn(): bool
|
|
|
|
|
{
|
|
|
|
|
return !empty($this->currentUserId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws LucentException
|
|
|
|
|
*/
|
|
|
|
|
public function login(string $email, string $token): void
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$user = $this->userRepo->findByEmail(new Email($email));
|
|
|
|
|
|
|
|
|
|
if ($user->isEmpty()) {
|
|
|
|
|
throw new LucentException("You account was not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($user->get()->role === Role::REMOVED) {
|
|
|
|
|
throw new LucentException("Your account is not active");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($user->get()->mailToken !== $token) {
|
|
|
|
|
throw new LucentException("Token has expired or is invalid");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Carbon::parse($user->get()->loggedInAt)->lte(Carbon::now()->subHours(1))) {
|
|
|
|
|
throw new LucentException("Token has expired.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$newUser = $user->get();
|
|
|
|
|
$newUser->updatedAt = Carbon::now()->toJson();
|
|
|
|
|
$newUser->mailToken = null;
|
|
|
|
|
$this->userRepo->update($newUser);
|
|
|
|
|
|
|
|
|
|
$this->session->put(["user" => $user->get()->safe()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function create(string $name, string $email, string $role): User
|
|
|
|
|
{
|
|
|
|
|
$user = new User(
|
|
|
|
|
id: (string)Str::uuid(),
|
|
|
|
|
name: new Name($name),
|
|
|
|
|
email: new Email($email),
|
|
|
|
|
role: Role::from($role),
|
|
|
|
|
createdAt: Carbon::now()->toJson(),
|
|
|
|
|
updatedAt: Carbon::now()->toJson(),
|
|
|
|
|
loggedInAt: Carbon::now()->toJson(),
|
|
|
|
|
mailToken: Token::new(32),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->userRepo->insert($user);
|
|
|
|
|
return $user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws LucentException
|
|
|
|
|
*/
|
|
|
|
|
public function sendLoginEmail(string $email): void
|
|
|
|
|
{
|
|
|
|
|
$emailAddress = (new Email($email));
|
|
|
|
|
$user = $this->userRepo->findByEmail($emailAddress);
|
|
|
|
|
|
|
|
|
|
if ($user->isEmpty()) {
|
|
|
|
|
throw new LucentException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($user->get()->role === Role::REMOVED) {
|
|
|
|
|
throw new LucentException("Cannot reset email if the user is not active");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$newToken = $this->userRepo->updateLoginToken($user->get()->id);
|
|
|
|
|
|
|
|
|
|
Mail::to($email)->send(
|
|
|
|
|
new LoginMail(
|
|
|
|
|
$email,
|
|
|
|
|
$newToken,
|
|
|
|
|
$this->channelService->channel->lucentUrl
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws LucentException
|
|
|
|
|
*/
|
|
|
|
|
public function changeRole(string $userId, string $newRole): void
|
|
|
|
|
{
|
|
|
|
|
$user = $this->userRepo->findById($userId);
|
|
|
|
|
|
|
|
|
|
if ($user->isEmpty()) {
|
|
|
|
|
throw new LucentException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$newUser = $user->get();
|
|
|
|
|
$newUser->role = Role::from($newRole);
|
|
|
|
|
$newUser->updatedAt = Carbon::now()->toJson();
|
|
|
|
|
$this->userRepo->update($newUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws LucentException
|
|
|
|
|
*/
|
2023-10-04 23:48:12 +03:00
|
|
|
public function updateName( string $name): void
|
2023-10-02 23:10:49 +03:00
|
|
|
{
|
|
|
|
|
$name = (new Name($name));
|
2023-10-04 23:48:12 +03:00
|
|
|
$this->userRepo->updateName($this->currentUserId(), $name);
|
2023-10-17 18:30:41 +03:00
|
|
|
$user = $this->userRepo->findById($this->currentUserId());
|
|
|
|
|
$this->session->put(["user" => $user->get()->safe()]);
|
|
|
|
|
}
|
2023-10-02 23:10:49 +03:00
|
|
|
|
2023-10-17 18:30:41 +03:00
|
|
|
/**
|
|
|
|
|
* @throws LucentException
|
|
|
|
|
*/
|
|
|
|
|
public function updateEmail(string $email): void
|
|
|
|
|
{
|
|
|
|
|
$email = (new Email($email));
|
|
|
|
|
$user = $this->userRepo->findByEmail($email);
|
|
|
|
|
if($user->isDefined()){
|
|
|
|
|
throw new LucentException("Email already assigned to user");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->userRepo->updateEmail($this->currentUserId(), $email);
|
|
|
|
|
$user = $this->userRepo->findById($this->currentUserId());
|
|
|
|
|
$this->session->put(["user" => $user->get()->safe()]);
|
2023-10-02 23:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws LucentException
|
|
|
|
|
*/
|
|
|
|
|
public function invite(
|
|
|
|
|
string $name,
|
|
|
|
|
string $email,
|
|
|
|
|
string $role
|
|
|
|
|
): User
|
|
|
|
|
{
|
|
|
|
|
$user = $this->create($name, $email, $role);
|
|
|
|
|
$this->sendLoginEmail($user->email);
|
|
|
|
|
return $user;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
/**
|
|
|
|
|
* @throws LucentException
|
|
|
|
|
*/
|
|
|
|
|
public function registerAdmin(
|
|
|
|
|
string $name,
|
|
|
|
|
string $email
|
|
|
|
|
): User
|
|
|
|
|
{
|
|
|
|
|
$user = $this->invite($name, $email, "admin");
|
|
|
|
|
$this->sendLoginEmail($user->email);
|
|
|
|
|
return $user;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
|
|
|
|
|
}
|