tcms-useradd 2.6 KB

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