| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Rubric::CLI::Command::user 0.157; | ||
| 4 | # ABSTRACT: Rubric user management commands | ||
| 5 | |||
| 6 | use parent qw(Rubric::CLI::Command); | ||
| 7 | |||
| 8 | use Digest::MD5 qw(md5_hex); | ||
| 9 | use Rubric::User; | ||
| 10 | |||
| 11 | sub usage_desc { "rubric user %o [username]" } | ||
| 12 | |||
| 13 | sub opt_spec { | ||
| 14 | return ( | ||
| 15 | [ "new-user|n", "add a user (requires --email and --password)" ], | ||
| 16 | [ "activate|a", "activate an existing user" ], | ||
| 17 | [ "password|pass|p=s", "set user's password" ], | ||
| 18 | [ "email|e=s", "set user's email address" ], | ||
| 19 | ); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub validate_args { | ||
| 23 | my ($self, $opt, $args) = @_; | ||
| 24 | |||
| 25 | die $self->usage->text unless @$args == 1; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub run { | ||
| 29 | my ($self, $opt, $args) = @_; | ||
| 30 | |||
| 31 | my $username = $args->[0]; | ||
| 32 | |||
| 33 | die "--new-user and --activate are mutually exclusive" | ||
| 34 | if $opt->{new_user} and $opt->{activate}; | ||
| 35 | |||
| 36 | if ($opt->{new_user}) { | ||
| 37 | die "--new-user requries --email and --password" | ||
| 38 | unless $opt->{email} and $opt->{password}; | ||
| 39 | |||
| 40 | my $user = Rubric::User->create({ | ||
| 41 | username => $username, | ||
| 42 | password => md5_hex($opt->{password}), | ||
| 43 | email => $opt->{email}, | ||
| 44 | }); | ||
| 45 | |||
| 46 | die "couldn't create user" unless $user; | ||
| 47 | |||
| 48 | print "created user $user"; | ||
| 49 | exit; | ||
| 50 | } | ||
| 51 | |||
| 52 | my $user = Rubric::User->retrieve($username); | ||
| 53 | |||
| 54 | die "couldn't find user for '$username'" unless $user; | ||
| 55 | |||
| 56 | if ($opt->{activate}) { | ||
| 57 | $user->verification_code(undef); | ||
| 58 | print "activated user account\n"; | ||
| 59 | } | ||
| 60 | |||
| 61 | if ($opt->{email}) { | ||
| 62 | $user->email($opt->{email}); | ||
| 63 | print "changed email\n"; | ||
| 64 | } | ||
| 65 | |||
| 66 | if ($opt->{password}) { | ||
| 67 | $user->password(md5_hex($opt->{password})); | ||
| 68 | print "changed password\n"; | ||
| 69 | } | ||
| 70 | |||
| 71 | $user->update; | ||
| 72 | |||
| 73 | print "username: ", $user->username, "\n"; | ||
| 74 | print "email : ", $user->email, "\n"; | ||
| 75 | } | ||
| 76 | |||
| 77 | 1; | ||
| 78 | |||
| 79 | __END__ |