foldershare-8.x-1.2/src/Utilities/UserUtilities.php

src/Utilities/UserUtilities.php
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
 
namespace Drupal\foldershare\Utilities;
 
use Drupal\Core\Database\Database;
use Drupal\user\Entity\User;
 
/**
 * Defines static utility functions for working with users.
 *
 * The functions in this class support user queries based upon the user's
 * account name, account email address, and display name.
 *
 * <B>Warning:</B> This class is strictly internal to the FolderShare
 * module. The class's existance, name, and content may change from
 * release to release without any promise of backwards compatability.
 *
 * @ingroup foldershare
 */
final class UserUtilities {
 
  /*--------------------------------------------------------------------
   *
   * Configuration.
   *
   *-------------------------------------------------------------------*/
 
  /**
   * Returns TRUE if the "Real name" contributed module is installed.
   *
   * @return bool
   *   Returns TRUE if installed.
   */
  public static function isRealnameInstalled() {
    return \Drupal::moduleHandler()->moduleExists('realname');
  }
 
  /**
   * Returns the configured name for the anonymous account.
   *
   * The anonymous (uid = 0) account is a virtual account for visitors that
   * are not logged in. The display name for the account can be set via
   * a User module configuration setting.
   *
   * The configured name is typically the display name for the account,
   * but display name hook implementations can override it.
   *
   * @return string
   *   The configured name for the anonymous account.
   */
  public static function getAnonymousName() {
    return \Drupal::config('user.settings')->get('anonymous');
  }
 
  /**
   * Returns the names of modules implementing the display name hook.
   *
   * The User::getDisplayName() method invokes the 'user_format_name_alter'
   * hook used by modules to override the account name and provide a more
   * descriptive name, such as a user's full name. The common "Real name"
   * module is one such module, which uses tokens to assemble a name from
   * one or more other fields on a User entity.
   *
   * This method returns a list of modules implementing the hook.
   *
   * @return string[]
   *   Returns the machine names of modules implementing the display name
   *   hook.
   */
  public static function getDisplayNameHookModules() {
    return \Drupal::moduleHandler()
      ->getImplementations('user_format_name_alter');
  }
 
  /*--------------------------------------------------------------------
   *
   * Functions.
   *
   *-------------------------------------------------------------------*/
 
  /**
   * Returns the user ID for a given user name, display name, or email address.
   *
   * Database tables are queried for an exact case insensitive match.
   *
   * Display name queries are only available if the third-party "Real name"
   * module is installed. The module uses tokens to assemble a name from
   * site-defined fields, then caches that name in a database table that is
   * queried here.
   *
   * @param string $name
   *   The account name, display name, or email address of a user.
   *
   * @return int
   *   Returns the user ID for the matched user, or (-1) if not found.
   *
   * @see ::findUserByAccountName()
   * @see ::findUserByAccountEmail()
   * @see ::findUserByDisplayName()
   * @see ::findSimilarUsers()
   */
  public static function findUser(string $name) {
    $uid = self::findUserByAccountName($name);
    if ($uid !== (-1)) {
      return $uid;
    }
 
    $uid = self::findUserByAccountEmail($name);
    if ($uid !== (-1)) {
      return $uid;
    }
 
    $uid = self::findUserByDisplayName($name, FALSE);
    if ($uid !== (-1)) {
      return $uid;
    }
 
    return (-1);
  }
 
  /**
   * Returns the user ID for a given user account name.
   *
   * Database tables are queried for an exact case insensitive match.
   *
   * Account names are expected to be unique within a site. A database
   * table query can therefore return at most one entry.
   *
   * @param string $name
   *   The account name of a user.
   *
   * @return int
   *   Returns the user ID for the matched user, or (-1) if not found.
   *
   * @see ::findUser()
   * @see ::findUserByAccountEmail()
   * @see ::findUserByDisplayName()
   * @see ::findSimilarUsers()
   */
  public static function findUserByAccountName(string $name) {
    // Note: This query could be done by user_load_by_name() in the user
    // module. However, that function does a case *sensitive* search and
    // it loads the entity. We want a case *insensitive* search and we
    // only want the user ID.
    $connection = Database::getConnection();
    $select = $connection->select('users_field_data', 'u');
    $select->addField('u', 'uid', 'uid');
    $select->addExpression('LOWER(u.name)', 'unamelower');
    $select->condition('unamelower', mb_strtolower($name), '=');
    $select->range(0, 1);
    $uids = $select->execute()->fetchCol(0);
 
    if (empty($uids) === TRUE) {
      return (-1);
    }
 
    // Return the first match. There should be only one since account names
    // are required to be unique.
    return (int) $uids[0];
  }
 
  /**
   * Returns the user ID for a given user account email address.
   *
   * Database tables are queried for an exact case insensitive match.
   *
   * Account email addresses need not be unique within a site. Multiple
   * accounts may have the same address. When there are multiple matches,
   * this function returns the match with the lowest user ID, which will
   * be the earliest account created.
   *
   * @param string $email
   *   The account email address of a user.
   *
   * @return int
   *   Returns the user ID for the matched user, or (-1) if not found.
   *
   * @see ::findUser()
   * @see ::findUserByAccountName()
   * @see ::findUserByDisplayName()
   * @see ::findSimilarUsers()
   */
  public static function findUserByAccountEmail(string $email) {
    // Note: This query could be done by user_load_by_mail() in the user
    // module. However, that function does a case *sensitive* search and
    // it loads the entity. We want a case *insensitive* search and we
    // only want the user ID.
    $connection = Database::getConnection();
    $select = $connection->select('users_field_data', 'u');
    $select->addField('u', 'uid', 'uid');
    $select->addExpression('LOWER(u.mail)', 'umaillower');
    $select->condition('umaillower', mb_strtolower($email), '=');
    $select->orderBy('uid');
    $select->range(0, 1);
    $uids = $select->execute()->fetchCol(0);
 
    if (empty($uids) === TRUE) {
      return (-1);
    }
 
    // Return the first match. It is possible for more than one account to
    // have the same email address, though this is unlikely. The order-by
    // clause above has insured that the first value has the lowest user ID.
    return (int) $uids[0];
  }
 
  /**
   * Returns the user ID for a given user display name.
   *
   * This method searches for a user entity with a matching display name,
   * ignoring case. If no match is found, (-1) is returned.
   *
   * There are three cases handled:
   *
   * 1. There are no implementations of the 'user_format_name_alter' hook, and
   *    therefore the display name is the account name. A special case is
   *    handled for anonymous, which uses the configured display name.
   *
   * 2. There is one hook implementation and it is for the common "Real name"
   *    contributed module. A database search on its tables is then possible.
   *
   * 3. There are one or more hook implementations and they are not for
   *    the "Real name" module. A slow search through every User entity is
   *    required to find the display name.
   *
   * Cases 1 and 2 are always handled. Case 3 is performed only if
   * $searchUsers is TRUE.
   *
   * @param string $name
   *   The display name of a user.
   * @param bool $searchUsers
   *   (optional, default = TRUE) When TRUE, the method fails and returns
   *   (-1) if there are no known shortcuts to getting the display name,
   *   and the fallback would require loading every User entity, which can
   *   be slow.
   *
   * @return int
   *   Returns the user ID for the matched user, or (-1) if not found.
   *
   * @see ::findUser()
   * @see ::findUserByAccountName()
   * @see ::findUserByAccountEmail()
   * @see ::findSimilarUsers()
   * @see ::isRealnameInstalled()
   * @see ::getDisplayNameHookModules()
   */
  public static function findUserByDisplayName(
    string $name,
    bool $searchUsers = FALSE) {
 
    $lowerName = mb_strtolower($name);
 
    $hookModules = self::getDisplayNameHookModules();
 
    if (empty($hookModules) === TRUE) {
      // There are no display name hook implementations.
      //
      // The display name falls back to the account name. Anonymous is an
      // exception because it always falls back to the configured name for
      // the account.
      $anonymousName = mb_strtolower(
        \Drupal::config('user.settings')->get('anonymous'));
      if ($anonymousName === $lowerName) {
        return 0;
      }
 
      return self::findUserByAccountName($name);
    }
 
    if (count($hookModules) === 1 && $hookModules[0] === 'realname') {
      // There is one display name hook implementation and it is the "realname"
      // contributed module.  This is a common case.
      //
      // We can search realname's database table of cached computed display
      // names. However, the table does not include a name for every user.
      // If the fields used by a realname configuration are empty for an
      // account, the display name will be empty. Additionally, a value is
      // only computed for a user when a display name is needed. Until then,
      // there will be no entry in realname's table for the user.
      $connection = Database::getConnection();
      $select = $connection->select('realname', 'r');
      $select->addField('r', 'uid', 'uid');
      $select->addField('r', 'realname', 'realname');
      $select->addExpression('LOWER(r.realname)', 'rrealnamelower');
      $select->condition('rrealnamelower', $lowerName, '=');
      $select->orderBy('uid');
      $select->range(0, 1);
      $qresults = $select->execute()->fetchAll();
 
      if (empty($qresults) === TRUE) {
        // There is no entry for the user in realname's table. Check the
        // account name.
        return self::findUserByAccountName($name);
      }
 
      if (empty($qresults[0]->realname) === TRUE) {
        // There is an entry for the user, but it is empty. Check the
        // account name.
        return self::findUserByAccountName($name);
      }
 
      return (int) $qresults[0]->uid;
    }
 
    // There are either multiple hook implementations, or there is just one
    // implementation but it is not the "realname" module. The only way to
    // get the display name is by repeated calls to User::getDisplayName().
    // This requires loading all User entities, which will be slooooow.
    if ($searchUsers === FALSE) {
      // No easy way to get the display name, and search disabled.
      return (-1);
    }
 
    $uids = self::getAllUserIds();
    foreach ($uids as $uid) {
      $user = User::load($uid);
      if ($user !== NULL &&
          mb_strtolower($user->getDisplayName()) === $lowerName) {
        return $uid;
      }
      unset($user);
    }
 
    return (-1);
  }
 
