Files
firefly-iii/app/Http/Controllers/PreferencesController.php

241 lines
8.7 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* PreferencesController.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* 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:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2017-03-24 11:07:38 +01:00
declare(strict_types=1);
2016-05-20 08:57:45 +02:00
namespace FireflyIII\Http\Controllers;
2015-02-25 21:19:06 +01:00
2016-03-03 20:45:27 +01:00
use FireflyIII\Http\Requests\TokenFormRequest;
2016-05-20 11:02:07 +02:00
use FireflyIII\Models\AccountType;
2016-10-10 07:49:39 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-03-24 11:07:38 +01:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Http\Request;
use PragmaRX\Google2FA\Contracts\Google2FA;
2015-03-10 17:26:31 +01:00
use Preferences;
use Session;
use View;
2015-02-25 21:19:06 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class PreferencesController.
2015-02-25 21:19:06 +01:00
*/
2015-03-10 17:26:31 +01:00
class PreferencesController extends Controller
{
2015-02-25 21:19:06 +01:00
/**
2016-02-04 07:28:39 +01:00
*
2015-02-25 21:19:06 +01:00
*/
public function __construct()
{
2015-04-28 15:26:30 +02:00
parent::__construct();
2016-10-29 07:44:46 +02:00
$this->middleware(
function ($request, $next) {
2017-12-16 19:46:36 +01:00
app('view')->share('title', trans('firefly.preferences'));
app('view')->share('mainTitleIcon', 'fa-gear');
2016-10-29 07:44:46 +02:00
return $next($request);
}
);
2015-02-25 21:19:06 +01:00
}
/**
* @param Google2FA $google2fa
*
* @return View
*/
public function code(Google2FA $google2fa)
{
2017-05-03 21:12:49 +02:00
$domain = $this->getDomain();
2017-10-22 20:22:02 +02:00
$secret = $google2fa->generateSecretKey();
2016-03-19 07:56:57 +01:00
Session::flash('two-factor-secret', $secret);
2017-10-22 20:25:40 +02:00
$image = $google2fa->getQRCodeInline($domain, auth()->user()->email, $secret, 200);
2016-03-19 07:56:57 +01:00
return view('preferences.code', compact('image'));
}
/**
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2017-12-17 14:30:53 +01:00
* @throws \Exception
* @throws \Exception
*/
public function deleteCode()
{
Preferences::delete('twoFactorAuthEnabled');
Preferences::delete('twoFactorAuthSecret');
Session::flash('success', strval(trans('firefly.pref_two_factor_auth_disabled')));
Session::flash('info', strval(trans('firefly.pref_two_factor_auth_remove_it')));
return redirect(route('preferences.index'));
}
2015-02-25 21:19:06 +01:00
/**
2016-10-10 07:49:39 +02:00
* @param AccountRepositoryInterface $repository
2015-05-03 12:58:55 +02:00
*
2016-05-20 11:02:07 +02:00
* @return View
2015-02-25 21:19:06 +01:00
*/
2016-10-10 07:49:39 +02:00
public function index(AccountRepositoryInterface $repository)
2015-02-25 21:19:06 +01:00
{
2017-09-16 09:24:48 +02:00
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$viewRangePref = Preferences::get('viewRange', '1M');
$viewRange = $viewRangePref->data;
$frontPageAccounts = Preferences::get('frontPageAccounts', []);
$language = Preferences::get('language', config('firefly.default_language', 'en_US'))->data;
$listPageSize = Preferences::get('listPageSize', 50)->data;
2017-09-16 09:24:48 +02:00
$customFiscalYear = Preferences::get('customFiscalYear', 0)->data;
$showDeps = Preferences::get('showDepositsFrontpage', false)->data;
$fiscalYearStartStr = Preferences::get('fiscalYearStart', '01-01')->data;
$fiscalYearStart = date('Y') . '-' . $fiscalYearStartStr;
$tjOptionalFields = Preferences::get('transaction_journal_optional_fields', [])->data;
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', 0)->data; // twoFactorAuthEnabled
2017-11-15 12:25:49 +01:00
$has2faSecret = null !== Preferences::get('twoFactorAuthSecret'); // hasTwoFactorAuthSecret
2015-12-24 08:35:08 +01:00
2016-01-27 21:52:21 +01:00
return view(
'preferences.index',
compact(
2017-11-15 10:52:29 +01:00
'language',
'accounts',
'frontPageAccounts',
'tjOptionalFields',
'viewRange',
'customFiscalYear',
'listPageSize',
2017-11-15 10:52:29 +01:00
'fiscalYearStart',
'is2faEnabled',
'has2faSecret',
'showDeps'
)
2016-01-27 21:52:21 +01:00
);
2015-02-25 21:19:06 +01:00
}
/**
* @param TokenFormRequest $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2017-01-03 17:26:31 +01:00
* @SuppressWarnings(PHPMD.UnusedFormalParameter) // it's unused but the class does some validation.
*/
public function postCode(TokenFormRequest $request)
{
Preferences::set('twoFactorAuthEnabled', 1);
2016-03-19 07:59:55 +01:00
Preferences::set('twoFactorAuthSecret', Session::get('two-factor-secret'));
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.saved_preferences')));
Preferences::mark();
return redirect(route('preferences.index'));
}
2015-02-25 21:19:06 +01:00
/**
2017-04-09 07:56:46 +02:00
* @param Request $request
* @param UserRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 21:19:06 +01:00
*/
2017-03-24 11:07:38 +01:00
public function postIndex(Request $request, UserRepositoryInterface $repository)
2015-02-25 21:19:06 +01:00
{
// front page accounts
$frontPageAccounts = [];
if (is_array($request->get('frontPageAccounts'))) {
foreach ($request->get('frontPageAccounts') as $id) {
2015-05-14 12:10:42 +02:00
$frontPageAccounts[] = intval($id);
}
Preferences::set('frontPageAccounts', $frontPageAccounts);
2015-02-25 21:19:06 +01:00
}
// view range:
Preferences::set('viewRange', $request->get('viewRange'));
2015-02-25 21:19:06 +01:00
// forget session values:
Session::forget('start');
Session::forget('end');
Session::forget('range');
// custom fiscal year
2017-11-15 12:25:49 +01:00
$customFiscalYear = 1 === intval($request->get('customFiscalYear'));
2016-12-18 17:54:11 +01:00
$fiscalYearStart = date('m-d', strtotime(strval($request->get('fiscalYearStart'))));
Preferences::set('customFiscalYear', $customFiscalYear);
Preferences::set('fiscalYearStart', $fiscalYearStart);
// show deposits frontpage:
2017-11-15 12:25:49 +01:00
$showDepositsFrontpage = 1 === intval($request->get('showDepositsFrontpage'));
Preferences::set('showDepositsFrontpage', $showDepositsFrontpage);
2016-04-21 08:59:15 +02:00
// save page size:
Preferences::set('listPageSize', 50);
$listPageSize = intval($request->get('listPageSize'));
if ($listPageSize > 0 && $listPageSize < 1337) {
Preferences::set('listPageSize', $listPageSize);
2016-04-21 08:59:15 +02:00
}
$twoFactorAuthEnabled = false;
$hasTwoFactorAuthSecret = false;
2017-03-24 11:07:38 +01:00
if (!$repository->hasRole(auth()->user(), 'demo')) {
// two factor auth
$twoFactorAuthEnabled = intval($request->get('twoFactorAuthEnabled'));
2017-11-15 12:25:49 +01:00
$hasTwoFactorAuthSecret = null !== Preferences::get('twoFactorAuthSecret');
// If we already have a secret, just set the two factor auth enabled to 1, and let the user continue with the existing secret.
if ($hasTwoFactorAuthSecret) {
Preferences::set('twoFactorAuthEnabled', $twoFactorAuthEnabled);
}
2016-03-03 20:45:27 +01:00
}
2015-05-14 09:59:30 +02:00
// language:
$lang = $request->get('language');
2016-04-26 21:40:15 +02:00
if (in_array($lang, array_keys(config('firefly.languages')))) {
2015-05-14 09:59:30 +02:00
Preferences::set('language', $lang);
}
// optional fields for transactions:
$setOptions = $request->get('tj');
$optionalTj = [
'interest_date' => isset($setOptions['interest_date']),
'book_date' => isset($setOptions['book_date']),
'process_date' => isset($setOptions['process_date']),
'due_date' => isset($setOptions['due_date']),
'payment_date' => isset($setOptions['payment_date']),
'invoice_date' => isset($setOptions['invoice_date']),
'internal_reference' => isset($setOptions['internal_reference']),
'notes' => isset($setOptions['notes']),
'attachments' => isset($setOptions['attachments']),
];
Preferences::set('transaction_journal_optional_fields', $optionalTj);
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.saved_preferences')));
2016-03-03 20:45:27 +01:00
Preferences::mark();
// if we don't have a valid secret yet, redirect to the code page.
// AND USER HAS ACTUALLY ENABLED 2FA
2017-11-15 12:25:49 +01:00
if (!$hasTwoFactorAuthSecret && 1 === $twoFactorAuthEnabled) {
2016-03-03 20:45:27 +01:00
return redirect(route('preferences.code'));
}
return redirect(route('preferences.index'));
2016-03-03 20:45:27 +01:00
}
2016-03-07 20:17:43 +01:00
/**
* @return string
*/
2016-12-14 18:59:12 +01:00
private function getDomain(): string
2016-03-07 20:17:43 +01:00
{
$url = url()->to('/');
$parts = parse_url($url);
return $parts['host'];
}
2015-02-25 21:19:06 +01:00
}