userController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Controller to handle user actions.
  5. */
  6. class FreshRSS_user_Controller extends FreshRSS_ActionController {
  7. /**
  8. * The username is also used as folder name, file name, and part of SQL table name.
  9. * '_' is a reserved internal username.
  10. */
  11. public const USERNAME_PATTERN = '([0-9a-zA-Z_][0-9a-zA-Z_.@\-]{1,38}|[0-9a-zA-Z])';
  12. public static function checkUsername(string $username): bool {
  13. return preg_match('/^' . self::USERNAME_PATTERN . '$/', $username) === 1;
  14. }
  15. public static function userExists(string $username): bool {
  16. $config_path = USERS_PATH . '/' . $username . '/config.php';
  17. if (@file_exists($config_path)) {
  18. return true;
  19. } elseif (@file_exists($config_path . '.bak.php')) {
  20. Minz_Log::warning('Config for user “' . $username . '” not found. Attempting to restore from backup.', ADMIN_LOG);
  21. if (!copy($config_path . '.bak.php', $config_path)) {
  22. @unlink($config_path);
  23. return false;
  24. }
  25. return @file_exists($config_path);
  26. }
  27. return false;
  28. }
  29. /** @param array<string,mixed> $userConfigUpdated */
  30. public static function updateUser(string $user, ?string $email, string $passwordPlain, array $userConfigUpdated = []): bool {
  31. $userConfig = get_user_configuration($user);
  32. if ($userConfig === null) {
  33. return false;
  34. }
  35. if ($email !== null && $userConfig->mail_login !== $email) {
  36. $userConfig->mail_login = $email;
  37. if (FreshRSS_Context::systemConf()->force_email_validation) {
  38. $salt = FreshRSS_Context::systemConf()->salt;
  39. $userConfig->email_validation_token = sha1($salt . uniqid('' . mt_rand(), true));
  40. $mailer = new FreshRSS_User_Mailer();
  41. $mailer->send_email_need_validation($user, $userConfig);
  42. }
  43. }
  44. if ($passwordPlain != '') {
  45. $passwordHash = FreshRSS_password_Util::hash($passwordPlain);
  46. $userConfig->passwordHash = $passwordHash;
  47. }
  48. foreach ($userConfigUpdated as $configName => $configValue) {
  49. if ($configName !== '' && $configValue !== null) {
  50. $userConfig->_attribute($configName, $configValue);
  51. }
  52. }
  53. $ok = $userConfig->save();
  54. return $ok;
  55. }
  56. public function updateAction(): void {
  57. if (!FreshRSS_Auth::hasAccess('admin')) {
  58. Minz_Error::error(403);
  59. }
  60. if (Minz_Request::isPost()) {
  61. $passwordPlain = Minz_Request::paramString('newPasswordPlain', true);
  62. Minz_Request::_param('newPasswordPlain'); //Discard plain-text password ASAP
  63. $_POST['newPasswordPlain'] = '';
  64. $username = Minz_Request::paramString('username');
  65. $ok = self::updateUser($username, null, $passwordPlain, [
  66. 'token' => Minz_Request::paramString('token') ?: null,
  67. ]);
  68. if ($ok) {
  69. $isSelfUpdate = Minz_User::name() === $username;
  70. if ($passwordPlain == '' || !$isSelfUpdate) {
  71. Minz_Request::good(_t('feedback.user.updated', $username), ['c' => 'user', 'a' => 'manage']);
  72. } else {
  73. Minz_Request::good(_t('feedback.profile.updated'), ['c' => 'index', 'a' => 'index']);
  74. }
  75. } else {
  76. Minz_Request::bad(_t('feedback.user.updated.error', $username), ['c' => 'user', 'a' => 'manage']);
  77. }
  78. }
  79. }
  80. /**
  81. * This action displays the user profile page.
  82. */
  83. public function profileAction(): void {
  84. if (!FreshRSS_Auth::hasAccess()) {
  85. Minz_Error::error(403);
  86. }
  87. $email_not_verified = FreshRSS_Context::userConf()->email_validation_token != '';
  88. $this->view->disable_aside = false;
  89. if ($email_not_verified) {
  90. $this->view->_layout('simple');
  91. $this->view->disable_aside = true;
  92. }
  93. FreshRSS_View::prependTitle(_t('conf.profile.title') . ' · ');
  94. FreshRSS_View::appendScript(Minz_Url::display('/scripts/vendor/bcrypt.js?' . @filemtime(PUBLIC_PATH . '/scripts/vendor/bcrypt.js')));
  95. if (Minz_Request::isPost() && Minz_User::name() != null) {
  96. $old_email = FreshRSS_Context::userConf()->mail_login;
  97. $email = Minz_Request::paramString('email');
  98. $passwordPlain = Minz_Request::paramString('newPasswordPlain', true);
  99. Minz_Request::_param('newPasswordPlain'); //Discard plain-text password ASAP
  100. $_POST['newPasswordPlain'] = '';
  101. if (FreshRSS_Context::systemConf()->force_email_validation && empty($email)) {
  102. Minz_Request::bad(
  103. _t('user.email.feedback.required'),
  104. ['c' => 'user', 'a' => 'profile']
  105. );
  106. }
  107. if (!empty($email) && !validateEmailAddress($email)) {
  108. Minz_Request::bad(
  109. _t('user.email.feedback.invalid'),
  110. ['c' => 'user', 'a' => 'profile']
  111. );
  112. }
  113. $ok = self::updateUser(
  114. Minz_User::name(),
  115. $email,
  116. $passwordPlain,
  117. [
  118. 'token' => Minz_Request::paramString('token'),
  119. ]
  120. );
  121. Minz_Session::_param('passwordHash', FreshRSS_Context::userConf()->passwordHash);
  122. if ($ok) {
  123. if (FreshRSS_Context::systemConf()->force_email_validation && $email !== $old_email) {
  124. Minz_Request::good(_t('feedback.profile.updated'), ['c' => 'user', 'a' => 'validateEmail']);
  125. } elseif ($passwordPlain == '') {
  126. Minz_Request::good(_t('feedback.profile.updated'), ['c' => 'user', 'a' => 'profile']);
  127. } else {
  128. Minz_Request::good(_t('feedback.profile.updated'), ['c' => 'index', 'a' => 'index']);
  129. }
  130. } else {
  131. Minz_Request::bad(_t('feedback.profile.error'), ['c' => 'user', 'a' => 'profile']);
  132. }
  133. }
  134. }
  135. public function purgeAction(): void {
  136. if (!FreshRSS_Auth::hasAccess('admin')) {
  137. Minz_Error::error(403);
  138. }
  139. if (Minz_Request::isPost()) {
  140. $username = Minz_Request::paramString('username');
  141. if (!FreshRSS_UserDAO::exists($username)) {
  142. Minz_Error::error(404);
  143. }
  144. $feedDAO = FreshRSS_Factory::createFeedDao($username);
  145. $feedDAO->purge();
  146. }
  147. }
  148. /**
  149. * This action displays the user management page.
  150. */
  151. public function manageAction(): void {
  152. if (!FreshRSS_Auth::hasAccess('admin')) {
  153. Minz_Error::error(403);
  154. }
  155. FreshRSS_View::prependTitle(_t('admin.user.title') . ' · ');
  156. if (Minz_Request::isPost()) {
  157. $action = Minz_Request::paramString('action');
  158. switch ($action) {
  159. case 'delete':
  160. $this->deleteAction();
  161. break;
  162. case 'update':
  163. $this->updateAction();
  164. break;
  165. case 'purge':
  166. $this->purgeAction();
  167. break;
  168. case 'promote':
  169. $this->promoteAction();
  170. break;
  171. case 'demote':
  172. $this->demoteAction();
  173. break;
  174. case 'enable':
  175. $this->enableAction();
  176. break;
  177. case 'disable':
  178. $this->disableAction();
  179. break;
  180. }
  181. }
  182. $this->view->show_email_field = FreshRSS_Context::systemConf()->force_email_validation;
  183. $this->view->current_user = Minz_Request::paramString('u');
  184. foreach (listUsers() as $user) {
  185. $this->view->users[$user] = $this->retrieveUserDetails($user);
  186. }
  187. }
  188. /**
  189. * @param array<string,mixed> $userConfigOverride
  190. * @throws Minz_ConfigurationNamespaceException
  191. * @throws Minz_PDOConnectionException
  192. */
  193. public static function createUser(string $new_user_name, ?string $email, string $passwordPlain,
  194. array $userConfigOverride = [], bool $insertDefaultFeeds = true): bool {
  195. $userConfig = [];
  196. $customUserConfigPath = join_path(DATA_PATH, 'config-user.custom.php');
  197. if (file_exists($customUserConfigPath)) {
  198. $customUserConfig = include($customUserConfigPath);
  199. if (is_array($customUserConfig)) {
  200. $userConfig = $customUserConfig;
  201. }
  202. }
  203. $userConfig = array_merge($userConfig, $userConfigOverride);
  204. $ok = self::checkUsername($new_user_name);
  205. $homeDir = join_path(DATA_PATH, 'users', $new_user_name);
  206. // create basepath if missing
  207. if (!is_dir(join_path(DATA_PATH, 'users'))) {
  208. $ok &= mkdir(join_path(DATA_PATH, 'users'), 0770, true);
  209. }
  210. $configPath = '';
  211. if ($ok) {
  212. $languages = Minz_Translate::availableLanguages();
  213. if (empty($userConfig['language']) || !in_array($userConfig['language'], $languages, true)) {
  214. $userConfig['language'] = 'en';
  215. }
  216. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers()), true); //Not an existing user, case-insensitive
  217. $configPath = join_path($homeDir, 'config.php');
  218. $ok &= !file_exists($configPath);
  219. }
  220. if ($ok) {
  221. // $homeDir must not exist beforehand,
  222. // otherwise it might be multiple remote parties racing to register one username
  223. $ok = mkdir($homeDir, 0770, true);
  224. if ($ok) {
  225. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($userConfig, true) . ';') !== false);
  226. }
  227. }
  228. if ($ok) {
  229. $newUserDAO = FreshRSS_Factory::createUserDao($new_user_name);
  230. $ok &= $newUserDAO->createUser();
  231. if ($ok && $insertDefaultFeeds) {
  232. $opmlPath = DATA_PATH . '/opml.xml';
  233. if (!file_exists($opmlPath)) {
  234. $opmlPath = FRESHRSS_PATH . '/opml.default.xml';
  235. }
  236. $importController = new FreshRSS_importExport_Controller();
  237. try {
  238. $importController->importFile($opmlPath, $opmlPath, $new_user_name);
  239. } catch (Exception $e) {
  240. Minz_Log::error('Error while importing default OPML for user ' . $new_user_name . ': ' . $e->getMessage());
  241. }
  242. }
  243. $ok &= self::updateUser($new_user_name, $email, $passwordPlain);
  244. }
  245. return (bool)$ok;
  246. }
  247. /**
  248. * This action creates a new user.
  249. *
  250. * Request parameters are:
  251. * - new_user_language
  252. * - new_user_name
  253. * - new_user_email
  254. * - new_user_passwordPlain
  255. * - r (i.e. a redirection url, optional)
  256. *
  257. * @todo clean up this method. Idea: write a method to init a user with basic information.
  258. */
  259. public function createAction(): void {
  260. if (!FreshRSS_Auth::hasAccess('admin') && max_registrations_reached()) {
  261. Minz_Error::error(403);
  262. }
  263. if (Minz_Request::isPost()) {
  264. $new_user_name = Minz_Request::paramString('new_user_name');
  265. $email = Minz_Request::paramString('new_user_email');
  266. $passwordPlain = Minz_Request::paramString('new_user_passwordPlain', true);
  267. $badRedirectUrl = [
  268. 'c' => Minz_Request::paramString('originController') ?: 'auth',
  269. 'a' => Minz_Request::paramString('originAction') ?: 'register',
  270. ];
  271. if (!self::checkUsername($new_user_name)) {
  272. Minz_Request::bad(
  273. _t('user.username.invalid'),
  274. $badRedirectUrl
  275. );
  276. }
  277. if (FreshRSS_UserDAO::exists($new_user_name)) {
  278. Minz_Request::bad(
  279. _t('user.username.taken', $new_user_name),
  280. $badRedirectUrl
  281. );
  282. }
  283. if (!FreshRSS_password_Util::check($passwordPlain)) {
  284. Minz_Request::bad(
  285. _t('user.password.invalid'),
  286. $badRedirectUrl
  287. );
  288. }
  289. if (!FreshRSS_Auth::hasAccess('admin')) {
  290. // TODO: We may want to ask the user to accept TOS before first login
  291. $tos_enabled = file_exists(TOS_FILENAME);
  292. $accept_tos = Minz_Request::paramBoolean('accept_tos');
  293. if ($tos_enabled && !$accept_tos) {
  294. Minz_Request::bad(_t('user.tos.feedback.invalid'), $badRedirectUrl);
  295. }
  296. }
  297. if (FreshRSS_Context::systemConf()->force_email_validation && empty($email)) {
  298. Minz_Request::bad(
  299. _t('user.email.feedback.required'),
  300. $badRedirectUrl
  301. );
  302. }
  303. if (!empty($email) && !validateEmailAddress($email)) {
  304. Minz_Request::bad(
  305. _t('user.email.feedback.invalid'),
  306. $badRedirectUrl
  307. );
  308. }
  309. $ok = self::createUser($new_user_name, $email, $passwordPlain, [
  310. 'language' => Minz_Request::paramString('new_user_language') ?: FreshRSS_Context::userConf()->language,
  311. 'timezone' => Minz_Request::paramString('new_user_timezone'),
  312. 'is_admin' => Minz_Request::paramBoolean('new_user_is_admin'),
  313. 'enabled' => true,
  314. ]);
  315. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  316. $_POST['new_user_passwordPlain'] = '';
  317. invalidateHttpCache();
  318. // If the user has admin access, it means he’s already logged in
  319. // and we don’t want to login with the new account. Otherwise, the
  320. // user just created its account himself so he probably wants to
  321. // get started immediately.
  322. if ($ok && !FreshRSS_Auth::hasAccess('admin')) {
  323. $user_conf = get_user_configuration($new_user_name);
  324. if ($user_conf !== null) {
  325. Minz_Session::_params([
  326. Minz_User::CURRENT_USER => $new_user_name,
  327. 'passwordHash' => $user_conf->passwordHash,
  328. 'csrf' => false,
  329. ]);
  330. FreshRSS_Auth::giveAccess();
  331. } else {
  332. $ok = false;
  333. }
  334. }
  335. if ($ok) {
  336. Minz_Request::setGoodNotification(_t('feedback.user.created', $new_user_name));
  337. } else {
  338. Minz_Request::setBadNotification(_t('feedback.user.created.error', $new_user_name));
  339. }
  340. }
  341. if (FreshRSS_Auth::hasAccess('admin')) {
  342. $redirect_url = ['c' => 'user', 'a' => 'manage'];
  343. } else {
  344. $redirect_url = ['c' => 'index', 'a' => 'index'];
  345. }
  346. Minz_Request::forward($redirect_url, true);
  347. }
  348. public static function deleteUser(string $username): bool {
  349. $ok = self::checkUsername($username);
  350. if ($ok) {
  351. $default_user = FreshRSS_Context::systemConf()->default_user;
  352. $ok &= (strcasecmp($username, $default_user) !== 0); //It is forbidden to delete the default user
  353. }
  354. $user_data = join_path(DATA_PATH, 'users', $username);
  355. $ok &= is_dir($user_data);
  356. if ($ok) {
  357. FreshRSS_fever_Util::deleteKey($username);
  358. Minz_ModelPdo::$usesSharedPdo = false;
  359. $oldUserDAO = FreshRSS_Factory::createUserDao($username);
  360. $ok &= $oldUserDAO->deleteUser();
  361. Minz_ModelPdo::$usesSharedPdo = true;
  362. $ok &= recursive_unlink($user_data);
  363. $filenames = glob(PSHB_PATH . '/feeds/*/' . $username . '.txt');
  364. if (!empty($filenames)) {
  365. array_map('unlink', $filenames);
  366. }
  367. }
  368. return (bool)$ok;
  369. }
  370. /**
  371. * This action validates an email address, based on the token sent by email.
  372. * It also serves the main page when user is blocked.
  373. *
  374. * Request parameters are:
  375. * - username
  376. * - token
  377. *
  378. * This route works with GET requests since the URL is provided by email.
  379. * The security risks (e.g. forged URL by an attacker) are not very high so
  380. * it’s ok.
  381. *
  382. * It returns 404 error if `force_email_validation` is disabled or if the
  383. * user doesn’t exist.
  384. *
  385. * It returns 403 if user isn’t logged in and `username` param isn’t passed.
  386. */
  387. public function validateEmailAction(): void {
  388. if (!FreshRSS_Context::systemConf()->force_email_validation) {
  389. Minz_Error::error(404);
  390. }
  391. FreshRSS_View::prependTitle(_t('user.email.validation.title') . ' · ');
  392. $this->view->_layout('simple');
  393. $username = Minz_Request::paramString('username');
  394. $token = Minz_Request::paramString('token');
  395. if ($username !== '') {
  396. $user_config = get_user_configuration($username);
  397. } elseif (FreshRSS_Auth::hasAccess()) {
  398. $user_config = FreshRSS_Context::userConf();
  399. } else {
  400. Minz_Error::error(403);
  401. return;
  402. }
  403. if (!FreshRSS_UserDAO::exists($username) || $user_config === null) {
  404. Minz_Error::error(404);
  405. return;
  406. }
  407. if ($user_config->email_validation_token === '') {
  408. Minz_Request::good(
  409. _t('user.email.validation.feedback.unnecessary'),
  410. ['c' => 'index', 'a' => 'index']
  411. );
  412. }
  413. if ($token != '') {
  414. if ($user_config->email_validation_token !== $token) {
  415. Minz_Request::bad(
  416. _t('user.email.validation.feedback.wrong_token'),
  417. ['c' => 'user', 'a' => 'validateEmail']
  418. );
  419. }
  420. $user_config->email_validation_token = '';
  421. if ($user_config->save()) {
  422. Minz_Request::good(
  423. _t('user.email.validation.feedback.ok'),
  424. ['c' => 'index', 'a' => 'index']
  425. );
  426. } else {
  427. Minz_Request::bad(
  428. _t('user.email.validation.feedback.error'),
  429. ['c' => 'user', 'a' => 'validateEmail']
  430. );
  431. }
  432. }
  433. }
  434. /**
  435. * This action resends a validation email to the current user.
  436. *
  437. * It only acts on POST requests but doesn’t require any param (except the
  438. * CSRF token).
  439. *
  440. * It returns 403 error if the user is not logged in or 404 if request is
  441. * not POST. Else it redirects silently to the index if user has already
  442. * validated its email, or to the user#validateEmail route.
  443. */
  444. public function sendValidationEmailAction(): void {
  445. if (!FreshRSS_Auth::hasAccess()) {
  446. Minz_Error::error(403);
  447. }
  448. if (!Minz_Request::isPost()) {
  449. Minz_Error::error(404);
  450. }
  451. $username = Minz_User::name();
  452. if (FreshRSS_Context::userConf()->email_validation_token === '') {
  453. Minz_Request::forward([
  454. 'c' => 'index',
  455. 'a' => 'index',
  456. ], true);
  457. }
  458. $mailer = new FreshRSS_User_Mailer();
  459. $ok = $username != null && $mailer->send_email_need_validation($username, FreshRSS_Context::userConf());
  460. $redirect_url = ['c' => 'user', 'a' => 'validateEmail'];
  461. if ($ok) {
  462. Minz_Request::good(
  463. _t('user.email.validation.feedback.email_sent'),
  464. $redirect_url
  465. );
  466. } else {
  467. Minz_Request::bad(
  468. _t('user.email.validation.feedback.email_failed'),
  469. $redirect_url
  470. );
  471. }
  472. }
  473. /**
  474. * This action delete an existing user.
  475. *
  476. * Request parameter is:
  477. * - username
  478. *
  479. * @todo clean up this method. Idea: create a User->clean() method.
  480. */
  481. public function deleteAction(): void {
  482. $username = Minz_Request::paramString('username');
  483. $self_deletion = Minz_User::name() === $username;
  484. if (!FreshRSS_Auth::hasAccess('admin') && !$self_deletion) {
  485. Minz_Error::error(403);
  486. }
  487. $redirect_url = ['c' => 'user', 'a' => 'manage'];
  488. if (Minz_Request::isPost()) {
  489. $ok = true;
  490. if ($self_deletion) {
  491. // We check the password if it’s a self-destruction
  492. $nonce = Minz_Session::paramString('nonce');
  493. $challenge = Minz_Request::paramString('challenge');
  494. $ok &= FreshRSS_FormAuth::checkCredentials(
  495. $username, FreshRSS_Context::userConf()->passwordHash,
  496. $nonce, $challenge
  497. );
  498. }
  499. if ($ok) {
  500. $ok &= self::deleteUser($username);
  501. }
  502. if ($ok && $self_deletion) {
  503. FreshRSS_Auth::removeAccess();
  504. $redirect_url = ['c' => 'index', 'a' => 'index'];
  505. }
  506. invalidateHttpCache();
  507. if ($ok) {
  508. Minz_Request::setGoodNotification(_t('feedback.user.deleted', $username));
  509. } else {
  510. Minz_Request::setBadNotification(_t('feedback.user.deleted.error', $username));
  511. }
  512. }
  513. Minz_Request::forward($redirect_url, true);
  514. }
  515. public function promoteAction(): void {
  516. $this->toggleAction('is_admin', true);
  517. }
  518. public function demoteAction(): void {
  519. $this->toggleAction('is_admin', false);
  520. }
  521. public function enableAction(): void {
  522. $this->toggleAction('enabled', true);
  523. }
  524. public function disableAction(): void {
  525. $this->toggleAction('enabled', false);
  526. }
  527. private function toggleAction(string $field, bool $value): void {
  528. if (!FreshRSS_Auth::hasAccess('admin')) {
  529. Minz_Error::error(403);
  530. }
  531. if (!Minz_Request::isPost()) {
  532. Minz_Error::error(403);
  533. }
  534. $username = Minz_Request::paramString('username');
  535. if (!FreshRSS_UserDAO::exists($username)) {
  536. Minz_Error::error(404);
  537. }
  538. if (null === $userConfig = get_user_configuration($username)) {
  539. Minz_Error::error(500);
  540. return;
  541. }
  542. if ($field === '') {
  543. Minz_Error::error(400, 'Invalid field name');
  544. return;
  545. }
  546. $userConfig->_attribute($field, $value);
  547. $ok = $userConfig->save();
  548. FreshRSS_UserDAO::touch($username);
  549. if ($ok) {
  550. Minz_Request::good(_t('feedback.user.updated', $username), ['c' => 'user', 'a' => 'manage']);
  551. } else {
  552. Minz_Request::bad(
  553. _t('feedback.user.updated.error', $username),
  554. ['c' => 'user', 'a' => 'manage']
  555. );
  556. }
  557. }
  558. public function detailsAction(): void {
  559. if (!FreshRSS_Auth::hasAccess('admin')) {
  560. Minz_Error::error(403);
  561. }
  562. $username = Minz_Request::paramString('username');
  563. if (!FreshRSS_UserDAO::exists($username)) {
  564. Minz_Error::error(404);
  565. }
  566. if (Minz_Request::paramBoolean('ajax')) {
  567. $this->view->_layout(null);
  568. }
  569. $this->view->username = $username;
  570. $this->view->details = $this->retrieveUserDetails($username);
  571. FreshRSS_View::prependTitle($username . ' · ' . _t('gen.menu.user_management') . ' · ');
  572. }
  573. /** @return array{'feed_count':int,'article_count':int,'database_size':int,'language':string,'mail_login':string,'enabled':bool,'is_admin':bool,'last_user_activity':string,'is_default':bool} */
  574. private function retrieveUserDetails(string $username): array {
  575. $feedDAO = FreshRSS_Factory::createFeedDao($username);
  576. $entryDAO = FreshRSS_Factory::createEntryDao($username);
  577. $databaseDAO = FreshRSS_Factory::createDatabaseDAO($username);
  578. $userConfiguration = get_user_configuration($username);
  579. if ($userConfiguration === null) {
  580. throw new Exception('Error loading user configuration!');
  581. }
  582. return [
  583. 'feed_count' => $feedDAO->count(),
  584. 'article_count' => $entryDAO->count(),
  585. 'database_size' => $databaseDAO->size(),
  586. 'language' => $userConfiguration->language,
  587. 'mail_login' => $userConfiguration->mail_login,
  588. 'enabled' => $userConfiguration->enabled,
  589. 'is_admin' => $userConfiguration->is_admin,
  590. 'last_user_activity' => date('c', FreshRSS_UserDAO::mtime($username)) ?: '',
  591. 'is_default' => FreshRSS_Context::systemConf()->default_user === $username,
  592. ];
  593. }
  594. }