tcms-useradd 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env perl
  2. package tcms::useradd;
  3. use strict;
  4. use warnings;
  5. use FindBin::libs;
  6. use Getopt::Long;
  7. use Pod::Usage;
  8. use List::Util qw{first};
  9. use Trog::Auth;
  10. use Trog::Data;
  11. use Trog::Config;
  12. use Trog::Log;
  13. # Don't murder our terminal when done
  14. $ENV{NOHUP} = 1;
  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. does not update user pages. Use this to reset user passwords or fix broken users.
  18. =head2 USAGE
  19. tcms-useradd --user foo --password bar --display_name baz --contact_email foo@bar.baz --acl fred --acl wilma
  20. =head2 OPTIONS
  21. =over 4
  22. =item --user
  23. Specify the user to add, or edit if the user already exists.
  24. =item --password
  25. Set a password for the user. Leave blank if you want to keep the password for an existing user.
  26. =item --display_name
  27. Set the display name for the user.
  28. =item --contact_email
  29. Set the contact email for the user.
  30. =item --acl
  31. Set an acl for this user. May be passed multiple times.
  32. Defaults to 'admin' acl.
  33. =item --help, --man
  34. Display this output.
  35. =back
  36. =cut
  37. sub main {
  38. Trog::Log::log_init();
  39. my %options;
  40. Getopt::Long::GetOptionsFromArray(
  41. \@_,
  42. 'user=s' => \$options{user},
  43. 'display_name=s' => \$options{display_name},
  44. 'help|?' => \$options{help},
  45. 'password=s' => \$options{password},
  46. 'contact_email=s' => \$options{contact_email},
  47. 'acl=s@' => \$options{acl},
  48. );
  49. pod2usage( -exitval => 0, -verbose => 1 ) if $options{help};
  50. $options{acl} //= [];
  51. $options{acl} = [ $options{acl} ] if $options{acl} && ref $options{acl} ne 'ARRAY';
  52. $options{acl} = ['admin'] unless @{ $options{acl} };
  53. Trog::Auth::killsession( $options{user} );
  54. eval { Trog::Auth::useradd( $options{user}, $options{display_name}, $options{password}, $options{acl}, $options{contact_email} ) } or do {
  55. return 1;
  56. };
  57. # Find the user's post and edit it
  58. my $conf = Trog::Config::get();
  59. my $data = Trog::Data->new($conf);
  60. my @userposts = $data->get( tags => ['about'], acls => [qw{admin}] );
  61. my $user_obj = first { ( $_->{user} || '' ) eq $options{user} } @userposts;
  62. my %merged = (
  63. %$user_obj,
  64. %options,
  65. $options{display_name} ? ( local_href => "/users/$options{display_name}" ) : ( local_href => $user_obj->{local_href} ),
  66. );
  67. # We don't want the password in plain text
  68. delete $merged{password};
  69. # The ACLs a user posesses is not necessarily what ACLs you need to view or edit a user's profile.
  70. delete $merged{acl};
  71. $data->add( \%merged );
  72. return 0;
  73. }
  74. exit main(@ARGV) unless caller;
  75. 1;