UserCollection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Common\Flex
  5. *
  6. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Common\Flex\Types\Users;
  10. use Grav\Common\Flex\FlexCollection;
  11. use Grav\Common\User\Interfaces\UserCollectionInterface;
  12. use Grav\Common\User\Interfaces\UserInterface;
  13. use function is_string;
  14. /**
  15. * Class UserCollection
  16. * @package Grav\Common\Flex\Types\Users
  17. *
  18. * @extends FlexCollection<UserObject>
  19. */
  20. class UserCollection extends FlexCollection implements UserCollectionInterface
  21. {
  22. /**
  23. * @return array
  24. */
  25. public static function getCachedMethods(): array
  26. {
  27. return [
  28. 'authorize' => 'session',
  29. ] + parent::getCachedMethods();
  30. }
  31. /**
  32. * Load user account.
  33. *
  34. * Always creates user object. To check if user exists, use $this->exists().
  35. *
  36. * @param string $username
  37. * @return UserObject
  38. */
  39. public function load($username): UserInterface
  40. {
  41. $username = (string)$username;
  42. if ($username !== '') {
  43. $key = $this->filterUsername($username);
  44. $user = $this->get($key);
  45. if ($user) {
  46. return $user;
  47. }
  48. } else {
  49. $key = '';
  50. }
  51. $directory = $this->getFlexDirectory();
  52. /** @var UserObject $object */
  53. $object = $directory->createObject(
  54. [
  55. 'username' => $username,
  56. 'state' => 'enabled'
  57. ],
  58. $key
  59. );
  60. return $object;
  61. }
  62. /**
  63. * Find a user by username, email, etc
  64. *
  65. * @param string $query the query to search for
  66. * @param string|string[] $fields the fields to search
  67. * @return UserObject
  68. */
  69. public function find($query, $fields = ['username', 'email']): UserInterface
  70. {
  71. if (is_string($query) && $query !== '') {
  72. foreach ((array)$fields as $field) {
  73. if ($field === 'key') {
  74. $user = $this->get($query);
  75. } elseif ($field === 'storage_key') {
  76. $user = $this->withKeyField('storage_key')->get($query);
  77. } elseif ($field === 'flex_key') {
  78. $user = $this->withKeyField('flex_key')->get($query);
  79. } elseif ($field === 'username') {
  80. $user = $this->get($this->filterUsername($query));
  81. } else {
  82. $user = parent::find($query, $field);
  83. }
  84. if ($user instanceof UserObject) {
  85. return $user;
  86. }
  87. }
  88. }
  89. return $this->load('');
  90. }
  91. /**
  92. * Delete user account.
  93. *
  94. * @param string $username
  95. * @return bool True if user account was found and was deleted.
  96. */
  97. public function delete($username): bool
  98. {
  99. $user = $this->load($username);
  100. $exists = $user->exists();
  101. if ($exists) {
  102. $user->delete();
  103. }
  104. return $exists;
  105. }
  106. /**
  107. * @param string $key
  108. * @return string
  109. */
  110. protected function filterUsername(string $key): string
  111. {
  112. $storage = $this->getFlexDirectory()->getStorage();
  113. if (method_exists($storage, 'normalizeKey')) {
  114. return $storage->normalizeKey($key);
  115. }
  116. return mb_strtolower($key);
  117. }
  118. }