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

107 lines
3.1 KiB
PHP
Raw Normal View History

<?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/>.
*/
2018-07-08 07:59:58 +02:00
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
declare(strict_types=1);
namespace FireflyIII\Http\Middleware;
use Auth;
use Closure;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\Request;
2017-12-26 17:33:53 +01:00
use Log;
/**
2017-11-15 12:25:49 +01: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 12:25:49 +01:00
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
*
* @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');
app('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);
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);
}
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,
]
);
}
}
2018-07-17 22:21:03 +02:00