User.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * @package Grav\Common\User
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 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\User\FlexUser;
  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 FlexUser\User
  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. *
  30. * @return UserInterface
  31. * @deprecated 1.6 Use $grav['accounts']->load(...) instead.
  32. */
  33. public static function load($username)
  34. {
  35. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->' . __FUNCTION__ . '() instead', E_USER_DEPRECATED);
  36. return static::getCollection()->load($username);
  37. }
  38. /**
  39. * Find a user by username, email, etc
  40. *
  41. * Always creates user object. To check if user exists, use $this->exists().
  42. *
  43. * @param string $query the query to search for
  44. * @param array $fields the fields to search
  45. * @return UserInterface
  46. * @deprecated 1.6 Use $grav['accounts']->find(...) instead.
  47. */
  48. public static function find($query, $fields = ['username', 'email'])
  49. {
  50. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->' . __FUNCTION__ . '() instead', E_USER_DEPRECATED);
  51. return static::getCollection()->find($query, $fields);
  52. }
  53. /**
  54. * Remove user account.
  55. *
  56. * @param string $username
  57. * @return bool True if the action was performed
  58. * @deprecated 1.6 Use $grav['accounts']->delete(...) instead.
  59. */
  60. public static function remove($username)
  61. {
  62. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->delete() instead', E_USER_DEPRECATED);
  63. return static::getCollection()->delete($username);
  64. }
  65. /**
  66. * @return UserCollectionInterface
  67. */
  68. protected static function getCollection()
  69. {
  70. return Grav::instance()['accounts'];
  71. }
  72. }
  73. } else {
  74. /**
  75. * @deprecated 1.6 Use $grav['accounts'] instead of static calls. In type hints, use UserInterface.
  76. */
  77. class User extends DataUser\User
  78. {
  79. /**
  80. * Load user account.
  81. *
  82. * Always creates user object. To check if user exists, use $this->exists().
  83. *
  84. * @param string $username
  85. *
  86. * @return UserInterface
  87. * @deprecated 1.6 Use $grav['accounts']->load(...) instead.
  88. */
  89. public static function load($username)
  90. {
  91. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->' . __FUNCTION__ . '() instead', E_USER_DEPRECATED);
  92. return static::getCollection()->load($username);
  93. }
  94. /**
  95. * Find a user by username, email, etc
  96. *
  97. * Always creates user object. To check if user exists, use $this->exists().
  98. *
  99. * @param string $query the query to search for
  100. * @param array $fields the fields to search
  101. * @return UserInterface
  102. * @deprecated 1.6 Use $grav['accounts']->find(...) instead.
  103. */
  104. public static function find($query, $fields = ['username', 'email'])
  105. {
  106. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->' . __FUNCTION__ . '() instead', E_USER_DEPRECATED);
  107. return static::getCollection()->find($query, $fields);
  108. }
  109. /**
  110. * Remove user account.
  111. *
  112. * @param string $username
  113. * @return bool True if the action was performed
  114. * @deprecated 1.6 Use $grav['accounts']->delete(...) instead.
  115. */
  116. public static function remove($username)
  117. {
  118. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use $grav[\'accounts\']->delete() instead', E_USER_DEPRECATED);
  119. return static::getCollection()->delete($username);
  120. }
  121. /**
  122. * @return UserCollectionInterface
  123. */
  124. protected static function getCollection()
  125. {
  126. return Grav::instance()['accounts'];
  127. }
  128. }
  129. }