User.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @package Grav\Common\User
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\User;
  9. use Grav\Common\Grav;
  10. use Grav\Common\User\DataUser;
  11. use Grav\Common\Flex;
  12. use Grav\Common\User\Interfaces\UserCollectionInterface;
  13. use Grav\Common\User\Interfaces\UserInterface;
  14. if (!defined('GRAV_USER_INSTANCE')) {
  15. throw new \LogicException('User class was called too early!');
  16. }
  17. if (defined('GRAV_USER_INSTANCE') && GRAV_USER_INSTANCE === 'FLEX') {
  18. /**
  19. * @deprecated 1.6 Use $grav['accounts'] instead of static calls. In type hints, please use UserInterface.
  20. */
  21. class User extends Flex\Types\Users\UserObject
  22. {
  23. /**
  24. * Load user account.
  25. *
  26. * Always creates user object. To check if user exists, use $this->exists().
  27. *
  28. * @param string $username
  29. * @return UserInterface
  30. * @deprecated 1.6 Use $grav['accounts']->load(...) instead.
  31. */
  32. public static function load($username)
  33. {
  34. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->' . __FUNCTION__ . '() instead', E_USER_DEPRECATED);
  35. return static::getCollection()->load($username);
  36. }
  37. /**
  38. * Find a user by username, email, etc
  39. *
  40. * Always creates user object. To check if user exists, use $this->exists().
  41. *
  42. * @param string $query the query to search for
  43. * @param array $fields the fields to search
  44. * @return UserInterface
  45. * @deprecated 1.6 Use $grav['accounts']->find(...) instead.
  46. */
  47. public static function find($query, $fields = ['username', 'email'])
  48. {
  49. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->' . __FUNCTION__ . '() instead', E_USER_DEPRECATED);
  50. return static::getCollection()->find($query, $fields);
  51. }
  52. /**
  53. * Remove user account.
  54. *
  55. * @param string $username
  56. * @return bool True if the action was performed
  57. * @deprecated 1.6 Use $grav['accounts']->delete(...) instead.
  58. */
  59. public static function remove($username)
  60. {
  61. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->delete() instead', E_USER_DEPRECATED);
  62. return static::getCollection()->delete($username);
  63. }
  64. /**
  65. * @return UserCollectionInterface
  66. */
  67. protected static function getCollection()
  68. {
  69. return Grav::instance()['accounts'];
  70. }
  71. }
  72. } else {
  73. /**
  74. * @deprecated 1.6 Use $grav['accounts'] instead of static calls. In type hints, use UserInterface.
  75. */
  76. class User extends DataUser\User
  77. {
  78. /**
  79. * Load user account.
  80. *
  81. * Always creates user object. To check if user exists, use $this->exists().
  82. *
  83. * @param string $username
  84. * @return UserInterface
  85. * @deprecated 1.6 Use $grav['accounts']->load(...) instead.
  86. */
  87. public static function load($username)
  88. {
  89. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->' . __FUNCTION__ . '() instead', E_USER_DEPRECATED);
  90. return static::getCollection()->load($username);
  91. }
  92. /**
  93. * Find a user by username, email, etc
  94. *
  95. * Always creates user object. To check if user exists, use $this->exists().
  96. *
  97. * @param string $query the query to search for
  98. * @param array $fields the fields to search
  99. * @return UserInterface
  100. * @deprecated 1.6 Use $grav['accounts']->find(...) instead.
  101. */
  102. public static function find($query, $fields = ['username', 'email'])
  103. {
  104. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->' . __FUNCTION__ . '() instead', E_USER_DEPRECATED);
  105. return static::getCollection()->find($query, $fields);
  106. }
  107. /**
  108. * Remove user account.
  109. *
  110. * @param string $username
  111. * @return bool True if the action was performed
  112. * @deprecated 1.6 Use $grav['accounts']->delete(...) instead.
  113. */
  114. public static function remove($username)
  115. {
  116. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->delete() instead', E_USER_DEPRECATED);
  117. return static::getCollection()->delete($username);
  118. }
  119. /**
  120. * @return UserCollectionInterface
  121. */
  122. protected static function getCollection()
  123. {
  124. return Grav::instance()['accounts'];
  125. }
  126. }
  127. }