This commit is contained in:
James Cole 2016-12-12 15:24:47 +01:00
parent 06ff450d31
commit 3e3c48314f
7 changed files with 105 additions and 31 deletions

View File

@ -0,0 +1,38 @@
<?php
/**
* DeletedUser.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Events;
use Illuminate\Queue\SerializesModels;
/**
* Class DeletedUser
*
* @package FireflyIII\Events
*/
class DeletedUser extends Event
{
use SerializesModels;
public $email;
/**
* Create a new event instance. This event is triggered when a user deletes themselves.
*
* @param string $email
*/
public function __construct(string $email)
{
$this->email = $email;
}
}

View File

@ -16,9 +16,11 @@ namespace FireflyIII\Handlers\Events;
use Exception;
use FireflyConfig;
use FireflyIII\Events\ConfirmedUser;
use FireflyIII\Events\DeletedUser;
use FireflyIII\Events\RegisteredUser;
use FireflyIII\Events\RequestedNewPassword;
use FireflyIII\Events\ResentConfirmation;
use FireflyIII\Models\Configuration;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Mail\Message;
@ -75,6 +77,28 @@ class UserEventHandler
return true;
}
/**
* @param DeletedUser $event
*
* @return bool
*/
public function saveEmailAddress(DeletedUser $event): bool
{
$email = hash('sha256', $event->email);
Log::debug(sprintf('Hash of email is %s', $email));
/** @var Configuration $configuration */
$configuration = FireflyConfig::get('deleted_users', []);
$content = $configuration->data;
if (!is_array($content)) {
$content = [];
}
$content[] = $email;
$configuration->data = $content;
$configuration->save();
return true;
}
/**
* This method will send a newly registered user a confirmation message, urging him or her to activate their account.
*
@ -194,7 +218,6 @@ class UserEventHandler
}
/**
* @param User $user
* @param string $ipAddress

View File

@ -15,9 +15,9 @@ namespace FireflyIII\Http\Controllers;
use FireflyIII\Http\Requests\DeleteAccountFormRequest;
use FireflyIII\Http\Requests\ProfileFormRequest;
use FireflyIII\User;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Hash;
use Preferences;
use Log;
use Session;
use View;
@ -112,12 +112,12 @@ class ProfileController extends Controller
}
/**
* @param UserRepositoryInterface $repository
* @param DeleteAccountFormRequest $request
*
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function postDeleteAccount(DeleteAccountFormRequest $request)
public function postDeleteAccount(UserRepositoryInterface $repository, DeleteAccountFormRequest $request)
{
// old, new1, new2
if (!Hash::check($request->get('password'), auth()->user()->password)) {
@ -125,34 +125,16 @@ class ProfileController extends Controller
return redirect(route('profile.delete-account'));
}
// store some stuff for the future:
$registration = Preferences::get('registration_ip_address')->data;
$confirmation = Preferences::get('confirmation_ip_address')->data;
// DELETE!
$email = auth()->user()->email;
auth()->user()->delete();
$user = auth()->user();
Log::info(sprintf('User #%d has opted to delete their account', auth()->user()->id));
// make repository delete user:
auth()->logout();
Session::flush();
$repository->destroy($user);
Session::flash('gaEventCategory', 'user');
Session::flash('gaEventAction', 'delete-account');
// create a new user with the same email address so re-registration is blocked.
$newUser = User::create(
[
'email' => $email,
'password' => 'deleted',
'blocked' => 1,
'blocked_code' => 'deleted',
]
);
if (strlen($registration) > 0) {
Preferences::setForUser($newUser, 'registration_ip_address', $registration);
}
if (strlen($confirmation) > 0) {
Preferences::setForUser($newUser, 'confirmation_ip_address', $confirmation);
}
return redirect(route('index'));
}

View File

@ -41,6 +41,10 @@ class EventServiceProvider extends ServiceProvider
[
'FireflyIII\Handlers\Events\UserEventHandler@storeConfirmationIpAddress',
],
'FireflyIII\Events\DeletedUser' => // is a User related event.
[
'FireflyIII\Handlers\Events\UserEventHandler@saveEmailAddress',
],
'FireflyIII\Events\RegisteredUser' => // is a User related event.
[
'FireflyIII\Handlers\Events\UserEventHandler@sendRegistrationMail',

View File

@ -15,10 +15,12 @@ namespace FireflyIII\Repositories\User;
use FireflyConfig;
use FireflyIII\Events\DeletedUser;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\Role;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
use Preferences;
/**
@ -60,6 +62,24 @@ class UserRepository implements UserRepositoryInterface
return $this->all()->count();
}
/**
* @param User $user
*
* @return bool
*/
public function destroy(User $user): bool
{
$email = $user->email;
Log::debug(sprintf('Calling delete() on user %d', $user->id));
$user->delete();
// trigger event:
event(new DeletedUser($email));
return true;
}
/**
* @param int $userId
*

View File

@ -48,6 +48,13 @@ interface UserRepositoryInterface
*/
public function count(): int;
/**
* @param User $user
*
* @return bool
*/
public function destroy(User $user): bool;
/**
* @param int $userId
*

View File

@ -45,7 +45,7 @@ class FireflyConfig
* @param $name
* @param null $default
*
* @return Configuration|null
* @return \FireflyIII\Models\Configuration|null
*/
public function get($name, $default = null)
{