Trigger event when user is updating, which allows data modification

parent 92ab1dba
...@@ -2,12 +2,59 @@ ...@@ -2,12 +2,59 @@
namespace Modules\User\Events; namespace Modules\User\Events;
use Modules\User\Entities\UserInterface;
class UserIsUpdating class UserIsUpdating
{ {
public $user; /**
* @var UserInterface
*/
private $user;
/**
* @var array
*/
private $attributes;
/**
* @var array
*/
private $original;
public function __construct($user) public function __construct(UserInterface $user, array $data)
{ {
$this->user = $user; $this->user = $user;
$this->attributes = $data;
$this->original = $data;
}
/**
* @return array
*/
public function getOriginal()
{
return $this->original;
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->attributes = array_merge($this->attributes, $attributes);
}
/**
* @return UserInterface
*/
public function getUser()
{
return $this->user;
} }
} }
...@@ -126,10 +126,9 @@ class SentinelUserRepository implements UserRepository ...@@ -126,10 +126,9 @@ class SentinelUserRepository implements UserRepository
{ {
$this->checkForNewPassword($data); $this->checkForNewPassword($data);
$user->fill($data); event($event = new UserIsUpdating($user, $data));
event(new UserIsUpdating($user));
$user->fill($event->getAttributes());
$user->save(); $user->save();
event(new UserWasUpdated($user)); event(new UserWasUpdated($user));
...@@ -152,10 +151,9 @@ class SentinelUserRepository implements UserRepository ...@@ -152,10 +151,9 @@ class SentinelUserRepository implements UserRepository
$this->checkForManualActivation($user, $data); $this->checkForManualActivation($user, $data);
$user = $user->fill($data); event($event = new UserIsUpdating($user, $data));
event(new UserIsUpdating($user));
$user->fill($event->getAttributes());
$user->save(); $user->save();
event(new UserWasUpdated($user)); event(new UserWasUpdated($user));
......
...@@ -104,7 +104,7 @@ class SentinelUserRepositoryTest extends BaseUserTestCase ...@@ -104,7 +104,7 @@ class SentinelUserRepositoryTest extends BaseUserTestCase
]); ]);
Event::assertDispatched(UserIsCreating::class, function ($e) { Event::assertDispatched(UserIsCreating::class, function ($e) {
return $e->original['email'] === 'n.widart@gmail.com'; return $e->getOriginal()['email'] === 'n.widart@gmail.com';
}); });
} }
...@@ -237,10 +237,45 @@ class SentinelUserRepositoryTest extends BaseUserTestCase ...@@ -237,10 +237,45 @@ class SentinelUserRepositoryTest extends BaseUserTestCase
return $e->user->id === $user->id; return $e->user->id === $user->id;
}); });
Event::assertDispatched(UserIsUpdating::class, function ($e) use ($user) { Event::assertDispatched(UserIsUpdating::class, function ($e) use ($user) {
return $e->user->id === $user->id; return $e->getUser()->id === $user->id;
});
}
/** @test */
public function it_triggers_event_when_user_is_updating()
{
$user = $this->user->create([
'email' => 'n.widart@gmail.com',
'password' => 'demo1234',
]);
Event::fake();
$this->user->update($user, ['first_name' => 'John', 'last_name' => 'Doe']);
Event::assertDispatched(UserIsUpdating::class, function ($e) use ($user) {
return $e->getUser()->id === $user->id &&
$e->getAttributes()['first_name'] === 'John';
}); });
} }
/** @test */
public function it_can_change_properties_before_update()
{
Event::listen(UserIsUpdating::class, function (UserIsUpdating $event) {
$event->setAttributes(['first_name' => 'Jane']);
});
$user = $this->user->create([
'email' => 'n.widart@gmail.com',
'password' => 'demo1234',
]);
$this->user->update($user, ['first_name' => 'John', 'last_name' => 'Doe']);
$this->assertEquals('Jane', $this->user->find(1)->first_name);
}
/** @test */ /** @test */
public function it_updates_user_and_syncs_roles() public function it_updates_user_and_syncs_roles()
{ {
...@@ -277,10 +312,28 @@ class SentinelUserRepositoryTest extends BaseUserTestCase ...@@ -277,10 +312,28 @@ class SentinelUserRepositoryTest extends BaseUserTestCase
return $e->user->id === $user->id; return $e->user->id === $user->id;
}); });
Event::assertDispatched(UserIsUpdating::class, function ($e) use ($user) { Event::assertDispatched(UserIsUpdating::class, function ($e) use ($user) {
return $e->user->id === $user->id; return $e->getUser()->id === $user->id;
}); });
} }
/** @test */
public function it_can_change_properties_before_update_and_sync_roles()
{
Event::listen(UserIsUpdating::class, function (UserIsUpdating $event) {
$event->setAttributes(['first_name' => 'Jane']);
});
$this->createRole('Admin');
$user = $this->user->createWithRoles([
'email' => 'n.widart@gmail.com',
'password' => 'demo1234',
], [1]);
$this->user->updateAndSyncRoles($user->id, ['first_name' => 'John', 'last_name' => 'Doe', 'activated' => 1], [1]);
$this->assertEquals('Jane', $this->user->find(1)->first_name);
}
/** @test */ /** @test */
public function it_deletes_a_user() public function it_deletes_a_user()
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment