2017-02-04 08:42:06 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Sandstorm.php
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
*
|
2017-10-21 08:40:00 +02: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 14:44:05 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-04 08:42:06 +01:00
|
|
|
*/
|
2018-07-08 07:59:58 +02:00
|
|
|
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2017-02-04 08:42:06 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Middleware;
|
|
|
|
|
|
|
|
use Auth;
|
|
|
|
use Closure;
|
2017-02-22 21:27:39 +01:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
2017-02-04 08:42:06 +01:00
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Http\Request;
|
2017-12-26 17:33:53 +01:00
|
|
|
use Log;
|
2017-02-04 08:42:06 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class Sandstorm.
|
2017-02-04 08:42:06 +01:00
|
|
|
*/
|
|
|
|
class Sandstorm
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Detects if is using Sandstorm, and responds by logging the user
|
|
|
|
* in and/or creating an account.
|
|
|
|
*
|
2017-11-15 12:25:49 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @param string|null $guard
|
2017-02-04 08:42:06 +01:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next, $guard = null)
|
|
|
|
{
|
|
|
|
// is in Sandstorm environment?
|
2018-04-02 15:10:40 +02:00
|
|
|
$sandstorm = 1 === (int)getenv('SANDSTORM');
|
2018-07-14 16:08:34 +02:00
|
|
|
app('view')->share('SANDSTORM', $sandstorm);
|
2017-02-04 08:42:06 +01:00
|
|
|
if (!$sandstorm) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're in sandstorm! is user a guest?
|
|
|
|
if (Auth::guard($guard)->guest()) {
|
2017-02-22 21:27:39 +01:00
|
|
|
/** @var UserRepositoryInterface $repository */
|
|
|
|
$repository = app(UserRepositoryInterface::class);
|
2018-04-02 15:10:40 +02:00
|
|
|
$userId = (string)$request->header('X-Sandstorm-User-Id');
|
2018-07-24 17:46:34 +02:00
|
|
|
|
2018-07-17 22:21:03 +02:00
|
|
|
// catch anonymous:
|
2018-07-22 08:10:16 +02:00
|
|
|
$userId = '' === $userId ? 'anonymous' : $userId;
|
2018-07-17 22:21:03 +02:00
|
|
|
$email = $userId . '@firefly';
|
|
|
|
|
2018-07-24 17:46:34 +02:00
|
|
|
// always grab the first user in the Sandstorm DB:
|
|
|
|
$user = $repository->findByEmail($email) ?? $repository->first();
|
|
|
|
// or create somebody if necessary.
|
|
|
|
$user = $user ?? $this->createUser($email);
|
|
|
|
|
|
|
|
// then log this user in:
|
|
|
|
Log::info(sprintf('Sandstorm user ID is "%s"', $userId));
|
|
|
|
Log::info(sprintf('Access to database under "%s"', $email));
|
2018-07-17 22:21:03 +02:00
|
|
|
Auth::guard($guard)->login($user);
|
|
|
|
$repository->attachRole($user, 'owner');
|
|
|
|
app('view')->share('SANDSTORM_ANON', false);
|
|
|
|
}
|
2017-10-21 21:51:09 +02:00
|
|
|
|
2018-07-17 22:21:03 +02:00
|
|
|
return $next($request);
|
|
|
|
}
|
2017-02-22 21:27:39 +01:00
|
|
|
|
|
|
|
|
2018-07-17 22:21:03 +02:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Create a user.
|
|
|
|
*
|
2018-07-17 22:21:03 +02:00
|
|
|
* @param string $email
|
|
|
|
*
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
private function createUser(string $email): User
|
|
|
|
{
|
|
|
|
$repository = app(UserRepositoryInterface::class);
|
2017-12-26 17:33:53 +01:00
|
|
|
|
2018-07-17 22:21:03 +02:00
|
|
|
return $repository->store(
|
|
|
|
[
|
|
|
|
'blocked' => false,
|
|
|
|
'blocked_code' => null,
|
|
|
|
'email' => $email,
|
|
|
|
]
|
|
|
|
);
|
2017-02-04 08:42:06 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 22:21:03 +02:00
|
|
|
|
|
|
|
|