forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserRepository.php
More file actions
276 lines (228 loc) · 7.05 KB
/
Copy pathUserRepository.php
File metadata and controls
276 lines (228 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Ushahidi User Repository
*
* Also implements registration checks
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
namespace Ushahidi\App\Repository;
use Ohanzee\DB;
use Ushahidi\Core\Entity;
use Ushahidi\Core\Entity\User;
use Ushahidi\Core\Entity\UserRepository as UserRepositoryContract;
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Tool\Hasher;
use Ushahidi\Core\Usecase\User\RegisterRepository;
use Ushahidi\Core\Usecase\User\ResetPasswordRepository;
use League\Event\ListenerInterface;
use Ushahidi\Core\Traits\Event;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserRepository extends OhanzeeRepository implements
UserRepositoryContract,
RegisterRepository,
ResetPasswordRepository
{
/**
* @var Hasher
*/
protected $hasher;
// Use Event trait to trigger events
use Event;
/**
* @param Hasher $hasher
* @return $this
*/
public function setHasher(Hasher $hasher)
{
$this->hasher = $hasher;
return $this;
}
// OhanzeeRepository
protected function getTable()
{
return 'users';
}
// OhanzeeRepository
public function getEntity(array $data = null)
{
if (!empty($data['id'])) {
$data += [
'contacts' => $this->getContacts($data['id']),
];
}
return new User($data);
}
protected function getContacts($entity_id)
{
// Unfortunately there is a circular reference created if the Contact repo is
// injected into the User repo to avoid this we access the table directly
// NOTE: This creates a hard coded dependency on the table naming for contacts
$query = DB::select('*')->from('contacts')
->where('user_id', '=', $entity_id);
$results = $query->execute($this->db());
return $results->as_array();
}
// CreateRepository
public function create(Entity $entity)
{
$state = [
'created' => time(),
'password' => $this->hasher->hash($entity->password),
];
$entity->setState($state);
if ($entity->role === 'admin') {
$this->updateIntercomAdminUsers($entity);
}
return parent::create($entity);
}
// CreateRepository
public function createWithHash(Entity $entity)
{
$state = [
'created' => time()
];
$entity->setState($state);
if ($entity->role === 'admin') {
$this->updateIntercomAdminUsers($entity);
}
return parent::create($entity);
}
// UpdateRepository
public function update(Entity $entity)
{
$user = $entity->getChanged();
unset($user['contacts']);
$user['updated'] = time();
if ($entity->hasChanged('password')) {
$user['password'] = $this->hasher->hash($entity->password);
}
if ($entity->role === 'admin') {
$this->updateIntercomAdminUsers($entity);
}
return $this->executeUpdate(['id' => $entity->id], $user);
}
// SearchRepository
public function getSearchFields()
{
return ['email', 'role', 'q' /* LIKE realname, email */];
}
// SearchRepository
public function setSearchConditions(SearchData $search)
{
$query = $this->search_query;
$table = $this->getTable();
if ($search->q) {
$query->and_where_open();
$query->where('email', 'LIKE', "%" . $search->q . "%");
$query->or_where('realname', 'LIKE', "%" . $search->q . "%");
$query->and_where_close();
// Adding search contacts
$query->join('contacts', 'left')->on("$table.id", '=', 'contacts.user_id')
->or_where('contacts.contact', 'like', '%' . $search->q . '%');
}
if ($search->role) {
$role = $search->role;
if (!is_array($search->role)) {
$role = explode(',', $search->role);
}
$query->where('role', 'IN', $role);
}
return $query;
}
// UserRepository
public function getByEmail($email)
{
return $this->getEntity($this->selectOne(compact('email')));
}
// RegisterRepository
public function isUniqueEmail($email)
{
return $this->selectCount(compact('email')) === 0;
}
// RegisterRepository
public function register(Entity $entity)
{
return $this->executeInsert([
'realname' => $entity->realname,
'email' => $entity->email,
'password' => $this->hasher->hash($entity->password),
'created' => time()
]);
}
// ResetPasswordRepository
public function getResetToken(Entity $entity)
{
$token = Hash::make(Str::random(40));
$input = [
'reset_token' => $token,
'user_id' => $entity->id,
'created' => time()
];
// Save the token
$query = DB::insert('user_reset_tokens')
->columns(array_keys($input))
->values(array_values($input))
->execute($this->db());
return $token;
}
// ResetPasswordRepository
public function isValidResetToken($token)
{
$result = DB::select([DB::expr('COUNT(*)'), 'total'])
->from('user_reset_tokens')
->where('reset_token', '=', $token)
->where('created', '>', time() - 1800) // Expire tokens after less than 30 mins
->execute($this->db());
$count = $result->get('total') ?: 0;
return $count !== 0;
}
// ResetPasswordRepository
public function setPassword($token, $password)
{
$sub = DB::select('user_id')
->from('user_reset_tokens')
->where('reset_token', '=', $token);
$this->executeUpdate(['id' => $sub], [
'password' => $this->hasher->hash($password)
]);
}
// ResetPasswordRepository
public function deleteResetToken($token)
{
$result = DB::delete('user_reset_tokens')
->where('reset_token', '=', $token)
->execute($this->db());
}
/**
* Get total count of entities
* @param Array $where
* @return int
*/
public function getTotalCount(array $where = [])
{
return $this->selectCount($where);
}
// DeleteRepository
public function delete(Entity $entity)
{
if ($entity->role === 'admin') {
$this->updateIntercomAdminUsers($entity);
}
return parent::delete($entity);
}
/**
* Pass User count to Intercom
* takes a postive/negative offset by which to increase/decrease count for create/delete
* @param Integer $offset
* @return void
*/
protected function updateIntercomAdminUsers($user)
{
$this->emit($this->event, $user);
}
}