http://www.perlmonks.org?node_id=663254


in reply to Re: CGI::Application::Authentication and Static Pages
in thread CGI::Application::Authentication and Static Pages

I hate to write (ok it's a macro - but still) even the my $self = shift; part in each runmode - and you have the check in each one?

I've came up with this before Authen plugin was written ...

== this is the "base module shared by all - I use base *it* instead of + CGI::App directly. package YPTP::App; use strict; use base 'CGI::Application'; use base 'YPTP::DataBase'; use base 'YPTP::Email'; use CGI::Application::Plugin::TT; # TemplateToolkit use CGI::Application::Plugin::Session; # CGI::Session use CGI::Application::Plugin::AutoRunmode; sub cgiapp_init { my $self = shift; .... ussual setup stuff ... } # In case most of pages are public - if not I set it to return 0 sub authorize { my $self = shift; return 1; } sub cgiapp_prerun { my ($self, $run_mode) = @_; # CGI::APP doesn't alow you to change runmode at init stage - say +if there is an error with # DB connection ... So I set it there and catch it here. if( $self->param('error') ){ $self->prerun_mode('ERROR'); } # Maybe only some runmodes need to be protected - so we send the r +unmode name to decide unless( $self->authorize($run_mode) ){ # Error $self->prerun_mode('NOT_AUTHORIZED'); } } === some module containing runmodes connected logically package YPTP::Runmodes::Admin; use strict; use base 'YPTP::App'; sub authorize { my $self = shift; my $runmode = shift; my $type = $self->session->param('type'); return 1 if($type eq 'admin'); # admin can do anything return 0; # everyone else can't do a thing }
I'm obviously using CGI::Application::Dispatch to decide which file/module to call. So for each file containing runmodes you can override the default authorize method and even check auth based on runmode to be called.
sub authorize { my $self = shift; my $runmode = shift; my $tip = $self->session->param('tip'); return 1 if($tip eq 'admin'); # Admin moze sve ! :) my $auth = { 'index' => 1, # everyone profil_forma => $tip eq 'poslodavac', profil_obrada => $tip eq 'poslodavac', '_default' => $tip eq 'poslodavac' }; if(defined $auth->{$runmode}) { return $auth->{$runmode}; } else { return $auth->{_default}; } }

Have you tried freelancing? Check out Scriptlance - I work there. For more info about Scriptlance and freelancing in general check out my home node.