| 1 | package Smolder::Control; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'CGI::Application'; | ||
| 5 | use Carp qw(confess); | ||
| 6 | use CGI::Application::Plugin::ValidateRM; | ||
| 7 | use CGI::Application::Plugin::TT; | ||
| 8 | use CGI::Application::Plugin::LogDispatch; | ||
| 9 | use CGI::Application::Plugin::JSON qw(:all); | ||
| 10 | use Template::Plugin::Cycle; | ||
| 11 | use CGI::Cookie; | ||
| 12 | # This has nice output, but can't get it to work more than once... - JS | ||
| 13 | # BEGIN { $ENV{CGI_APP_DEBUG} = 1 ; } use CGI::Application::Plugin::DebugScreen; | ||
| 14 | use Smolder; | ||
| 15 | use Smolder::Util; | ||
| 16 | use Smolder::Conf qw(ErrorsToScreen HostName LogFile LogLevel TemplateDir); | ||
| 17 | use Smolder::DB::Developer; | ||
| 18 | use Smolder::DB::Project; | ||
| 19 | use File::Spec::Functions qw(catdir catfile tmpdir); | ||
| 20 | |||
| 21 | #{package Template::Perl; | ||
| 22 | # # Import debugging functions into templates (should be switched on with a config) | ||
| 23 | # use Smolder::Debug; | ||
| 24 | #} | ||
| 25 | |||
| 26 | sub run { | ||
| 27 | my $self = shift; | ||
| 28 | |||
| 29 | if (ErrorsToScreen) { | ||
| 30 | $self->error_mode('error_message'); | ||
| 31 | local $SIG{__DIE__} = sub { confess($_[0]) }; | ||
| 32 | local $SIG{__WARN__} = sub { die $_[0] if ($_[0] =~ /Deep recursion/) }; | ||
| 33 | $self->SUPER::run(@_); | ||
| 34 | } | ||
| 35 | else { | ||
| 36 | $self->SUPER::run(@_); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | # setup our logging | ||
| 41 | __PACKAGE__->add_callback( | ||
| 42 | init => sub { | ||
| 43 | my $self = shift; | ||
| 44 | if (LogFile) { | ||
| 45 | # setup log dispatch to send to a file | ||
| 46 | $self->log_config( | ||
| 47 | APPEND_NEWLINE => 1, | ||
| 48 | LOG_DISPATCH_MODULES => [ | ||
| 49 | { | ||
| 50 | module => 'Log::Dispatch::File', | ||
| 51 | name => 'smolder_log', | ||
| 52 | mode => 'append', | ||
| 53 | min_level => LogLevel, | ||
| 54 | filename => LogFile, | ||
| 55 | } | ||
| 56 | ], | ||
| 57 | ); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | ); | ||
| 61 | |||
| 62 | # setup our protection | ||
| 63 | __PACKAGE__->add_callback( | ||
| 64 | init => sub { | ||
| 65 | my $self = shift; | ||
| 66 | $self->run_modes(['forbidden']); | ||
| 67 | } | ||
| 68 | ); | ||
| 69 | __PACKAGE__->add_callback( | ||
| 70 | prerun => sub { | ||
| 71 | my $self = shift; | ||
| 72 | my $q = $self->query; | ||
| 73 | my $cookie = CGI::Cookie->fetch(); | ||
| 74 | $cookie = $cookie->{smolder}; | ||
| 75 | my $ai = Smolder::AuthInfo->new(); | ||
| 76 | my @user_groups; | ||
| 77 | |||
| 78 | # make sure we have a cookie and a session | ||
| 79 | if (ref $cookie) { | ||
| 80 | my $value = $cookie->value; | ||
| 81 | if ($value) { | ||
| 82 | $ai->parse($value); | ||
| 83 | if( $ai->id ) { | ||
| 84 | $ENV{REMOTE_USER} = $ai->id; | ||
| 85 | @user_groups = @{$ai->groups}; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | # log them in if the username and password are passed | ||
| 91 | if (!$ENV{REMOTE_USER} && ($q->param('username') && $q->param('password'))) { | ||
| 92 | my $dev = | ||
| 93 | Smolder::Control::Public::Auth::do_login($self, $q->param('username'), | ||
| 94 | $q->param('password')); | ||
| 95 | @user_groups = $dev->groups if $dev; | ||
| 96 | } | ||
| 97 | |||
| 98 | # make them anonymous if we don't have anything up to this point | ||
| 99 | $ENV{REMOTE_USER} ||= 'anon'; | ||
| 100 | |||
| 101 | # if our module requires any auth groups, then make sure we are a member | ||
| 102 | # of that group | ||
| 103 | if (my $group = $self->require_group) { | ||
| 104 | my $found = 0; | ||
| 105 | foreach my $ug (@user_groups) { | ||
| 106 | if ($ug eq $group) { | ||
| 107 | $found = 1; | ||
| 108 | last; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | unless ($found) { | ||
| 113 | $self->prerun_mode('forbidden'); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | ); | ||
| 118 | sub require_group { } | ||
| 119 | |||
| 120 | =head1 GLOBAL RUN MODES | ||
| 121 | |||
| 122 | =head2 forbidden | ||
| 123 | |||
| 124 | Shows a FORBIDDEN message if a user tries to act on a project that is not | ||
| 125 | marked as 'forbibben' | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub forbidden { | ||
| 130 | my $self = shift; | ||
| 131 | return $self->error_message("You shouldn't be here. Consider yourself warned."); | ||
| 132 | } | ||
| 133 | |||
| 134 | =head1 NAME | ||
| 135 | |||
| 136 | Smolder::Control | ||
| 137 | |||
| 138 | =head1 DESCRIPTION | ||
| 139 | |||
| 140 | This module serves as a base class for all controller classes in smolder. As such | ||
| 141 | it defines some behavior with regard to templates, form validation, etc | ||
| 142 | and provides some utility methods for accessing this data. | ||
| 143 | |||
| 144 | =head1 VARIABLES | ||
| 145 | |||
| 146 | =head2 MP2 | ||
| 147 | |||
| 148 | Will be true if we are running under Apache2/mod_perl2 | ||
| 149 | |||
| 150 | if( $Smolder::Control::MP2 ) { | ||
| 151 | ... | ||
| 152 | } | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | our $MP2 = | ||
| 157 | defined $ENV{MOD_PERL_API_VERSION} | ||
| 158 | ? $ENV{MOD_PERL_API_VERSION} == 2 | ||
| 159 | : 0; | ||
| 160 | |||
| 161 | =head1 METHODS | ||
| 162 | |||
| 163 | =head2 developer | ||
| 164 | |||
| 165 | This method will return the L<Smolder::DB::Developer> object that this request | ||
| 166 | is associated with, if it's not a public request. This information is pulled | ||
| 167 | from the C<$ENV{REMOTE_USER}> which is set by C<mod_auth_tkt>. | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub developer { | ||
| 172 | my $self = shift; | ||
| 173 | unless ($self->param('__developer')) { | ||
| 174 | |||
| 175 | # REMOTE_USER is set in our prerun | ||
| 176 | my $dev; | ||
| 177 | if ($ENV{REMOTE_USER} eq 'anon') { | ||
| 178 | $dev = Smolder::DB::Developer->get_guest(); | ||
| 179 | } else { | ||
| 180 | $dev = Smolder::DB::Developer->retrieve($ENV{REMOTE_USER}); | ||
| 181 | } | ||
| 182 | $self->param(__developer => $dev); | ||
| 183 | } | ||
| 184 | return $self->param('__developer'); | ||
| 185 | } | ||
| 186 | |||
| 187 | =head2 can_see_project | ||
| 188 | |||
| 189 | This method, will return whether the current user making the request | ||
| 190 | has permissions to view the given a L<Smolder::DB::Project> object. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub can_see_project { | ||
| 195 | my ($self, $proj) = @_; | ||
| 196 | return $proj->public || $proj->has_developer($self->developer); | ||
| 197 | } | ||
| 198 | |||
| 199 | =head2 public_projects | ||
| 200 | |||
| 201 | This method will return the L<Smolder::DB::Projects> that are marked as 'public'. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub public_projects { | ||
| 206 | my $self = shift; | ||
| 207 | my @projs = Smolder::DB::Project->search(public => 1, {order_by => 'name'}); | ||
| 208 | return \@projs; | ||
| 209 | } | ||
| 210 | |||
| 211 | =head2 error_message | ||
| 212 | |||
| 213 | A simple run mode to display an error message. This should not be used to show expected | ||
| 214 | messages, but rather to display un-recoverable and un-expected occurances. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub error_message { | ||
| 219 | my ($self, $msg) = @_; | ||
| 220 | $self->log->warning("An error occurred: $msg"); | ||
| 221 | (my $html_msg = $msg) =~ s/\n/<br>/g; | ||
| 222 | return $self->tt_process('error_message.tmpl', {message => $html_msg,},); | ||
| 223 | } | ||
| 224 | |||
| 225 | =head2 tt_process | ||
| 226 | |||
| 227 | This method is provided by the L<TT Plugin|CGI::Application::Plugin::TT> plugin. It is used | ||
| 228 | to choose and process the Template Toolkit templates. If no name is provided for the | ||
| 229 | template (as the first argument) then the package name and the run mode will be used | ||
| 230 | to determine which template to use. For instance: | ||
| 231 | |||
| 232 | $self->tt_process({ arg1 => 'foo', arg2 => 'bar' }); | ||
| 233 | |||
| 234 | If this was done in the C<Smolder::Control::Foo> package for the 'list' run mode then | ||
| 235 | it would use the F<templates/Foo/list.tmpl> template. If you want to use a different template | ||
| 236 | then you can explicitly specify it as well: | ||
| 237 | |||
| 238 | $self->tt_process('Foo/list.tmpl', { arg1 => 'foo', arg2 => 'bar' }); | ||
| 239 | |||
| 240 | See L<TEMPLATE_CONFIGURATION> for more details. | ||
| 241 | |||
| 242 | =head2 dfv_msgs | ||
| 243 | |||
| 244 | This is a convenience method to get access to the last L<Data::FormValidator> messages | ||
| 245 | that were created due to a form validation failure. These messages are simply flags indicating | ||
| 246 | which fields were missinage, which failed their constraints and which constraints failed. | ||
| 247 | |||
| 248 | See L<FORM VALIDATION> for more information. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | sub dfv_msgs { | ||
| 253 | my $self = shift; | ||
| 254 | my $results; | ||
| 255 | |||
| 256 | # we need to eval{} 'cause ValidateRM doesn't like dfv_results() being called | ||
| 257 | # without check_rm() being called first. | ||
| 258 | eval { $results = $self->dfv_results }; | ||
| 259 | if (!$@) { | ||
| 260 | return $results->msgs(); | ||
| 261 | } else { | ||
| 262 | return {}; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | =head2 auto_complete_results | ||
| 267 | |||
| 268 | This method takes an array ref of values to be returned to an AJAX Autocomplete | ||
| 269 | field. | ||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | sub auto_complete_results { | ||
| 274 | my ($self, $values) = @_; | ||
| 275 | my $html = '<ul>'; | ||
| 276 | foreach (@$values) { | ||
| 277 | $html .= '<li>' . $self->query->escapeHTML($_) . '</li>'; | ||
| 278 | } | ||
| 279 | return $html . '</ul>'; | ||
| 280 | } | ||
| 281 | |||
| 282 | =head2 static_url | ||
| 283 | |||
| 284 | This method will take the URL and add the smolder version number | ||
| 285 | to the front so that caching can be more aggressive. This is only | ||
| 286 | done if it's not a developer install, so that developers aren't | ||
| 287 | frustrated by having to fight with browser caches. | ||
| 288 | |||
| 289 | =cut | ||
| 290 | |||
| 291 | sub static_url { | ||
| 292 | my ($self, $url) = @_; | ||
| 293 | |||
| 294 | # TODO - fix this after the switch to CGI::Application::Server | ||
| 295 | return $url; | ||
| 296 | |||
| 297 | $url =~ s/^\///; | ||
| 298 | return catfile('', $Smolder::VERSION, $url); | ||
| 299 | } | ||
| 300 | |||
| 301 | =head2 add_message | ||
| 302 | |||
| 303 | Adds an message that will be displayed to the user. | ||
| 304 | Takes the following name-value pairs; | ||
| 305 | |||
| 306 | =over | ||
| 307 | |||
| 308 | =item msg | ||
| 309 | |||
| 310 | The text of the message to send. It will be HTML escaped, so | ||
| 311 | it must not contain HTML. | ||
| 312 | |||
| 313 | =item type | ||
| 314 | |||
| 315 | The type of the message, either C<info> or C<warning>. By | ||
| 316 | default C<info> is assumed. | ||
| 317 | |||
| 318 | =back | ||
| 319 | |||
| 320 | =cut | ||
| 321 | |||
| 322 | sub add_message { | ||
| 323 | my ($self, %args) = @_; | ||
| 324 | my $msgs = $self->json_header_value('messages') || []; | ||
| 325 | push(@$msgs, {type => ($args{type} || 'info'), msg => ($args{msg} || '')}); | ||
| 326 | $self->add_json_header(messages => $msgs); | ||
| 327 | } | ||
| 328 | |||
| 329 | =head1 TEMPLATE CONFIGURATION | ||
| 330 | |||
| 331 | As mentioned above, template access/control is performed through the | ||
| 332 | L<CGI::Application::Plugin::TT> plugin. The important are the settings used: | ||
| 333 | |||
| 334 | =over | ||
| 335 | |||
| 336 | =item The search path of templates is F<lib/Smolder/Data/templates> | ||
| 337 | |||
| 338 | =item All templates are wrapped with the F<templates/wrapper.tmpl> | ||
| 339 | template unless the C<ajax> CGI param is set. | ||
| 340 | |||
| 341 | =item Recursion is allowed for template INCLUDE and PROCESS | ||
| 342 | |||
| 343 | =item The following FILTERS are available to each template: | ||
| 344 | |||
| 345 | =over | ||
| 346 | |||
| 347 | =item pass_fail_color | ||
| 348 | |||
| 349 | Given a percentage (usually of passing tests to the total number run) | ||
| 350 | this filter will return an HTML RGB color suitable for a colorful indicator | ||
| 351 | of performance. | ||
| 352 | |||
| 353 | =back | ||
| 354 | |||
| 355 | =back | ||
| 356 | |||
| 357 | =cut | ||
| 358 | |||
| 359 | # configuration options for CAP::TT (Template Toolkit) | ||
| 360 | my $TT_CONFIG = { | ||
| 361 | TEMPLATE_OPTIONS => { | ||
| 362 | EVAL_PERL => 1, | ||
| 363 | COMPILE_DIR => tmpdir(), | ||
| 364 | INCLUDE_PATH => TemplateDir, | ||
| 365 | COMPILE_EXT => '.ttc', | ||
| 366 | WRAPPER => 'wrapper.tmpl', | ||
| 367 | RECURSION => 1, | ||
| 368 | ABSOLUTE => 1, | ||
| 369 | FILTERS => { | ||
| 370 | pass_fail_color => \&Smolder::Util::pass_fail_color, | ||
| 371 | format_time => \&Smolder::Util::format_time, | ||
| 372 | }, | ||
| 373 | }, | ||
| 374 | TEMPLATE_NAME_GENERATOR => sub { | ||
| 375 | my $self = shift; | ||
| 376 | |||
| 377 | # the directory is based on the object's package name | ||
| 378 | my $mod = ref $self; | ||
| 379 | $mod =~ s/Smolder::Control:://; | ||
| 380 | my $dir = catdir(split(/::/, $mod)); | ||
| 381 | |||
| 382 | # the filename is the method name of the caller | ||
| 383 | (caller(2))[3] =~ /([^:]+)$/; | ||
| 384 | my $name = $1; | ||
| 385 | if ($name eq 'tt_process') { | ||
| 386 | |||
| 387 | # we were called from tt_process, so go back once more on the caller stack | ||
| 388 | (caller(3))[3] =~ /([^:]+)$/; | ||
| 389 | $name = $1; | ||
| 390 | } | ||
| 391 | return catfile($dir, $name . '.tmpl'); | ||
| 392 | }, | ||
| 393 | |||
| 394 | TEMPLATE_PRECOMPILE_DIR => TemplateDir, | ||
| 395 | }; | ||
| 396 | __PACKAGE__->tt_config($TT_CONFIG); | ||
| 397 | |||
| 398 | __PACKAGE__->add_callback( | ||
| 399 | 'tt_pre_process', | ||
| 400 | sub { | ||
| 401 | my ($self, $file, $vars) = @_; | ||
| 402 | if ($self->query->param('ajax')) { | ||
| 403 | $vars->{no_wrapper} = 1; | ||
| 404 | $vars->{ajax} = 1; | ||
| 405 | } | ||
| 406 | $vars->{smolder_version} = $Smolder::VERSION; | ||
| 407 | $vars->{odd_even} = Template::Plugin::Cycle->new(qw(odd even)); | ||
| 408 | $vars->{url_base} = Smolder::Util::url_base(); | ||
| 409 | return; | ||
| 410 | } | ||
| 411 | ); | ||
| 412 | |||
| 413 | =head1 FORM VALIDATION | ||
| 414 | |||
| 415 | For form validation we use L<CGI::Application::Plugin::ValidateRM> which in | ||
| 416 | turn uses L<Data::FormValidator>. We further customize the validation by | ||
| 417 | providing the C<untaint_all_constraints> option which means that some values | ||
| 418 | will become "transformed" (dates will become L<DateTime> objects, etc). | ||
| 419 | |||
| 420 | We also customize the resulting hash of messages that is generated upon | ||
| 421 | validation failure. All failed and missing constraints will become err_$field. All | ||
| 422 | fields that were present but failed a constraint will become invalid_$name | ||
| 423 | (where $name is the name of the field or the name of the constraint if it's | ||
| 424 | named). And all missing constraints will have a missing_$field message. | ||
| 425 | Also, the 'any_errors' message will be set. | ||
| 426 | |||
| 427 | =cut | ||
| 428 | |||
| 429 | __PACKAGE__->add_callback( | ||
| 430 | init => sub { | ||
| 431 | my $self = shift; | ||
| 432 | $self->param( | ||
| 433 | 'dfv_defaults' => { | ||
| 434 | filters => ['trim'], | ||
| 435 | msgs => \&_create_dfv_msgs, | ||
| 436 | untaint_all_constraints => 1, | ||
| 437 | } | ||
| 438 | ); | ||
| 439 | } | ||
| 440 | ); | ||
| 441 | |||
| 442 | sub _create_dfv_msgs { | ||
| 443 | my $dfv = shift; | ||
| 444 | my %msgs; | ||
| 445 | |||
| 446 | # if there's anything wrong | ||
| 447 | if (!$dfv->success) { | ||
| 448 | |||
| 449 | # add 'any_errors' | ||
| 450 | $msgs{any_errors} = 1; | ||
| 451 | |||
| 452 | if ($dfv->has_invalid) { | ||
| 453 | |||
| 454 | # add any error messages for failed (possibly named) constraints | ||
| 455 | foreach my $failed ($dfv->invalid) { | ||
| 456 | $msgs{"err_$failed"} = 1; | ||
| 457 | $msgs{"invalid_$failed"} = 1; | ||
| 458 | my $names = $dfv->invalid($failed); | ||
| 459 | foreach my $name (@$names) { | ||
| 460 | next if (ref $name); # skip regexes | ||
| 461 | $msgs{"invalid_$name"} = 1; | ||
| 462 | } | ||
| 463 | } | ||
| 464 | } | ||
| 465 | |||
| 466 | # now add for missing | ||
| 467 | if ($dfv->has_missing) { | ||
| 468 | foreach my $missing ($dfv->missing) { | ||
| 469 | $msgs{"err_$missing"} = 1; | ||
| 470 | $msgs{"missing_$missing"} = 1; | ||
| 471 | $msgs{'has_missing'} = 1; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | } | ||
| 475 | return \%msgs; | ||
| 476 | } | ||
| 477 | |||
| 478 | 1; |