]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Root.pm
Use status 403 for access denied
[ndwebbie.git] / lib / NDWeb / Controller / Root.pm
1 package NDWeb::Controller::Root;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 use ND::Include;
8 use Geo::IP;
9
10
11 #
12 # Sets the actions in this controller to be registered with no prefix
13 # so they function identically to actions created in MyApp.pm
14 #
15 __PACKAGE__->config->{namespace} = '';
16
17 =head1 NAME
18
19 NDWeb::Controller::Root - Root Controller for NDWeb
20
21 =head1 DESCRIPTION
22
23 [enter your description here]
24
25 =head1 METHODS
26
27 =cut
28
29 =head2 default
30
31 =cut
32
33 sub index : Local Path Args(0) {
34         my ( $self, $c ) = @_;
35
36         $c->res->redirect($c->uri_for('/wiki'));
37 }
38
39 sub default : Path {
40         my ( $self, $c ) = @_;
41         $c->response->status(404);
42 }
43
44 sub login : Local {
45         my ($self, $c) = @_;
46
47         if ($c->login){
48                 my $gi = Geo::IP->new(GEOIP_STANDARD);
49                 my $country = $gi->country_code_by_addr($c->req->address) || '??';
50
51                 my $remember = 0;
52                 if ($c->req->param('remember')){
53                         $c->session_time_to_live( 604800 ); # expire in one week.
54                         $remember = 1;
55                 }
56                 my $log = $c->model->prepare(q{INSERT INTO session_log
57                         (uid,time,ip,country,session,remember)
58                         VALUES ($1,NOW(),$2,$3,$4,$5)
59                 });
60                 $log->execute($c->user->id,$c->req->address
61                         ,$country,$c->sessionid,$remember);
62
63                 $c->forward('redirect');
64                 return;
65         } elsif ($c->req->method eq 'POST'){
66                 $c->res->status(400);
67         }
68 }
69
70 sub logout : Local {
71         my ($self, $c) = @_;
72         $c->logout;
73         $c->delete_session("logout");
74         $c->res->redirect($c->uri_for('index'));
75 }
76
77 sub begin : Private {
78         my ($self, $c) = @_;
79
80          $c->res->header( 'Cache-Control' =>
81                 'no-store, no-cache, must-revalidate,'.
82                 'post-check=0, pre-check=0, max-age=0'
83         );
84         $c->res->header( 'Pragma' => 'no-cache' );
85         $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' );
86 }
87
88 sub listTargets : Private {
89         my ($self, $c) = @_;
90
91         my $dbh = $c ->model;
92
93         my $query = $dbh->prepare(q{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick, 
94                 (released_coords AND old_claim(timestamp)) AS released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
95 FROM raid_claims c
96         JOIN raid_targets t ON c.target = t.id
97         JOIN raids r ON t.raid = r.id
98         JOIN current_planet_stats p ON t.planet = p.id
99 WHERE c.uid = $1 AND r.tick+c.wave > tick() AND r.open AND not r.removed
100 ORDER BY r.tick+c.wave,x,y,z});
101         $query->execute($c->user->id) or die $dbh->errstr;
102         my @targets;
103         while (my $target = $query->fetchrow_hashref){
104                 push @targets, $target;
105         }
106
107         $c->stash(claimedtargets => \@targets);
108 }
109
110 sub listAlliances : Private {
111         my ($self, $c) = @_;
112         my @alliances;
113         push @alliances,{id => -1, name => ''};
114         my $query = $c->model->prepare(q{SELECT id,name FROM alliances ORDER BY LOWER(name)});
115         $query->execute;
116         while (my $ally = $query->fetchrow_hashref){
117                 push @alliances,$ally;
118         }
119         $c->stash(alliances => \@alliances);
120 }
121
122 sub sslurl {
123         return $_[0];
124 }
125
126 sub auto : Private {
127         my ($self, $c) = @_;
128         my $dbh = $c ->model;
129
130         $c->stash(dbh => $dbh);
131
132         $c->stash(sslurl => \&sslurl);
133
134         $dbh->do(q{SET timezone = 'GMT'});
135
136         $c->stash(TICK =>$dbh->selectrow_array('SELECT tick()',undef));
137         $c->stash(STICK =>$dbh->selectrow_array('SELECT max(tick) FROM planet_stats',undef));
138         $c->stash->{game}->{tick} = $c->stash->{TICK};
139
140         if ($c->user_exists){
141                 $c->stash(UID => $c->user->id);
142         }else{
143                 $c->stash(UID => -4);
144         }
145 }
146
147 sub redirect : Private {
148         my ($self, $c) = @_;
149         $c->res->redirect($c->uri_for('/'.$c->flash->{referrer}));
150 }
151
152 sub access_denied : Private {
153         my ($self, $c, $action) = @_;
154
155         $c->stash->{template} = 'access_denied.tt2';
156         $c->res->status(403);
157
158 }
159
160 =head2 end
161
162 Attempt to render a view, if needed.
163
164 =cut 
165
166 sub end : ActionClass('RenderView') {
167         my ($self, $c) = @_;
168
169         if ($c->res->status == 302){
170                 return;
171         }
172
173         my $dbh = $c ->model;
174
175         if (scalar @{ $c->error } ){
176                 if ($c->error->[0] =~ m/Can't call method "id" on an undefined value at/){
177                         $c->stash->{template} = 'access_denied.tt2';
178                         $c->res->status(403);
179                         $c->clear_errors;
180                 }elsif ($c->error->[0] =~ m/Missing roles: /){
181                         $c->stash->{template} = 'access_denied.tt2';
182                         $c->res->status(403);
183                         $c->clear_errors;
184                 }
185         }
186
187         if ($c->user_exists){
188                 my $fleetupdate = 0;
189                 if ($c->check_user_roles(qw/member_menu/)){
190                         $fleetupdate = $dbh->selectrow_array(q{SELECT tick FROM fleets WHERE sender = ?
191                                 AND mission = 'Full fleet' AND tick > tick() - 24
192                                 },undef,$c->user->planet);
193                         $fleetupdate = 0 unless defined $fleetupdate;
194                 }
195
196                 my ($unread,$newposts) = $dbh->selectrow_array(q{SELECT * FROM unread_posts($1)}
197                         ,undef,$c->user->id);
198
199                 $c->stash(user => {
200                         id => $c->user->id,
201                         name => $c->user->username,
202                         css => $c->user->css,
203                         newposts => $newposts,
204                         unreadposts => $unread
205                 });
206                 $c->stash->{user}->{attacker} = $c->check_user_roles(qw/attack_menu/)
207                         && (!$c->check_user_roles(qw/member_menu/)
208                                 || ($c->user->planet && (($c->stash->{TICK} - $fleetupdate < 24)
209                                         || $c->check_user_roles(qw/no_fleet_update/)))),
210                 $c->forward('listTargets');
211         }
212         my $birthdays = $dbh->prepare(q{SELECT username
213                         ,date_part('year',age(birthday)) AS age
214                         FROM users WHERE birthday IS NOT NULL
215                                 AND mmdd(birthday) = mmdd(CURRENT_DATE)
216                 });
217         $birthdays->execute;
218         $c->stash(birthdays => $birthdays->fetchall_arrayref({}));
219
220         if ($c->res->status == 200 || $c->req->method eq 'GET'){
221                 $c->flash(referrer => $c->req->path);
222         }
223 }
224
225 =head1 AUTHOR
226
227 Michael Andreen (harv@ruin.nu)
228
229 =head1 LICENSE
230
231 GPL 2, or later.
232
233 =cut
234
235 1;