userController.php 22 KB

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