@@ -27,8 +27,8 @@ class User extends UserBase
27
27
public $ rules = [
28
28
'email ' => 'required|between:6,255|email|unique:backend_users ' ,
29
29
'login ' => 'required|between:2,255|unique:backend_users ' ,
30
- 'password ' => 'required:create|between:4,255 |confirmed ' ,
31
- 'password_confirmation ' => 'required_with:password|between:4,255 '
30
+ 'password ' => 'required:create|min:4 |confirmed ' ,
31
+ 'password_confirmation ' => 'required_with:password|min:4 '
32
32
];
33
33
34
34
/**
@@ -209,4 +209,66 @@ public function unsuspend()
209
209
{
210
210
BackendAuth::findThrottleByUserId ($ this ->id )->unsuspend ();
211
211
}
212
+
213
+ //
214
+ // Impersonation
215
+ //
216
+
217
+ /**
218
+ * Returns an array of merged permissions based on the user's individual permissions
219
+ * and their group permissions filtering out any permissions the impersonator doesn't
220
+ * have access to (if the current user is being impersonated)
221
+ *
222
+ * @return array
223
+ */
224
+ public function getMergedPermissions ()
225
+ {
226
+ if (!$ this ->mergedPermissions ) {
227
+ $ permissions = parent ::getMergedPermissions ();
228
+
229
+ // If the user is being impersonated filter out any permissions the impersonator doesn't have access to already
230
+ if (BackendAuth::isImpersonator ()) {
231
+ $ impersonator = BackendAuth::getImpersonator ();
232
+ if ($ impersonator && $ impersonator !== $ this ) {
233
+ foreach ($ permissions as $ i => $ permission ) {
234
+ if (!$ impersonator ->hasAccess ($ permission )) {
235
+ unset($ permissions [$ i ]);
236
+ }
237
+ }
238
+ $ this ->mergedPermissions = $ permissions ;
239
+ }
240
+ }
241
+ }
242
+
243
+ return $ this ->mergedPermissions ;
244
+ }
245
+
246
+ /**
247
+ * Check if this user can be impersonated by the provided impersonator
248
+ * Super users cannot be impersonated and all users cannot be impersonated unless there is an impersonator
249
+ * present and the impersonator has access to `backend.impersonate_users`, and the impersonator is not the
250
+ * user being impersonated
251
+ *
252
+ * @param \Winter\Storm\Auth\Models\User|false $impersonator The user attempting to impersonate this user, false when not available
253
+ * @return boolean
254
+ */
255
+ public function canBeImpersonated ($ impersonator = false )
256
+ {
257
+ if (
258
+ $ this ->isSuperUser () ||
259
+ !$ impersonator ||
260
+ !($ impersonator instanceof static) ||
261
+ !$ impersonator ->hasAccess ('backend.impersonate_users ' ) ||
262
+ $ impersonator === $ this
263
+ ) {
264
+ return false ;
265
+ }
266
+
267
+ // Clear the merged permissions before the impersonation starts
268
+ // so that they are correct even if they had been loaded prior
269
+ // to the impersonation starting
270
+ $ this ->mergedPermissions = null ;
271
+
272
+ return true ;
273
+ }
212
274
}
0 commit comments