1
|
|
|
<?php namespace NukaCode\Users\Repositories;
|
2
|
|
|
|
3
|
|
|
use Illuminate\Events\Dispatcher;
|
4
|
|
|
use Illuminate\Support\Facades\Session;
|
5
|
|
|
use Laracasts\Commander\Events\EventGenerator;
|
6
|
|
|
use NukaCode\Users\Events\UserWasCreated;
|
7
|
|
|
use NukaCode\Core\Repositories\BaseRepository;
|
8
|
|
|
use NukaCode\Core\Ajax\Ajax;
|
9
|
|
|
use NukaCode\Core\View\Helpers\Crud;
|
10
|
|
|
use NukaCode\Core\View\Image;
|
11
|
|
|
|
12
|
|
|
class UserRepository extends BaseRepository {
|
13
|
|
|
|
14
|
|
|
use EventGenerator;
|
15
|
|
|
|
16
|
|
|
/**
|
17
|
|
|
* @var \Illuminate\Events\Dispatcher
|
18
|
|
|
*/
|
19
|
|
|
protected $events;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* @var \NukaCode\Core\Ajax\Ajax
|
23
|
|
|
*/
|
24
|
|
|
protected $ajax;
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* @var \NukaCode\Core\View\Helpers\Crud
|
28
|
|
|
*/
|
29
|
|
|
private $crud;
|
30
|
|
|
|
31
|
|
|
public function __construct(\User $user, Dispatcher $events, Ajax $ajax, Crud $crud)
|
32
|
|
|
{
|
33
|
|
|
$this->model = $user;
|
34
|
|
|
$this->events = $events;
|
35
|
|
|
$this->ajax = $ajax;
|
36
|
|
|
$this->crud = $crud;
|
37
|
|
|
}
|
38
|
|
|
|
39
|
|
|
public function set($user)
|
40
|
|
|
{
|
41
|
|
|
if ($user instanceof \User) {
|
42
|
|
|
$this->entity = $user;
|
43
|
|
|
} else {
|
44
|
|
|
throw new \InvalidArgumentException('Invalid user passed.');
|
45
|
|
|
}
|
46
|
|
|
}
|
47
|
|
|
|
48
|
|
|
public function create($input)
|
49
|
|
|
{
|
50
|
|
|
$user = new \User;
|
51
|
|
|
$user->username = $input['username'];
|
52
|
|
|
$user->password = $input['password'];
|
53
|
|
|
$user->email = $input['email'];
|
54
|
|
|
$user->status_id = 1;
|
55
|
|
|
|
56
|
|
|
if (isset($input['firstName'])) {
|
57
|
|
|
$user->firstName = $input['firstName'];
|
58
|
|
|
}
|
59
|
|
|
|
60
|
|
|
if (isset($input['lastName'])) {
|
61
|
|
|
$user->lastName = $input['lastName'];
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
$this->entity = $user;
|
65
|
|
|
|
66
|
|
|
$result = $this->save();
|
67
|
|
|
|
68
|
|
|
if ($result) {
|
69
|
|
|
// Send out the event
|
70
|
|
|
$this->raise(new UserWasCreated($this->getEntity()));
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
return $result;
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
/**
|
77
|
|
|
* @param array $input
|
78
|
|
|
*/
|
79
|
|
|
public function update($input)
|
80
|
|
|
{
|
81
|
|
|
$this->checkEntity();
|
82
|
|
|
$this->requireSingle();
|
83
|
|
|
|
84
|
|
|
$input = e_array($input);
|
85
|
|
|
|
86
|
|
|
if ($input != null) {
|
87
|
|
|
$this->entity->displayName = $this->arrayOrEntity('displayName', $input);
|
88
|
|
|
$this->entity->firstName = $this->arrayOrEntity('firstName', $input);
|
89
|
|
|
$this->entity->lastName = $this->arrayOrEntity('lastName', $input);
|
90
|
|
|
$this->entity->email = $this->arrayOrEntity('email', $input);
|
91
|
|
|
$this->entity->location = $this->arrayOrEntity('location', $input);
|
92
|
|
|
$this->entity->url = $this->arrayOrEntity('url', $input);
|
93
|
|
|
|
94
|
|
|
$this->save();
|
95
|
|
|
}
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
View Code Duplication |
public function addRole($roleId)
|
99
|
|
|
{
|
100
|
|
|
$this->checkEntity();
|
101
|
|
|
$this->requireSingle();
|
102
|
|
|
|
103
|
|
|
try {
|
104
|
|
|
$this->entity->roles()->attach($roleId);
|
105
|
|
|
|
106
|
|
|
$this->save();
|
107
|
|
|
} catch (\Exception $e) {
|
108
|
|
|
$this->ajax->setStatus('error');
|
109
|
|
|
$this->ajax->addError('role', $e->getMessage());
|
110
|
|
|
|
111
|
|
|
return false;
|
112
|
|
|
}
|
113
|
|
|
}
|
114
|
|
|
|
115
|
|
View Code Duplication |
public function removeRole($roleId)
|
116
|
|
|
{
|
117
|
|
|
$this->checkEntity();
|
118
|
|
|
$this->requireSingle();
|
119
|
|
|
|
120
|
|
|
try {
|
121
|
|
|
$this->entity->roles()->detach($roleId);
|
122
|
|
|
|
123
|
|
|
$this->save();
|
124
|
|
|
} catch (\Exception $e) {
|
125
|
|
|
$this->ajax->setStatus('error');
|
126
|
|
|
$this->ajax->addError('role', $e->getMessage());
|
127
|
|
|
|
128
|
|
|
return false;
|
129
|
|
|
}
|
130
|
|
|
}
|
131
|
|
|
|
132
|
|
|
public function setRoles($roleIds = [])
|
133
|
|
|
{
|
134
|
|
|
$this->checkEntity();
|
135
|
|
|
$this->requireSingle();
|
136
|
|
|
|
137
|
|
|
try {
|
138
|
|
|
$this->entity->roles()->detach();
|
139
|
|
|
|
140
|
|
|
if (count($roleIds) > 0) {
|
141
|
|
|
$this->entity->roles()->attach($roleIds);
|
142
|
|
|
}
|
143
|
|
|
|
144
|
|
|
$this->save();
|
145
|
|
|
} catch (\Exception $e) {
|
146
|
|
|
$this->ajax->setStatus('error');
|
147
|
|
|
$this->ajax->addError('roles', $e->getMessage());
|
148
|
|
|
|
149
|
|
|
return false;
|
150
|
|
|
}
|
151
|
|
|
}
|
152
|
|
|
|
153
|
|
|
public function updatePassword($input)
|
154
|
|
|
{
|
155
|
|
|
$this->checkEntity();
|
156
|
|
|
$this->requireSingle();
|
157
|
|
|
|
158
|
|
|
$input = e_array($input);
|
159
|
|
|
|
160
|
|
|
try {
|
161
|
|
|
$this->entity->verifyPassword($input);
|
162
|
|
|
} catch (\Exception $e) {
|
163
|
|
|
$this->ajax->setStatus('error');
|
164
|
|
|
$this->ajax->addError('password', $e->getMessage());
|
165
|
|
|
|
166
|
|
|
return false;
|
167
|
|
|
}
|
168
|
|
|
|
169
|
|
|
// Save the new password
|
170
|
|
|
$this->entity->password = $input['new_password'];
|
171
|
|
|
|
172
|
|
|
$this->save();
|
173
|
|
|
}
|
174
|
|
|
|
175
|
|
|
public function uploadAvatar($avatar, $username)
|
176
|
|
|
{
|
177
|
|
|
$image = new Image;
|
178
|
|
|
$results = $image->addImage(public_path('img/avatars/User'), $avatar, \Str::studly($username));
|
179
|
|
|
|
180
|
|
|
return $results;
|
181
|
|
|
}
|
182
|
|
|
|
183
|
|
|
public function getVisiblePreferences()
|
184
|
|
|
{
|
185
|
|
|
return \User_Preference::where('hiddenFlag', 0)->orderByNameAsc()->get();
|
186
|
|
|
}
|
187
|
|
|
|
188
|
|
|
public function updatePreferenceByKeyName($preferenceKeyName, $preferenceValue)
|
189
|
|
|
{
|
190
|
|
|
$this->checkEntity();
|
191
|
|
|
|
192
|
|
|
$preference = $this->getPreferenceByKeyName($preferenceKeyName);
|
193
|
|
|
|
194
|
|
|
$this->setPreferenceValue($preference->id, $preferenceValue);
|
195
|
|
|
}
|
196
|
|
|
|
197
|
|
|
public function getPreferenceWithArray($preferenceKeyName)
|
198
|
|
|
{
|
199
|
|
|
$this->checkEntity();
|
200
|
|
|
|
201
|
|
|
$preference = $this->getPreferenceByKeyName($preferenceKeyName);
|
202
|
|
|
$preferenceArray = $this->getPreferenceOptionsArray($preference->id);
|
203
|
|
|
|
204
|
|
|
return [$preference, $preferenceArray];
|
205
|
|
|
}
|
206
|
|
|
|
207
|
|
|
public function getPreferenceByKeyName($preferenceKeyName)
|
208
|
|
|
{
|
209
|
|
|
$this->checkEntity();
|
210
|
|
|
|
211
|
|
|
return $this->entity->getPreferenceByKeyName($preferenceKeyName);
|
212
|
|
|
}
|
213
|
|
|
|
214
|
|
|
public function getPreferenceOptionsArray($preferenceId)
|
215
|
|
|
{
|
216
|
|
|
$this->checkEntity();
|
217
|
|
|
|
218
|
|
|
return $this->entity->getPreferenceOptionsArray($preferenceId);
|
219
|
|
|
}
|
220
|
|
|
|
221
|
|
|
public function setPreferenceValue($preferenceId, $preferenceValue)
|
222
|
|
|
{
|
223
|
|
|
$this->checkEntity();
|
224
|
|
|
|
225
|
|
|
$this->entity->setPreferenceValue($preferenceId, $preferenceValue);
|
226
|
|
|
}
|
227
|
|
|
|
228
|
|
|
protected function checkEntity()
|
229
|
|
|
{
|
230
|
|
|
if ($this->entity == null) {
|
231
|
|
|
$this->entity = Session::get('activeUser');
|
232
|
|
|
}
|
233
|
|
|
}
|
234
|
|
|
} |