| 1 | package CallBackery::Controller::RpcService; | ||
| 2 | |||
| 3 | use Mojo::Base qw(Mojolicious::Plugin::Qooxdoo::JsonRpcController); | ||
| 4 | use CallBackery::Exception qw(mkerror); | ||
| 5 | use CallBackery::User; | ||
| 6 | # use Data::Dumper; | ||
| 7 | use Scalar::Util qw(blessed weaken); | ||
| 8 | use Mojo::JSON qw(encode_json decode_json); | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | CallBackery::RpcService - RPC services for CallBackery | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | This module gets instantiated by L<CallBackery> and provides backend functionality. | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | This module provides the following methods | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | # the name of the service we provide | ||
| 25 | has service => 'default'; | ||
| 26 | |||
| 27 | =head2 allow_rpc_access(method) | ||
| 28 | |||
| 29 | is this method accessible? | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | my %allow = ( | ||
| 34 | getBaseConfig => 1, | ||
| 35 | login => 1, | ||
| 36 | logout => 1, | ||
| 37 | ping => 1, | ||
| 38 | getUserConfig => 2, | ||
| 39 | getPluginConfig => 2, | ||
| 40 | validatePluginData => 2, | ||
| 41 | processPluginData => 2, | ||
| 42 | getPluginData => 2, | ||
| 43 | getSessionCookie => 2 | ||
| 44 | ); | ||
| 45 | |||
| 46 | has config => sub { | ||
| 47 | shift->app->config; | ||
| 48 | }; | ||
| 49 | |||
| 50 | has user => sub { | ||
| 51 | my $self = shift; | ||
| 52 | my $obj = $self->app->userObject->new(controller=>$self); | ||
| 53 | # | ||
| 54 | weaken $obj->{controller}; | ||
| 55 | return $obj; | ||
| 56 | }; | ||
| 57 | |||
| 58 | has log => sub { | ||
| 59 | my $log = shift->app->log; | ||
| 60 | return $log; | ||
| 61 | }; | ||
| 62 | |||
| 63 | has pluginMap => sub { | ||
| 64 | my $map = shift->config->cfgHash->{PLUGIN}; | ||
| 65 | return $map; | ||
| 66 | }; | ||
| 67 | |||
| 68 | |||
| 69 | sub allow_rpc_access { | ||
| 70 | my $self = shift; | ||
| 71 | my $method = shift; | ||
| 72 | return (exists $allow{$method} and ($allow{$method} == 1 or $self->user->isUserAuthenticated)); | ||
| 73 | } | ||
| 74 | |||
| 75 | =head2 ping() | ||
| 76 | |||
| 77 | check if the server is happy with our authentication state | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub ping { | ||
| 82 | return 'pong'; | ||
| 83 | } | ||
| 84 | |||
| 85 | =head2 getSessionCookie() | ||
| 86 | |||
| 87 | Return a timeestamped session cookie. For use in the X-Session-Cookie header or as a xsc field | ||
| 88 | in form submissions. Note that session cookies for form submissions are only valid for 2 seconds. | ||
| 89 | So you have to get a fresh one from the server before submitting your form. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub getSessionCookie { | ||
| 94 | shift->user->makeSessionCookie(); | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 getConfig() | ||
| 98 | |||
| 99 | get some gloabal configuration information into the interface | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub getBaseConfig { | ||
| 104 | my $self = shift; | ||
| 105 | return $self->config->cfgHash->{FRONTEND}; | ||
| 106 | } | ||
| 107 | |||
| 108 | =head2 login(user,password) | ||
| 109 | |||
| 110 | Check login and provide the user specific interface configuration as a response. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub login { ## no critic (RequireArgUnpacking) | ||
| 115 | my $self = shift; | ||
| 116 | my $login = shift; | ||
| 117 | my $password = shift; | ||
| 118 | my $cfg = $self->config->cfgHash->{BACKEND}; | ||
| 119 | my $ok = $self->user->login($login,$password); | ||
| 120 | if (eval { blessed $ok && $ok->isa('Mojo::Promise') }){ | ||
| 121 | my $ret = Mojo::Promise->new; | ||
| 122 | $ok->then( | ||
| 123 | sub { | ||
| 124 | if (shift){ | ||
| 125 | $ret->resolve({ | ||
| 126 | sessionCookie => $self->user->makeSessionCookie() | ||
| 127 | }); | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | $ret->resolve(undef); | ||
| 131 | } | ||
| 132 | }, | ||
| 133 | sub { | ||
| 134 | $ret->reject(@_); | ||
| 135 | } | ||
| 136 | ); | ||
| 137 | return $ret; | ||
| 138 | } | ||
| 139 | if ($ok) { | ||
| 140 | return { | ||
| 141 | sessionCookie => $self->user->makeSessionCookie() | ||
| 142 | }; | ||
| 143 | } | ||
| 144 | return undef; | ||
| 145 | } | ||
| 146 | |||
| 147 | =head2 logout | ||
| 148 | |||
| 149 | Kill the session. | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub logout { | ||
| 154 | my $self = shift; | ||
| 155 | $self->session(expires=>1); | ||
| 156 | return 'http://youtu.be/KGsTNugVctI'; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | =head2 instantiatePlugin | ||
| 162 | |||
| 163 | get an instance for the given plugin | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | sub instantiatePlugin { | ||
| 168 | my $self = shift; | ||
| 169 | my $name = shift; | ||
| 170 | my $args = shift; | ||
| 171 | my $user = $self->user; | ||
| 172 | return $self->config->instantiatePlugin($name,$user,$args); | ||
| 173 | } | ||
| 174 | |||
| 175 | =head2 processPluginData(plugin,args) | ||
| 176 | |||
| 177 | handle form sumissions | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub processPluginData { | ||
| 182 | my $self = shift; | ||
| 183 | my $plugin = shift; | ||
| 184 | # creating two statements will make things | ||
| 185 | # easier to debug since there is only one | ||
| 186 | # thing that can go wrong per line. | ||
| 187 | my $instance = $self->instantiatePlugin($plugin); | ||
| 188 | return $instance->processData(@_); | ||
| 189 | } | ||
| 190 | |||
| 191 | =head2 validateField(plugin,args) | ||
| 192 | |||
| 193 | validate the content of the given field for the given plugin | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | sub validatePluginData { | ||
| 198 | my $self = shift; | ||
| 199 | my $plugin = shift; | ||
| 200 | return $self->instantiatePlugin($plugin) | ||
| 201 | ->validateData(@_); | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 getPluginData(plugin,args); | ||
| 205 | |||
| 206 | return the current value for the given field | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | sub getPluginData { | ||
| 211 | my $self = shift; | ||
| 212 | my $plugin = shift; | ||
| 213 | return $self->instantiatePlugin($plugin) | ||
| 214 | ->getData(@_); | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | =head2 getUserConfig | ||
| 219 | |||
| 220 | returns user specific configuration information | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | sub getUserConfig { | ||
| 225 | my $self = shift; | ||
| 226 | my @plugins; | ||
| 227 | my $ph = $self->pluginMap; | ||
| 228 | for my $name (@{$ph->{list}}){ | ||
| 229 | my $obj = eval { | ||
| 230 | $self->instantiatePlugin($name); | ||
| 231 | }; | ||
| 232 | warn "$@" if $@; | ||
| 233 | next unless $obj; | ||
| 234 | push @plugins, { | ||
| 235 | tabName => $obj->tabName, | ||
| 236 | name => $obj->name, | ||
| 237 | instantiationMode => $obj->instantiationMode | ||
| 238 | }; | ||
| 239 | } | ||
| 240 | return { | ||
| 241 | userInfo => $self->user->userInfo, | ||
| 242 | plugins => \@plugins, | ||
| 243 | }; | ||
| 244 | } | ||
| 245 | |||
| 246 | =head2 getPluginConfig(plugin,args) | ||
| 247 | |||
| 248 | returns a plugin configuration removing all the 'back end' keys and non ARRAY or HASH | ||
| 249 | references in the process. | ||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | sub getPluginConfig { | ||
| 254 | my $self = shift; | ||
| 255 | my $plugin = shift; | ||
| 256 | my $args = shift; | ||
| 257 | my $obj = $self->instantiatePlugin($plugin,$args); | ||
| 258 | return $obj->filterHashKey($obj->screenCfg,'backend'); | ||
| 259 | } | ||
| 260 | |||
| 261 | =head2 runEventActions(event[,args]) | ||
| 262 | |||
| 263 | Call the eventAction handlers of all configured plugins. Currently the following events | ||
| 264 | are known. | ||
| 265 | |||
| 266 | changeConfig | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | sub runEventActions { | ||
| 271 | my $self = shift; | ||
| 272 | my $event = shift; | ||
| 273 | for my $obj (@{$self->config->configPlugins}){ | ||
| 274 | weaken $obj->controller($self)->{controller}; | ||
| 275 | if (my $action = $obj->eventActions->{$event}){ | ||
| 276 | $action->(@_) | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | =head2 setPreDestroyAction(key,callback); | ||
| 282 | |||
| 283 | This can be used to have tasks completed at the end of a webtransaction since | ||
| 284 | the controller gets instantiated per transaction. | ||
| 285 | An example application would be backing up the configuration changes only | ||
| 286 | once even if more than one configChange event has occured. | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | my $runPreDestroyActions = sub { | ||
| 291 | my $self = shift; | ||
| 292 | my $actions = $self->{preDestroyActions} // {}; | ||
| 293 | $self->log->debug('destroying controller'); | ||
| 294 | for my $key (keys %$actions){ | ||
| 295 | $self->log->debug('running preDestroyAction '.$key); | ||
| 296 | eval { | ||
| 297 | $actions->{$key}->(); | ||
| 298 | }; | ||
| 299 | if ($@){ | ||
| 300 | $self->log->error("preDestoryAction $key: ".$@); | ||
| 301 | } | ||
| 302 | # and thus hopefully releasing the controller | ||
| 303 | delete $actions->{$key}; | ||
| 304 | } | ||
| 305 | delete $self->{preDestroyActions} | ||
| 306 | }; | ||
| 307 | |||
| 308 | sub setPreDestroyAction { | ||
| 309 | my $self = shift; | ||
| 310 | my $key = shift; | ||
| 311 | my $cb = shift; | ||
| 312 | if (not $self->{preDestroyActions}){ | ||
| 313 | # we want to run these pretty soon, basically as soon as | ||
| 314 | # controll returns to the ioloop | ||
| 315 | Mojo::IOLoop->timer("0.2" => sub{ $self->$runPreDestroyActions }); | ||
| 316 | } | ||
| 317 | $self->{preDestroyActions}{$key} = $cb; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub DESTROY { | ||
| 321 | my $self = shift; | ||
| 322 | $self->log->debug('Destroying RpcService controller'); | ||
| 323 | } | ||
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | =head2 handleUpload | ||
| 328 | |||
| 329 | Process incoming upload request. This is getting called via a route and NOT | ||
| 330 | in the usual way, hence we have to render our own response! | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | |||
| 335 | sub handleUpload { | ||
| 336 | my $self = shift; | ||
| 337 | if (not $self->user->isUserAuthenticated){ | ||
| 338 | return $self->render(text=>encode_json({exception=>{message=>'Access Denied',code=>4922}})); | ||
| 339 | } | ||
| 340 | my $name = $self->param('name'); | ||
| 341 | if (not $name){ | ||
| 342 | return self->render(text=>encode_json({exception=>{message=>'Plugin Name missing',code=>3934}})); | ||
| 343 | } | ||
| 344 | |||
| 345 | my $upload = $self->req->upload('file'); | ||
| 346 | if (not $upload){ | ||
| 347 | return $self->render(text=>encode_json({exception=>{message=>'Upload Missing',code=>9384}})); | ||
| 348 | } | ||
| 349 | my $obj = $self->config->instantiatePlugin($name,$self->user); | ||
| 350 | |||
| 351 | my $form; | ||
| 352 | if (my $formData = $self->req->param('formData')){ | ||
| 353 | $form = eval { decode_json($formData) }; | ||
| 354 | if ($@){ | ||
| 355 | return $self->render(text=>encode_json({exception=>{message=>'Data Decoding Problem '.$@,code=>7932}})); | ||
| 356 | } | ||
| 357 | } | ||
| 358 | $form->{uploadObj} = $upload; | ||
| 359 | |||
| 360 | my $return = eval { | ||
| 361 | $obj->processData({ | ||
| 362 | key => $self->req->param('key'), | ||
| 363 | formData => $form, | ||
| 364 | }); | ||
| 365 | }; | ||
| 366 | if ($@){ | ||
| 367 | if (blessed $@){ | ||
| 368 | if ($@->isa('CallBackery::Exception')){ | ||
| 369 | return $self->render(text=>encode_json({exception=>{message=>$@->message,code=>$@->code}})); | ||
| 370 | } | ||
| 371 | elsif ($@->isa('Mojo::Exception')){ | ||
| 372 | return $self->render(text=>encode_json({exception=>{message=>$@->message,code=>9999}})); | ||
| 373 | } | ||
| 374 | } | ||
| 375 | return $self->render(text=>encode_json({exception=>{message=>$@,code=>9999}})); | ||
| 376 | } | ||
| 377 | if (evan { blessed $return and $return->isa('Mojo::Promise')}){ | ||
| 378 | $return->then(sub { | ||
| 379 | $self->render(text=>encode_json(shift)); | ||
| 380 | }, | ||
| 381 | sub { | ||
| 382 | my $err = shift; | ||
| 383 | if (blessed $err){ | ||
| 384 | if ($err->isa('CallBackery::Exception')){ | ||
| 385 | return $self->render(text=>encode_json({exception=>{message=>$err->message,code=>$err->code}})); | ||
| 386 | } | ||
| 387 | elsif ($err->isa('Mojo::Exception')){ | ||
| 388 | return $self->render(text=>encode_json({exception=>{message=>$err->message,code=>9999}})); | ||
| 389 | } | ||
| 390 | } | ||
| 391 | return $self->render(text=>encode_json({exception=>{message=>$err,code=>9999}})); | ||
| 392 | }); | ||
| 393 | $self->render_later; | ||
| 394 | return $return; | ||
| 395 | } | ||
| 396 | #warn Dumper $return; | ||
| 397 | $self->render(text=>encode_json($return)); | ||
| 398 | } | ||
| 399 | |||
| 400 | =head2 handleDownload | ||
| 401 | |||
| 402 | Process incoming download request. The handler expects two parameters: name | ||
| 403 | for the plugin instance and formData for the data of the webform. | ||
| 404 | |||
| 405 | The handler getting the request must return a hash with the following elements: | ||
| 406 | |||
| 407 | =over | ||
| 408 | |||
| 409 | =item filename | ||
| 410 | |||
| 411 | name of the download when saved | ||
| 412 | |||
| 413 | =item type | ||
| 414 | |||
| 415 | mime-type of the download | ||
| 416 | |||
| 417 | =item asset | ||
| 418 | |||
| 419 | a L<Mojo::Asset>. eg C<Mojo::Asset::File->new(path => '/etc/passwd')>. | ||
| 420 | |||
| 421 | =back | ||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 | sub handleDownload { | ||
| 426 | my $self = shift; | ||
| 427 | |||
| 428 | if (not $self->user->isUserAuthenticated){ | ||
| 429 | return $self->render(text=>encode_json({exception=>{message=>'Access Denied',code=>3928}})); | ||
| 430 | } | ||
| 431 | |||
| 432 | my $name = $self->param('name'); | ||
| 433 | my $key = $self->param('key'); | ||
| 434 | if (not $name){ | ||
| 435 | return $self->render(text=>encode_json({exception=>{message=>'Plugin Name missing',code=>3923}})); | ||
| 436 | } | ||
| 437 | |||
| 438 | my $obj = $self->config->instantiatePlugin($name,$self->user); | ||
| 439 | |||
| 440 | my $form; | ||
| 441 | if (my $formData = $self->req->param('formData')){ | ||
| 442 | $form = eval { decode_json($formData) }; | ||
| 443 | if ($@){ | ||
| 444 | return $self->render(text=>encode_json({exception=>{message=>'Data Decoding Problem '.$@,code=>3923}})); | ||
| 445 | } | ||
| 446 | } | ||
| 447 | my $map = eval { | ||
| 448 | $obj->processData({ | ||
| 449 | key => $key, | ||
| 450 | formData => $form, | ||
| 451 | }); | ||
| 452 | }; | ||
| 453 | if ($@){ | ||
| 454 | if (blessed $@){ | ||
| 455 | if ($@->isa('CallBackery::Exception')){ | ||
| 456 | return $self->render(text=>encode_json({exception=>{message=>$@->message,code=>$@->code}})); | ||
| 457 | } | ||
| 458 | elsif ($@->isa('Mojo::Exception')){ | ||
| 459 | return $self->render(text=>encode_json({exception=>{message=>$@->message,code=>9999}})); | ||
| 460 | } | ||
| 461 | } | ||
| 462 | return $self->render(text=>encode_json({exception=>{message=>$@,code=>9999}})); | ||
| 463 | } | ||
| 464 | if (eval { blessed $map and $map->isa('Mojo::Promise')}){ | ||
| 465 | $map->then(sub { | ||
| 466 | my $map = shift; | ||
| 467 | $self->res->headers->content_type($map->{type}.';name=' .$map->{filename}); | ||
| 468 | $self->res->headers->content_disposition('attachment;filename='.$map->{filename}); | ||
| 469 | $self->res->content->asset($map->{asset}); | ||
| 470 | $self->rendered(200); | ||
| 471 | }, | ||
| 472 | sub { | ||
| 473 | my $err = shift; | ||
| 474 | if (blessed $err){ | ||
| 475 | if ($err->isa('CallBackery::Exception')){ | ||
| 476 | return $self->render(text=>encode_json({exception=>{message=>$err->message,code=>$err->code}})); | ||
| 477 | } | ||
| 478 | elsif ($err->isa('Mojo::Exception')){ | ||
| 479 | return $self->render(text=>encode_json({exception=>{message=>$err->message,code=>9999}})); | ||
| 480 | } | ||
| 481 | } | ||
| 482 | return $self->render(text=>encode_json({exception=>{message=>$err,code=>9999}})); | ||
| 483 | }); | ||
| 484 | $self->render_later; | ||
| 485 | return $map; | ||
| 486 | } | ||
| 487 | #warn Dumper $return; | ||
| 488 | $self->res->headers->content_type($map->{type}.';name=' .$map->{filename}); | ||
| 489 | $self->res->headers->content_disposition('attachment;filename='.$map->{filename}); | ||
| 490 | $self->res->content->asset($map->{asset}); | ||
| 491 | $self->rendered(200); | ||
| 492 | } | ||
| 493 | |||
| 494 | |||
| 495 | 1; | ||
| 496 | __END__ |