From 3e3c48314ff9a7cbdeeca6ff3ed417469b898662 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 12 Dec 2016 15:24:47 +0100 Subject: [PATCH] Code for #456 --- app/Events/DeletedUser.php | 38 ++++++++++++++++++ app/Handlers/Events/UserEventHandler.php | 25 +++++++++++- app/Http/Controllers/ProfileController.php | 40 +++++-------------- app/Providers/EventServiceProvider.php | 4 ++ app/Repositories/User/UserRepository.php | 20 ++++++++++ .../User/UserRepositoryInterface.php | 7 ++++ app/Support/FireflyConfig.php | 2 +- 7 files changed, 105 insertions(+), 31 deletions(-) create mode 100644 app/Events/DeletedUser.php diff --git a/app/Events/DeletedUser.php b/app/Events/DeletedUser.php new file mode 100644 index 0000000000..2670b2c48a --- /dev/null +++ b/app/Events/DeletedUser.php @@ -0,0 +1,38 @@ +email = $email; + } +} \ No newline at end of file diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Handlers/Events/UserEventHandler.php index ef3b1d212f..4390f710ef 100644 --- a/app/Handlers/Events/UserEventHandler.php +++ b/app/Handlers/Events/UserEventHandler.php @@ -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 diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index e14fabe16b..8f8fbc6b07 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -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')); } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index fdcf61b5dd..8af4b0348c 100755 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -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', diff --git a/app/Repositories/User/UserRepository.php b/app/Repositories/User/UserRepository.php index 87ac639290..f33171c1e4 100644 --- a/app/Repositories/User/UserRepository.php +++ b/app/Repositories/User/UserRepository.php @@ -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 * diff --git a/app/Repositories/User/UserRepositoryInterface.php b/app/Repositories/User/UserRepositoryInterface.php index 1855fc1da1..66283a9ba0 100644 --- a/app/Repositories/User/UserRepositoryInterface.php +++ b/app/Repositories/User/UserRepositoryInterface.php @@ -48,6 +48,13 @@ interface UserRepositoryInterface */ public function count(): int; + /** + * @param User $user + * + * @return bool + */ + public function destroy(User $user): bool; + /** * @param int $userId * diff --git a/app/Support/FireflyConfig.php b/app/Support/FireflyConfig.php index 32ff7958c3..c2bd7800f7 100644 --- a/app/Support/FireflyConfig.php +++ b/app/Support/FireflyConfig.php @@ -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) {