Optimize some code.

This commit is contained in:
James Cole
2018-07-17 22:21:03 +02:00
parent 9299efd086
commit b886cc1333
19 changed files with 194 additions and 127 deletions

View File

@@ -82,6 +82,9 @@ class Authenticate
* @return mixed
* @throws \Illuminate\Auth\AuthenticationException
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function authenticate(array $guards)
{

View File

@@ -52,17 +52,17 @@ class AuthenticateTwoFactor
}
/** @noinspection PhpUnusedParameterInspection */
/**
* @param $request
* @param Closure $next
* @param array ...$guards
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|mixed
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\Container\ContainerExceptionInterface
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function handle($request, Closure $next, ...$guards)
public function handle($request, Closure $next)
{
/** @noinspection PhpUndefinedMethodInspection */
if ($this->auth->guest()) {

View File

@@ -60,12 +60,11 @@ class Binder
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string[] ...$guards
*
* @return mixed
*
*/
public function handle($request, Closure $next, ...$guards)
public function handle($request, Closure $next)
{
foreach ($request->route()->parameters() as $key => $value) {
if (isset($this->binders[$key])) {

View File

@@ -45,6 +45,9 @@ class Installer
* @param \Closure $next
*
* @return mixed
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function handle($request, Closure $next)
{

View File

@@ -49,7 +49,7 @@ class IsSandStormUser
}
if (1 === (int)getenv('SANDSTORM')) {
Session::flash('warning', (string)trans('firefly.sandstorm_not_available'));
app('session')->flash('warning', (string)trans('firefly.sandstorm_not_available'));
return response()->redirectTo(route('index'));
}

View File

@@ -27,7 +27,6 @@ use Carbon\Carbon;
use Closure;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Http\Request;
use Session;
/**
* Class SessionFilter.
@@ -114,25 +113,21 @@ class Range
}
/**
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function setRange(): void
{
// ignore preference. set the range to be the current month:
if (!Session::has('start') && !Session::has('end')) {
if (!app('session')->has('start') && !app('session')->has('end')) {
$viewRange = app('preferences')->get('viewRange', '1M')->data;
if (null === $viewRange) {
$viewRange = '1M';
app('preferences')->set('viewRange', '1M');
}
$start = new Carbon;
$start = app('navigation')->updateStartDate($viewRange, $start);
$end = app('navigation')->updateEndDate($viewRange, $start);
$start = new Carbon;
$start = app('navigation')->updateStartDate($viewRange, $start);
$end = app('navigation')->updateEndDate($viewRange, $start);
Session::put('start', $start);
Session::put('end', $end);
app('session')->put('start', $start);
app('session')->put('end', $end);
}
if (!Session::has('first')) {
if (!app('session')->has('first')) {
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$journal = $repository->firstNull();
@@ -141,7 +136,7 @@ class Range
if (null !== $journal) {
$first = $journal->date ?? $first;
}
Session::put('first', $first);
app('session')->put('first', $first);
}
}
}

View File

@@ -38,16 +38,15 @@ class RedirectIfTwoFactorAuthenticated
* @param string|null $guard
*
* @return mixed
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
$is2faEnabled = app('preferences')->get('twoFactorAuthEnabled', false)->data;
$has2faSecret = null !== app('preferences')->get('twoFactorAuthSecret');
// grab 2auth information from cookie.
$is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated');
$is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated');
if ($is2faEnabled && $has2faSecret && $is2faAuthed) {
return response()->redirectTo(route('index'));

View File

@@ -62,73 +62,39 @@ class Sandstorm
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$userId = (string)$request->header('X-Sandstorm-User-Id');
Log::debug(sprintf('Sandstorm user ID is "%s"', $userId));
$count = $repository->count();
// catch anonymous:
$userId = $userId === '' ? 'anonymous' : $userId;
$email = $userId . '@firefly';
$user = $repository->findByEmail($email) ?? $this->createUser($email);
Log::debug(sprintf('Sandstorm user email is "%s"', $email));
// 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 (1 === $count && \strlen($userId) > 0) {
// login as first user user.
$user = $repository->first();
/** @noinspection NullPointerExceptionInspection */
Auth::guard($guard)->login($user);
app('view')->share('SANDSTORM_ANON', false);
return $next($request);
}
if (1 === $count && '' === $userId) {
// login but indicate anonymous
$user = User::first();
/** @noinspection NullPointerExceptionInspection */
Auth::guard($guard)->login($user);
app('view')->share('SANDSTORM_ANON', true);
return $next($request);
}
if (0 === $count && \strlen($userId) > 0) {
// create new user.
$email = $userId . '@firefly';
/** @var User $user */
$user = $repository->store(
[
'blocked' => false,
'blocked_code' => null,
'email' => $email,
]
);
Auth::guard($guard)->login($user);
// also make the user an admin
$repository->attachRole($user, 'owner');
// share value.
app('view')->share('SANDSTORM_ANON', false);
return $next($request);
}
if (0 === $count && '' === $userId) {
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.');
}
Auth::guard($guard)->login($user);
$repository->attachRole($user, 'owner');
app('view')->share('SANDSTORM_ANON', false);
}
// if in Sandstorm, user logged in, still must check if user is anon.
$userId = (string)$request->header('X-Sandstorm-User-Id');
if ('' === $userId) {
app('view')->share('SANDSTORM_ANON', true);
return $next($request);
}
app('view')->share('SANDSTORM_ANON', false);
return $next($request);
}
/**
* @param string $email
*
* @return User
*/
private function createUser(string $email): User
{
$repository = app(UserRepositoryInterface::class);
return $repository->store(
[
'blocked' => false,
'blocked_code' => null,
'email' => $email,
]
);
}
}