4
0

create-user.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. require(__DIR__ . '/_cli.php');
  5. $cliOptions = new class extends CliOptionsParser {
  6. public string $user;
  7. public string $password;
  8. public string $apiPassword;
  9. public string $language;
  10. public string $email;
  11. public string $token;
  12. public int $purgeAfterMonths;
  13. public int $feedMinArticles;
  14. public int $feedTtl;
  15. public int $sinceHoursPostsPerRss;
  16. public int $maxPostsPerRss;
  17. public bool $noDefaultFeeds;
  18. public function __construct() {
  19. $this->addRequiredOption('user', (new CliOption('user')));
  20. $this->addOption('password', (new CliOption('password')));
  21. $this->addOption('apiPassword', (new CliOption('api-password'))->deprecatedAs('api_password'));
  22. $this->addOption('language', (new CliOption('language')));
  23. $this->addOption('email', (new CliOption('email')));
  24. $this->addOption('token', (new CliOption('token')));
  25. $this->addOption(
  26. 'purgeAfterMonths',
  27. (new CliOption('purge-after-months'))->typeOfInt()->deprecatedAs('purge_after_months')
  28. );
  29. $this->addOption(
  30. 'feedMinArticles',
  31. (new CliOption('feed-min-articles-default'))->typeOfInt()->deprecatedAs('feed_min_articles_default')
  32. );
  33. $this->addOption(
  34. 'feedTtl',
  35. (new CliOption('feed-ttl-default'))->typeOfInt()->deprecatedAs('feed_ttl_default')
  36. );
  37. $this->addOption(
  38. 'sinceHoursPostsPerRss',
  39. (new CliOption('since-hours-posts-per-rss'))->typeOfInt()->deprecatedAs('since_hours_posts_per_rss')
  40. );
  41. $this->addOption(
  42. 'maxPostsPerRss',
  43. (new CliOption('max-posts-per-rss'))->typeOfInt()->deprecatedAs('max_posts_per_rss')
  44. );
  45. $this->addOption(
  46. 'noDefaultFeeds',
  47. (new CliOption('no-default-feeds'))->withValueNone()->deprecatedAs('no_default_feeds')
  48. );
  49. parent::__construct();
  50. }
  51. };
  52. if (!empty($cliOptions->errors)) {
  53. fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
  54. }
  55. $username = $cliOptions->user;
  56. if (!empty(preg_grep("/^$username$/i", listUsers()))) {
  57. fail('FreshRSS warning: username already exists “' . $username . '”', EXIT_CODE_ALREADY_EXISTS);
  58. }
  59. echo 'FreshRSS creating user “', $username, "”…\n";
  60. $values = [
  61. 'language' => $cliOptions->language ?? null,
  62. 'mail_login' => $cliOptions->email ?? null,
  63. 'token' => $cliOptions->token ?? null,
  64. 'old_entries' => $cliOptions->purgeAfterMonths ?? null,
  65. 'keep_history_default' => $cliOptions->feedMinArticles ?? null,
  66. 'ttl_default' => $cliOptions->feedTtl ?? null,
  67. 'since_hours_posts_per_rss' => $cliOptions->sinceHoursPostsPerRss ?? null,
  68. 'max_posts_per_rss' => $cliOptions->maxPostsPerRss ?? null,
  69. ];
  70. $values = array_filter($values, fn($v): bool => $v !== null && $v !== '');
  71. $ok = FreshRSS_user_Controller::createUser(
  72. $username,
  73. $cliOptions->email ?? null,
  74. $cliOptions->password ?? '',
  75. $values,
  76. !$cliOptions->noDefaultFeeds
  77. );
  78. if (!$ok) {
  79. fail('FreshRSS could not create user!');
  80. }
  81. if (isset($cliOptions->apiPassword)) {
  82. $username = cliInitUser($username);
  83. $error = FreshRSS_api_Controller::updatePassword($cliOptions->apiPassword);
  84. if ($error !== false) {
  85. fail($error);
  86. }
  87. }
  88. invalidateHttpCache(FreshRSS_Context::systemConf()->default_user);
  89. echo 'ℹ️ Remember to refresh the feeds of the user: ', $username ,
  90. "\t", './cli/actualize-user.php --user ', $username, "\n";
  91. accessRights();
  92. done($ok);