| 1 | package OpenInteract::Component; | ||
| 2 | |||
| 3 | # $Id: Component.pm,v 2.16 2002/09/16 19:53:34 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use SPOPS::Secure qw( :level ); | ||
| 7 | use Data::Dumper qw( Dumper ); | ||
| 8 | |||
| 9 | $OpenInteract::Component::VERSION = sprintf("%d.%02d", q$Revision: 2.16 $ =~ /(\d+)\.(\d+)/); | ||
| 10 | |||
| 11 | use constant DEFAULT_METHOD => 'handler'; | ||
| 12 | use constant NOTFOUND_SECURITY => SEC_LEVEL_NONE; | ||
| 13 | |||
| 14 | sub handler { | ||
| 15 | my ( $class, $p, $return_info ) = @_; | ||
| 16 | my $R = OpenInteract::Request->instance; | ||
| 17 | unless ( $p->{name} ) { | ||
| 18 | $R->scrib( 0, "ERROR: cannot execute component without a name!", | ||
| 19 | "Information passed in:\n", Dumper( $p ) ); | ||
| 20 | return undef; | ||
| 21 | } | ||
| 22 | |||
| 23 | my @modified_action = $class->_modify_action( $p->{name} ); | ||
| 24 | my $action_info = $R->lookup_action([ $p->{name}, @modified_action ], | ||
| 25 | { return => 'info', skip_default => 1 }); | ||
| 26 | |||
| 27 | # If we don't find a action, assume that we're simply calling a | ||
| 28 | # template with the specified name; note that if we pass in | ||
| 29 | # 'template' (like for boxes) that are in the 'pkg::template' | ||
| 30 | # format we set the 'package' key below | ||
| 31 | |||
| 32 | unless ( scalar keys %{ $action_info } ) { | ||
| 33 | $R->DEBUG && $R->scrib( 1, "No action info returned; just using template name." ); | ||
| 34 | $action_info->{name} = $p->{name}; | ||
| 35 | $action_info->{template} = $p->{template} || $action_info->{name}; | ||
| 36 | $action_info->{package} = $p->{package}; | ||
| 37 | $R->DEBUG && $R->scrib( 1, "Using template ($action_info->{template})", | ||
| 38 | "(package if needed: $action_info->{package})" ); | ||
| 39 | } | ||
| 40 | |||
| 41 | unless ( $action_info->{template} or | ||
| 42 | $action_info->{class} or | ||
| 43 | ref $action_info->{code} ) { | ||
| 44 | $R->DEBUG && $R->scrib( 1, "No relevant action info; using name ($p->{name}) as template" ); | ||
| 45 | $action_info->{template} = $p->{name}; | ||
| 46 | } | ||
| 47 | $R->DEBUG && $R->scrib( 2, "Action_info:\n", Dumper( $action_info ) ); | ||
| 48 | |||
| 49 | # If the template is not in 'pkg::template' format, set it up so | ||
| 50 | # Most of this confusing stuff is for backward compatibility; in a | ||
| 51 | # few revs we'll take it out | ||
| 52 | |||
| 53 | if ( $action_info->{template} ) { | ||
| 54 | if ( $action_info->{template} =~ /^(\w+)::(\w+)$/ ) { | ||
| 55 | $action_info->{template_name} = $action_info->{template}; | ||
| 56 | $action_info->{package} = $1; | ||
| 57 | $action_info->{template} = $2; | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | $action_info->{template_name} = join( '::', $action_info->{package}, $action_info->{template} ); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | $action_info->{package} ||= $action_info->{package_name}; | ||
| 65 | |||
| 66 | # If the component defines a key named 'params', use that | ||
| 67 | # information as the data to feed the component. Otherwise just use | ||
| 68 | # whatever was passed in. | ||
| 69 | |||
| 70 | my $params = ( ref $p->{params} ) ? $p->{params} : $p; | ||
| 71 | $R->DEBUG && $R->scrib( 3, "Parameters being passed to component:\n", Dumper( $params ) ); | ||
| 72 | |||
| 73 | # Now that we have information for the component, go ahead and | ||
| 74 | # process it -- output goes to $html. Note that if no component | ||
| 75 | # information is specified the 'template_name' key in $action_info | ||
| 76 | # should have been specified. If no template is found then | ||
| 77 | # OpenInteract::Template will give us an error message in $html | ||
| 78 | |||
| 79 | my ( $html ); | ||
| 80 | |||
| 81 | # Process the standalone template | ||
| 82 | |||
| 83 | if ( $action_info->{template_name} ) { | ||
| 84 | $R->DEBUG && $R->scrib( 1, "Generic template action with ($action_info->{template_name})" ); | ||
| 85 | $html = eval { $R->template->handler( {}, $params, | ||
| 86 | { name => $action_info->{template_name} } ) }; | ||
| 87 | } | ||
| 88 | |||
| 89 | # Run the code reference | ||
| 90 | |||
| 91 | elsif ( ref $action_info->{code} eq 'CODE' ) { | ||
| 92 | $html = eval { $action_info->{code}->( $params ) }; | ||
| 93 | } | ||
| 94 | |||
| 95 | # Run the class->method call | ||
| 96 | |||
| 97 | elsif ( my $action_class = $action_info->{class} ) { | ||
| 98 | $action_info->{method} ||= DEFAULT_METHOD; | ||
| 99 | $R->DEBUG && $R->scrib( 1, "Calling component with $action_class->$action_info->{method}" ); | ||
| 100 | |||
| 101 | # Check security unless explicitly told not to | ||
| 102 | |||
| 103 | unless ( $action_info->{security} eq 'no' ) { | ||
| 104 | my $target_level = SEC_LEVEL_NONE; | ||
| 105 | ( $target_level, $params->{level} ) = $class->_check_security( $action_info ); | ||
| 106 | return undef unless ( $params->{level} >= $target_level ); | ||
| 107 | } | ||
| 108 | my $method = $action_info->{method}; | ||
| 109 | $html = eval { $action_class->$method( $params ) }; | ||
| 110 | } | ||
| 111 | $R->scrib( 0, "Died with error message: $@" ) if ( $@ ); | ||
| 112 | $R->DEBUG && $R->scrib( 2, "Component generated the following:\n$html" ); | ||
| 113 | |||
| 114 | # If they want the action information used by the component | ||
| 115 | # (evidenced by requesting a list of information from the call) , | ||
| 116 | # send the content and the action info to the caller | ||
| 117 | |||
| 118 | return ( $return_info ) ? ( $html, $action_info ) : $html; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | sub _check_security { | ||
| 124 | my ( $class, $action_info ) = @_; | ||
| 125 | my $R = OpenInteract::Request->instance; | ||
| 126 | no strict 'refs'; | ||
| 127 | my $required_level = SEC_LEVEL_WRITE; | ||
| 128 | if ( my $verbose_level = $action_info->{security_required} ) { | ||
| 129 | $required_level = SEC_LEVEL_WRITE if ( $verbose_level eq 'WRITE' ); | ||
| 130 | $required_level = SEC_LEVEL_READ if ( $verbose_level eq 'READ' ); | ||
| 131 | $required_level = SEC_LEVEL_NONE if ( $verbose_level eq 'NONE' ); | ||
| 132 | } | ||
| 133 | elsif ( $action_info->{class} ) { | ||
| 134 | my %all_levels = %{ $action_info->{class} . '::security' }; | ||
| 135 | $required_level = $all_levels{ $action_info->{method} } || NOTFOUND_SECURITY; | ||
| 136 | } | ||
| 137 | $R->DEBUG && $R->scrib( 1, "Checking security for component." ); | ||
| 138 | my $current_level = eval { $R->secure->check_security({ | ||
| 139 | security_object_class => $R->security_object, | ||
| 140 | db => $R->db, | ||
| 141 | user => $R->{auth}->{user}, | ||
| 142 | class => $action_info->{class}, | ||
| 143 | object_id => '0' }) }; | ||
| 144 | $current_level ||= SEC_LEVEL_WRITE; | ||
| 145 | $R->DEBUG && $R->scrib( 1, "Target security: ($required_level); Actual: ($current_level)" ); | ||
| 146 | return ( $required_level, $current_level ); | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | # Create additional names under which we might find the action in the | ||
| 152 | # action table | ||
| 153 | |||
| 154 | sub _modify_action { | ||
| 155 | my ( $class, $action ) = @_; | ||
| 156 | my @modified = ( lc $action ); | ||
| 157 | $action =~ s/[\_\-\s]//g; | ||
| 158 | push @modified, $action; | ||
| 159 | return @modified; | ||
| 160 | } | ||
| 161 | |||
| 162 | 1; | ||
| 163 | |||
| 164 | __END__ |