tcms-useradd 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. # Read a password off the terminal without echoing it, so it stays out of the
  49. # shell history and off the screen. Falls back to a plain read when we aren't
  50. # on a TTY, which is what makes `echo hunter2 | tcms-useradd ...` work. Returns
  51. # undef only at the end of the input, and q{} for an empty line.
  52. my sub read_password ($prompt) {
  53. my $password = IO::Prompter::prompt( $prompt, -echo => q{}, -in => *STDIN, -out => *STDERR );
  54. return $password ? "$password" : undef;
  55. }
  56. # What a brand new user's profile post looks like before we merge anything over
  57. # the top of it. Mirrors what setup() builds for the very first user.
  58. my sub default_user_post { ## no critic (ProhibitSingleUseLexicalSubs) -- 14 lines, which would overcomplicate main()
  59. return {
  60. data => 'Default user',
  61. preview => '/img/avatar/humm.gif',
  62. wallpaper => '/img/sys/testpattern.jpg',
  63. tags => ['about'],
  64. visibility => 'public',
  65. acls => ['admin'],
  66. callback => 'Trog::Routes::HTML::users',
  67. method => 'GET',
  68. form => 'profile.tx',
  69. aliases => [],
  70. };
  71. }
  72. sub main (@args) {
  73. my %options;
  74. Getopt::Long::GetOptionsFromArray(
  75. \@args,
  76. 'user=s' => \$options{user},
  77. 'display_name=s' => \$options{display_name},
  78. 'help|?' => \$options{help},
  79. 'password:s' => \$options{password},
  80. 'contact_email=s' => \$options{contact_email},
  81. 'acl=s@' => \$options{acl},
  82. );
  83. pod2usage( -exitval => 0, -verbose => 1 ) if $options{help};
  84. unless ( $options{user} ) {
  85. print {*STDERR} "--user is required.\n"; ## no critic (ProhibitPrintSTDERR) -- for whoever ran this
  86. return 2;
  87. }
  88. # --password with no value means "ask me", as opposed to leaving the option
  89. # out entirely, which means "leave the password alone".
  90. if ( defined $options{password} && !$options{password} ) {
  91. $options{password} = read_password("New password for '$options{user}': ");
  92. unless ( $options{password} ) {
  93. print {*STDERR} "No password provided.\n"; ## no critic (ProhibitPrintSTDERR) -- for whoever ran this
  94. return 2;
  95. }
  96. # A person at a terminal always enters a line, even an empty one, so
  97. # they always confirm. Piped input with one line has no second line,
  98. # and there is nothing to confirm against.
  99. my $again = read_password("Confirm: ");
  100. if ( defined $again && $options{password} ne $again ) {
  101. print {*STDERR} "Passwords did not match.\n"; ## no critic (ProhibitPrintSTDERR) -- for whoever ran this
  102. return 2;
  103. }
  104. }
  105. $options{acl} //= [];
  106. $options{acl} = [ $options{acl} ] if $options{acl} && ref $options{acl} ne 'ARRAY';
  107. $options{acl} = ['admin'] unless @{ $options{acl} };
  108. # Log to the file the server writes, as the user this run adds or edits.
  109. Trog::Log::log_init( 'logs/tcms.log', 'info' );
  110. $Trog::Log::user = $options{user} // 'nobody';
  111. # Find the user's existing post, if they have one.
  112. my $conf = Trog::Config::get();
  113. my $data = Trog::Data->new($conf);
  114. my @userposts = $data->get( tags => ['about'], acls => [qw{admin}] );
  115. my $user_obj = first { ( $_->{user} || '' ) eq $options{user} } @userposts;
  116. # A user we've never seen before has no post to edit, so start from the
  117. # defaults rather than dereferencing undef.
  118. my $is_new = !$user_obj;
  119. $user_obj //= default_user_post();
  120. if ($is_new) {
  121. unless ( $options{password} && $options{display_name} && $options{contact_email} ) {
  122. 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
  123. return 2;
  124. }
  125. }
  126. # Only the options actually passed should win. Merging %options wholesale
  127. # writes an undef over every field the caller didn't mention, which is how
  128. # resetting a password used to blank out the display name and contact email.
  129. my %provided = map { $_ => $options{$_} } grep { defined $options{$_} } qw{user display_name contact_email};
  130. my %merged = (
  131. %$user_obj,
  132. %provided,
  133. );
  134. # The display name is the title of the post, and the URL it lives at.
  135. $merged{title} = $merged{display_name} if $merged{display_name};
  136. $merged{local_href} = "/users/$merged{display_name}" if $merged{display_name};
  137. Trog::Auth::killsession( $options{user} );
  138. local $@;
  139. eval { Trog::Auth::useradd( $options{user}, $merged{display_name}, $options{password}, $options{acl}, $merged{contact_email} ); 1 } or do {
  140. print {*STDERR} "Could not add user '$options{user}': $@"; ## no critic (ProhibitPrintSTDERR) -- for whoever ran this
  141. return 1;
  142. };
  143. eval { $data->add( \%merged ); 1 } or do {
  144. my @errors = ref $@ eq 'ARRAY' ? @{$@} : ($@);
  145. 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
  146. return 1;
  147. };
  148. print "@{[ $is_new ? 'Added' : 'Updated' ]} user '$options{user}'.\n";
  149. return 0;
  150. }
  151. exit main(@ARGV) unless caller;
  152. 1;