1
|
|
|
<?php namespace Modules\User\Repositories\Sentinel; |
2
|
|
|
|
3
|
|
|
use Cartalyst\Sentinel\Laravel\Facades\Activation; |
4
|
|
|
use Cartalyst\Sentinel\Laravel\Facades\Sentinel; |
5
|
|
|
use Illuminate\Support\Facades\Hash; |
6
|
|
|
use Modules\User\Events\UserHasRegistered; |
7
|
|
|
use Modules\User\Events\UserWasUpdated; |
8
|
|
|
use Modules\User\Exceptions\UserNotFoundException; |
9
|
|
|
use Modules\User\Repositories\UserRepository; |
10
|
|
|
|
11
|
|
|
class SentinelUserRepository implements UserRepository |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Modules\User\Entities\Sentinel\User |
15
|
|
|
*/ |
16
|
|
|
protected $user; |
17
|
|
|
/** |
18
|
|
|
* @var \Cartalyst\Sentinel\Roles\EloquentRole |
19
|
|
|
*/ |
20
|
|
|
protected $role; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->user = Sentinel::getUserRepository()->createModel(); |
25
|
|
|
$this->role = Sentinel::getRoleRepository()->createModel(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns all the users |
30
|
|
|
* @return object |
31
|
|
|
*/ |
32
|
|
|
public function all() |
33
|
|
|
{ |
34
|
|
|
return $this->user->all(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a user resource |
39
|
|
|
* @param $data |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
public function create(array $data) |
43
|
|
|
{ |
44
|
|
|
$user = $this->user->create((array) $data); |
45
|
|
|
|
46
|
|
|
event(new UserHasRegistered($user)); |
47
|
|
|
|
48
|
|
|
return $user; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a user and assign roles to it |
53
|
|
|
* @param array $data |
54
|
|
|
* @param array $roles |
55
|
|
|
* @param bool $activated |
56
|
|
|
*/ |
57
|
|
|
public function createWithRoles($data, $roles, $activated = false) |
58
|
|
|
{ |
59
|
|
|
$this->hashPassword($data); |
60
|
|
|
$user = $this->create((array) $data); |
61
|
|
|
|
62
|
|
|
if (!empty($roles)) { |
63
|
|
|
$user->roles()->attach($roles); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($activated) { |
67
|
|
|
$activation = Activation::create($user); |
68
|
|
|
Activation::complete($user, $activation->code); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create a user and assign roles to it |
74
|
|
|
* But don't fire the user created event |
75
|
|
|
* @param array $data |
76
|
|
|
* @param array $roles |
77
|
|
|
* @param bool $activated |
78
|
|
|
*/ |
79
|
|
|
public function createWithRolesFromCli($data, $roles, $activated = false) |
80
|
|
|
{ |
81
|
|
|
$this->hashPassword($data); |
82
|
|
|
$user = $this->user->create((array) $data); |
83
|
|
|
|
84
|
|
|
if (!empty($roles)) { |
85
|
|
|
$user->roles()->attach($roles); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($activated) { |
89
|
|
|
$activation = Activation::create($user); |
90
|
|
|
Activation::complete($user, $activation->code); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Find a user by its ID |
96
|
|
|
* @param $id |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public function find($id) |
100
|
|
|
{ |
101
|
|
|
return $this->user->find($id); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Update a user |
106
|
|
|
* @param $user |
107
|
|
|
* @param $data |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function update($user, $data) |
111
|
|
|
{ |
112
|
|
|
$user = $user->update($data); |
113
|
|
|
|
114
|
|
|
event(new UserWasUpdated($user)); |
115
|
|
|
|
116
|
|
|
return $user; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $userId |
121
|
|
|
* @param $data |
122
|
|
|
* @param $roles |
123
|
|
|
* @internal param $user |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function updateAndSyncRoles($userId, $data, $roles) |
127
|
|
|
{ |
128
|
|
|
$user = $this->user->find($userId); |
129
|
|
|
|
130
|
|
|
$this->checkForNewPassword($data); |
131
|
|
|
|
132
|
|
|
$this->checkForManualActivation($user, $data); |
133
|
|
|
|
134
|
|
|
$user = $user->fill($data); |
135
|
|
|
$user->save(); |
136
|
|
|
|
137
|
|
|
event(new UserWasUpdated($user)); |
138
|
|
|
|
139
|
|
|
if (!empty($roles)) { |
140
|
|
|
$user->roles()->sync($roles); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Deletes a user |
146
|
|
|
* @param $id |
147
|
|
|
* @throws UserNotFoundException |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
|
|
public function delete($id) |
151
|
|
|
{ |
152
|
|
|
if ($user = $this->user->find($id)) { |
153
|
|
|
return $user->delete(); |
154
|
|
|
}; |
155
|
|
|
|
156
|
|
|
throw new UserNotFoundException(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Find a user by its credentials |
161
|
|
|
* @param array $credentials |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
public function findByCredentials(array $credentials) |
165
|
|
|
{ |
166
|
|
|
return Sentinel::findByCredentials($credentials); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Hash the password key |
171
|
|
|
* @param array $data |
172
|
|
|
*/ |
173
|
|
|
private function hashPassword(array &$data) |
174
|
|
|
{ |
175
|
|
|
$data['password'] = Hash::make($data['password']); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Check if there is a new password given |
180
|
|
|
* If not, unset the password field |
181
|
|
|
* @param array $data |
182
|
|
|
*/ |
183
|
|
|
private function checkForNewPassword(array &$data) |
184
|
|
|
{ |
185
|
|
|
if (! $data['password']) { |
186
|
|
|
unset($data['password']); |
187
|
|
|
|
188
|
|
|
return; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$data['password'] = Hash::make($data['password']); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Check and manually activate or remove activation for the user |
196
|
|
|
* @param $user |
197
|
|
|
* @param array $data |
198
|
|
|
*/ |
199
|
|
|
private function checkForManualActivation($user, array &$data) |
200
|
|
|
{ |
201
|
|
|
if (Activation::completed($user) && !$data['activated']) { |
202
|
|
|
return Activation::remove($user); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if (!Activation::completed($user) && $data['activated']) { |
206
|
|
|
$activation = Activation::create($user); |
207
|
|
|
|
208
|
|
|
return Activation::complete($user, $activation->code); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|