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