Commit dece0495 authored by Nicolas Widart's avatar Nicolas Widart Committed by GitHub

Merge pull request #251 from mikemand/hotfix/user-was-updated-event

Fix the UserWasUpdated event only receiving boolean on update
parents 1f7bd85a 30f5f808
<?php
namespace Modules\User\Events;
class UserIsUpdating
{
public $user;
public function __construct($user)
{
$this->user = $user;
}
}
<?php
namespace Modules\User\Events;
class UserWasCreated
{
public $user;
public function __construct($user)
{
$this->user = $user;
}
}
...@@ -7,6 +7,8 @@ use Cartalyst\Sentinel\Laravel\Facades\Sentinel; ...@@ -7,6 +7,8 @@ use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Modules\User\Entities\Sentinel\User; use Modules\User\Entities\Sentinel\User;
use Modules\User\Events\UserHasRegistered; use Modules\User\Events\UserHasRegistered;
use Modules\User\Events\UserIsUpdating;
use Modules\User\Events\UserWasCreated;
use Modules\User\Events\UserWasUpdated; use Modules\User\Events\UserWasUpdated;
use Modules\User\Exceptions\UserNotFoundException; use Modules\User\Exceptions\UserNotFoundException;
use Modules\User\Repositories\UserRepository; use Modules\User\Repositories\UserRepository;
...@@ -39,14 +41,20 @@ class SentinelUserRepository implements UserRepository ...@@ -39,14 +41,20 @@ class SentinelUserRepository implements UserRepository
/** /**
* Create a user resource * Create a user resource
* @param $data * @param array $data
* @param bool $activated
* @return mixed * @return mixed
*/ */
public function create(array $data) public function create(array $data, $activated = false)
{ {
$user = $this->user->create((array) $data); $user = $this->user->create((array) $data);
event(new UserHasRegistered($user)); if ($activated) {
$this->activateUser($user);
event(new UserWasCreated($user));
} else {
event(new UserHasRegistered($user));
}
return $user; return $user;
} }
...@@ -60,16 +68,11 @@ class SentinelUserRepository implements UserRepository ...@@ -60,16 +68,11 @@ class SentinelUserRepository implements UserRepository
public function createWithRoles($data, $roles, $activated = false) public function createWithRoles($data, $roles, $activated = false)
{ {
$this->hashPassword($data); $this->hashPassword($data);
$user = $this->create((array) $data); $user = $this->create((array) $data, $activated);
if (!empty($roles)) { if (!empty($roles)) {
$user->roles()->attach($roles); $user->roles()->attach($roles);
} }
if ($activated) {
$activation = Activation::create($user);
Activation::complete($user, $activation->code);
}
} }
/** /**
...@@ -83,17 +86,12 @@ class SentinelUserRepository implements UserRepository ...@@ -83,17 +86,12 @@ class SentinelUserRepository implements UserRepository
public function createWithRolesFromCli($data, $roles, $activated = false) public function createWithRolesFromCli($data, $roles, $activated = false)
{ {
$this->hashPassword($data); $this->hashPassword($data);
$user = $this->user->create((array) $data); $user = $this->user->create((array) $data, $activated);
if (!empty($roles)) { if (!empty($roles)) {
$user->roles()->attach($roles); $user->roles()->attach($roles);
} }
if ($activated) {
$activation = Activation::create($user);
Activation::complete($user, $activation->code);
}
return $user; return $user;
} }
...@@ -115,7 +113,11 @@ class SentinelUserRepository implements UserRepository ...@@ -115,7 +113,11 @@ class SentinelUserRepository implements UserRepository
*/ */
public function update($user, $data) public function update($user, $data)
{ {
$user = $user->update($data); $user->fill($data);
event(new UserIsUpdating($user));
$user->save();
event(new UserWasUpdated($user)); event(new UserWasUpdated($user));
...@@ -138,6 +140,9 @@ class SentinelUserRepository implements UserRepository ...@@ -138,6 +140,9 @@ class SentinelUserRepository implements UserRepository
$this->checkForManualActivation($user, $data); $this->checkForManualActivation($user, $data);
$user = $user->fill($data); $user = $user->fill($data);
event(new UserIsUpdating($user));
$user->save(); $user->save();
event(new UserWasUpdated($user)); event(new UserWasUpdated($user));
...@@ -214,4 +219,15 @@ class SentinelUserRepository implements UserRepository ...@@ -214,4 +219,15 @@ class SentinelUserRepository implements UserRepository
return Activation::complete($user, $activation->code); return Activation::complete($user, $activation->code);
} }
} }
/**
* Activate a user automatically
*
* @param $user
*/
private function activateUser($user)
{
$activation = Activation::create($user);
Activation::complete($user, $activation->code);
}
} }
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