| 1 | package Silki::Controller::Base; | ||
| 2 | { | ||
| 3 | $Silki::Controller::Base::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use autodie; | ||
| 11 | use Carp qw( croak ); | ||
| 12 | use Silki::Config; | ||
| 13 | use Silki::I18N qw( loc ); | ||
| 14 | use Silki::JSON; | ||
| 15 | use Silki::Schema; | ||
| 16 | use Silki::Schema::File; | ||
| 17 | use Silki::Web::CSS; | ||
| 18 | use Silki::Web::Javascript; | ||
| 19 | |||
| 20 | use Moose; | ||
| 21 | |||
| 22 | BEGIN { extends 'Catalyst::Controller::REST' } | ||
| 23 | |||
| 24 | sub begin : Private { | ||
| 25 | my $self = shift; | ||
| 26 | my $c = shift; | ||
| 27 | |||
| 28 | Silki::Schema->ClearObjectCaches(); | ||
| 29 | |||
| 30 | return unless $c->request()->looks_like_browser(); | ||
| 31 | |||
| 32 | my $config = Silki::Config->instance(); | ||
| 33 | |||
| 34 | unless ( $config->is_production() || $config->is_profiling() ) { | ||
| 35 | $_->new()->create_single_file() | ||
| 36 | for qw( Silki::Web::CSS Silki::Web::Javascript ); | ||
| 37 | } | ||
| 38 | |||
| 39 | my $user = $c->user(); | ||
| 40 | my @langs = $user->is_system_user() ? () : $user->locale_code(); | ||
| 41 | |||
| 42 | Silki::I18N->SetLanguage(@langs); | ||
| 43 | |||
| 44 | return 1; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub end : Private { | ||
| 48 | my $self = shift; | ||
| 49 | my $c = shift; | ||
| 50 | |||
| 51 | return $self->next::method($c) | ||
| 52 | if $c->stash()->{rest}; | ||
| 53 | |||
| 54 | # Catalyst::Plugin::XSendfile seems to be designed to only work with | ||
| 55 | # Lighthttpd, and deletes any file over 16kb, which we don't want to do. I | ||
| 56 | # should probably patch it at some point. | ||
| 57 | if ( my $file = $c->response()->header('X-Sendfile') ) { | ||
| 58 | my ($engine) = ( ref $c->engine() ) =~ /^Catalyst::Engine::(.+)$/; | ||
| 59 | |||
| 60 | if ( $engine =~ /^HTTP/ ) { | ||
| 61 | if ( -f $file ) { | ||
| 62 | open my $fh, '<', $file; | ||
| 63 | $c->response()->body($fh); | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | $c->log() | ||
| 67 | ->error( | ||
| 68 | "X-sendfile pointed at nonexistent file - $file\n"); | ||
| 69 | $c->response()->status(404); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | return; | ||
| 74 | } | ||
| 75 | |||
| 76 | if ( ( !$c->response()->status() || $c->response()->status() == 200 ) | ||
| 77 | && !$c->response()->body() | ||
| 78 | && !@{ $c->error() || [] } ) { | ||
| 79 | $c->forward( $c->view() ); | ||
| 80 | } | ||
| 81 | |||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _set_entity { | ||
| 86 | my $self = shift; | ||
| 87 | my $c = shift; | ||
| 88 | my $entity = shift; | ||
| 89 | |||
| 90 | $c->response()->content_type('application/json'); | ||
| 91 | $c->response()->body( Silki::JSON->Encode($entity) ); | ||
| 92 | |||
| 93 | return 1; | ||
| 94 | } | ||
| 95 | |||
| 96 | my %MethodPermission = ( | ||
| 97 | GET => 'Read', | ||
| 98 | POST => 'Edit', | ||
| 99 | PUT => 'Edit', | ||
| 100 | DELETE => 'Delete', | ||
| 101 | ); | ||
| 102 | |||
| 103 | sub _require_permission_for_wiki { | ||
| 104 | my $self = shift; | ||
| 105 | my $c = shift; | ||
| 106 | my $wiki = shift; | ||
| 107 | my $perm = shift; | ||
| 108 | |||
| 109 | $perm ||= $MethodPermission{ uc $c->request()->method() }; | ||
| 110 | |||
| 111 | croak 'No permission specified in call to _require_permission_for_wiki' | ||
| 112 | unless $perm; | ||
| 113 | |||
| 114 | my $user = $c->user(); | ||
| 115 | |||
| 116 | return | ||
| 117 | if $user->has_permission_in_wiki( | ||
| 118 | wiki => $wiki, | ||
| 119 | permission => Silki::Schema::Permission->$perm(), | ||
| 120 | ); | ||
| 121 | |||
| 122 | my $perms = $wiki->permissions(); | ||
| 123 | |||
| 124 | if ( $user->is_guest() ) { | ||
| 125 | if ( $perms->{Authenticated}{$perm} ) { | ||
| 126 | $c->session_object()->add_message( | ||
| 127 | loc( | ||
| 128 | 'You must log in to to perform this action in the %1 wiki.', | ||
| 129 | $wiki->title(), | ||
| 130 | ) | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | $c->session_object()->add_message( | ||
| 135 | loc( | ||
| 136 | 'You must be a member of the %1 wiki to perform this action.', | ||
| 137 | $wiki->title() | ||
| 138 | ) | ||
| 139 | ); | ||
| 140 | } | ||
| 141 | |||
| 142 | my $uri = $c->domain()->application_uri( | ||
| 143 | path => '/user/login_form', | ||
| 144 | query => { return_to => $c->request()->uri() }, | ||
| 145 | ); | ||
| 146 | |||
| 147 | $c->redirect_and_detach($uri); | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | if ( $user->is_wiki_member($wiki) ) { | ||
| 151 | $c->session_object()->add_message( | ||
| 152 | loc( | ||
| 153 | 'You do not have %1 permissions in this wiki.', | ||
| 154 | lc $perm | ||
| 155 | ) | ||
| 156 | ); | ||
| 157 | |||
| 158 | } | ||
| 159 | else { | ||
| 160 | $c->session_object()->add_message( | ||
| 161 | loc( | ||
| 162 | 'You must be a member of the %1 wiki to perform this action.', | ||
| 163 | $wiki->title() | ||
| 164 | ) | ||
| 165 | ); | ||
| 166 | } | ||
| 167 | |||
| 168 | my $role = $user->role_in_wiki($wiki); | ||
| 169 | |||
| 170 | my $uri; | ||
| 171 | if ( $perms->{$role}{Read} ) { | ||
| 172 | $uri | ||
| 173 | = $c->stash()->{page} | ||
| 174 | ? $c->stash()->{page}->uri() | ||
| 175 | : $wiki->uri(); | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | $uri = $c->domain()->uri(); | ||
| 179 | } | ||
| 180 | |||
| 181 | $c->redirect_and_detach($uri); | ||
| 182 | } | ||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | sub _require_site_admin { | ||
| 187 | my $self = shift; | ||
| 188 | my $c = shift; | ||
| 189 | |||
| 190 | return if $c->user()->is_admin(); | ||
| 191 | |||
| 192 | $c->redirect_and_detach( $c->domain()->application_uri( path => '/' ) ); | ||
| 193 | } | ||
| 194 | |||
| 195 | __PACKAGE__->meta()->make_immutable(); | ||
| 196 | |||
| 197 | 1; | ||
| 198 | |||
| 199 | # ABSTRACT: Controller base class | ||
| 200 | |||
| 201 | __END__ |