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

166 lines
4.5 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05: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 01:57:45 -05:00
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers;
2015-02-25 14:19:06 -06:00
2016-08-03 23:07:53 -05:00
use FireflyIII\Events\UserIsDeleted;
use FireflyIII\Http\Requests\DeleteAccountFormRequest;
2015-02-25 14:19:06 -06:00
use FireflyIII\Http\Requests\ProfileFormRequest;
2015-07-26 00:39:04 -05:00
use FireflyIII\User;
2015-02-25 14:19:06 -06:00
use Hash;
2016-08-03 23:14:08 -05:00
use Preferences;
2015-02-25 14:19:06 -06:00
use Session;
use View;
2015-02-25 14:19:06 -06:00
/**
* Class ProfileController
*
* @package FireflyIII\Http\Controllers
*/
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-01-08 11:29:47 -06:00
}
2015-02-25 14:19:06 -06:00
/**
* @return View
2015-02-25 14:19:06 -06:00
*/
public function changePassword()
{
2016-09-16 05:15:58 -05:00
return view('profile.change-password')->with('title', auth()->user()->email)->with('subTitle', trans('firefly.change_your_password'))->with(
2015-02-25 14:19:06 -06:00
'mainTitleIcon', 'fa-user'
);
}
/**
* @return View
*/
public function deleteAccount()
{
2016-09-16 05:15:58 -05:00
return view('profile.delete-account')->with('title', auth()->user()->email)->with('subTitle', trans('firefly.delete_account'))->with(
'mainTitleIcon', 'fa-user'
);
}
/**
* @return View
*
*/
2015-05-03 02:19:14 -05:00
public function index()
2015-04-28 08:26:30 -05:00
{
2016-09-16 05:15:58 -05:00
return view('profile.index')->with('title', trans('firefly.profile'))->with('subTitle', auth()->user()->email)->with('mainTitleIcon', 'fa-user');
}
2015-02-25 14:19:06 -06:00
/**
2015-05-03 05:58:55 -05:00
* @param ProfileFormRequest $request
*
2015-02-25 14:19:06 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
*/
public function postChangePassword(ProfileFormRequest $request)
{
// 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-09-16 05:15:58 -05:00
auth()->user()->password = bcrypt($request->get('new_password'));
auth()->user()->save();
2015-02-25 14:19:06 -06:00
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.password_changed')));
2015-02-25 14:19:06 -06:00
2015-07-06 09:27:21 -05:00
return redirect(route('profile'));
2015-02-25 14:19:06 -06:00
}
2015-05-03 02:19:14 -05:00
/**
2015-05-03 05:58:55 -05:00
* @param DeleteAccountFormRequest $request
2015-05-03 02:19:14 -05:00
*
2015-05-03 05:58:55 -05:00
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
2015-05-03 02:19:14 -05:00
*/
public function postDeleteAccount(DeleteAccountFormRequest $request)
{
// 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-08-03 23:07:53 -05:00
// respond to deletion:
2016-09-16 05:15:58 -05:00
event(new UserIsDeleted(auth()->user(), $request->ip()));
2016-08-03 23:07:53 -05:00
2016-08-03 23:10:30 -05:00
// store some stuff for the future:
2016-08-03 23:14:08 -05:00
$registration = Preferences::get('registration_ip_address')->data;
$confirmation = Preferences::get('confirmation_ip_address')->data;
2016-08-03 23:07:53 -05:00
2015-05-03 02:19:14 -05:00
// DELETE!
2016-09-16 05:15:58 -05:00
$email = auth()->user()->email;
auth()->user()->delete();
2015-05-03 02:19:14 -05:00
Session::flush();
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
// create a new user with the same email address so re-registration is blocked.
2016-08-03 23:14:08 -05:00
$newUser = User::create(
2015-07-26 00:39:04 -05:00
[
'email' => $email,
'password' => 'deleted',
'blocked' => 1,
2016-01-15 16:12:52 -06:00
'blocked_code' => 'deleted',
2015-07-26 00:39:04 -05:00
]
);
2016-08-03 23:14:08 -05:00
if (strlen($registration) > 0) {
Preferences::setForUser($newUser, 'registration_ip_address', $registration);
}
if (strlen($confirmation) > 0) {
Preferences::setForUser($newUser, 'confirmation_ip_address', $confirmation);
}
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
}