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
|
|
|
*/
|
|
|
|
|
2016-05-20 01:57:45 -05:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-02-25 14:19:06 -06:00
|
|
|
|
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;
|
2015-02-25 14:19:06 -06:00
|
|
|
use Hash;
|
2016-12-12 08:24:47 -06:00
|
|
|
use Log;
|
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);
|
|
|
|
}
|
|
|
|
);
|
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-12-25 06:09:29 -06:00
|
|
|
if (auth()->user()->hasRole('demo')) {
|
|
|
|
Session::flash('info', strval(trans('firefly.cannot_change_demo')));
|
|
|
|
|
|
|
|
return redirect(route('profile.index'));
|
|
|
|
}
|
|
|
|
|
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-12-25 06:09:29 -06:00
|
|
|
if (auth()->user()->hasRole('demo')) {
|
|
|
|
Session::flash('info', strval(trans('firefly.cannot_delete_demo')));
|
|
|
|
|
|
|
|
return redirect(route('profile.index'));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return view('profile.index', compact('subTitle', 'userId'));
|
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
|
|
|
{
|
2016-12-25 06:09:29 -06:00
|
|
|
if (auth()->user()->hasRole('demo')) {
|
|
|
|
Session::flash('info', strval(trans('firefly.cannot_change_demo')));
|
|
|
|
|
|
|
|
return redirect(route('profile.index'));
|
|
|
|
}
|
|
|
|
|
2015-02-25 14:19:06 -06:00
|
|
|
// old, new1, new2
|
2016-09-16 05:15:58 -05:00
|
|
|
if (!Hash::check($request->get('current_password'), auth()->user()->password)) {
|
2016-03-20 05:38:01 -05:00
|
|
|
Session::flash('error', strval(trans('firefly.invalid_current_password')));
|
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
|
|
|
}
|
2015-05-03 02:19:14 -05:00
|
|
|
$result = $this->validatePassword($request->get('current_password'), $request->get('new_password'));
|
2015-02-25 14:19:06 -06:00
|
|
|
if (!($result === true)) {
|
|
|
|
Session::flash('error', $result);
|
|
|
|
|
2015-07-06 09:27:21 -05:00
|
|
|
return redirect(route('profile.change-password'));
|
2015-02-25 14:19:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the user with the new password.
|
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-12-25 06:09:29 -06:00
|
|
|
if (auth()->user()->hasRole('demo')) {
|
|
|
|
Session::flash('info', strval(trans('firefly.cannot_delete_demo')));
|
|
|
|
|
|
|
|
return redirect(route('profile.index'));
|
|
|
|
}
|
|
|
|
|
2015-05-03 02:19:14 -05:00
|
|
|
// old, new1, new2
|
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
|
|
|
|
2016-01-24 08:58:16 -06:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $old
|
|
|
|
* @param string $new1
|
|
|
|
*
|
|
|
|
* @return string|bool
|
|
|
|
*/
|
2016-02-05 02:25:15 -06:00
|
|
|
protected function validatePassword(string $old, string $new1)
|
2016-01-24 08:58:16 -06:00
|
|
|
{
|
|
|
|
if ($new1 == $old) {
|
|
|
|
return trans('firefly.should_change');
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-03 02:19:14 -05:00
|
|
|
|
2015-02-25 14:19:06 -06:00
|
|
|
}
|