-
Notifications
You must be signed in to change notification settings - Fork 48
/
Root.pm
172 lines (127 loc) · 4.35 KB
/
Root.pm
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
package MojoMojo::Controller::Root;
use parent 'Catalyst::Controller';
__PACKAGE__->config->{namespace} = '';
=head1 NAME
MojoMojo::Controller::Root - Controller to run before all others
=head1 ACTIONS
=head2 begin (builtin)
TODO
=cut
sub begin : Private {
my ( $self, $c ) = @_;
if($c->sessionid && $c->session->{lang}) {
$c->languages([$c->session->{lang}]);
}
else {
$c->languages([$c->pref('default_lang')]) if $c->pref('default_lang');
}
# $c->stash->{path} is set by MojoMojo::prepare_path, which overrides the built-in Catalyst method
if ( $c->stash->{path} ) {
my ( $path_pages, $proto_pages ) =
$c->model('DBIC::Page')->path_pages( $c->stash->{path} );
@{ $c->stash }{qw/ path_pages proto_pages /} = ( $path_pages, $proto_pages );
$c->stash->{page} = $path_pages->[ @$path_pages - 1 ];
$c->stash->{user} = $c->user->obj() if $c->user_exists && $c->user;
}
}
=head2 default (global)
Default action - display the error page (message.tt), for example when a
nonexistent action was requested (like C</parent_page/child_page.fireworks>).
=cut
sub default : Path {
my ( $self, $c ) = @_;
$c->res->status(404);
$c->stash->{message} = $c->loc(
'The requested URL was not found: x',
'<span class="error_detail">' . $c->stash->{pre_hacked_uri} . '</span>'
);
$c->stash->{template} = 'message.tt';
}
=head2 set_lang
(Re)set language of current session.
=cut
sub set_lang :Global {
my ($self,$c) = @_;
$c->session->{lang}=$c->req->params->{lang};
$c->res->redirect($c->req->params->{redir});
}
=head2 render
Finally, use ActionClass RenderView to render the content.
=cut
sub render : ActionClass('RenderView') {
my ($self) = shift;
my ($c) = @_;
$c->stash->{path} ||= '/';
}
=head2 end (builtin)
At the end of any request, forward to view unless there is a template
or response, then render the template. If param 'die' is passed,
show a debug screen.
=cut
sub end : Private {
my ( $self, $c ) = @_;
my $theme=$c->pref('theme');
# if theme doesn't exist
if ( ! -d $c->path_to('root','static','themes',$theme)) {
$theme='default';
$c->pref('theme',$theme);
}
$c->stash->{additional_template_paths} =
[ $c->path_to('root','themes',$theme) ];
$c->req->uri->path( $c->stash->{pre_hacked_uri}->path )
if ref $c->stash->{pre_hacked_uri};
$c->forward('render');
}
=head2 auto
Runs for all requests, checks if user is in need of validation, and
intercepts the request if so. Also, if the requested page doesn't exist,
prevents any other actions from running, and detaches straight to
L<MojoMojo::Controller::Page/suggest>.
=cut
sub auto : Private {
my ( $self, $c ) = @_;
# Prevent most actions from running on non-existent pages. This fixes issues #36 and #80.
# 'render' should be allowed so that jsrpc/render can be used to preview newly created pages while the first version is being typed in
my $proto_pages = $c->stash->{proto_pages};
$c->detach('MojoMojo::Controller::Page', 'suggest')
if ($proto_pages && @$proto_pages && $c->action->name !~ /^(edit|render|login|logout|register|recover_pass)$/);
if ( $c->pref('enforce_login') ) {
# allow a few actions
if ( grep $c->action->name eq $_, qw/login logout recover_pass register/ ) {
return 1;
}
if ( !$c->user_exists ) {
$c->res->redirect(
$c->uri_for(
$c->stash->{path} . '.login' # send them to the login for this page
)
);
}
}
return 1 unless $c->stash->{user};
return 1 if $c->stash->{user}->active != -1;
return 1 if $c->req->action eq 'logout';
$c->stash->{template} = 'user/validate.tt';
}
=head2 exit
A way to exit from MojoMojo. Useful when testing leaks.
=cut
sub exit : Local {
my ($self, $c) = @_;
if ($ENV{MOJOMOJO_EXIT_OK}) {
exit(0);
}
else {
# $c->stash( template => 'error.tt' );
$c->res->status (403); # forbidden
$c->res->body('EXIT NOT OK');
$c->detach();
}
}
=head1 AUTHOR
Marcus Ramberg <mramberg@cpan.org>
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;