  /**
   * Returns a list of user IDs that are similar to a given name.
   *
   * Database tables are queried for a case insensitive match that includes
   * the given text somewhere within the account name, account email, or
   * display name.
   *
   * The user entity's account names are always searched for matches with
   * the given name.
   *
   * If the third-party "Real name" module is installed, its table of
   * display names is searched for matches with the given name.
   *
   * If $matchEmail is TRUE, the user entity's email addresses are searched
   * for matches with the given name.
   *
   * The returned list of user IDs is ordered alphabetically on the
   * account name.
   *
   * @param string $name
   *   The name or name fragment to search for.
   * @param bool $matchEmail
   *   (optional, default = FALSE) Whether to look for a match against user
   *   email addresses.
   * @param bool $excludeBlocked
   *   (optional, default = TRUE) Whether to include blocked accounts in the
   *   returned list.
   * @param int[] $excludeUids
   *   (optional, default = []) A list of user IDs to exclude from the returned
   *   list.
   * @param int $maxReturn
   *   (optional, default = 10) The maximum number of matches to return. If
   *   this is <= 0, all matches are returned.
   *
   * @return int[]
   *   Returns a list of integer User entity IDs, or an empty list if no
   *   matches are found. IDs are sorted on the account name.
   *
   * @see ::findUser()
   * @see ::findUserByAccountName()
   * @see ::findUserByAccountEmail()
   * @see ::findUserByDisplayName()
   * @see ::isRealnameInstalled()
   */
  public static function findSimilarUsers(
    string $name,
    bool $matchEmail = FALSE,
    bool $excludeBlocked = TRUE,
    array $excludeUids = [],
    int $maxReturn = 10) {
 
    // Query the user entity's fields to get the user ID and account name
    // matches.
    $connection = Database::getConnection();
    $select = $connection->select('users_field_data', 'u');
    $select->addField('u', 'uid', 'uid');
 
    $likeGroup = $select->orConditionGroup();
    $likeGroup->condition('u.name', '%' . $name . '%', 'LIKE');
    $select->orderBy('u.name', 'ASC');
 
    // Optionally search the email address too.
    if ($matchEmail === TRUE) {
      $likeGroup->condition('u.mail', '%' . $name . '%', 'LIKE');
    }
 
    // If the "Real name" module is enabled, check its fields for display
    // name matches.
    if (self::isRealnameInstalled() === TRUE) {
      $select->leftJoin('realname', 'r', '(r.uid = u.uid)');
      $likeGroup->condition('r.realname', '%' . $name . '%', 'LIKE');
    }
 
    // Require a match AND optionally exclude blocked users and
    // those on an exclude list.
    $allowGroup = $select->andConditionGroup();
    $allowGroup->condition($likeGroup);
    if ($excludeBlocked === TRUE) {
      $allowGroup->condition('u.status', 1);
    }
 
    if (empty($excludeUids) === FALSE) {
      $allowGroup->condition('u.uid', $excludeUids, 'NOT IN');
    }
 
    $select = $select->condition($allowGroup);
 
    // Optioinally limit the number of returned results.
    if ($maxReturn > 0) {
      $select->range(0, $maxReturn);
    }
 
    // Get the results and insure they are all integers.
    $nums = $select->execute()->fetchCol(0);
    $uids = [];
    foreach ($nums as $num) {
      $uids[] = (int) $num;
    }
 
    return $uids;
  }
 
