User.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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\DataUser;
  9. use Grav\Common\Data\Blueprint;
  10. use Grav\Common\Data\Blueprints;
  11. use Grav\Common\Data\Data;
  12. use Grav\Common\File\CompiledYamlFile;
  13. use Grav\Common\Grav;
  14. use Grav\Common\Page\Media;
  15. use Grav\Common\Page\Medium\ImageMedium;
  16. use Grav\Common\Page\Medium\MediumFactory;
  17. use Grav\Common\User\Authentication;
  18. use Grav\Common\User\Interfaces\UserInterface;
  19. use Grav\Common\User\Traits\UserTrait;
  20. class User extends Data implements UserInterface
  21. {
  22. use UserTrait;
  23. protected $_media;
  24. /**
  25. * User constructor.
  26. * @param array $items
  27. * @param Blueprint $blueprints
  28. */
  29. public function __construct(array $items = [], $blueprints = null)
  30. {
  31. // User can only be authenticated via login.
  32. unset($items['authenticated'], $items['authorized']);
  33. // Always set blueprints.
  34. if (null === $blueprints) {
  35. $blueprints = (new Blueprints)->get('user/account');
  36. }
  37. parent::__construct($items, $blueprints);
  38. }
  39. /**
  40. * @param string $offset
  41. * @return bool
  42. */
  43. public function offsetExists($offset)
  44. {
  45. $value = parent::offsetExists($offset);
  46. // Handle special case where user was logged in before 'authorized' was added to the user object.
  47. if (false === $value && $offset === 'authorized') {
  48. $value = $this->offsetExists('authenticated');
  49. }
  50. return $value;
  51. }
  52. /**
  53. * @param string $offset
  54. * @return mixed
  55. */
  56. public function offsetGet($offset)
  57. {
  58. $value = parent::offsetGet($offset);
  59. // Handle special case where user was logged in before 'authorized' was added to the user object.
  60. if (null === $value && $offset === 'authorized') {
  61. $value = $this->offsetGet('authenticated');
  62. $this->offsetSet($offset, $value);
  63. }
  64. return $value;
  65. }
  66. public function isValid(): bool
  67. {
  68. return $this->items !== null;
  69. }
  70. /**
  71. * Update object with data
  72. *
  73. * @param array $data
  74. * @param array $files
  75. * @return $this
  76. */
  77. public function update(array $data, array $files = [])
  78. {
  79. // Note: $this->merge() would cause infinite loop as it calls this method.
  80. parent::merge($data);
  81. return $this;
  82. }
  83. /**
  84. * Save user without the username
  85. */
  86. public function save()
  87. {
  88. /** @var CompiledYamlFile $file */
  89. $file = $this->file();
  90. if (!$file || !$file->filename()) {
  91. user_error(__CLASS__ . ': calling \$user = new ' . __CLASS__ . "() is deprecated since Grav 1.6, use \$grav['accounts']->load(\$username) or \$grav['accounts']->load('') instead", E_USER_DEPRECATED);
  92. }
  93. if ($file) {
  94. $username = $this->get('username');
  95. if (!$file->filename()) {
  96. $locator = Grav::instance()['locator'];
  97. $file->filename($locator->findResource('account://' . mb_strtolower($username) . YAML_EXT, true, true));
  98. }
  99. // if plain text password, hash it and remove plain text
  100. $password = $this->get('password');
  101. if ($password) {
  102. $this->set('hashed_password', Authentication::create($password));
  103. $this->undef('password');
  104. }
  105. $data = $this->items;
  106. unset($data['username'], $data['authenticated'], $data['authorized']);
  107. $file->save($data);
  108. }
  109. }
  110. public function getMedia()
  111. {
  112. if (null === $this->_media) {
  113. // Media object should only contain avatar, nothing else.
  114. $media = new Media($this->getMediaFolder(), $this->getMediaOrder(), false);
  115. $path = $this->getAvatarFile();
  116. if ($path && is_file($path)) {
  117. $medium = MediumFactory::fromFile($path);
  118. if ($medium) {
  119. $media->add(basename($path), $medium);
  120. }
  121. }
  122. $this->_media = $media;
  123. }
  124. return $this->_media;
  125. }
  126. public function getMediaFolder()
  127. {
  128. return $this->blueprints()->fields()['avatar']['destination'] ?? 'user://accounts/avatars';
  129. }
  130. public function getMediaOrder()
  131. {
  132. return [];
  133. }
  134. /**
  135. * Serialize user.
  136. */
  137. public function __sleep()
  138. {
  139. return [
  140. 'items',
  141. 'storage'
  142. ];
  143. }
  144. /**
  145. * Unserialize user.
  146. */
  147. public function __wakeup()
  148. {
  149. $this->gettersVariable = 'items';
  150. $this->nestedSeparator = '.';
  151. if (null === $this->items) {
  152. $this->items = [];
  153. }
  154. // Always set blueprints.
  155. if (null === $this->blueprints) {
  156. $this->blueprints = (new Blueprints)->get('user/account');
  157. }
  158. }
  159. /**
  160. * Merge two configurations together.
  161. *
  162. * @param array $data
  163. * @return $this
  164. * @deprecated 1.6 Use `->update($data)` instead (same but with data validation & filtering, file upload support).
  165. */
  166. public function merge(array $data)
  167. {
  168. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use ->update($data) method instead', E_USER_DEPRECATED);
  169. return $this->update($data);
  170. }
  171. /**
  172. * Return media object for the User's avatar.
  173. *
  174. * @return ImageMedium|null
  175. * @deprecated 1.6 Use ->getAvatarImage() method instead.
  176. */
  177. public function getAvatarMedia()
  178. {
  179. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use getAvatarImage() method instead', E_USER_DEPRECATED);
  180. return $this->getAvatarImage();
  181. }
  182. /**
  183. * Return the User's avatar URL
  184. *
  185. * @return string
  186. * @deprecated 1.6 Use ->getAvatarUrl() method instead.
  187. */
  188. public function avatarUrl()
  189. {
  190. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use getAvatarUrl() method instead', E_USER_DEPRECATED);
  191. return $this->getAvatarUrl();
  192. }
  193. /**
  194. * Checks user authorization to the action.
  195. * Ensures backwards compatibility
  196. *
  197. * @param string $action
  198. *
  199. * @return bool
  200. * @deprecated 1.5 Use ->authorize() method instead.
  201. */
  202. public function authorise($action)
  203. {
  204. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use authorize() method instead', E_USER_DEPRECATED);
  205. return $this->authorize($action);
  206. }
  207. /**
  208. * Implements Countable interface.
  209. *
  210. * @return int
  211. * @deprecated 1.6 Method makes no sense for user account.
  212. */
  213. public function count()
  214. {
  215. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6', E_USER_DEPRECATED);
  216. return parent::count();
  217. }
  218. protected function getAvatarFile(): ?string
  219. {
  220. $avatars = $this->get('avatar');
  221. if (\is_array($avatars) && $avatars) {
  222. $avatar = array_shift($avatars);
  223. return $avatar['path'] ?? null;
  224. }
  225. return null;
  226. }
  227. }