userController.php 21 KB

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