tcms-useradd 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. 'user=s' => \$options{user},
  40. 'display_name=s' => \$options{display_name},
  41. 'help|?' => \$options{help},
  42. 'password=s' => \$options{password},
  43. 'contact_email=s' => \$options{contact_email},
  44. 'acl=s@' => \$options{acl},
  45. );
  46. pod2usage(-exitval => 0, -verbose => 1) if $options{help};
  47. $options{acl} //= [];
  48. $options{acl} = [$options{acl}] if $options{acl} && ref $options{acl} ne 'ARRAY';
  49. $options{acl} = ['admin'] unless @{$options{acl}};
  50. #Trog::Auth::killsession($options{user});
  51. #Trog::Auth::useradd( $options{user}, $options{display_name}, $options{password}, $options{acl}, $options{contact_email} );
  52. # Find the user's post and edit it
  53. my $conf = Trog::Config::get();
  54. my $data = Trog::Data->new($conf);
  55. my @userposts = $data->get( tags => ['about'], acls => [qw{admin}]);
  56. my $user_obj = first { ($_->{user} || '') eq $options{user} } @userposts;
  57. my %merged = (
  58. %$user_obj,
  59. %options,
  60. );
  61. # We don't want the password in plain text
  62. delete $merged{password};
  63. # The ACLs a user posesses is not necessarily what ACLs you need to view a user's profile.
  64. delete $merged{acl};
  65. $data->add(\%merged);
  66. return 0;
  67. }
  68. exit main(@ARGV) unless caller;
  69. 1;