| 1 | package Test2::Harness::UI::Controller::User; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000150'; | ||
| 6 | |||
| 7 | use Text::Xslate(); | ||
| 8 | use Test2::Harness::UI::Util qw/share_dir/; | ||
| 9 | use Test2::Harness::UI::Response qw/resp error/; | ||
| 10 | use Test2::Harness::UI::UUID qw/uuid_inflate/; | ||
| 11 | |||
| 12 | use Email::Sender::Simple qw(sendmail); | ||
| 13 | use Email::Simple; | ||
| 14 | use Email::Simple::Creator; | ||
| 15 | |||
| 16 | use parent 'Test2::Harness::UI::Controller'; | ||
| 17 | use Test2::Harness::UI::Util::HashBase qw/-title/; | ||
| 18 | |||
| 19 | sub handle { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | my $req = $self->{+REQUEST}; | ||
| 23 | |||
| 24 | my $res = resp(200); | ||
| 25 | $res->add_css('user.css'); | ||
| 26 | $self->process_form($res) if keys %{$req->parameters}; | ||
| 27 | |||
| 28 | my $user = $req->user; | ||
| 29 | |||
| 30 | unless($user) { | ||
| 31 | $res->raw_body($self->login()); | ||
| 32 | return $res; | ||
| 33 | } | ||
| 34 | |||
| 35 | $self->{+TITLE} = 'User Settings'; | ||
| 36 | |||
| 37 | my $tx = Text::Xslate->new(path => [share_dir('templates')]); | ||
| 38 | my $sort_val = {active => 1, disabled => 2, revoked => 3}; | ||
| 39 | my $content = $tx->render( | ||
| 40 | 'user.tx', | ||
| 41 | { | ||
| 42 | base_uri => $req->base->as_string, | ||
| 43 | user => $user, | ||
| 44 | keys => [sort { $sort_val->{$a->status} <=> $sort_val->{$b->status} } $user->api_keys->all], | ||
| 45 | emails => [sort { $b->is_primary <=> $a->is_primary || $a->domain cmp $b->domain || $a->local cmp $b->local } $user->emails], | ||
| 46 | perms => [sort { $a->project->name cmp $b->project->name } $user->permissions], | ||
| 47 | } | ||
| 48 | ); | ||
| 49 | |||
| 50 | $res->raw_body($content); | ||
| 51 | return $res; | ||
| 52 | } | ||
| 53 | |||
| 54 | my %KEY_ACTION_MAP = (enable => 'active', disable => 'disabled', revoke => 'revoked'); | ||
| 55 | sub process_form { | ||
| 56 | my $self = shift; | ||
| 57 | my ($res) = @_; | ||
| 58 | |||
| 59 | my $req = $self->{+REQUEST}; | ||
| 60 | my $schema = $self->schema; | ||
| 61 | |||
| 62 | my $p = $req->parameters; | ||
| 63 | |||
| 64 | my $action = lc($p->{action} || ''); | ||
| 65 | |||
| 66 | # This one we allow non-post, all others need post. | ||
| 67 | if ('logout' eq $action) { | ||
| 68 | $req->session_host->update({'user_id' => undef}); | ||
| 69 | return $res->add_msg("You have been logged out."); | ||
| 70 | } | ||
| 71 | elsif ($action eq 'verify') { | ||
| 72 | my $evcode_id = $p->{verification_code} | ||
| 73 | or return $res->add_error("Invalid verification code"); | ||
| 74 | |||
| 75 | my $code = $schema->resultset('EmailVerificationCode')->find({evcode_id => uuid_inflate($evcode_id)}) | ||
| 76 | or return $res->add_error("Invalid verification code"); | ||
| 77 | |||
| 78 | my $email = $code->email; | ||
| 79 | $email->update({verified => 1}); | ||
| 80 | |||
| 81 | $code->delete(); | ||
| 82 | |||
| 83 | return $res->add_msg("Email address verified"); | ||
| 84 | } | ||
| 85 | |||
| 86 | die error(405) unless $req->method eq 'POST'; | ||
| 87 | |||
| 88 | if ('login' eq $action) { | ||
| 89 | my $username = $p->{username} or return $res->add_error("username is required"); | ||
| 90 | my $password = $p->{password} or return $res->add_error("password is required"); | ||
| 91 | |||
| 92 | my $user = $schema->resultset('User')->find({username => $username}); | ||
| 93 | |||
| 94 | return $res->add_error("Invalid username or password") | ||
| 95 | unless $user && $user->verify_password($password); | ||
| 96 | |||
| 97 | $req->session_host->update({'user_id' => $user->user_id}); | ||
| 98 | return $res->add_msg("You have been logged in."); | ||
| 99 | } | ||
| 100 | |||
| 101 | if ('add email' eq $action) { | ||
| 102 | my $user = $req->user or return $res->add_error("You must be logged in"); | ||
| 103 | |||
| 104 | my $addr = $p->{new_email} // ''; | ||
| 105 | return $res->add_error("Invalid Email") | ||
| 106 | unless $addr =~ m/^(.+)@(.+\..+)$/; | ||
| 107 | |||
| 108 | my ($local, $domain) = ($1, $2); | ||
| 109 | |||
| 110 | if ($schema->resultset('Email')->find({local => $local, domain => $domain})) { | ||
| 111 | return $res->add_error("This email is already in use."); | ||
| 112 | } | ||
| 113 | |||
| 114 | my $email = eval { $schema->resultset('Email')->create({user_id => $user->user_id, local => $local, domain => $domain}) }; | ||
| 115 | $res->add_error("Unable to add email: $@") unless $email; | ||
| 116 | |||
| 117 | $self->send_verification_code($email); | ||
| 118 | |||
| 119 | return $res->add_msg("Email '$local\@$domain' added, please check your email for the verification code"); | ||
| 120 | } | ||
| 121 | |||
| 122 | if ('generate key' eq $action) { | ||
| 123 | my $key_name = $p->{key_name} or return $res->add_error("a key name is required"); | ||
| 124 | my $user = $req->user or return $res->add_error("You must be logged in"); | ||
| 125 | |||
| 126 | my $key = $user->gen_api_key($key_name); | ||
| 127 | |||
| 128 | return $res->add_msg("Key '$key_name' generated: " . $key->value); | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | if ('change password' eq $action) { | ||
| 133 | my $user = $req->user or return $res->add_error("You must be logged in"); | ||
| 134 | my $old_password = $p->{old_password} or return $res->add_error("current password is required"); | ||
| 135 | my $new_password_1 = $p->{new_password_1} or return $res->add_error("new password is required"); | ||
| 136 | my $new_password_2 = $p->{new_password_2} or return $res->add_error("new password (again) is required"); | ||
| 137 | |||
| 138 | return $res->add_error("New password fields do not match") | ||
| 139 | unless $new_password_1 eq $new_password_2; | ||
| 140 | |||
| 141 | return $res->add_error("Incorrect password") unless $user->verify_password($old_password); | ||
| 142 | |||
| 143 | $user->set_password($new_password_1); | ||
| 144 | |||
| 145 | return $res->add_msg("Password Changed."); | ||
| 146 | } | ||
| 147 | |||
| 148 | if ($p->{api_key_id} && $KEY_ACTION_MAP{$action}) { | ||
| 149 | my $key_id = uuid_inflate($p->{api_key_id}); | ||
| 150 | my $user = $req->user or return $res->add_error("You must be logged in"); | ||
| 151 | |||
| 152 | my $key = $schema->resultset('ApiKey')->find({api_key_id => $key_id, user_id => $user->user_id}); | ||
| 153 | return $res->add_error("Invalid key") unless $key; | ||
| 154 | |||
| 155 | $key->update({status => $KEY_ACTION_MAP{$action}}); | ||
| 156 | return $res->add_msg("Key status changed."); | ||
| 157 | } | ||
| 158 | |||
| 159 | if (my $email_id = $p->{email_id}) { | ||
| 160 | $email_id = uuid_inflate($email_id) or return $res->add_error("Invalid email id"); | ||
| 161 | my $user = $req->user or return $res->add_error("You must be logged in"); | ||
| 162 | my $email = $schema->resultset('Email')->find({email_id => $email_id, user_id => $user->user_id}); | ||
| 163 | return $res->add_error("Invalid Email") unless $email; | ||
| 164 | |||
| 165 | if ($action eq 'make primary') { | ||
| 166 | my $pri = $schema->resultset('PrimaryEmail')->update_or_create({user_id => $user->user_id, email_id => $email_id}); | ||
| 167 | return $res->add_error("Could not make email primary: $@") unless $pri; | ||
| 168 | return $res->add_msg("Set primary email address."); | ||
| 169 | } | ||
| 170 | elsif ($action eq 'delete') { | ||
| 171 | $email->delete(); | ||
| 172 | return $res->add_msg("Email address deleted"); | ||
| 173 | } | ||
| 174 | elsif ($action eq 'send verification code') { | ||
| 175 | my $code = $self->send_verification_code($email); | ||
| 176 | return $res->add_msg("Verification code sent"); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | return $res->add_error("Invalid form submission"); | ||
| 181 | } | ||
| 182 | |||
| 183 | sub login { | ||
| 184 | my $self = shift; | ||
| 185 | |||
| 186 | $self->{+TITLE} = 'Login'; | ||
| 187 | |||
| 188 | my $tx = Text::Xslate->new(path => [share_dir('templates')]); | ||
| 189 | return $tx->render( | ||
| 190 | 'login.tx', | ||
| 191 | { | ||
| 192 | base_uri => $self->{+REQUEST}->base->as_string, | ||
| 193 | } | ||
| 194 | ); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub send_verification_code { | ||
| 198 | my $self = shift; | ||
| 199 | my ($email) = @_; | ||
| 200 | |||
| 201 | my $schema = $self->schema; | ||
| 202 | |||
| 203 | my $code = $schema->resultset('EmailVerificationCode')->find_or_create({email_id => $email->email_id}); | ||
| 204 | my $text = $code->evcode_id; | ||
| 205 | |||
| 206 | my $config = $self->{+CONFIG}; | ||
| 207 | |||
| 208 | my $msg = Email::Simple->create( | ||
| 209 | header => [ | ||
| 210 | To => $email->address, | ||
| 211 | From => $config->email, | ||
| 212 | Subject => "Email verification code", | ||
| 213 | ], | ||
| 214 | body => "Verification code: $text\n", | ||
| 215 | ); | ||
| 216 | |||
| 217 | sendmail($msg); | ||
| 218 | |||
| 219 | return $code; | ||
| 220 | } | ||
| 221 | |||
| 222 | 1; | ||
| 223 | |||
| 224 | __END__ |