Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
0e903c71
Unverified
Commit
0e903c71
authored
Jul 11, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Trigger event when user is updating, which allows data modification
parent
92ab1dba
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
109 additions
and
11 deletions
+109
-11
UserIsUpdating.php
Modules/User/Events/UserIsUpdating.php
+49
-2
SentinelUserRepository.php
...les/User/Repositories/Sentinel/SentinelUserRepository.php
+4
-6
SentinelUserRepositoryTest.php
Modules/User/Tests/SentinelUserRepositoryTest.php
+56
-3
No files found.
Modules/User/Events/UserIsUpdating.php
View file @
0e903c71
...
...
@@ -2,12 +2,59 @@
namespace
Modules\User\Events
;
use
Modules\User\Entities\UserInterface
;
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
->
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
;
}
}
Modules/User/Repositories/Sentinel/SentinelUserRepository.php
View file @
0e903c71
...
...
@@ -126,10 +126,9 @@ class SentinelUserRepository implements UserRepository
{
$this
->
checkForNewPassword
(
$data
);
$user
->
fill
(
$data
);
event
(
new
UserIsUpdating
(
$user
));
event
(
$event
=
new
UserIsUpdating
(
$user
,
$data
));
$user
->
fill
(
$event
->
getAttributes
());
$user
->
save
();
event
(
new
UserWasUpdated
(
$user
));
...
...
@@ -152,10 +151,9 @@ class SentinelUserRepository implements UserRepository
$this
->
checkForManualActivation
(
$user
,
$data
);
$user
=
$user
->
fill
(
$data
);
event
(
new
UserIsUpdating
(
$user
));
event
(
$event
=
new
UserIsUpdating
(
$user
,
$data
));
$user
->
fill
(
$event
->
getAttributes
());
$user
->
save
();
event
(
new
UserWasUpdated
(
$user
));
...
...
Modules/User/Tests/SentinelUserRepositoryTest.php
View file @
0e903c71
...
...
@@ -104,7 +104,7 @@ class SentinelUserRepositoryTest extends BaseUserTestCase
]);
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
return
$e
->
user
->
id
===
$user
->
id
;
});
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 */
public
function
it_updates_user_and_syncs_roles
()
{
...
...
@@ -277,10 +312,28 @@ class SentinelUserRepositoryTest extends BaseUserTestCase
return
$e
->
user
->
id
===
$user
->
id
;
});
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 */
public
function
it_deletes_a_user
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment