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

161 lines
4.2 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* ProfileController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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 08:57:45 +02:00
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers;
2015-02-25 21:19:06 +01:00
use FireflyIII\Http\Requests\DeleteAccountFormRequest;
2015-02-25 21:19:06 +01:00
use FireflyIII\Http\Requests\ProfileFormRequest;
2016-12-12 15:24:47 +01:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2015-02-25 21:19:06 +01:00
use Hash;
2016-12-12 15:24:47 +01:00
use Log;
2015-02-25 21:19:06 +01:00
use Session;
use View;
2015-02-25 21:19:06 +01:00
/**
* Class ProfileController
*
* @package FireflyIII\Http\Controllers
*/
class ProfileController extends Controller
{
/**
* ProfileController constructor.
*/
2016-01-08 18:29:47 +01:00
public function __construct()
{
2016-01-08 20:40:48 +01:00
parent::__construct();
2016-10-20 19:10:43 +02:00
2016-10-29 07:44:46 +02:00
$this->middleware(
function ($request, $next) {
View::share('title', trans('firefly.profile'));
View::share('mainTitleIcon', 'fa-user');
return $next($request);
}
);
2016-01-08 18:29:47 +01:00
}
2015-02-25 21:19:06 +01:00
/**
* @return View
2015-02-25 21:19:06 +01:00
*/
public function changePassword()
{
2016-10-29 07:44:46 +02: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 21:19:06 +01:00
}
/**
* @return View
*/
public function deleteAccount()
{
2016-10-29 07:44:46 +02:00
$title = auth()->user()->email;
$subTitle = strval(trans('firefly.delete_account'));
$subTitleIcon = 'fa-trash';
return view('profile.delete-account', compact('title', 'subTitle', 'subTitleIcon'));
}
/**
* @return View
*
*/
2015-05-03 09:19:14 +02:00
public function index()
2015-04-28 15:26:30 +02:00
{
2016-10-20 19:10:43 +02:00
$subTitle = auth()->user()->email;
$userId = auth()->user()->id;
return view('profile.index', compact('subTitle', 'userId'));
}
2015-02-25 21:19:06 +01:00
/**
2015-05-03 12:58:55 +02:00
* @param ProfileFormRequest $request
*
2015-02-25 21:19:06 +01:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
*/
public function postChangePassword(ProfileFormRequest $request)
{
// old, new1, new2
2016-09-16 12:15:58 +02:00
if (!Hash::check($request->get('current_password'), auth()->user()->password)) {
2016-03-20 11:38:01 +01:00
Session::flash('error', strval(trans('firefly.invalid_current_password')));
2015-02-25 21:19:06 +01:00
2015-07-06 16:27:21 +02:00
return redirect(route('profile.change-password'));
2015-02-25 21:19:06 +01:00
}
2015-05-03 09:19:14 +02:00
$result = $this->validatePassword($request->get('current_password'), $request->get('new_password'));
2015-02-25 21:19:06 +01:00
if (!($result === true)) {
Session::flash('error', $result);
2015-07-06 16:27:21 +02:00
return redirect(route('profile.change-password'));
2015-02-25 21:19:06 +01:00
}
// update the user with the new password.
2016-09-16 12:15:58 +02:00
auth()->user()->password = bcrypt($request->get('new_password'));
auth()->user()->save();
2015-02-25 21:19:06 +01:00
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.password_changed')));
2015-02-25 21:19:06 +01:00
return redirect(route('profile.index'));
2015-02-25 21:19:06 +01:00
}
2015-05-03 09:19:14 +02:00
/**
2016-12-12 15:24:47 +01:00
* @param UserRepositoryInterface $repository
2015-05-03 12:58:55 +02:00
* @param DeleteAccountFormRequest $request
2015-05-03 09:19:14 +02:00
*
2016-12-12 15:24:47 +01:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-05-03 09:19:14 +02:00
*/
2016-12-12 15:24:47 +01:00
public function postDeleteAccount(UserRepositoryInterface $repository, DeleteAccountFormRequest $request)
2015-05-03 09:19:14 +02:00
{
// old, new1, new2
2016-09-16 12:15:58 +02:00
if (!Hash::check($request->get('password'), auth()->user()->password)) {
2016-03-20 11:38:01 +01:00
Session::flash('error', strval(trans('firefly.invalid_password')));
2015-05-03 09:19:14 +02:00
2015-07-06 16:27:21 +02:00
return redirect(route('profile.delete-account'));
2015-02-25 21:19:06 +01:00
}
2016-12-12 15:24:47 +01: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 09:19:14 +02:00
Session::flush();
2016-12-12 15:24:47 +01:00
$repository->destroy($user);
2015-05-25 07:26:19 +02:00
Session::flash('gaEventCategory', 'user');
Session::flash('gaEventAction', 'delete-account');
2015-02-25 21:19:06 +01:00
2015-07-26 07:39:04 +02:00
2015-07-06 16:27:21 +02:00
return redirect(route('index'));
2015-02-25 21:19:06 +01:00
}
2015-05-03 09:19:14 +02:00
2016-01-24 15:58:16 +01:00
/**
*
* @param string $old
* @param string $new1
*
* @return string|bool
*/
2016-02-05 09:25:15 +01:00
protected function validatePassword(string $old, string $new1)
2016-01-24 15:58:16 +01:00
{
if ($new1 == $old) {
return trans('firefly.should_change');
}
return true;
}
2015-05-03 09:19:14 +02:00
2015-02-25 21:19:06 +01:00
}