From 208f6a0ee00288db26e5c8f478fd8a4e7ccf88a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Freyd?= Date: Tue, 13 Jun 2017 00:16:10 -0400 Subject: [PATCH] Remove encryption on the Gifts table (#249) This is just causing too much headache, especially since I made the rookie mistake of not checking the max length of some entries when inserting the gift. That leads to errors in the decryption process. In production, out of the thousands of gifts already entered, only 5 are not decryptable. I have no other choice than resetting these 5 rows. I'm really sorry about this. If you do end up having problems running the migration in your own instance, you need to remove the rows that are un-decryptable in the Gifts table as well. --- app/Events/Gift/GiftCreated.php | 39 --------------- app/Events/Gift/GiftDeleted.php | 39 --------------- app/Gift.php | 13 ++--- app/Http/Controllers/PeopleController.php | 44 +++++++++++++++-- app/Listeners/Gift/DecreaseNumberOfGifts.php | 48 ------------------- app/Listeners/Gift/IncrementNumberOfGifts.php | 40 ---------------- app/Listeners/Gift/LogGiftCreatedEvent.php | 38 --------------- app/Listeners/Gift/RemoveAllGiftEvents.php | 40 ---------------- app/Providers/EventServiceProvider.php | 16 ------- ...7_06_13_035059_remove_gifts_encryption.php | 37 ++++++++++++++ tests/Unit/GiftTest.php | 6 +-- 11 files changed, 84 insertions(+), 276 deletions(-) delete mode 100644 app/Events/Gift/GiftCreated.php delete mode 100644 app/Events/Gift/GiftDeleted.php delete mode 100644 app/Listeners/Gift/DecreaseNumberOfGifts.php delete mode 100644 app/Listeners/Gift/IncrementNumberOfGifts.php delete mode 100644 app/Listeners/Gift/LogGiftCreatedEvent.php delete mode 100644 app/Listeners/Gift/RemoveAllGiftEvents.php create mode 100644 database/migrations/2017_06_13_035059_remove_gifts_encryption.php diff --git a/app/Events/Gift/GiftCreated.php b/app/Events/Gift/GiftCreated.php deleted file mode 100644 index d4e467ad7..000000000 --- a/app/Events/Gift/GiftCreated.php +++ /dev/null @@ -1,39 +0,0 @@ -gift = $gift; - } - - /** - * Get the channels the event should broadcast on. - * - * @return Channel|array - */ - public function broadcastOn() - { - return new PrivateChannel('channel-name'); - } -} diff --git a/app/Events/Gift/GiftDeleted.php b/app/Events/Gift/GiftDeleted.php deleted file mode 100644 index b36b38ac5..000000000 --- a/app/Events/Gift/GiftDeleted.php +++ /dev/null @@ -1,39 +0,0 @@ -gift = $gift; - } - - /** - * Get the channels the event should broadcast on. - * - * @return Channel|array - */ - public function broadcastOn() - { - return new PrivateChannel('channel-name'); - } -} diff --git a/app/Gift.php b/app/Gift.php index 8b878fb67..418071e79 100644 --- a/app/Gift.php +++ b/app/Gift.php @@ -3,8 +3,6 @@ namespace App; use App\Helpers\DateHelper; -use App\Events\Gift\GiftCreated; -use App\Events\Gift\GiftDeleted; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; @@ -14,11 +12,6 @@ class Gift extends Model 'date_offered', ]; - protected $events = [ - 'created' => GiftCreated::class, - 'deleted' => GiftDeleted::class, - ]; - /** * Get the account record associated with the gift. */ @@ -51,7 +44,7 @@ class Gift extends Model return null; } - return decrypt($this->name); + return $this->name; } public function getUrl() @@ -60,7 +53,7 @@ class Gift extends Model return null; } - return decrypt($this->url); + return $this->url; } public function getComment() @@ -69,7 +62,7 @@ class Gift extends Model return null; } - return decrypt($this->comment); + return $this->comment; } public function getValue() diff --git a/app/Http/Controllers/PeopleController.php b/app/Http/Controllers/PeopleController.php index 25a5c7452..7d8ca5def 100644 --- a/app/Http/Controllers/PeopleController.php +++ b/app/Http/Controllers/PeopleController.php @@ -1386,10 +1386,10 @@ class PeopleController extends Controller $gift = new Gift; $gift->contact_id = $contact->id; $gift->account_id = $contact->account_id; - $gift->name = encrypt($title); + $gift->name = $title; if ($url != '') { - $gift->url = encrypt($url); + $gift->url = $url; } if ($value != '') { @@ -1397,7 +1397,7 @@ class PeopleController extends Controller } if ($comment != '') { - $gift->comment = encrypt($comment); + $gift->comment = $comment; } if ($giftOffered == 'is_an_idea') { @@ -1428,6 +1428,17 @@ class PeopleController extends Controller $gift->save(); + $contact->logEvent('gift', $gift->id, 'create'); + + // increment counter + if ($gift->is_an_idea == 'true') { + $contact->number_of_gifts_ideas = $contact->number_of_gifts_ideas + 1; + } else { + $contact->number_of_gifts_offered = $contact->number_of_gifts_offered + 1; + } + + $contact->save(); + $request->session()->flash('success', trans('people.gifts_add_success')); return redirect('/people/'.$contact->id); @@ -1452,6 +1463,33 @@ class PeopleController extends Controller return redirect()->route('people.index'); } + // Delete all events + $events = Event::where('contact_id', $gift->contact_id) + ->where('account_id', $gift->account_id) + ->where('object_type', 'gift') + ->where('object_id', $gift->id) + ->get(); + + foreach ($events as $event) { + $event->delete(); + } + + // Decrease number of gifts + if ($gift->is_an_idea == 'true') { + $contact->number_of_gifts_ideas = $contact->number_of_gifts_ideas - 1; + + if ($contact->number_of_gifts_ideas < 1) { + $contact->number_of_gifts_ideas = 0; + } + } else { + $contact->number_of_gifts_offered = $contact->number_of_gifts_offered - 1; + + if ($contact->number_of_gifts_offered < 1) { + $contact->number_of_gifts_offered = 0; + } + } + $contact->save(); + $gift->delete(); $request->session()->flash('success', trans('people.gifts_delete_success')); diff --git a/app/Listeners/Gift/DecreaseNumberOfGifts.php b/app/Listeners/Gift/DecreaseNumberOfGifts.php deleted file mode 100644 index 60546d00a..000000000 --- a/app/Listeners/Gift/DecreaseNumberOfGifts.php +++ /dev/null @@ -1,48 +0,0 @@ -gift->contact_id); - - if ($event->gift->is_an_idea == 'true') { - $contact->number_of_gifts_ideas = $contact->number_of_gifts_ideas - 1; - - if ($contact->number_of_gifts_ideas < 1) { - $contact->number_of_gifts_ideas = 0; - } - } else { - $contact->number_of_gifts_offered = $contact->number_of_gifts_offered - 1; - - if ($contact->number_of_gifts_offered < 1) { - $contact->number_of_gifts_offered = 0; - } - } - - $contact->save(); - } -} diff --git a/app/Listeners/Gift/IncrementNumberOfGifts.php b/app/Listeners/Gift/IncrementNumberOfGifts.php deleted file mode 100644 index 9d751d5ad..000000000 --- a/app/Listeners/Gift/IncrementNumberOfGifts.php +++ /dev/null @@ -1,40 +0,0 @@ -gift->contact_id); - - if ($event->gift->is_an_idea == 'true') { - $contact->number_of_gifts_ideas = $contact->number_of_gifts_ideas + 1; - } else { - $contact->number_of_gifts_offered = $contact->number_of_gifts_offered + 1; - } - - $contact->save(); - } -} diff --git a/app/Listeners/Gift/LogGiftCreatedEvent.php b/app/Listeners/Gift/LogGiftCreatedEvent.php deleted file mode 100644 index 6c281232a..000000000 --- a/app/Listeners/Gift/LogGiftCreatedEvent.php +++ /dev/null @@ -1,38 +0,0 @@ -account_id = $event->gift->account_id; - $eventToSave->contact_id = $event->gift->contact_id; - $eventToSave->object_type = 'gift'; - $eventToSave->object_id = $event->gift->id; - $eventToSave->nature_of_operation = 'create'; - $eventToSave->save(); - } -} diff --git a/app/Listeners/Gift/RemoveAllGiftEvents.php b/app/Listeners/Gift/RemoveAllGiftEvents.php deleted file mode 100644 index 47277b419..000000000 --- a/app/Listeners/Gift/RemoveAllGiftEvents.php +++ /dev/null @@ -1,40 +0,0 @@ -gift->contact_id) - ->where('account_id', $event->gift->account_id) - ->where('object_type', 'gift') - ->where('object_id', $event->gift->id) - ->get(); - - foreach ($events as $event) { - $event->delete(); - } - } -} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index ecfdddf7d..65c8965d7 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -13,22 +13,6 @@ class EventServiceProvider extends ServiceProvider * @var array */ protected $listen = [ - 'App\Events\Reminder\ReminderCreated' => [ - 'App\Listeners\Reminder\LogReminderCreatedEvent', - 'App\Listeners\Reminder\IncrementNumberOfReminders', - ], - 'App\Events\Reminder\ReminderDeleted' => [ - 'App\Listeners\Reminder\RemoveAllReminderEvents', - 'App\Listeners\Reminder\DecreaseNumberOfReminders', - ], - 'App\Events\Gift\GiftCreated' => [ - 'App\Listeners\Gift\LogGiftCreatedEvent', - 'App\Listeners\Gift\IncrementNumberOfGifts', - ], - 'App\Events\Gift\GiftDeleted' => [ - 'App\Listeners\Gift\RemoveAllGiftEvents', - 'App\Listeners\Gift\DecreaseNumberOfGifts', - ], ]; /** diff --git a/database/migrations/2017_06_13_035059_remove_gifts_encryption.php b/database/migrations/2017_06_13_035059_remove_gifts_encryption.php new file mode 100644 index 000000000..73c99d917 --- /dev/null +++ b/database/migrations/2017_06_13_035059_remove_gifts_encryption.php @@ -0,0 +1,37 @@ +id; + if (!is_null ($gift->name)) { + $gift->name = decrypt($gift->name); + } + + if (!is_null ($gift->comment)) { + $gift->comment = decrypt($gift->comment); + } + + if (!is_null ($gift->url)) { + $gift->url = decrypt($gift->url); + } + + $gift->save(); + } + } +} diff --git a/tests/Unit/GiftTest.php b/tests/Unit/GiftTest.php index 676c54e62..bb84982eb 100644 --- a/tests/Unit/GiftTest.php +++ b/tests/Unit/GiftTest.php @@ -23,7 +23,7 @@ class GiftTest extends TestCase public function testGetNameReturnsName() { $gift = new Gift; - $gift->name = encrypt('This is a test'); + $gift->name = 'This is a test'; $this->assertEquals( 'This is a test', @@ -41,7 +41,7 @@ class GiftTest extends TestCase public function testGetURLReturnsURL() { $gift = new Gift; - $gift->url = encrypt('https://test.com'); + $gift->url = 'https://test.com'; $this->assertEquals( 'https://test.com', @@ -59,7 +59,7 @@ class GiftTest extends TestCase public function testGetCommentReturnsComment() { $gift = new Gift; - $gift->comment = encrypt('this is a test'); + $gift->comment = 'this is a test'; $this->assertEquals( 'this is a test',