summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Quantum/Plugin/Glue.pm
blob: f04b9c0254994a1cc39e19014f108f9e6313424f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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::Plugin::Glue;
use 5.10.1;
use Mojo::Base 'Mojolicious::Plugin';

use Try::Tiny;
use Bugzilla::Constants;
use Bugzilla::Logging;
use Bugzilla::RNG ();
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 {
  my ($self, $app, $conf) = @_;

  my %D;
  if ($ENV{BUGZILLA_HTTPD_ARGS}) {
    my $args = decode_json($ENV{BUGZILLA_HTTPD_ARGS});
    foreach my $arg (@$args) {
      if ($arg =~ /^-D(\w+)$/) {
        $D{$1} = 1;
      }
      else {
        die "Unknown httpd arg: $arg";
      }
    }
  }

  $app->hook(
    before_dispatch => sub {
      my ($c) = @_;
      if ($D{HTTPD_IN_SUBDIR}) {
        my $path = $c->req->url->path;
        if ($path =~ s{^/bmo}{}s) {
          $c->stash->{bmo_prefix} = 1;
          $c->req->url->path($path);
        }
      }
      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 %params;

      # Helpers
      foreach my $method (grep {m/^\w+\z/} keys %{$renderer->helpers}) {
        my $sub = $renderer->helpers->{$method};
        $params{$method} = sub { $c->$sub(@_) };
      }

      # Stash values
      $params{$_} = $c->stash->{$_} for grep {m/^\w+\z/} keys %{$c->stash};

      $params{self} = $params{c} = $c;

      my $name = sprintf '%s.%s.tmpl', $options->{template}, $options->{format};
      my $template = Bugzilla->template;
      $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);
      }
    }
  );

  $app->log(MojoX::Log::Log4perl::Tiny->new(
    logger => Log::Log4perl->get_logger(ref $app)
  ));
}

1;