tcms-useradd 2.4 KB

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