]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Root.pm
Better session support and auth logging
[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
37 sub default : Path {
38         my ( $self, $c ) = @_;
39         $c->res->body( 'Page not found' );
40         $c->response->status(404);
41 }
42
43 sub login : Local {
44         my ($self, $c) = @_;
45
46         if ($c->login){
47                 my $gi = Geo::IP->new(GEOIP_STANDARD);
48                 my $country = $gi->country_code_by_addr($c->req->address) || '??';
49
50                 my $remember = 0;
51                 if ($c->req->param('remember')){
52                         $c->session_time_to_live( 604800 ); # expire in one week.
53                         $remember = 1;
54                 }
55                 my $log = $c->model->prepare(q{INSERT INTO session_log
56                         (uid,time,ip,country,session,remember)
57                         VALUES ($1,NOW(),$2,$3,$4,$5)
58                 });
59                 $log->execute($c->user->id,$c->req->address
60                         ,$country,$c->sessionid,$remember);
61
62                 $c->res->redirect($c->req->referer);
63                 return;
64         }
65 }
66
67 sub logout : Local {
68         my ($self, $c) = @_;
69         $c->logout;
70         $c->delete_session("logout");
71         $c->res->redirect($c->uri_for('index'));
72 }
73
74 sub begin : Private {
75         my ($self, $c) = @_;
76
77          $c->res->header( 'Cache-Control' =>
78                 'no-store, no-cache, must-revalidate,'.
79                 'post-check=0, pre-check=0, max-age=0'
80         );
81         $c->res->header( 'Pragma' => 'no-cache' );
82         $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' );
83 }
84
85 sub listTargets : Private {
86         my ($self, $c) = @_;
87
88         my $dbh = $c ->model;
89
90         my $query = $dbh->prepare(q{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick, 
91                 (released_coords AND old_claim(timestamp)) AS released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
92 FROM raid_claims c
93         JOIN raid_targets t ON c.target = t.id
94         JOIN raids r ON t.raid = r.id
95         JOIN current_planet_stats p ON t.planet = p.id
96 WHERE c.uid = $1 AND r.tick+c.wave > tick() AND r.open AND not r.removed
97 ORDER BY r.tick+c.wave,x,y,z});
98         $query->execute($c->user->id) or die $dbh->errstr;
99         my @targets;
100         while (my $target = $query->fetchrow_hashref){
101                 push @targets, $target;
102         }
103
104         $c->stash(claimedtargets => \@targets);
105 }
106
107 sub listAlliances : Private {
108         my ($self, $c) = @_;
109         my @alliances;
110         push @alliances,{id => -1, name => ''};
111         my $query = $c->model->prepare(q{SELECT id,name FROM alliances ORDER BY LOWER(name)});
112         $query->execute;
113         while (my $ally = $query->fetchrow_hashref){
114                 push @alliances,$ally;
115         }
116         $c->stash(alliances => \@alliances);
117 }
118
119 sub auto : Private {
120         my ($self, $c) = @_;
121         my $dbh = $c ->model;
122
123         $c->stash(dbh => $dbh);
124
125         $dbh->do(q{SET timezone = 'GMT'});
126
127         $c->stash(TICK =>$dbh->selectrow_array('SELECT tick()',undef));
128         $c->stash(STICK =>$dbh->selectrow_array('SELECT max(tick) FROM planet_stats',undef));
129         $c->stash->{game}->{tick} = $c->stash->{TICK};
130
131         if ($c->user_exists){
132                 $c->stash(UID => $c->user->id);
133         }else{
134                 $c->stash(UID => -4);
135         }
136
137 }
138
139 sub access_denied : Private {
140         my ($self, $c, $action) = @_;
141
142         $c->stash->{template} = 'access_denied.tt2';
143
144 }
145
146 =head2 end
147
148 Attempt to render a view, if needed.
149
150 =cut 
151
152 sub end : ActionClass('RenderView') {
153         my ($self, $c) = @_;
154
155         my $dbh = $c ->model;
156
157         if (scalar @{ $c->error } ){
158                 if ($c->error->[0] =~ m/Can't call method "id" on an undefined value at/){
159                         $c->stash->{template} = 'access_denied.tt2';
160                         $c->clear_errors;
161                 }elsif ($c->error->[0] =~ m/Missing roles: /){
162                         $c->stash->{template} = 'access_denied.tt2';
163                         $c->clear_errors;
164                 }
165         }
166
167         if ($c->user_exists && $c->res->status == 200){
168                 my $fleetupdate = 0;
169                 if ($c->check_user_roles(qw/member_menu/)){
170                         $fleetupdate = $dbh->selectrow_array(q{SELECT tick FROM fleets WHERE sender = ?
171                                 AND mission = 'Full fleet' AND tick > tick() - 24
172                                 },undef,$c->user->planet);
173                         $fleetupdate = 0 unless defined $fleetupdate;
174                 }
175
176                 my ($unread,$newposts) = $dbh->selectrow_array(unread_query,undef,$c->user->id) or die $dbh->errstr;
177
178                 $c->stash(user => {
179                         id => $c->user->id,
180                         name => $c->user->username,
181                         css => $c->user->css,
182                         newposts => $newposts,
183                         unreadposts => $unread
184                 });
185                 $c->stash->{user}->{attacker} = $c->check_user_roles(qw/attack_menu/)
186                         && (!$c->check_user_roles(qw/member_menu/)
187                                 || ($c->user->planet && (($c->stash->{TICK} - $fleetupdate < 24)
188                                         || $c->check_user_roles(qw/no_fleet_update/)))),
189                 $c->forward('listTargets');
190         }
191 }
192
193 =head1 AUTHOR
194
195 Michael Andreen (harv@ruin.nu)
196
197 =head1 LICENSE
198
199 GPL 2, or later.
200
201 =cut
202
203 1;