firefly-iii/app/Http/Middleware/Sandstorm.php

134 lines
4.5 KiB
PHP
Raw Normal View History

<?php
/**
* Sandstorm.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Middleware;
use Auth;
use Closure;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\Request;
2017-12-26 10:33:53 -06:00
use Log;
use View;
/**
2017-11-15 05:25:49 -06:00
* Class Sandstorm.
*/
class Sandstorm
{
/**
* Detects if is using Sandstorm, and responds by logging the user
* in and/or creating an account.
*
2017-11-15 05:25:49 -06:00
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
*
* @return mixed
2017-11-15 05:25:49 -06:00
*
2017-02-24 22:57:01 -06:00
* @throws FireflyException
2017-10-22 13:13:02 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function handle(Request $request, Closure $next, $guard = null)
{
// is in Sandstorm environment?
2017-11-15 05:25:49 -06:00
$sandstorm = 1 === intval(getenv('SANDSTORM'));
View::share('SANDSTORM', $sandstorm);
if (!$sandstorm) {
return $next($request);
}
// we're in sandstorm! is user a guest?
if (Auth::guard($guard)->guest()) {
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$userId = strval($request->header('X-Sandstorm-User-Id'));
2017-12-26 10:33:53 -06:00
Log::debug(sprintf('Sandstorm user ID is "%s"', $userId));
$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.
2017-11-15 05:25:49 -06:00
if (1 === $count && strlen($userId) > 0) {
// login as first user user.
2017-12-26 10:33:53 -06:00
$user = $repository->first();
Auth::guard($guard)->login($user);
View::share('SANDSTORM_ANON', false);
2017-02-24 22:57:01 -06:00
return $next($request);
}
2017-11-15 05:25:49 -06:00
if (1 === $count && 0 === strlen($userId)) {
// login but indicate anonymous
2017-02-24 22:57:01 -06:00
$user = User::first();
Auth::guard($guard)->login($user);
View::share('SANDSTORM_ANON', true);
2017-02-24 22:57:01 -06:00
return $next($request);
}
2017-11-15 05:25:49 -06:00
if (0 === $count && strlen($userId) > 0) {
// create new user.
$email = $userId . '@firefly';
2017-02-24 22:57:01 -06:00
/** @var User $user */
2017-12-26 10:33:53 -06:00
$user = $repository->store(
[
2018-03-03 01:12:18 -06:00
'blocked' => false,
'blocked_code' => null,
'email' => $email,
]
);
Auth::guard($guard)->login($user);
2017-02-24 22:57:01 -06:00
2017-10-21 14:51:09 -05:00
// also make the user an admin
2017-12-26 10:33:53 -06:00
$repository->attachRole($user, 'owner');
// share value.
View::share('SANDSTORM_ANON', false);
2017-10-21 14:51:09 -05:00
return $next($request);
}
2017-11-15 05:25:49 -06:00
if (0 === $count && 0 === strlen($userId)) {
throw new FireflyException('The first visit to a new Firefly III administration cannot be by a guest user.');
}
if ($count > 1) {
2017-02-24 22:57:01 -06:00
throw new FireflyException('Your Firefly III installation has more than one user, which is weird.');
}
}
2017-12-26 10:33:53 -06:00
// if in Sandstorm, user logged in, still must check if user is anon.
$userId = strval($request->header('X-Sandstorm-User-Id'));
if (strlen($userId) === 0) {
View::share('SANDSTORM_ANON', true);
return $next($request);
}
View::share('SANDSTORM_ANON', false);
return $next($request);
}
}