| 
									
										
										
										
											2016-05-20 08:57:45 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * PreferencesController.php | 
					
						
							|  |  |  |  * Copyright (C) 2016 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * This software may be modified and distributed under the terms of the | 
					
						
							|  |  |  |  * Creative Commons Attribution-ShareAlike 4.0 International License. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See the LICENSE file for details. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2016-09-04 16:21:51 +02:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2016-03-07 20:14:24 +01:00
										 |  |  | 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
										 |  |  | /** | 
					
						
							|  |  |  |  * Class PreferencesController | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Http\Controllers | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  |                 View::share('title', trans('firefly.preferences')); | 
					
						
							|  |  |  |                 View::share('mainTitleIcon', 'fa-gear'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-02-25 21:19:06 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 20:19:05 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Google2FA $google2fa | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return View | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-07 20:14:24 +01:00
										 |  |  |     public function code(Google2FA $google2fa) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-04-08 06:51:16 +02:00
										 |  |  |         $domain    = $this->getDomain(); | 
					
						
							|  |  |  |         $secretKey = 'FIREFLYIII'; | 
					
						
							| 
									
										
										
										
											2017-04-08 08:53:53 +02:00
										 |  |  |         $secretKey = str_pad($secretKey, intval(pow(2, ceil(log(strlen($secretKey), 2)))), 'X'); | 
					
						
							| 
									
										
										
										
											2017-04-08 06:51:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |         /** @noinspection PhpMethodParametersCountMismatchInspection */ | 
					
						
							| 
									
										
										
										
											2017-04-08 06:51:16 +02:00
										 |  |  |         $secret = $google2fa->generateSecretKey(16, $secretKey); | 
					
						
							| 
									
										
										
										
											2016-03-19 07:56:57 +01:00
										 |  |  |         Session::flash('two-factor-secret', $secret); | 
					
						
							| 
									
										
										
										
											2017-02-16 21:01:22 +01:00
										 |  |  |         $image = $google2fa->getQRCodeInline('Firefly III at ' . $domain, auth()->user()->email, $secret, 150); | 
					
						
							| 
									
										
										
										
											2016-03-07 20:14:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:56:57 +01:00
										 |  |  |         return view('preferences.code', compact('image')); | 
					
						
							| 
									
										
										
										
											2016-03-07 20:14:24 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 20:28:26 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @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'))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 21:58:23 +01:00
										 |  |  |         return redirect(route('preferences.index')); | 
					
						
							| 
									
										
										
										
											2016-03-07 20:28:26 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-14 19:52:30 +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; | 
					
						
							|  |  |  |         $transactionPageSize   = Preferences::get('transactionPageSize', 50)->data; | 
					
						
							|  |  |  |         $customFiscalYear      = Preferences::get('customFiscalYear', 0)->data; | 
					
						
							|  |  |  |         $showDepositsFrontpage = 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          = !is_null(Preferences::get('twoFactorAuthSecret')); // hasTwoFactorAuthSecret
 | 
					
						
							|  |  |  |         $showIncomplete        = env('SHOW_INCOMPLETE_TRANSLATIONS', false) === true; | 
					
						
							| 
									
										
										
										
											2015-12-24 08:35:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-27 21:52:21 +01:00
										 |  |  |         return view( | 
					
						
							|  |  |  |             'preferences.index', | 
					
						
							| 
									
										
										
										
											2016-02-10 06:25:21 +01:00
										 |  |  |             compact( | 
					
						
							| 
									
										
										
										
											2016-09-04 16:21:51 +02:00
										 |  |  |                 'language', 'accounts', 'frontPageAccounts', 'tjOptionalFields', | 
					
						
							| 
									
										
										
										
											2016-04-21 08:59:15 +02:00
										 |  |  |                 'viewRange', 'customFiscalYear', 'transactionPageSize', 'fiscalYearStart', 'is2faEnabled', | 
					
						
							| 
									
										
										
										
											2016-10-14 19:52:30 +02:00
										 |  |  |                 'has2faSecret', 'showIncomplete', 'showDepositsFrontpage' | 
					
						
							| 
									
										
										
										
											2016-02-10 06:25:21 +01:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2016-01-27 21:52:21 +01:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-02-25 21:19:06 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 20:14:24 +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.
 | 
					
						
							| 
									
										
										
										
											2016-03-07 20:14:24 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     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-07 20:14:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:38:01 +01:00
										 |  |  |         Session::flash('success', strval(trans('firefly.saved_preferences'))); | 
					
						
							| 
									
										
										
										
											2016-03-07 20:14:24 +01:00
										 |  |  |         Preferences::mark(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 21:58:23 +01:00
										 |  |  |         return redirect(route('preferences.index')); | 
					
						
							| 
									
										
										
										
											2016-03-07 20:14:24 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 21:19:06 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-04-09 07:56:46 +02:00
										 |  |  |      * @param Request                 $request | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param UserRepositoryInterface $repository | 
					
						
							| 
									
										
										
										
											2016-09-04 16:21:51 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @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 = []; | 
					
						
							| 
									
										
										
										
											2016-09-04 16:21:51 +02:00
										 |  |  |         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:
 | 
					
						
							| 
									
										
										
										
											2016-09-04 16:21:51 +02:00
										 |  |  |         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'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-22 21:09:02 +10:00
										 |  |  |         // custom fiscal year
 | 
					
						
							| 
									
										
										
										
											2016-09-04 16:21:51 +02:00
										 |  |  |         $customFiscalYear = intval($request->get('customFiscalYear')) === 1; | 
					
						
							| 
									
										
										
										
											2016-12-18 17:54:11 +01:00
										 |  |  |         $fiscalYearStart  = date('m-d', strtotime(strval($request->get('fiscalYearStart')))); | 
					
						
							| 
									
										
										
										
											2016-01-22 21:09:02 +10:00
										 |  |  |         Preferences::set('customFiscalYear', $customFiscalYear); | 
					
						
							| 
									
										
										
										
											2016-01-24 16:47:39 +10:00
										 |  |  |         Preferences::set('fiscalYearStart', $fiscalYearStart); | 
					
						
							| 
									
										
										
										
											2016-01-22 21:09:02 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 19:52:30 +02:00
										 |  |  |         // show deposits frontpage:
 | 
					
						
							|  |  |  |         $showDepositsFrontpage = intval($request->get('showDepositsFrontpage')) === 1; | 
					
						
							|  |  |  |         Preferences::set('showDepositsFrontpage', $showDepositsFrontpage); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 08:59:15 +02:00
										 |  |  |         // save page size:
 | 
					
						
							| 
									
										
										
										
											2017-03-22 17:02:15 +01:00
										 |  |  |         Preferences::set('transactionPageSize', 50); | 
					
						
							| 
									
										
										
										
											2016-09-04 16:21:51 +02:00
										 |  |  |         $transactionPageSize = intval($request->get('transactionPageSize')); | 
					
						
							| 
									
										
										
										
											2016-04-25 18:43:09 +02:00
										 |  |  |         if ($transactionPageSize > 0 && $transactionPageSize < 1337) { | 
					
						
							| 
									
										
										
										
											2016-04-21 08:59:15 +02:00
										 |  |  |             Preferences::set('transactionPageSize', $transactionPageSize); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-26 08:57:07 +01:00
										 |  |  |         $twoFactorAuthEnabled   = false; | 
					
						
							|  |  |  |         $hasTwoFactorAuthSecret = false; | 
					
						
							| 
									
										
										
										
											2017-03-24 11:07:38 +01:00
										 |  |  |         if (!$repository->hasRole(auth()->user(), 'demo')) { | 
					
						
							| 
									
										
										
										
											2016-12-26 08:57:07 +01:00
										 |  |  |             // two factor auth
 | 
					
						
							|  |  |  |             $twoFactorAuthEnabled   = intval($request->get('twoFactorAuthEnabled')); | 
					
						
							|  |  |  |             $hasTwoFactorAuthSecret = !is_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
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-02-08 22:28:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 09:59:30 +02:00
										 |  |  |         // language:
 | 
					
						
							| 
									
										
										
										
											2016-09-04 16:21:51 +02:00
										 |  |  |         $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); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-04 16:21:51 +02: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 18:36:52 +02:00
										 |  |  |             'invoice_date'       => isset($setOptions['invoice_date']), | 
					
						
							| 
									
										
										
										
											2016-09-04 16:21:51 +02:00
										 |  |  |             'internal_reference' => isset($setOptions['internal_reference']), | 
					
						
							|  |  |  |             'notes'              => isset($setOptions['notes']), | 
					
						
							|  |  |  |             'attachments'        => isset($setOptions['attachments']), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         Preferences::set('transaction_journal_optional_fields', $optionalTj); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 21:19:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							| 
									
										
										
										
											2016-03-07 20:14:24 +01:00
										 |  |  |         // AND USER HAS ACTUALLY ENABLED 2FA
 | 
					
						
							|  |  |  |         if (!$hasTwoFactorAuthSecret && $twoFactorAuthEnabled === 1) { | 
					
						
							| 
									
										
										
										
											2016-03-03 20:45:27 +01:00
										 |  |  |             return redirect(route('preferences.code')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 21:58:23 +01:00
										 |  |  |         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
										 |  |  | } |