User.php 8.7 KB

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