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

457 lines
14 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* ProfileController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
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/>.
*/
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
use Auth;
2018-04-02 08:17:03 -05:00
use DB;
use FireflyIII\Events\UserChangedEmail;
use FireflyIII\Exceptions\FireflyException;
2017-01-05 03:06:46 -06:00
use FireflyIII\Exceptions\ValidationException;
2017-12-19 12:25:50 -06:00
use FireflyIII\Http\Middleware\IsDemoUser;
use FireflyIII\Http\Middleware\IsSandStormUser;
use FireflyIII\Http\Requests\DeleteAccountFormRequest;
use FireflyIII\Http\Requests\EmailFormRequest;
2015-02-25 14:19:06 -06:00
use FireflyIII\Http\Requests\ProfileFormRequest;
2018-03-08 22:45:22 -06:00
use FireflyIII\Http\Requests\TokenFormRequest;
use FireflyIII\Models\Preference;
2016-12-12 08:24:47 -06:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-03-24 05:07:38 -05:00
use FireflyIII\User;
2018-03-08 22:45:22 -06:00
use Google2FA;
2015-02-25 14:19:06 -06:00
use Hash;
2017-11-25 01:54:52 -06:00
use Illuminate\Contracts\Auth\Guard;
2018-04-02 08:17:03 -05:00
use Laravel\Passport\ClientRepository;
2018-04-02 08:26:33 -05:00
use Laravel\Passport\Passport;
2016-12-12 08:24:47 -06:00
use Log;
2018-04-02 08:26:33 -05:00
use phpseclib\Crypt\RSA;
use Preferences;
use View;
2015-02-25 14:19:06 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class ProfileController.
2017-11-25 01:54:52 -06:00
*
* @method Guard guard()
2015-02-25 14:19:06 -06:00
*/
class ProfileController extends Controller
{
/**
* ProfileController constructor.
*/
2016-01-08 11:29:47 -06:00
public function __construct()
{
2016-01-08 13:40:48 -06:00
parent::__construct();
2016-10-20 12:10:43 -05:00
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.profile'));
app('view')->share('mainTitleIcon', 'fa-user');
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2017-12-19 12:25:50 -06:00
$this->middleware(IsDemoUser::class)->except(['index']);
$this->middleware(IsSandStormUser::class)->except('index');
2016-01-08 11:29:47 -06:00
}
2015-02-25 14:19:06 -06:00
/**
* @return View
*/
public function changeEmail()
{
$title = auth()->user()->email;
$email = auth()->user()->email;
2018-04-02 08:10:40 -05:00
$subTitle = (string)trans('firefly.change_your_email');
$subTitleIcon = 'fa-envelope';
return view('profile.change-email', compact('title', 'subTitle', 'subTitleIcon', 'email'));
}
2015-02-25 14:19:06 -06:00
/**
* @return View
2015-02-25 14:19:06 -06:00
*/
public function changePassword()
{
2016-10-29 00:44:46 -05:00
$title = auth()->user()->email;
2018-04-02 08:10:40 -05:00
$subTitle = (string)trans('firefly.change_your_password');
2016-10-29 00:44:46 -05:00
$subTitleIcon = 'fa-key';
return view('profile.change-password', compact('title', 'subTitle', 'subTitleIcon'));
2015-02-25 14:19:06 -06:00
}
2018-03-08 22:45:22 -06:00
/**
* View that generates a 2FA code for the user.
2018-03-10 15:38:20 -06:00
*
2018-03-08 22:45:22 -06:00
* @return View
*/
public function code()
{
$domain = $this->getDomain();
$secret = Google2FA::generateSecretKey();
2018-04-22 10:10:11 -05:00
session()->flash('two-factor-secret', $secret);
2018-03-08 22:45:22 -06:00
$image = Google2FA::getQRCodeInline($domain, auth()->user()->email, $secret, 200);
return view('profile.code', compact('image'));
}
/**
* @param UserRepositoryInterface $repository
* @param string $token
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2017-12-22 11:32:43 -06:00
*
* @throws FireflyException
*/
public function confirmEmailChange(UserRepositoryInterface $repository, string $token)
{
// find preference with this token value.
$set = Preferences::findByName('email_change_confirm_token');
$user = null;
Log::debug(sprintf('Found %d preferences', $set->count()));
/** @var Preference $preference */
foreach ($set as $preference) {
if ($preference->data === $token) {
Log::debug('Found user');
$user = $preference->user;
}
}
// update user to clear blocked and blocked_code.
2017-11-15 05:25:49 -06:00
if (null === $user) {
Log::debug('Found no user');
throw new FireflyException('Invalid token.');
}
Log::debug('Will unblock user.');
$repository->unblockUser($user);
// return to login.
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.login_with_new_email'));
return redirect(route('login'));
}
/**
* @return View
*/
public function deleteAccount()
{
2016-10-29 00:44:46 -05:00
$title = auth()->user()->email;
2018-04-02 08:10:40 -05:00
$subTitle = (string)trans('firefly.delete_account');
2016-10-29 00:44:46 -05:00
$subTitleIcon = 'fa-trash';
return view('profile.delete-account', compact('title', 'subTitle', 'subTitleIcon'));
}
2018-03-10 15:38:20 -06:00
/**
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function deleteCode()
{
Preferences::delete('twoFactorAuthEnabled');
Preferences::delete('twoFactorAuthSecret');
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.pref_two_factor_auth_disabled'));
session()->flash('info', (string)trans('firefly.pref_two_factor_auth_remove_it'));
2018-03-10 15:38:20 -06:00
return redirect(route('profile.index'));
}
2018-03-08 22:45:22 -06:00
/**
2018-03-11 10:24:07 -05:00
* @param UserRepositoryInterface $repository
*
2018-03-08 22:45:22 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function enable2FA(UserRepositoryInterface $repository)
{
if ($repository->hasRole(auth()->user(), 'demo')) {
return redirect(route('profile.index'));
}
$hasTwoFactorAuthSecret = (null !== Preferences::get('twoFactorAuthSecret'));
// if we don't have a valid secret yet, redirect to the code page to get one.
if (!$hasTwoFactorAuthSecret) {
return redirect(route('profile.code'));
}
// If FF3 already has a secret, just set the two factor auth enabled to 1,
// and let the user continue with the existing secret.
Preferences::set('twoFactorAuthEnabled', 1);
return redirect(route('profile.index'));
}
/**
* @return View
*/
2015-05-03 02:19:14 -05:00
public function index()
2015-04-28 08:26:30 -05:00
{
2018-04-02 08:17:03 -05:00
// check if client token thing exists (default one)
2018-04-02 08:26:33 -05:00
$count = DB::table('oauth_clients')
->where('personal_access_client', 1)
->whereNull('user_id')->count();
$this->createOAuthKeys();
2018-04-02 08:17:03 -05:00
if ($count === 0) {
/** @var ClientRepository $repository */
$repository = app(ClientRepository::class);
$repository->createPersonalAccessClient(null, config('app.name') . ' Personal Access Client', 'http://localhost');
}
2018-03-08 22:45:22 -06:00
$subTitle = auth()->user()->email;
$userId = auth()->user()->id;
2018-04-02 08:10:40 -05:00
$enabled2FA = (int)Preferences::get('twoFactorAuthEnabled', 0)->data === 1;
2016-10-20 12:10:43 -05:00
// get access token or create one.
$accessToken = Preferences::get('access_token', null);
2017-11-15 05:25:49 -06:00
if (null === $accessToken) {
$token = auth()->user()->generateAccessToken();
$accessToken = Preferences::set('access_token', $token);
}
2018-03-08 22:45:22 -06:00
return view('profile.index', compact('subTitle', 'userId', 'accessToken', 'enabled2FA'));
}
/**
* @param EmailFormRequest $request
* @param UserRepositoryInterface $repository
*
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function postChangeEmail(EmailFormRequest $request, UserRepositoryInterface $repository)
{
/** @var User $user */
$user = auth()->user();
$newEmail = $request->string('email');
$oldEmail = $user->email;
if ($newEmail === $user->email) {
2018-04-22 10:10:11 -05:00
session()->flash('error', (string)trans('firefly.email_not_changed'));
return redirect(route('profile.change-email'))->withInput();
}
$existing = $repository->findByEmail($newEmail);
2017-11-15 05:25:49 -06:00
if (null !== $existing) {
// force user logout.
Auth::guard()->logout();
$request->session()->invalidate();
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.email_changed'));
return redirect(route('index'));
}
// now actually update user:
$repository->changeEmail($user, $newEmail);
// call event.
$ipAddress = $request->ip();
event(new UserChangedEmail($user, $newEmail, $oldEmail, $ipAddress));
// force user logout.
Auth::guard()->logout();
$request->session()->invalidate();
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.email_changed'));
return redirect(route('index'));
}
2015-02-25 14:19:06 -06:00
/**
2016-12-18 10:54:11 -06:00
* @param ProfileFormRequest $request
* @param UserRepositoryInterface $repository
2015-05-03 05:58:55 -05:00
*
2016-12-18 10:54:11 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 14:19:06 -06:00
*/
2016-12-18 10:54:11 -06:00
public function postChangePassword(ProfileFormRequest $request, UserRepositoryInterface $repository)
2015-02-25 14:19:06 -06:00
{
2017-03-24 05:07:38 -05:00
// the request has already validated both new passwords must be equal.
$current = $request->get('current_password');
$new = $request->get('new_password');
2017-01-05 03:06:46 -06:00
try {
2017-03-24 05:07:38 -05:00
$this->validatePassword(auth()->user(), $current, $new);
2017-01-05 03:06:46 -06:00
} catch (ValidationException $e) {
2018-04-22 10:10:11 -05:00
session()->flash('error', $e->getMessage());
2015-02-25 14:19:06 -06:00
2015-07-06 09:27:21 -05:00
return redirect(route('profile.change-password'));
2015-02-25 14:19:06 -06:00
}
2016-12-18 10:54:11 -06:00
$repository->changePassword(auth()->user(), $request->get('new_password'));
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.password_changed'));
2015-02-25 14:19:06 -06:00
return redirect(route('profile.index'));
2015-02-25 14:19:06 -06:00
}
2018-03-10 15:38:20 -06:00
/**
* @param TokenFormRequest $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @SuppressWarnings(PHPMD.UnusedFormalParameter) // it's unused but the class does some validation.
*/
public function postCode(TokenFormRequest $request)
{
Preferences::set('twoFactorAuthEnabled', 1);
2018-04-22 10:12:22 -05:00
Preferences::set('twoFactorAuthSecret', session()->get('two-factor-secret'));
2018-03-10 15:38:20 -06:00
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.saved_preferences'));
2018-03-10 15:38:20 -06:00
Preferences::mark();
return redirect(route('profile.index'));
}
2015-05-03 02:19:14 -05:00
/**
2016-12-12 08:24:47 -06:00
* @param UserRepositoryInterface $repository
2015-05-03 05:58:55 -05:00
* @param DeleteAccountFormRequest $request
2015-05-03 02:19:14 -05:00
*
2016-12-12 08:24:47 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-05-03 02:19:14 -05:00
*/
2016-12-12 08:24:47 -06:00
public function postDeleteAccount(UserRepositoryInterface $repository, DeleteAccountFormRequest $request)
2015-05-03 02:19:14 -05:00
{
2016-09-16 05:15:58 -05:00
if (!Hash::check($request->get('password'), auth()->user()->password)) {
2018-04-22 10:10:11 -05:00
session()->flash('error', (string)trans('firefly.invalid_password'));
2015-05-03 02:19:14 -05:00
2015-07-06 09:27:21 -05:00
return redirect(route('profile.delete-account'));
2015-02-25 14:19:06 -06:00
}
2016-12-12 08:24:47 -06:00
$user = auth()->user();
Log::info(sprintf('User #%d has opted to delete their account', auth()->user()->id));
// make repository delete user:
auth()->logout();
2018-04-22 10:12:22 -05:00
session()->flush();
2016-12-12 08:24:47 -06:00
$repository->destroy($user);
2015-07-06 09:27:21 -05:00
return redirect(route('index'));
2015-02-25 14:19:06 -06:00
}
2015-05-03 02:19:14 -05:00
/**
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2017-11-15 03:52:29 -06:00
public function regenerate()
{
$token = auth()->user()->generateAccessToken();
Preferences::set('access_token', $token);
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.token_regenerated'));
return redirect(route('profile.index'));
}
/**
2017-12-17 07:30:53 -06:00
* @param UserRepositoryInterface $repository
* @param string $token
* @param string $hash
*
2017-11-17 22:46:19 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2017-11-18 09:30:45 -06:00
*
* @throws FireflyException
*/
public function undoEmailChange(UserRepositoryInterface $repository, string $token, string $hash)
{
// find preference with this token value.
$set = Preferences::findByName('email_change_undo_token');
$user = null;
/** @var Preference $preference */
foreach ($set as $preference) {
if ($preference->data === $token) {
$user = $preference->user;
}
}
2017-11-15 05:25:49 -06:00
if (null === $user) {
throw new FireflyException('Invalid token.');
}
// found user.
// which email address to return to?
2017-12-22 11:32:43 -06:00
$set = Preferences::beginsWith($user, 'previous_email_');
/** @var string $match */
$match = null;
foreach ($set as $entry) {
$hashed = hash('sha256', $entry->data);
if ($hashed === $hash) {
$match = $entry->data;
break;
}
}
2017-11-15 05:25:49 -06:00
if (null === $match) {
throw new FireflyException('Invalid token.');
}
// change user back
// now actually update user:
$repository->changeEmail($user, $match);
$repository->unblockUser($user);
// return to login.
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.login_with_old_email'));
return redirect(route('login'));
}
2016-01-24 08:58:16 -06:00
/**
2017-03-24 05:07:38 -05:00
* @param User $user
* @param string $current
2017-01-05 03:06:46 -06:00
* @param string $new
2016-01-24 08:58:16 -06:00
*
2017-01-05 03:06:46 -06:00
* @return bool
2017-11-15 05:25:49 -06:00
*
2017-01-05 03:06:46 -06:00
* @throws ValidationException
2016-01-24 08:58:16 -06:00
*/
2017-03-24 05:07:38 -05:00
protected function validatePassword(User $user, string $current, string $new): bool
2016-01-24 08:58:16 -06:00
{
if (!Hash::check($current, $user->password)) {
2018-04-02 08:10:40 -05:00
throw new ValidationException((string)trans('firefly.invalid_current_password'));
2017-03-24 05:07:38 -05:00
}
if ($current === $new) {
2018-04-02 08:10:40 -05:00
throw new ValidationException((string)trans('firefly.should_change'));
2016-01-24 08:58:16 -06:00
}
return true;
}
2018-03-08 22:45:22 -06:00
2018-04-02 08:26:33 -05:00
/**
*
*/
private function createOAuthKeys()
{
$rsa = new RSA();
$keys = $rsa->createKey(4096);
[$publicKey, $privateKey] = [
Passport::keyPath('oauth-public.key'),
Passport::keyPath('oauth-private.key'),
];
if (file_exists($publicKey) || file_exists($privateKey)) {
return;
}
Log::alert('NO OAuth keys were found. They have been created.');
file_put_contents($publicKey, array_get($keys, 'publickey'));
file_put_contents($privateKey, array_get($keys, 'privatekey'));
}
2018-03-08 22:45:22 -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
}