-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathUser.php
More file actions
309 lines (262 loc) · 8.08 KB
/
User.php
File metadata and controls
309 lines (262 loc) · 8.08 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php namespace Backend\Models;
use Mail;
use Event;
use Backend;
use BackendAuth;
use Winter\Storm\Auth\Models\User as UserBase;
/**
* Administrator user model
*
* @package winter\wn-backend-module
* @author Alexey Bobkov, Samuel Georges
*/
class User extends UserBase
{
use \Winter\Storm\Database\Traits\SoftDelete;
/**
* @var string The database table used by the model.
*/
protected $table = 'backend_users';
/**
* Validation rules
*/
public $rules = [
'email' => 'required|between:6,255|email|unique:backend_users',
'login' => 'required|between:2,255|unique:backend_users',
'password' => 'sometimes|min:4|confirmed',
'password_confirmation' => 'sometimes|required_with:password|min:4'
];
/**
* @var array Attributes that should be cast to dates
*/
protected $dates = [
'activated_at',
'last_login',
'created_at',
'updated_at',
'deleted_at',
];
/**
* Relations
*/
public $belongsToMany = [
'groups' => [UserGroup::class, 'table' => 'backend_users_groups']
];
public $belongsTo = [
'role' => UserRole::class
];
public $attachOne = [
'avatar' => \System\Models\File::class
];
/**
* Purge attributes from data set.
*/
protected $purgeable = ['password_confirmation', 'send_invite'];
/**
* @var string Login attribute
*/
public static $loginAttribute = 'login';
/**
* @return string Returns the user's full name.
*/
public function getFullNameAttribute()
{
return trim($this->first_name . ' ' . $this->last_name);
}
/**
* Gets a code for when the user is persisted to a cookie or session which identifies the user.
* @return string
*/
public function getPersistCode()
{
// Option A: @todo config
// return parent::getPersistCode();
// Option B:
if (!$this->persist_code) {
return parent::getPersistCode();
}
return $this->persist_code;
}
/**
* Returns the public image file path to this user's avatar.
*/
public function getAvatarThumb($size = 25, $options = null)
{
if (is_string($options)) {
$options = ['default' => $options];
}
elseif (!is_array($options)) {
$options = [];
}
// Default is "mm" (Mystery man)
$default = array_get($options, 'default', 'mm');
if ($this->avatar) {
return $this->avatar->getThumb($size, $size, $options);
}
return '//www.gravatar.com/avatar/' .
md5(strtolower(trim($this->email))) .
'?s='. $size .
'&d='. urlencode($default);
}
/**
* After create event
* @return void
*/
public function afterCreate()
{
$this->restorePurgedValues();
if ($this->send_invite) {
$this->sendInvitation();
}
}
/**
* After login event
* @return void
*/
public function afterLogin()
{
parent::afterLogin();
/**
* @event backend.user.login
* Provides an opportunity to interact with the Backend User model after the user has logged in
*
* Example usage:
*
* Event::listen('backend.user.login', function ((\Backend\Models\User) $user) {
* Flash::success(sprintf('Welcome %s!', $user->getFullNameAttribute()));
* });
*
*/
Event::fire('backend.user.login', [$this]);
}
/**
* Generates a link to the backend, or a password reset link if no password was set on creation.
* @return string
*/
public function getInvitationLink()
{
if (!$this->password) {
$code = $this->getResetPasswordCode();
return Backend::url('backend/auth/reset/' . $this->id . '/' . $code);
}
return Backend::url('backend');
}
/**
* Sends an invitation to the user using template "backend::mail.invite".
* @return void
*/
public function sendInvitation()
{
$data = [
'name' => $this->full_name,
'login' => $this->login,
'link' => $this->getInvitationLink(),
];
Mail::send('backend::mail.invite', $data, function ($message) {
$message->to($this->email, $this->full_name);
});
}
/**
* Sends a password restore link to the user using template "backend::mail.restore".
* @return void
*/
public function sendPasswordRestore()
{
$code = $this->getResetPasswordCode();
$link = Backend::url('backend/auth/reset/' . $this->id . '/' . $code);
$data = [
'name' => $this->full_name,
'link' => $link,
];
Mail::send('backend::mail.restore', $data, function ($message) {
$message->to($this->email, $this->full_name)->subject(trans('backend::lang.account.password_reset'));
});
}
public function getGroupsOptions()
{
$result = [];
foreach (UserGroup::all() as $group) {
$result[$group->id] = [$group->name, $group->description];
}
return $result;
}
public function getRoleOptions()
{
$result = [];
foreach (UserRole::all() as $role) {
$result[$role->id] = [$role->name, $role->description];
}
return $result;
}
/**
* Check if the user is suspended.
* @return bool
*/
public function isSuspended()
{
return BackendAuth::findThrottleByUserId($this->id)->checkSuspended();
}
/**
* Remove the suspension on this user.
* @return void
*/
public function unsuspend()
{
BackendAuth::findThrottleByUserId($this->id)->unsuspend();
}
//
// Impersonation
//
/**
* Returns an array of merged permissions based on the user's individual permissions
* and their group permissions filtering out any permissions the impersonator doesn't
* have access to (if the current user is being impersonated)
*
* @return array
*/
public function getMergedPermissions()
{
if (!$this->mergedPermissions) {
$permissions = parent::getMergedPermissions();
// If the user is being impersonated filter out any permissions the impersonator doesn't have access to already
if (BackendAuth::isImpersonator()) {
$impersonator = BackendAuth::getImpersonator();
if ($impersonator && $impersonator !== $this) {
foreach ($permissions as $i => $permission) {
if (!$impersonator->hasAccess($permission)) {
unset($permissions[$i]);
}
}
$this->mergedPermissions = $permissions;
}
}
}
return $this->mergedPermissions;
}
/**
* Check if this user can be impersonated by the provided impersonator
* Super users cannot be impersonated and all users cannot be impersonated unless there is an impersonator
* present and the impersonator has access to `backend.impersonate_users`, and the impersonator is not the
* user being impersonated
*
* @param \Winter\Storm\Auth\Models\User|false $impersonator The user attempting to impersonate this user, false when not available
* @return boolean
*/
public function canBeImpersonated($impersonator = false)
{
if (
$this->isSuperUser() ||
!$impersonator ||
!($impersonator instanceof static) ||
!$impersonator->hasAccess('backend.impersonate_users') ||
$impersonator === $this
) {
return false;
}
// Clear the merged permissions before the impersonation starts
// so that they are correct even if they had been loaded prior
// to the impersonation starting
$this->mergedPermissions = null;
return true;
}
}