guest()) { /** @var UserRepositoryInterface $repository */ $repository = app(UserRepositoryInterface::class); $userId = strval($request->header('X-Sandstorm-User-Id')); $count = $repository->count(); // if there already is one user in this instance, we assume this is // the "main" user. Firefly's nature does not allow other users to // access the same data so we have no choice but to simply login // the new user to the same account and just forget about Bob and Alice // and any other differences there may be between these users. if ($count === 1 && strlen($userId) > 0) { // login as first user user. $user = User::first(); Auth::guard($guard)->login($user); View::share('SANDSTORM_ANON', false); return $next($request); } if ($count === 1 && strlen($userId) === 0) { // login but indicate anonymous $user = User::first(); Auth::guard($guard)->login($user); View::share('SANDSTORM_ANON', true); return $next($request); } if ($count === 0 && strlen($userId) > 0) { // create new user. $email = $userId . '@firefly'; /** @var User $user */ $user = User::create( [ 'email' => $email, 'password' => str_random(16), ] ); Auth::guard($guard)->login($user); return $next($request); } if ($count === 0 && strlen($userId) === 0) { throw new FireflyException('The first visit to a new Firefly III administration cannot be by a guest user.'); } if ($count > 1) { throw new FireflyException('Your Firefly III installation has more than one user, which is weird.'); } } return $next($request); } }