summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Quantum
diff options
context:
space:
mode:
authorDylan William Hardison <dylan@hardison.net>2018-10-02 20:22:05 +0200
committerdklawren <dklawren@users.noreply.github.com>2018-10-02 20:22:05 +0200
commit62412db14081dd66cd5b2701b598b5af9eb31528 (patch)
tree7e84a852449e530a0b0bae5ef581eace63c81e83 /Bugzilla/Quantum
parentabe9b579f25120898b714d4b73343918169d48ac (diff)
downloadbugzilla-62412db14081dd66cd5b2701b598b5af9eb31528.tar.gz
bugzilla-62412db14081dd66cd5b2701b598b5af9eb31528.tar.xz
add helpers for handling logins and error handling
Diffstat (limited to 'Bugzilla/Quantum')
-rw-r--r--Bugzilla/Quantum/CGI.pm3
-rw-r--r--Bugzilla/Quantum/Home.pm26
-rw-r--r--Bugzilla/Quantum/Plugin/Glue.pm111
3 files changed, 123 insertions, 17 deletions
diff --git a/Bugzilla/Quantum/CGI.pm b/Bugzilla/Quantum/CGI.pm
index 945a87d5b..317c189cc 100644
--- a/Bugzilla/Quantum/CGI.pm
+++ b/Bugzilla/Quantum/CGI.pm
@@ -19,7 +19,7 @@ use File::Spec::Functions qw(catfile);
use File::Slurper qw(read_text);
use English qw(-no_match_vars);
use Bugzilla::Quantum::Stdout;
-use Bugzilla::Constants qw(bz_locations);
+use Bugzilla::Constants qw(bz_locations USAGE_MODE_BROWSER);
our $C;
my %SEEN;
@@ -61,6 +61,7 @@ sub load_one {
# the finally block calls cleanup.
$c->stash->{cleanup_guard}->dismiss;
+ Bugzilla->usage_mode(USAGE_MODE_BROWSER);
try {
Bugzilla->init_page();
$inner->();
diff --git a/Bugzilla/Quantum/Home.pm b/Bugzilla/Quantum/Home.pm
new file mode 100644
index 000000000..b3f1ec1d1
--- /dev/null
+++ b/Bugzilla/Quantum/Home.pm
@@ -0,0 +1,26 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# This Source Code Form is "Incompatible With Secondary Licenses", as
+# defined by the Mozilla Public License, v. 2.0.
+
+package Bugzilla::Quantum::Home;
+use Mojo::Base 'Mojolicious::Controller';
+
+use Bugzilla::Error;
+use Try::Tiny;
+use Bugzilla::Constants;
+
+sub index {
+ my ($c) = @_;
+ $c->bugzilla->login(LOGIN_REQUIRED) or return;
+ try {
+ ThrowUserError('invalid_username', { login => 'batman' }) if $c->param('error');
+ $c->render(handler => 'bugzilla', template => 'index');
+ } catch {
+ $c->bugzilla->error_page($_);
+ };
+}
+
+1;
diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm
index e9d0056d0..8f4144589 100644
--- a/Bugzilla/Quantum/Plugin/Glue.pm
+++ b/Bugzilla/Quantum/Plugin/Glue.pm
@@ -13,7 +13,10 @@ use Try::Tiny;
use Bugzilla::Constants;
use Bugzilla::Logging;
use Bugzilla::RNG ();
-use JSON::MaybeXS qw(decode_json);
+use Bugzilla::Util qw(with_writable_database);
+use Mojo::Util qw(secure_compare);
+use Mojo::JSON qw(decode_json);
+use Scalar::Util qw(blessed);
use Scope::Guard;
sub register {
@@ -44,34 +47,110 @@ sub register {
}
Log::Log4perl::MDC->put( request_id => $c->req->request_id );
$c->stash->{cleanup_guard} = Scope::Guard->new( \&Bugzilla::cleanup );
+ Bugzilla->usage_mode(USAGE_MODE_MOJO);
}
);
-
$app->secrets( [ Bugzilla->localconfig->{side_wide_secret} ] );
$app->renderer->add_handler(
'bugzilla' => sub {
my ( $renderer, $c, $output, $options ) = @_;
- my $vars = delete $c->stash->{vars};
+ my %params;
# Helpers
- my %helper;
- foreach my $method ( grep {m/^\w+\z/} keys %{ $renderer->helpers } ) {
- my $sub = $renderer->helpers->{$method};
- $helper{$method} = sub { $c->$sub(@_) };
+ foreach my $method (grep { m/^\w+\z/ } keys %{$renderer->helpers}) {
+ my $sub = $renderer->helpers->{$method};
+ $params{$method} = sub { $c->$sub(@_) };
}
- $vars->{helper} = \%helper;
+ # Stash values
+ $params{$_} = $c->stash->{$_} for grep { m/^\w+\z/ } keys %{$c->stash};
- # The controller
- $vars->{c} = $c;
- my $name = $options->{template};
- unless ( $name =~ /\./ ) {
- $name = sprintf '%s.%s.tmpl', $options->{template}, $options->{format};
- }
+ $params{self} = $params{c} = $c;
+
+ my $name = sprintf '%s.%s.tmpl', $options->{template}, $options->{format};
my $template = Bugzilla->template;
- $template->process( $name, $vars, $output )
- or die $template->error;
+ $template->process( $name, \%params, $output )
+ or die $template->error;
+ }
+ );
+ $app->helper(
+ 'bugzilla.login_redirect_if_required' => sub {
+ my ( $c, $type ) = @_;
+
+ if ( $type == LOGIN_REQUIRED ) {
+ $c->redirect_to('/login');
+ return undef;
+ }
+ else {
+ return Bugzilla->user;
+ }
+ }
+ );
+ $app->helper(
+ 'bugzilla.login' => sub {
+ my ( $c, $type ) = @_;
+ $type //= LOGIN_NORMAL;
+
+ return Bugzilla->user if Bugzilla->user->id;
+
+ $type = LOGIN_REQUIRED if $c->param('GoAheadAndLogIn') || Bugzilla->params->{requirelogin};
+
+ # Allow templates to know that we're in a page that always requires
+ # login.
+ if ( $type == LOGIN_REQUIRED ) {
+ Bugzilla->request_cache->{page_requires_login} = 1;
+ }
+
+ my $login_cookie = $c->cookie("Bugzilla_logincookie");
+ my $user_id = $c->cookie("Bugzilla_login");
+ my $ip_addr = $c->tx->remote_address;
+
+ return $c->bugzilla->login_redirect_if_required($type) unless ( $login_cookie && $user_id );
+
+ my $db_cookie = Bugzilla->dbh->selectrow_array(
+ q{
+ SELECT cookie
+ FROM logincookies
+ WHERE cookie = ?
+ AND userid = ?
+ AND (restrict_ipaddr = 0 OR ipaddr = ?)
+ },
+ undef,
+ ( $login_cookie, $user_id, $ip_addr )
+ );
+
+ if ( defined $db_cookie && secure_compare( $login_cookie, $db_cookie ) ) {
+ my $user = Bugzilla::User->check( { id => $user_id, cache => 1 } );
+
+ # If we logged in successfully, then update the lastused
+ # time on the login cookie
+ with_writable_database {
+ Bugzilla->dbh->do( q{ UPDATE logincookies SET lastused = NOW() WHERE cookie = ? },
+ undef, $login_cookie );
+ };
+ Bugzilla->set_user($user);
+ return $user;
+ }
+ else {
+ return $c->bugzilla->login_redirect_if_required($type);
+ }
+ }
+ );
+ $app->helper(
+ 'bugzilla.error_page' => sub {
+ my ( $c, $error ) = @_;
+ if ( blessed $error && $error->isa('Bugzilla::Error::Base') ) {
+ $c->render(
+ handler => 'bugzilla',
+ template => $error->template,
+ error => $error->message,
+ %{ $error->vars }
+ );
+ }
+ else {
+ $c->reply->exception($error);
+ }
}
);