aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Root.pm
blob: 7f70623ae7326afd328576c125a1c386605bceb5 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
package FixMyStreet::App::Controller::Root;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller' }

__PACKAGE__->config( namespace => '' );

=head1 NAME

FixMyStreet::App::Controller::Root - Root Controller for FixMyStreet::App

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=head2 begin

Any pre-flight checking for all requests

=cut
sub begin : Private {
    my ( $self, $c ) = @_;

    $c->forward( 'check_login_required' );
}


=head2 auto

Set up general things for this instance

=cut

sub auto : Private {
    my ( $self, $c ) = @_;

    # decide which cobrand this request should use
    $c->setup_request();

    return 1;
}

=head2 index

Home page.

If request includes certain parameters redirect to '/around' - this is to
preserve old behaviour.

=cut

sub index : Path : Args(0) {
    my ( $self, $c ) = @_;

    my @old_param_keys = ( 'pc', 'x', 'y', 'e', 'n', 'lat', 'lon' );
    my %old_params = ();

    foreach my $key (@old_param_keys) {
        my $val = $c->get_param($key);
        next unless $val;
        $old_params{$key} = $val;
    }

    if ( scalar keys %old_params ) {
        my $around_uri = $c->uri_for( '/around', \%old_params );
        $c->res->redirect($around_uri);
        return;
    }

    if ($c->stash->{homepage_template}) {
        $c->stash->{template} = $c->stash->{homepage_template};
        $c->detach;
    }

    $c->forward('/auth/get_csrf_token');
}

=head2 default

Forward to the standard 404 error page

=cut

sub default : Path {
    my ( $self, $c ) = @_;
    $c->detach('/page_error_404_not_found', []);
}

=head2 page_error_404_not_found, page_error_410_gone

    $c->detach( '/page_error_404_not_found', [$error_msg] );
    $c->detach( '/page_error_410_gone',      [$error_msg] );

Display a 404 (not found) or 410 (gone) page. Pass in an optional error message in an arrayref.

=cut

sub page_error_404_not_found : Private {
    my ( $self, $c, $error_msg ) = @_;

    # Try getting static content that might be given under an admin proxy.
    # First the special generated JavaScript file
    $c->go('/js/translation_strings', [ $1 ], []) if $c->req->path =~ m{^admin/js/translation_strings\.(.*?)\.js$};
    # Then a generic static file
    $c->serve_static_file("web/$1") && return if $c->req->path =~ m{^admin/(.*)};

    $c->stash->{template}  = 'errors/page_error_404_not_found.html';
    $c->stash->{error_msg} = $error_msg;
    $c->response->status(404);
}

sub page_error_410_gone : Private {
    my ( $self, $c, $error_msg ) = @_;
    $c->stash->{template}  = 'index.html';
    $c->stash->{error} = $error_msg;
    $c->response->status(410);
}

sub page_error_403_access_denied : Private {
    my ( $self, $c, $error_msg ) = @_;
    $c->detach('page_error', [ $error_msg || _("Sorry, you don't have permission to do that."), 403 ]);
}

sub page_error_400_bad_request : Private {
    my ( $self, $c, $error_msg ) = @_;
    $c->forward('/auth/get_csrf_token');
    $c->detach('page_error', [ $error_msg, 400 ]);
}

sub page_error_500_internal_error : Private {
    my ( $self, $c, $error_msg ) = @_;
    $c->detach('page_error', [ $error_msg, 500 ]);
}

sub page_error : Private {
    my ($self, $c, $error_msg, $code) = @_;
    $c->stash->{template}  = 'errors/generic.html';
    $c->stash->{message} = $error_msg || _('Unknown error');
    $c->response->status($code);
}

sub check_login_required : Private {
    my ($self, $c) = @_;

    return if $c->user_exists || !FixMyStreet->config('LOGIN_REQUIRED');

    # Whitelisted URL patterns are allowed without login
    my $whitelist = qr{
          ^auth(/|$)
        | ^js/translation_strings\.(.*?)\.js
        | ^[PACQM]/  # various tokens that log the user in
    }x;
    return if $c->request->path =~ $whitelist;

    # Blacklisted URLs immediately 404
    # This is primarily to work around a Safari bug where the appcache
    # URL is requested in an infinite loop if it returns a 302 redirect.
    $c->detach('/page_error_404_not_found', []) if $c->request->path =~ /^offline/;

    $c->detach( '/auth/redirect' );
}

=head2 end

Attempt to render a view, if needed.

=cut

sub end : ActionClass('RenderView') {
}

__PACKAGE__->meta->make_immutable;

1;