  /**
   * Returns a list of all user IDs.
   *
   * @return int[]
   *   Returns a list of all user IDs.
   *
   * @see ::getAllAccountNames()
   * @see ::getAllDisplayNames()
   */
  public static function getAllUserIds() {
    $uids = \Drupal::entityTypeManager()
      ->getStorage('user')
      ->getQuery()
      ->execute();
 
    $n = count($uids);
    for ($i = 0; $i < $n; ++$i) {
      $uids[$i] = (int) $uids[$i];
    }
 
    return $uids;
  }
 
  /**
   * Returns an array of user account names with user ID keys.
   *
   * The returned list is not sorted.
   *
   * @return array
   *   Returns an associative array with user IDs as keys and account
   *   names as values.
   *
   * @see ::getAllUserIds()
   * @see ::getAllDisplayNames()
   */
  public static function getAllAccountNames() {
    $connection = Database::getConnection();
    $select = $connection->select('users_field_data', 'u');
    $select->addField('u', 'uid', 'uid');
    $select->addField('u', 'name', 'name');
    $select->orderby('name');
    $qresults = $select->execute()->fetchAll();
 
    $results = [];
    foreach ($qresults as $r) {
      $results[$r->uid] = $r->name;
    }
 
    return $results;
  }
 
  /**
   * Returns an array of user display names with user ID keys.
   *
   * The returned list is not sorted.
   *
   * @return array
   *   Returns an associative array with user IDs as keys and display
   *   names as values.
   *
   * @see ::getAllUserIds()
   * @see ::getAllAccountNames()
   * @see ::getDisplayNameHookModules()
   */
  public static function getAllDisplayNames() {
    $hookModules = self::getDisplayNameHookModules();
 
    if (empty($hookModules) === TRUE) {
      // There are no display name hook implementations. All display names
      // are just the account name, except for anonymous.
      $results = self::getAllAccountNames();
      $results[0] = self::getAnonymousName();
      return $results;
    }
 
    if (count($hookModules) === 1 && $hookModules[0] === 'realname') {
      // There is one display name hook implementation and it is the "realname"
      // contributed module.  This is a common case.
      //
      // Use realname's table of cached computed display names. However,
      // the table does not include a name for every user.  If the fields
      // used by a realname configuration are empty for an account, the
      // display name will be empty. Additionally, a value is only computed
      // for a user when a display name is needed. Until then, there will be
      // no entry in realname's table for the user.
      //
      // If there is no display name in the table yet, fall back to the
      // account name.
      $connection = Database::getConnection();
      $select = $connection->select('users_field_data', 'u');
      $select->addField('u', 'uid', 'uid');
      $select->addField('u', 'name', 'name');
      $select->leftJoin('realname', 'r', '(r.uid = u.uid)');
      $select->addField('r', 'realname', 'realname');
      $select->orderBy('uid');
      $qresults = $select->execute()->fetchAll();
 
      $results = [];
      foreach ($qresults as $r) {
        if (empty($r->realname) === TRUE) {
          $results[$r->uid] = $r->name;
        }
        else {
          $results[$r->uid] = $r->realname;
        }
      }
 
      if (isset($results[0]) === FALSE) {
        $results[0] = self::getAnonymousName();
      }
 
      return $results;
    }
 
    // There are either multiple hook implementations, or there is just one
    // implementation but it is not the "realname" module. The only way to
    // get the display name is by repeated calls to User::getDisplayName().
    // This requires loading all User entities, which will be slooooow.
    $uids = self::getAllUserIds();
    $results = [];
    foreach ($uids as $uid) {
      $user = User::load($uid);
      if ($user !== NULL) {
        $results[$user->id()] = $user->getDisplayName();
      }
      unset($user);
    }
 
    return $results;
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc