runningInConsole()){ return config("lucent.systemUserId"); }else{ return $this->session->get("user.id"); } } 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 */ public function updateName( string $name): void { $name = (new Name($name)); $this->userRepo->updateName($this->currentUserId(), $name); $user = $this->userRepo->findById($this->currentUserId()); $this->session->put(["user" => $user->get()->safe()]); } /** * @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()]); } /** * @throws LucentException */ public function invite( string $name, string $email, string $role ): User { $user = $this->create($name, $email, $role); $this->sendLoginEmail($user->email); return $user; } /** * @throws LucentException */ public function registerAdmin( string $name, string $email ): User { $user = $this->invite($name, $email, "admin"); $this->sendLoginEmail($user->email); return $user; } }