2016-05-20 01:57:45 -05:00
|
|
|
<?php
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* ProfileController.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05: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 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
|
|
|
|
2017-01-05 03:06:46 -06:00
|
|
|
use FireflyIII\Exceptions\ValidationException;
|
2017-03-24 05:07:38 -05:00
|
|
|
use FireflyIII\Http\Middleware\IsLimitedUser;
|
2015-04-22 00:54:56 -05:00
|
|
|
use FireflyIII\Http\Requests\DeleteAccountFormRequest;
|
2015-02-25 14:19:06 -06:00
|
|
|
use FireflyIII\Http\Requests\ProfileFormRequest;
|
2016-12-12 08:24:47 -06:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
2017-03-24 05:07:38 -05:00
|
|
|
use FireflyIII\User;
|
2015-02-25 14:19:06 -06:00
|
|
|
use Hash;
|
2016-12-12 08:24:47 -06:00
|
|
|
use Log;
|
2017-09-14 11:27:22 -05:00
|
|
|
use Preferences;
|
2015-02-25 14:19:06 -06:00
|
|
|
use Session;
|
2016-08-26 02:30:52 -05:00
|
|
|
use View;
|
2015-02-25 14:19:06 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ProfileController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers
|
|
|
|
*/
|
|
|
|
class ProfileController extends Controller
|
|
|
|
{
|
2016-01-09 01:20:55 -06:00
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
View::share('title', trans('firefly.profile'));
|
|
|
|
View::share('mainTitleIcon', 'fa-user');
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2017-03-24 05:07:38 -05:00
|
|
|
$this->middleware(IsLimitedUser::class);
|
|
|
|
|
2016-01-08 11:29:47 -06:00
|
|
|
}
|
2015-02-25 14:19:06 -06:00
|
|
|
|
|
|
|
/**
|
2016-08-26 02:30:52 -05: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;
|
|
|
|
$subTitle = strval(trans('firefly.change_your_password'));
|
|
|
|
$subTitleIcon = 'fa-key';
|
|
|
|
|
|
|
|
return view('profile.change-password', compact('title', 'subTitle', 'subTitleIcon'));
|
2015-02-25 14:19:06 -06:00
|
|
|
}
|
|
|
|
|
2015-04-22 00:54:56 -05:00
|
|
|
/**
|
2016-08-26 02:30:52 -05:00
|
|
|
* @return View
|
2015-04-22 00:54:56 -05:00
|
|
|
*/
|
|
|
|
public function deleteAccount()
|
|
|
|
{
|
2016-10-29 00:44:46 -05:00
|
|
|
$title = auth()->user()->email;
|
|
|
|
$subTitle = strval(trans('firefly.delete_account'));
|
|
|
|
$subTitleIcon = 'fa-trash';
|
|
|
|
|
|
|
|
return view('profile.delete-account', compact('title', 'subTitle', 'subTitleIcon'));
|
2015-04-22 00:54:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-26 02:30:52 -05:00
|
|
|
* @return View
|
2015-04-22 00:54:56 -05:00
|
|
|
*
|
|
|
|
*/
|
2015-05-03 02:19:14 -05:00
|
|
|
public function index()
|
2015-04-28 08:26:30 -05:00
|
|
|
{
|
2016-10-20 12:10:43 -05:00
|
|
|
$subTitle = auth()->user()->email;
|
|
|
|
$userId = auth()->user()->id;
|
|
|
|
|
2017-09-14 11:27:22 -05:00
|
|
|
// get access token or create one.
|
|
|
|
$accessToken = Preferences::get('access_token', null);
|
|
|
|
if (is_null($accessToken)) {
|
|
|
|
$token = auth()->user()->generateAccessToken();
|
|
|
|
$accessToken = Preferences::set('access_token', $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('profile.index', compact('subTitle', 'userId', 'accessToken'));
|
2015-04-22 00:54:56 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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'));
|
2016-03-20 05:38:01 -05:00
|
|
|
Session::flash('success', strval(trans('firefly.password_changed')));
|
2015-02-25 14:19:06 -06:00
|
|
|
|
2016-12-05 14:58:23 -06:00
|
|
|
return redirect(route('profile.index'));
|
2015-02-25 14:19:06 -06:00
|
|
|
}
|
|
|
|
|
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)) {
|
2016-03-20 05:38:01 -05:00
|
|
|
Session::flash('error', strval(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();
|
2015-05-03 02:19:14 -05:00
|
|
|
Session::flush();
|
2016-12-12 08:24:47 -06:00
|
|
|
$repository->destroy($user);
|
|
|
|
|
2015-05-25 00:26:19 -05:00
|
|
|
Session::flash('gaEventCategory', 'user');
|
|
|
|
Session::flash('gaEventAction', 'delete-account');
|
2015-02-25 14:19:06 -06:00
|
|
|
|
2015-07-26 00:39:04 -05:00
|
|
|
|
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
|
|
|
|
2017-09-14 11:27:22 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function regenerate()
|
|
|
|
{
|
|
|
|
$token = auth()->user()->generateAccessToken();
|
|
|
|
Preferences::set('access_token', $token);
|
|
|
|
Session::flash('success', strval(trans('firefly.token_regenerated')));
|
|
|
|
|
|
|
|
return redirect(route('profile.index'));
|
|
|
|
}
|
2017-04-09 00:44:22 -05:00
|
|
|
|
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
|
|
|
|
* @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
|
|
|
{
|
2017-04-09 00:44:22 -05:00
|
|
|
if (!Hash::check($current, $user->password)) {
|
2017-03-24 05:07:38 -05:00
|
|
|
throw new ValidationException(strval(trans('firefly.invalid_current_password')));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($current === $new) {
|
2017-01-05 03:06:46 -06:00
|
|
|
throw new ValidationException(strval(trans('firefly.should_change')));
|
2016-01-24 08:58:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-03 02:19:14 -05:00
|
|
|
|
2015-02-25 14:19:06 -06:00
|
|
|
}
|