diff --git a/src/lib/Hydra/Controller/User.pm b/src/lib/Hydra/Controller/User.pm index 3b9efb0a..9dd90b0b 100644 --- a/src/lib/Hydra/Controller/User.pm +++ b/src/lib/Hydra/Controller/User.pm @@ -75,7 +75,7 @@ sub logout_GET { sub persona_login :Path('/persona-login') Args(0) { my ($self, $c) = @_; $c->stash->{json} = {}; - die if $c->request->method ne "POST"; + requirePost($c); my $assertion = $c->req->params->{assertion} or die; @@ -85,10 +85,10 @@ sub persona_login :Path('/persona-login') Args(0) { { assertion => $assertion, audience => "http://localhost:3000/" }); - Catalyst::Exception->throw("Did not get a response from Persona.") unless $response->is_success; + error($c, "Did not get a response from Persona.") unless $response->is_success; my $d = decode_json($response->decoded_content) or die; - Catalyst::Exception->throw("Persona says: $d->{reason}") if $d->{status} ne "okay"; + error($c, "Persona says: $d->{reason}") if $d->{status} ne "okay"; my $email = $d->{email} or die; @@ -106,6 +106,16 @@ sub persona_login :Path('/persona-login') Args(0) { $c->set_authenticated($user); $c->stash->{json}->{result} = "ok"; + $c->flash->{flashMsg} = "You are now signed in as " . $email . ""; +} + + +sub persona_logout :Path('/persona-logout') Args(0) { + my ($self, $c) = @_; + $c->stash->{json} = {}; + requirePost($c); + $c->flash->{flashMsg} = "You are no longer signed in." if $c->user_exists(); + $c->logout; } diff --git a/src/lib/Hydra/Helper/CatalystUtils.pm b/src/lib/Hydra/Helper/CatalystUtils.pm index a13f2570..2cbb601a 100644 --- a/src/lib/Hydra/Helper/CatalystUtils.pm +++ b/src/lib/Hydra/Helper/CatalystUtils.pm @@ -89,7 +89,8 @@ sub getPreviousSuccessfulBuild { sub error { - my ($c, $msg) = @_; + my ($c, $msg, $status) = @_; + $c->response->status($status) if defined $status; $c->error($msg); $c->detach; # doesn't return } @@ -97,8 +98,7 @@ sub error { sub notFound { my ($c, $msg) = @_; - $c->response->status(404); - error($c, $msg); + error($c, $msg, 404); } @@ -113,8 +113,7 @@ sub backToReferer { sub requireLogin { my ($c) = @_; $c->session->{referer} = $c->request->uri; - $c->response->redirect($c->uri_for('/login')); - $c->detach; # doesn't return + error($c, "This page requires you to sign in.", 403); } @@ -130,24 +129,21 @@ sub requireProjectOwner { requireLogin($c) if !$c->user_exists; - error($c, "Only the project members or administrators can perform this operation.") + error($c, "Only the project members or administrators can perform this operation.", 403) unless isProjectOwner($c, $project); } sub isAdmin { my ($c) = @_; - return $c->user_exists && $c->check_user_roles('admin'); } sub requireAdmin { my ($c) = @_; - requireLogin($c) if !$c->user_exists; - - error($c, "Only administrators can perform this operation.") + error($c, "Only administrators can perform this operation.", 403) unless isAdmin($c); } diff --git a/src/root/layout.tt b/src/root/layout.tt index 78152407..66da558d 100644 --- a/src/root/layout.tt +++ b/src/root/layout.tt @@ -121,12 +121,11 @@ .fail(function() { bootbox.alert("Server request failed!"); }); }, onlogout: function() { - $.ajax({ - type: 'POST', - url: '/logout', - success: function(res, status, xhr) { window.location.reload(); }, - error: function(xhr, status, err) { alert("Logout failure: " + err); } - }); + $.post("[% c.uri_for('/persona-logout') %]") + .done(function(data) { + window.location.reload(); + }) + .fail(function() { bootbox.alert("Server request failed!"); }); } });