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

198 lines
6.6 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* PreferencesController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2016-05-20 01:57:45 -05:00
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers;
2015-02-25 14:19:06 -06:00
2016-03-03 13:45:27 -06:00
use Auth;
2016-05-20 04:02:07 -05:00
use FireflyIII\Crud\Account\AccountCrudInterface;
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;
2015-02-25 14:19:06 -06:00
use Input;
use PragmaRX\Google2FA\Contracts\Google2FA;
2015-03-10 11:26:31 -05:00
use Preferences;
use Session;
use View;
2015-02-25 14:19:06 -06:00
/**
* Class PreferencesController
*
* @package FireflyIII\Http\Controllers
*/
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();
2015-05-14 08:53:56 -05:00
View::share('title', trans('firefly.preferences'));
2015-02-25 14:19:06 -06:00
View::share('mainTitleIcon', 'fa-gear');
}
/**
* @param Google2FA $google2fa
*
* @return View
*/
public function code(Google2FA $google2fa)
{
2016-03-07 13:17:43 -06:00
$domain = $this->getDomain();
/** @noinspection PhpMethodParametersCountMismatchInspection */
$secret = $google2fa->generateSecretKey(16, Auth::user()->id);
2016-03-19 01:56:57 -05:00
Session::flash('two-factor-secret', $secret);
2016-03-19 10:51:52 -05:00
$image = $google2fa->getQRCodeInline('Firefly III at ' . $domain, null, $secret, 150);
2016-03-19 01:56:57 -05:00
return view('preferences.code', compact('image'));
}
/**
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
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'));
}
2015-02-25 14:19:06 -06:00
/**
2016-05-20 04:02:07 -05:00
* @param AccountCrudInterface $crud
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-05-20 04:02:07 -05:00
public function index(AccountCrudInterface $crud)
2015-02-25 14:19:06 -06:00
{
2016-05-20 04:02:07 -05:00
$accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
2016-04-21 01:59:15 -05:00
$viewRangePref = Preferences::get('viewRange', '1M');
$viewRange = $viewRangePref->data;
$frontPageAccounts = Preferences::get('frontPageAccounts', []);
$budgetMax = Preferences::get('budgetMaximum', 1000);
$language = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'))->data;
$budgetMaximum = $budgetMax->data;
$transactionPageSize = Preferences::get('transactionPageSize', 50)->data;
$customFiscalYear = Preferences::get('customFiscalYear', 0)->data;
$fiscalYearStartStr = Preferences::get('fiscalYearStart', '01-01')->data;
$fiscalYearStart = date('Y') . '-' . $fiscalYearStartStr;
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', 0)->data; // twoFactorAuthEnabled
$has2faSecret = !is_null(Preferences::get('twoFactorAuthSecret')); // hasTwoFactorAuthSecret
$showIncomplete = env('SHOW_INCOMPLETE_TRANSLATIONS', false) === true;
2015-12-24 01:35:08 -06:00
2016-01-27 14:52:21 -06:00
return view(
'preferences.index',
compact(
'budgetMaximum', 'language', 'accounts', 'frontPageAccounts',
2016-04-21 01:59:15 -05:00
'viewRange', 'customFiscalYear', 'transactionPageSize', 'fiscalYearStart', 'is2faEnabled',
2016-03-20 10:46:26 -05:00
'has2faSecret', 'showIncomplete'
)
2016-01-27 14:52:21 -06:00
);
2015-02-25 14:19:06 -06:00
}
/**
* @param TokenFormRequest $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
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-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.saved_preferences')));
Preferences::mark();
return redirect(route('preferences'));
}
2015-02-25 14:19:06 -06:00
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function postIndex()
{
// front page accounts
$frontPageAccounts = [];
2015-05-14 05:10:42 -05:00
if (is_array(Input::get('frontPageAccounts'))) {
foreach (Input::get('frontPageAccounts') as $id) {
$frontPageAccounts[] = intval($id);
}
Preferences::set('frontPageAccounts', $frontPageAccounts);
2015-02-25 14:19:06 -06:00
}
// view range:
Preferences::set('viewRange', Input::get('viewRange'));
// forget session values:
Session::forget('start');
Session::forget('end');
Session::forget('range');
// budget maximum:
$budgetMaximum = intval(Input::get('budgetMaximum'));
Preferences::set('budgetMaximum', $budgetMaximum);
// custom fiscal year
2016-04-27 03:38:51 -05:00
$customFiscalYear = intval(Input::get('customFiscalYear')) === 1;
$fiscalYearStart = date('m-d', strtotime(Input::get('fiscalYearStart')));
Preferences::set('customFiscalYear', $customFiscalYear);
Preferences::set('fiscalYearStart', $fiscalYearStart);
2016-04-21 01:59:15 -05:00
// save page size:
$transactionPageSize = intval(Input::get('transactionPageSize'));
2016-04-25 11:43:09 -05:00
if ($transactionPageSize > 0 && $transactionPageSize < 1337) {
2016-04-21 01:59:15 -05:00
Preferences::set('transactionPageSize', $transactionPageSize);
} else {
Preferences::set('transactionPageSize', 50);
}
// two factor auth
$twoFactorAuthEnabled = intval(Input::get('twoFactorAuthEnabled'));
$hasTwoFactorAuthSecret = !is_null(Preferences::get('twoFactorAuthSecret'));
2016-03-03 13:45:27 -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) {
2016-03-03 13:45:27 -06:00
Preferences::set('twoFactorAuthEnabled', $twoFactorAuthEnabled);
}
2015-05-14 02:59:30 -05:00
// language:
$lang = Input::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);
}
2015-02-25 14:19:06 -06:00
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.
// AND USER HAS ACTUALLY ENABLED 2FA
if (!$hasTwoFactorAuthSecret && $twoFactorAuthEnabled === 1) {
2016-03-03 13:45:27 -06:00
return redirect(route('preferences.code'));
}
return redirect(route('preferences'));
}
2016-03-07 13:17:43 -06:00
/**
* @return string
*/
private function getDomain() : string
{
$url = url()->to('/');
$parts = parse_url($url);
return $parts['host'];
}
2015-02-25 14:19:06 -06:00
}