tcms-useradd 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env perl
  2. package tcms::useradd;
  3. use 5.040;
  4. use re '/aasx';
  5. use warnings FATAL => 'all';
  6. use FindBin::libs;
  7. use Getopt::Long;
  8. use Pod::Usage;
  9. use IO::Prompter();
  10. use List::Util qw{first};
  11. use Trog::Auth;
  12. use Trog::Log;
  13. use Trog::Data;
  14. use Trog::Config;
  15. =head1 SYNOPSIS
  16. Add or edit a tCMS user. In the event of a user edit, every option save for user is optional,
  17. and the ones you leave out keep whatever value they already had.
  18. Creating a user who does not exist yet needs --password, --display_name and --contact_email,
  19. as there is no existing profile post to inherit them from.
  20. Use this to reset user passwords or fix broken users.
  21. =head2 USAGE
  22. tcms-useradd --user foo --password bar --display_name baz --contact_email foo@bar.baz --acl fred --acl wilma
  23. =head2 OPTIONS
  24. =over 4
  25. =item --user
  26. Specify the user to add, or edit if the user already exists.
  27. =item --password
  28. Set a password for the user. Omit the option entirely if you want to keep the
  29. password an existing user already has.
  30. Passing --password with no value prompts for one instead, with echo off, which
  31. keeps it out of your shell history and off your screen:
  32. tcms-useradd --user foo --password # prompts, twice, to confirm
  33. It can also be piped in, in which case it is read once and not confirmed:
  34. echo hunter2 | tcms-useradd --user foo --password
  35. Note that setting a password kills any session the user currently holds, so
  36. they will have to log back in.
  37. =item --display_name
  38. Set the display name for the user.
  39. =item --contact_email
  40. Set the contact email for the user.
  41. =item --acl
  42. Set an acl for this user. May be passed multiple times.
  43. Defaults to 'admin' acl.
  44. =item --help, --man
  45. Display this output.
  46. =back
  47. =cut
  48. # The logger is set up per-request by the server; a CLI run has to do it itself,
  49. # or the first thing Trog::Auth logs blows up on an undefined $log.
  50. my sub init_logs {
  51. my ($who) = @_;
  52. Trog::Log::log_init( 'logs/tcms.log', 'info' );
  53. $Trog::Log::user = $who // 'nobody';
  54. return;
  55. }
  56. # Read a password off the terminal without echoing it, so it stays out of the
  57. # shell history and off the screen. Falls back to a plain read when we aren't
  58. # on a TTY, which is what makes `echo hunter2 | tcms-useradd ...` work. Returns
  59. # undef only at the end of the input, and q{} for an empty line.
  60. my sub read_password ($prompt) {
  61. my $password = IO::Prompter::prompt( $prompt, -echo => q{}, -in => *STDIN, -out => *STDERR );
  62. return $password ? "$password" : undef;
  63. }
  64. # What a brand new user's profile post looks like before we merge anything over
  65. # the top of it. Mirrors what setup() builds for the very first user.
  66. my sub default_user_post {
  67. return {
  68. data => 'Default user',
  69. preview => '/img/avatar/humm.gif',
  70. wallpaper => '/img/sys/testpattern.jpg',
  71. tags => ['about'],
  72. visibility => 'public',
  73. acls => ['admin'],
  74. callback => 'Trog::Routes::HTML::users',
  75. method => 'GET',
  76. form => 'profile.tx',
  77. aliases => [],
  78. };
  79. }
  80. sub main (@args) {
  81. my %options;
  82. Getopt::Long::GetOptionsFromArray(
  83. \@args,
  84. 'user=s' => \$options{user},
  85. 'display_name=s' => \$options{display_name},
  86. 'help|?' => \$options{help},
  87. 'password:s' => \$options{password},
  88. 'contact_email=s' => \$options{contact_email},
  89. 'acl=s@' => \$options{acl},
  90. );
  91. pod2usage( -exitval => 0, -verbose => 1 ) if $options{help};
  92. unless ( $options{user} ) {
  93. print {*STDERR} "--user is required.\n"; ## no critic (ProhibitPrintSTDERR) -- for whoever ran this
  94. return 2;
  95. }
  96. # --password with no value means "ask me", as opposed to leaving the option
  97. # out entirely, which means "leave the password alone".
  98. if ( defined $options{password} && !$options{password} ) {
  99. $options{password} = read_password("New password for '$options{user}': ");
  100. unless ( $options{password} ) {
  101. print {*STDERR} "No password provided.\n"; ## no critic (ProhibitPrintSTDERR) -- for whoever ran this
  102. return 2;
  103. }
  104. # A person at a terminal always enters a line, even an empty one, so
  105. # they always confirm. Piped input with one line has no second line,
  106. # and there is nothing to confirm against.
  107. my $again = read_password("Confirm: ");
  108. if ( defined $again && $options{password} ne $again ) {
  109. print {*STDERR} "Passwords did not match.\n"; ## no critic (ProhibitPrintSTDERR) -- for whoever ran this
  110. return 2;
  111. }
  112. }
  113. $options{acl} //= [];
  114. $options{acl} = [ $options{acl} ] if $options{acl} && ref $options{acl} ne 'ARRAY';
  115. $options{acl} = ['admin'] unless @{ $options{acl} };
  116. init_logs( $options{user} );
  117. # Find the user's existing post, if they have one.
  118. my $conf = Trog::Config::get();
  119. my $data = Trog::Data->new($conf);
  120. my @userposts = $data->get( tags => ['about'], acls => [qw{admin}] );
  121. my $user_obj = first { ( $_->{user} || '' ) eq $options{user} } @userposts;
  122. # A user we've never seen before has no post to edit, so start from the
  123. # defaults rather than dereferencing undef.
  124. my $is_new = !$user_obj;
  125. $user_obj //= default_user_post();
  126. if ($is_new) {
  127. unless ( $options{password} && $options{display_name} && $options{contact_email} ) {
  128. print {*STDERR} "User '$options{user}' does not exist yet.\nCreating one needs --password, --display_name and --contact_email.\n"; ## no critic (ProhibitPrintSTDERR) -- for whoever ran this
  129. return 2;
  130. }
  131. }
  132. # Only the options actually passed should win. Merging %options wholesale
  133. # writes an undef over every field the caller didn't mention, which is how
  134. # resetting a password used to blank out the display name and contact email.
  135. my %provided = map { $_ => $options{$_} } grep { defined $options{$_} } qw{user display_name contact_email};
  136. my %merged = (
  137. %$user_obj,
  138. %provided,
  139. );
  140. # The display name is the title of the post, and the URL it lives at.
  141. $merged{title} = $merged{display_name} if $merged{display_name};
  142. $merged{local_href} = "/users/$merged{display_name}" if $merged{display_name};
  143. Trog::Auth::killsession( $options{user} );
  144. local $@;
  145. eval { Trog::Auth::useradd( $options{user}, $merged{display_name}, $options{password}, $options{acl}, $merged{contact_email} ); 1 } or do {
  146. print {*STDERR} "Could not add user '$options{user}': $@"; ## no critic (ProhibitPrintSTDERR) -- for whoever ran this
  147. return 1;
  148. };
  149. eval { $data->add( \%merged ); 1 } or do {
  150. my @errors = ref $@ eq 'ARRAY' ? @{$@} : ($@);
  151. print {*STDERR} "Saved the credentials for '$options{user}', but their profile post was rejected:\n" . join( "\n", @errors ) . "\n"; ## no critic (ProhibitPrintSTDERR) -- for whoever ran this
  152. return 1;
  153. };
  154. print "@{[ $is_new ? 'Added' : 'Updated' ]} user '$options{user}'.\n";
  155. return 0;
  156. }
  157. exit main(@ARGV) unless caller;
  158. 1;