| 1 | package OpenInteract::Handler::GenericDispatcher; | ||
| 2 | |||
| 3 | # $Id: GenericDispatcher.pm,v 1.92 2002/11/10 16:33:39 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( Exporter ); | ||
| 7 | use SPOPS::Secure qw( SEC_LEVEL_WRITE ); | ||
| 8 | |||
| 9 | use constant DEFAULT_SECURITY_KEY => 'DEFAULT'; | ||
| 10 | |||
| 11 | my $CLASS_TRACKING_KEY = 'class_cache_track'; | ||
| 12 | |||
| 13 | $OpenInteract::Handler::GenericDispatcher::VERSION = sprintf("%d.%02d", q$Revision: 1.92 $ =~ /(\d+)\.(\d+)/); | ||
| 14 | @OpenInteract::Handler::GenericDispatcher::EXPORT_OK = qw( DEFAULT_SECURITY_KEY ); | ||
| 15 | |||
| 16 | |||
| 17 | # Note that we do "no strict 'refs'" a few times in various methods | ||
| 18 | # throughout this packge -- it's just so we can refer to packge | ||
| 19 | # variables properly. | ||
| 20 | |||
| 21 | sub handler { | ||
| 22 | my ( $class, $p ) = @_; | ||
| 23 | my $R = OpenInteract::Request->instance; | ||
| 24 | |||
| 25 | # This routine should take care of parsing the task from the | ||
| 26 | # information passed in the $p hashref or via theURL and if not | ||
| 27 | # given, discerning a default; if there is no task returned, we're | ||
| 28 | # outta here. | ||
| 29 | |||
| 30 | $p->{TASK} ||= $class->_get_task; | ||
| 31 | |||
| 32 | $R->DEBUG && $R->scrib( 1, "Trying to run [$class] with [$p->{TASK}]" ); | ||
| 33 | |||
| 34 | # Default is to email the author there is no default task defined. | ||
| 35 | |||
| 36 | return $class->_no_task_found unless( $p->{TASK} ); | ||
| 37 | |||
| 38 | # If we are not allowed to run the task, the error handler should | ||
| 39 | # die() with the content for the page | ||
| 40 | |||
| 41 | unless ( $class->_task_allowed( $p->{TASK} ) ) { | ||
| 42 | $R->DEBUG && $R->scrib( 1, "[$p->{TASK}] is forbidden by [$class]; bailing." ); | ||
| 43 | $R->throw({ code => 303, | ||
| 44 | type => 'module', | ||
| 45 | extra => { task => $p->{TASK} } }); | ||
| 46 | } | ||
| 47 | |||
| 48 | # Check to see that user can do this task with security; if not | ||
| 49 | # routine should die() with an error message. | ||
| 50 | |||
| 51 | $p->{level} = $class->_check_task_security( $p->{TASK} ); | ||
| 52 | |||
| 53 | # For subclasses to override -- this would be useful if you want to | ||
| 54 | # create a new dispatcher by subclassing this class and, for | ||
| 55 | # instance, do a lookup in a hash or database as to what 'tab' | ||
| 56 | # should be selected in a menubar on your web page. | ||
| 57 | |||
| 58 | $class->_local_action( $p ); | ||
| 59 | my $task = $p->{TASK}; | ||
| 60 | return $class->$task( $p ); | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | ######################################## | ||
| 65 | # ACTIONS/TASKS | ||
| 66 | |||
| 67 | sub _local_action { return; } | ||
| 68 | |||
| 69 | |||
| 70 | # Get the task asked to do use the 'default_method' package variable | ||
| 71 | |||
| 72 | sub _get_task { | ||
| 73 | my ( $class ) = @_; | ||
| 74 | no strict 'refs'; | ||
| 75 | my $R = OpenInteract::Request->instance; | ||
| 76 | my $task = lc shift @{ $R->{path}{current} } || | ||
| 77 | ${ $class . '::default_method' }; | ||
| 78 | return $task; | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | # If there is no task defined, use the default method that $class | ||
| 83 | # has specified. What to do if $class hasn't specified one? | ||
| 84 | # We should probably bail, create an error object and send it | ||
| 85 | # to the module's author. (cool!) | ||
| 86 | |||
| 87 | sub _no_task_found { | ||
| 88 | my ( $class ) = @_; | ||
| 89 | my $author_msg = <<MSG; | ||
| 90 | Your module ($class) does not have a default task defined. | ||
| 91 | Please create the package variable '\$$class\:\:default_method' | ||
| 92 | as soon as possible. | ||
| 93 | |||
| 94 | Thanks! | ||
| 95 | |||
| 96 | The Management | ||
| 97 | MSG | ||
| 98 | no strict 'refs'; | ||
| 99 | my $R = OpenInteract::Request->instance; | ||
| 100 | return $R->throw( { code => 304, | ||
| 101 | type => 'module', | ||
| 102 | system_msg => "Author has not defined default task for $class", | ||
| 103 | extra => { email => ${ $class . '::author' }, | ||
| 104 | subject => "No default task defined for $class", | ||
| 105 | msg => $author_msg } } ); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _task_allowed { | ||
| 109 | my ( $class, $task ) = @_; | ||
| 110 | |||
| 111 | # Tasks beginning with '_' are not allowed by default | ||
| 112 | |||
| 113 | return undef if ( $task =~ /^_/ ); | ||
| 114 | no strict 'refs'; | ||
| 115 | |||
| 116 | # Check to see if this task is forbidden from being publicly called; if so, bail. | ||
| 117 | |||
| 118 | my %forbidden = map { $_ => 1 } @{ $class . '::forbidden_methods' }; | ||
| 119 | return ! $forbidden{ $task }; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | sub _check_task_security { | ||
| 124 | my ( $class, $task ) = @_; | ||
| 125 | no strict 'refs'; | ||
| 126 | my $R = OpenInteract::Request->instance; | ||
| 127 | |||
| 128 | # If the class uses security, $level will be overridden; if | ||
| 129 | # it does not use security, then it will be ignored. Note that | ||
| 130 | # we pass this on to the handler so it doesn't need to check the | ||
| 131 | # security again. | ||
| 132 | |||
| 133 | my $level = SEC_LEVEL_WRITE; | ||
| 134 | |||
| 135 | # Allow the handler to perform a shortcut if it wants to check | ||
| 136 | # security; note that if security for a task is not defined in the | ||
| 137 | # package, this check assumes WRITE security as a default | ||
| 138 | |||
| 139 | if ( $class->isa( 'SPOPS::Secure' ) ) { | ||
| 140 | $level = eval { $R->user->check_security({class => $class, | ||
| 141 | object_id => '0' }) }; | ||
| 142 | my %all_levels = %{ $class . '::security' }; | ||
| 143 | my $target_level = $all_levels{ $task } || | ||
| 144 | $all_levels{ DEFAULT_SECURITY_KEY() } || | ||
| 145 | SEC_LEVEL_WRITE; | ||
| 146 | $R->DEBUG && $R->scrib( 2, "Security after check for ($task):\n", | ||
| 147 | "user has: $level; user needs: $target_level" ); | ||
| 148 | |||
| 149 | # Security check failed, so bail (error handler die()s with an error message | ||
| 150 | |||
| 151 | if ( $level < $target_level ) { | ||
| 152 | $R->throw( { code => 305, type => 'security', | ||
| 153 | extra => { user_level => $level, | ||
| 154 | required_level => $target_level, | ||
| 155 | class => $class, | ||
| 156 | task => $task } } ); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | return $level; | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | ######################################## | ||
| 165 | # CACHING | ||
| 166 | |||
| 167 | sub check_cache { | ||
| 168 | my ( $class, $p, $key_params ) = @_; | ||
| 169 | my $R = OpenInteract::Request->instance; | ||
| 170 | my $cache = $R->cache; | ||
| 171 | if ( $R->{auth}{is_admin} || $p->{skip_cache} || ! $cache ) { | ||
| 172 | return undef; | ||
| 173 | } | ||
| 174 | |||
| 175 | my $key = $class->_make_cache_key( $p, $key_params ); | ||
| 176 | return undef unless ( $key ); | ||
| 177 | my $data = $cache->get({ key => $key }); | ||
| 178 | $R->DEBUG && $R->scrib( 1, "CACHE HIT! [Task: $p->{TASK}]" ) if ( $data ); | ||
| 179 | return $data; | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | sub _make_cache_key { | ||
| 184 | my ( $class, $p, $key_params ) = @_; | ||
| 185 | return undef unless ( ref $p->{ACTION}{cache_key} eq 'HASH' ); | ||
| 186 | return undef unless ( $p->{ACTION}{cache_key}{ $p->{TASK} } ); | ||
| 187 | return join( '||', $p->{ACTION}{cache_key}{ $p->{TASK} }, | ||
| 188 | map { join( '=', $_, $key_params->{ $_ } ) } | ||
| 189 | sort keys %{ $key_params } ); | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | sub generate_content { | ||
| 194 | my ( $class, @params ) = @_; | ||
| 195 | my ( $p, $key_params, $template_params, $variables, $template_source ); | ||
| 196 | my $num_params = scalar @params; | ||
| 197 | if ( $num_params == 4 ) { | ||
| 198 | ( $p, $key_params, $variables, $template_source ) = @params; | ||
| 199 | $template_params = {}; | ||
| 200 | } | ||
| 201 | elsif ( $num_params == 5 ) { | ||
| 202 | ( $p, $key_params, $template_params, $variables, $template_source ) = @params; | ||
| 203 | } | ||
| 204 | else { | ||
| 205 | die "Incorrect parameters passed to generate_content. ", | ||
| 206 | "(Need 4 or 5; given $num_params)\n"; | ||
| 207 | } | ||
| 208 | my $R = OpenInteract::Request->instance; | ||
| 209 | my $content = $R->template->handler( $template_params, $variables, $template_source ); | ||
| 210 | my $cache = $R->cache; | ||
| 211 | if ( $R->{auth}{is_admin} || $p->{skip_cache} || ! $cache ) { | ||
| 212 | return $content; | ||
| 213 | } | ||
| 214 | |||
| 215 | my $key = $class->_make_cache_key( $p, $key_params ); | ||
| 216 | if ( $key ) { | ||
| 217 | my $cache_expire = $p->{ACTION}{cache_expire} || {}; | ||
| 218 | $cache->set({ key => $key, | ||
| 219 | data => $content, | ||
| 220 | expire => $cache_expire->{ $p->{TASK} } }); | ||
| 221 | my $tracking = $cache->get({ key => $CLASS_TRACKING_KEY }) || {}; | ||
| 222 | push @{ $tracking->{ $class } }, $key; | ||
| 223 | $R->DEBUG && $R->scrib( 1, "Adding key [$key] to class [$class]" ); | ||
| 224 | $cache->set({ key => $CLASS_TRACKING_KEY, | ||
| 225 | data => $tracking }); | ||
| 226 | } | ||
| 227 | return $content; | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | sub clear_cache { | ||
| 232 | my ( $class ) = @_; | ||
| 233 | my $R = OpenInteract::Request->instance; | ||
| 234 | my $cache = $R->cache; | ||
| 235 | return unless ( $cache ); | ||
| 236 | |||
| 237 | $R->scrib( 0, "Trying to clear cache for items in class [$class]" ); | ||
| 238 | my $tracking = $cache->get({ key => $CLASS_TRACKING_KEY }); | ||
| 239 | unless ( ref $tracking eq 'HASH' and scalar keys %{ $tracking } ) { | ||
| 240 | $R->scrib( 0, "Nothing has yet been tracked, nothing to clear" ); | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | my $keys = $tracking->{ $class } || []; | ||
| 244 | foreach my $cache_key ( @{ $keys } ) { | ||
| 245 | $R->scrib( 0, "Clearing key [$cache_key]" ); | ||
| 246 | $cache->clear({ key => $cache_key }); | ||
| 247 | } | ||
| 248 | $tracking->{ $class } = []; | ||
| 249 | $cache->set({ key => $CLASS_TRACKING_KEY, data => $tracking }); | ||
| 250 | $R->scrib( 0, "Tracking data saved back" ); | ||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | ######################################## | ||
| 255 | # UTILITIES | ||
| 256 | |||
| 257 | # Return an object if we are able to construct it from parameters or | ||
| 258 | # wherever; if we have errors, raise them | ||
| 259 | |||
| 260 | sub _create_object { | ||
| 261 | my ( $class, $p ) = @_; | ||
| 262 | my $R = OpenInteract::Request->instance; | ||
| 263 | my $id_field_list = ( ref $p->{_id_field} ) | ||
| 264 | ? $p->{_id_field} : [ $p->{_id_field} ]; | ||
| 265 | my $object_class = $p->{_class}; | ||
| 266 | unless ( scalar @{ $id_field_list } and $object_class ) { | ||
| 267 | die "Cannot retrieve object without id_field and class definitions\n"; | ||
| 268 | } | ||
| 269 | |||
| 270 | my $oid = undef; | ||
| 271 | foreach my $id_field ( @{ $id_field_list } ) { | ||
| 272 | $oid = $R->apache->param( $id_field ); | ||
| 273 | last if ( $oid ); | ||
| 274 | } | ||
| 275 | return undef unless ( $oid ); | ||
| 276 | my $object = eval { $object_class->fetch( $oid ) }; | ||
| 277 | if ( $@ ) { | ||
| 278 | my $ei = OpenInteract::Error->set( SPOPS::Error->get ); | ||
| 279 | my $error_msg = undef; | ||
| 280 | if ( $ei->{type} eq 'security' ) { | ||
| 281 | $error_msg = "Permission denied: you do not have access to " . | ||
| 282 | "view the requested object."; | ||
| 283 | } | ||
| 284 | else { | ||
| 285 | $R->throw( { code => 404 } ); | ||
| 286 | $error_msg = "Error encountered trying to retrieve object. " . | ||
| 287 | "The error has been logged." | ||
| 288 | } | ||
| 289 | die "$error_msg\n"; | ||
| 290 | } | ||
| 291 | return $object; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub date_process { | ||
| 295 | my ( $class, $date ) = @_; | ||
| 296 | return {} if ( ! $date ); | ||
| 297 | my ( $y, $m, $d ) = split /\D/, $date; | ||
| 298 | $m =~ s/^0//; # do this so comparisons | ||
| 299 | $d =~ s/^0//; # within Template work | ||
| 300 | return { year => $y, month => $m, day => $d }; | ||
| 301 | } | ||
| 302 | |||
| 303 | sub date_read { | ||
| 304 | my ( $class, $prefix, $defaults ) = @_; | ||
| 305 | $defaults ||= {}; | ||
| 306 | my $apr = OpenInteract::Request->instance->apache; | ||
| 307 | my $day = $apr->param( "${prefix}_day" ) || $defaults->{day_default}; | ||
| 308 | my $month = $apr->param( "${prefix}_month" ) || $defaults->{month_default}; | ||
| 309 | my $year = $apr->param( "${prefix}_year" ) || $defaults->{year_default}; | ||
| 310 | return join '-', $year, $month, $day if ( $day and $month and $year ); | ||
| 311 | return undef; | ||
| 312 | } | ||
| 313 | |||
| 314 | 1; | ||
| 315 | |||
| 316 | __END__ |