/usr/share/perl5/Prophet/Server/Controller.pm is in libprophet-perl 0.750-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | package Prophet::Server::Controller;
use Any::Moose;
use Prophet::Util;
use Prophet::Web::Result;
has cgi => ( is => 'rw', isa => 'CGI' );
has failure_message => ( is => 'rw', isa => 'Str' );
has functions => ( is => 'rw', isa => 'HashRef' );
has app_handle => ( is => 'rw', isa => 'Prophet::App' );
has result => ( is => 'ro', isa => 'Prophet::Web::Result' );
=head1 NAME
=head1 METHODS
=head1 DESCRIPTION
=cut
=head1 METHODS
=cut
sub extract_functions_from_cgi {
my $self = shift;
my $functions = {};
foreach my $param ( $self->cgi->all_parameters ) {
next unless $param =~ /^prophet-function-(.*)$/;
my $name = $1;
$self->app_handle->log_fatal( "Duplicate function definition for @{[$name]}." ) if ( exists $functions->{$name} );
my $function_data = $self->cgi->param($param);
my $attr = $self->string_to_hash($function_data);
$attr->{name} = $name;
# For now, always execute
$attr->{execute} = 1;
# We MUST validate any function we're going to canonicalize
$attr->{validate} = 1 if $attr->{execute};
# We MUST canonicalize any function we're going to validate
$attr->{canonicalize} = 1 if $attr->{validate};
$functions->{$name} = $attr;
$functions->{$name}->{params} = $self->params_for_function_from_cgi($name);
}
$self->functions($functions);
}
sub params_for_function_from_cgi {
my $self = shift;
my $function = shift;
my $values;
for my $field ( $self->cgi->all_parameters ) {
if ( $field =~ /^prophet-field-function-$function-prop-(.*)$/ ) {
my $name = $1;
$values->{$name} = {
prop => $name,
value => ($self->cgi->param($field) || undef),
original_value => ($self->cgi->param( "original-value-" . $field ) || undef)
};
} elsif ( $field =~ /^prophet-fill-function-$function-prop-(.*)$/ ) {
my $name = $1;
my $meta = {};
my $value = $self->cgi->param($field);
next unless ( $value =~ /^function-(.*)\|result-(.*)$/ );
$values->{$name} = {
prop => $name,
from_function => $1,
from_result => $2
};
} else {
next;
}
}
return $values;
}
sub handle_functions {
my $self = shift;
my @workflow = qw(
extract_functions_from_cgi
canonicalize_functions
validate_functions
execute_functions
);
eval {
for (@workflow) {
$self->$_() ;
}
};
if (my $err = $@) {
$self->result->success(0);
$self->result->message($err);
}
}
sub canonicalize_functions {
my $self = shift;
my $functions = $self->functions;
foreach my $function ( sort { $functions->{$a}->{order} <=> $functions->{$b}->{order}} keys %{$functions}) {
next unless ($functions->{$function}->{canonicalize});
foreach my $param ( keys %{ $functions->{$function}->{params} } ) {
if (
defined $functions->{$function}->{params}->{$param}->{original_value} &&
($functions->{$function}->{params}->{$param}->{original_value} eq
$functions->{$function}->{params}->{$param}->{value} )) {
delete $functions->{$function}->{params}->{$param};
next;
}
}
}
}
sub validate_functions {
my $self = shift;
my $functions = $self->functions;
foreach my $function ( sort { $functions->{$a}->{order} <=> $functions->{$b}->{order}} keys %{$functions}) {
next unless ($functions->{$function}->{validate});
foreach my $param ( keys %{ $functions->{$function}->{params} } ) {
if (0) {
}
}
}
sub execute_functions {
my $self = shift;
my $functions = $self->functions;
foreach my $function ( sort { $functions->{$a}->{order} <=> $functions->{$b}->{order}} keys %{$functions}) {
$self->app_handle->log_debug("About to execute a function - ".$function);
$self->_fill_params_from_previous_functions($function);
next unless ($functions->{$function}->{execute});
if ($functions->{$function}->{action} eq 'update') {
$self->_exec_function_update($functions->{$function});
} elsif ($functions->{$function}->{action} eq 'create') {
$self->_exec_function_create($functions->{$function});
} else {
die "I don't know how to handle a ".$functions->{$function}->{action};
}
}
}
sub _fill_params_from_previous_functions {
my $self = shift;
my $function = shift;
my $params = $self->functions->{$function}->{params};
foreach my $param ( keys %$params ) {
if ( my $from_function = $params->{$param}->{from_function} ) {
my $from_result = $params->{$param}->{from_result};
my $function_result = $self->result->get($from_function);
# XXX TODO - $from_result should be locked down tighter
if ( $function_result->can($from_result) ) {
$params->{$param}->{value} = $function_result->$from_result();
}
}
}
}
sub _get_record_for_function {
my $self = shift;
my $function = shift;
my $functions = $self->functions;
if ($functions->{$function}->{action} eq 'update') {
return Prophet::Util->instantiate_record( uuid => $functions->{$function}->{uuid}, class=>$functions->{$function}->{class}, app_handle=> $self->app_handle);
} elsif ($functions->{$function}->{action} eq 'create') {
die $functions->{$function}->{class} ." is not a valid class " unless (UNIVERSAL::isa($functions->{$function}->{class}, 'Prophet::Record'));
return $functions->{$function}->{class}->new( app_handle => $self->app_handle);
} else {
die "I don't know how to handle a ".$functions->{$function}->{action};
}
}
}
sub _exec_function_create {
my $self = shift;
my $function = shift;
my $object = $self->_get_record_for_function($function->{name});
my ( $val, $msg ) = $object->create(
props => {
map { $function->{params}->{$_}->{prop} => $function->{params}->{$_}->{value}
} keys %{ $function->{params} }
}
);
my $res = Prophet::Web::FunctionResult->new( function_name => $function->{name},
class => $function->{class},
success => $object->uuid? 1 :0,
record_uuid => $object->uuid,
msg => ($msg || 'Record created'));
$self->result->set($function->{name} => $res);
}
sub _exec_function_update {
my $self = shift;
my $function = shift;
my $object = $self->_get_record_for_function($function->{name});
my ( $val, $msg ) = $object->set_props(
props => {
map {
$function->{params}->{$_}->{prop} => $function->{params}->{$_}->{value}
} keys %{ $function->{params} }
}
);
my $res = Prophet::Web::FunctionResult->new( function_name => $function->{name},
class => $function->{class},
success => $val? 1 :0,
record_uuid => $object->uuid,
msg => ($msg || 'Record updated'));
$self->result->set($function->{name} => $res);
}
sub string_to_hash {
my $self = shift;
my $data = shift;
my @bits = grep {$_} split( /\|/, $data );
my %attr = map { split( /=/, $_ ) } @bits;
return \%attr;
}
__PACKAGE__->meta->make_immutable;
no Any::Moose;
1;
|