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