| 1 | |||
| 2 | =encoding UTF-8 | ||
| 3 | |||
| 4 | =head1 Name | ||
| 5 | |||
| 6 | QBit::Application - base class for create applications. | ||
| 7 | |||
| 8 | =head1 Description | ||
| 9 | |||
| 10 | It union all project models. | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | package QBit::Application; | ||
| 15 | $QBit::Application::VERSION = '0.017'; | ||
| 16 | use qbit; | ||
| 17 | |||
| 18 | use base qw(QBit::Class); | ||
| 19 | |||
| 20 | use QBit::Application::_Utils::TmpLocale; | ||
| 21 | use QBit::Application::_Utils::TmpRights; | ||
| 22 | |||
| 23 | =head1 RO accessors | ||
| 24 | |||
| 25 | =over | ||
| 26 | |||
| 27 | =item | ||
| 28 | |||
| 29 | B<timelog> | ||
| 30 | |||
| 31 | =back | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | __PACKAGE__->mk_ro_accessors(qw(timelog)); | ||
| 36 | |||
| 37 | =head1 Package methods | ||
| 38 | |||
| 39 | =head2 init | ||
| 40 | |||
| 41 | Initialization application. | ||
| 42 | |||
| 43 | B<It is done:> | ||
| 44 | |||
| 45 | =over | ||
| 46 | |||
| 47 | =item | ||
| 48 | |||
| 49 | Set options ApplicationPath and FrameworkPath | ||
| 50 | |||
| 51 | =item | ||
| 52 | |||
| 53 | Read all configs | ||
| 54 | |||
| 55 | =item | ||
| 56 | |||
| 57 | Install die handler if needed | ||
| 58 | |||
| 59 | =item | ||
| 60 | |||
| 61 | Set default locale | ||
| 62 | |||
| 63 | =item | ||
| 64 | |||
| 65 | Initialization accessors (see "set_accessors") | ||
| 66 | |||
| 67 | =item | ||
| 68 | |||
| 69 | Preload accessors if needed | ||
| 70 | |||
| 71 | =back | ||
| 72 | |||
| 73 | B<No arguments.> | ||
| 74 | |||
| 75 | B<Example:> | ||
| 76 | |||
| 77 | my $app = Application->new(); # Application based on QBit::Application | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub init { | ||
| 82 | my ($self) = @_; | ||
| 83 | |||
| 84 | $self->SUPER::init(); | ||
| 85 | |||
| 86 | $self->{'__OPTIONS__'} = $self->{'__ORIG_OPTIONS__'} = {}; | ||
| 87 | |||
| 88 | my $app_module = ref($self) . '.pm'; | ||
| 89 | $app_module =~ s/::/\//g; | ||
| 90 | |||
| 91 | $self->{'__ORIG_OPTIONS__'}{'FrameworkPath'} = $INC{'QBit/Class.pm'} =~ /(.+?)QBit\/Class\.pm$/ ? $1 : './'; | ||
| 92 | $self->{'__ORIG_OPTIONS__'}{'ApplicationPath'} = | ||
| 93 | ($INC{$app_module} || '') =~ /(.*?\/?)(?:[^\/]*lib\/*)?$app_module$/ | ||
| 94 | ? ($1 || './') | ||
| 95 | : './'; | ||
| 96 | |||
| 97 | package_merge_isa_data( | ||
| 98 | ref($self), | ||
| 99 | $self->{'__ORIG_OPTIONS__'}, | ||
| 100 | sub { | ||
| 101 | my ($package, $res) = @_; | ||
| 102 | |||
| 103 | my $pkg_stash = package_stash($package); | ||
| 104 | |||
| 105 | foreach my $cfg (@{$pkg_stash->{'__OPTIONS__'} || []}) { | ||
| 106 | $cfg->{'config'} //= $self->read_config($cfg->{'filename'}); | ||
| 107 | |||
| 108 | foreach (keys %{$cfg->{'config'}}) { | ||
| 109 | warn gettext('%s: option "%s" replaced', $cfg->{'filename'}, $_) | ||
| 110 | if exists($res->{$_}); | ||
| 111 | $res->{$_} = $cfg->{'config'}{$_}; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | }, | ||
| 115 | __PACKAGE__ | ||
| 116 | ); | ||
| 117 | |||
| 118 | if ($self->get_option('install_die_handler')) { | ||
| 119 | $SIG{__DIE__} = \&qbit::Exceptions::die_handler; | ||
| 120 | } | ||
| 121 | |||
| 122 | my $locales = $self->get_option('locales', {}); | ||
| 123 | if (%$locales) { | ||
| 124 | my ($locale) = grep {$locales->{$_}{'default'}} keys(%$locales); | ||
| 125 | ($locale) = keys(%$locales) unless $locale; | ||
| 126 | |||
| 127 | $self->set_app_locale($locale); | ||
| 128 | } | ||
| 129 | |||
| 130 | $self->init_accessors(); | ||
| 131 | |||
| 132 | if ($self->get_option('preload_accessors')) { | ||
| 133 | $self->$_ foreach keys(%{$self->get_models()}); | ||
| 134 | } | ||
| 135 | |||
| 136 | delete($self->{'__OPTIONS__'}); # Options initializing in pre_run | ||
| 137 | } | ||
| 138 | |||
| 139 | =head2 set_accessors | ||
| 140 | |||
| 141 | Set accessors. Initialization accessors - one of the steps in sub "init". | ||
| 142 | If you used B<set_accessors> after B<init>, call sub B<init_accessors>. | ||
| 143 | |||
| 144 | You can use standard way for set accessors | ||
| 145 | |||
| 146 | use Application::Model::Users accessor => 'users'; | ||
| 147 | use QBit::Application::Model::RBAC::DB accessor => 'rbac', models => {db => 'app_db'}; | ||
| 148 | |||
| 149 | B<But use this method preferable.> | ||
| 150 | |||
| 151 | B<Reserved keys:> | ||
| 152 | |||
| 153 | =over | ||
| 154 | |||
| 155 | =item | ||
| 156 | |||
| 157 | accessor | ||
| 158 | |||
| 159 | =item | ||
| 160 | |||
| 161 | package | ||
| 162 | |||
| 163 | =item | ||
| 164 | |||
| 165 | models | ||
| 166 | |||
| 167 | =item | ||
| 168 | |||
| 169 | init | ||
| 170 | |||
| 171 | =item | ||
| 172 | |||
| 173 | app_pkg | ||
| 174 | |||
| 175 | =back | ||
| 176 | |||
| 177 | B<Arguments:> | ||
| 178 | |||
| 179 | =over | ||
| 180 | |||
| 181 | =item | ||
| 182 | |||
| 183 | B<%accessors> - Accessors (type: hash). Keys is accessor name, values is options for import. | ||
| 184 | |||
| 185 | =back | ||
| 186 | |||
| 187 | B<Example:> | ||
| 188 | |||
| 189 | __PACKAGE__->set_accessors( | ||
| 190 | app_db => { | ||
| 191 | package => 'Application::Model::DB', # key "package" required (Package name) | ||
| 192 | }, | ||
| 193 | rbac => { | ||
| 194 | package => 'QBit::Application::Model::RBAC::DB', | ||
| 195 | models => { # key "models" redefine accessors into rbac | ||
| 196 | db => 'app_db' | ||
| 197 | }, | ||
| 198 | }, | ||
| 199 | ); | ||
| 200 | |||
| 201 | # or run time | ||
| 202 | |||
| 203 | $app->set_accessors(...); | ||
| 204 | $app->init_accessors(); | ||
| 205 | |||
| 206 | #after | ||
| 207 | |||
| 208 | $app->app_db; # returns object of a class "Application::Model::DB" | ||
| 209 | $app->rbac; # returns object of a class "QBit::Application::Model::RBAC::DB" | ||
| 210 | |||
| 211 | $app->rbac->db; # returns object of a class "Application::Model::DB", but into package used "QBit::Application::Model::DB::RBAC" | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub set_accessors { | ||
| 216 | my ($self, %accessors) = @_; | ||
| 217 | |||
| 218 | my $package = ref($self) || $self; | ||
| 219 | |||
| 220 | my $app_stash = package_stash($package); | ||
| 221 | my $models = $app_stash->{'__MODELS__'} //= {}; | ||
| 222 | |||
| 223 | my $all_models = $self->get_models(); | ||
| 224 | |||
| 225 | foreach my $accessor (sort keys(%accessors)) { | ||
| 226 | throw gettext( | ||
| 227 | 'Accessor "%s" with class "%s" is exists, try set this accessor for class "%s" in package "%s"', | ||
| 228 | $accessor, | ||
| 229 | $all_models->{$accessor}{'package'}, | ||
| 230 | $accessors{$accessor}->{'package'}, $package | ||
| 231 | ) | ||
| 232 | if exists($all_models->{$accessor}); | ||
| 233 | |||
| 234 | $accessors{$accessor}->{'app_pkg'} = $package; | ||
| 235 | $accessors{$accessor}->{'accessor'} = $accessor; | ||
| 236 | |||
| 237 | $models->{$accessor} = $accessors{$accessor}; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | =head2 init_accessors | ||
| 242 | |||
| 243 | Initialization accessors. Used after calling B<set_accessors> in run time a code | ||
| 244 | |||
| 245 | B<No arguments.> | ||
| 246 | |||
| 247 | B<Example:> | ||
| 248 | |||
| 249 | $app->set_accessors(...); | ||
| 250 | $app->init_accessors(); | ||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | sub init_accessors { | ||
| 255 | my ($self) = @_; | ||
| 256 | |||
| 257 | no strict 'refs'; | ||
| 258 | package_merge_isa_data( | ||
| 259 | ref($self) || $self, | ||
| 260 | undef, | ||
| 261 | sub { | ||
| 262 | my ($package) = @_; | ||
| 263 | |||
| 264 | my $models = package_stash($package)->{'__MODELS__'} || {}; | ||
| 265 | |||
| 266 | foreach my $accessor (sort keys(%$models)) { | ||
| 267 | next if $models->{$accessor}{'init'}; | ||
| 268 | |||
| 269 | throw gettext('Accessor cannot have name "%s", it is name of method', $accessor) | ||
| 270 | if $self->can($accessor); | ||
| 271 | |||
| 272 | my %import_args = %{$models->{$accessor}}; | ||
| 273 | my $app_pkg = $import_args{'app_pkg'}; | ||
| 274 | my $package = $import_args{'package'}; | ||
| 275 | |||
| 276 | *{"${app_pkg}::${accessor}"} = sub { | ||
| 277 | $_[0]->{$accessor} //= do { | ||
| 278 | my $file_path = "$package.pm"; | ||
| 279 | $file_path =~ s/::/\//g; | ||
| 280 | |||
| 281 | unless ($INC{$file_path}) { | ||
| 282 | try { | ||
| 283 | require $file_path; | ||
| 284 | } | ||
| 285 | catch { | ||
| 286 | throw gettext('Failed require "%s": %s', $file_path, shift->message); | ||
| 287 | }; | ||
| 288 | |||
| 289 | $package->import(%import_args); | ||
| 290 | } | ||
| 291 | |||
| 292 | $package->new(app => $_[0], accessor => $accessor); | ||
| 293 | }; | ||
| 294 | }; | ||
| 295 | |||
| 296 | $models->{$accessor}{'init'} = TRUE; | ||
| 297 | } | ||
| 298 | |||
| 299 | }, | ||
| 300 | __PACKAGE__ | ||
| 301 | ); | ||
| 302 | } | ||
| 303 | |||
| 304 | =head2 config_opts | ||
| 305 | |||
| 306 | Set options in config | ||
| 307 | |||
| 308 | B<Arguments:> | ||
| 309 | |||
| 310 | =over | ||
| 311 | |||
| 312 | =item | ||
| 313 | |||
| 314 | B<%opts> - Options (type: hash) | ||
| 315 | |||
| 316 | =back | ||
| 317 | |||
| 318 | B<Example:> | ||
| 319 | |||
| 320 | __PACKAGE__->config_opts(param_name => 'Param'); | ||
| 321 | |||
| 322 | # later in your code: | ||
| 323 | |||
| 324 | my $param = $app->get_option('param_name'); # 'Param' | ||
| 325 | |||
| 326 | =cut | ||
| 327 | |||
| 328 | sub config_opts { | ||
| 329 | my ($self, %opts) = @_; | ||
| 330 | |||
| 331 | my $class = ref($self) || $self; | ||
| 332 | |||
| 333 | my $pkg_name = $class; | ||
| 334 | $pkg_name =~ s/::/\//g; | ||
| 335 | $pkg_name .= '.pm'; | ||
| 336 | |||
| 337 | $self->_push_pkg_opts($INC{$pkg_name} || $pkg_name => \%opts); | ||
| 338 | } | ||
| 339 | |||
| 340 | =head2 use_config | ||
| 341 | |||
| 342 | Set a file in config queue. The configuration is read in sub "init". In the same place are set the settings B<ApplicationPath> and B<FrameworkPath>. | ||
| 343 | |||
| 344 | B<QBit::Application options:> | ||
| 345 | |||
| 346 | =over | ||
| 347 | |||
| 348 | =item | ||
| 349 | |||
| 350 | B<locales> - type: hash | ||
| 351 | |||
| 352 | locales => { | ||
| 353 | ru => {name => 'Русский', code => 'ru_RU', default => 1}, | ||
| 354 | en => {name => 'English', code => 'en_GB'}, | ||
| 355 | }, | ||
| 356 | |||
| 357 | =item | ||
| 358 | |||
| 359 | B<preload_accessors> - type: int, values: 1/0 (1 - preload accessors, 0 - lazy load, default: 0) | ||
| 360 | |||
| 361 | =item | ||
| 362 | |||
| 363 | B<install_die_handler> - type: int, values: 1/0 (1 - set die handler B<qbit::Exceptions::die_handler>, default: 0) | ||
| 364 | |||
| 365 | =item | ||
| 366 | |||
| 367 | B<timelog_class> - type: string, values: B<QBit::TimeLog::XS/QBit::TimeLog> (default: B<QBit::TimeLog> - this is not a production solution, in production use XS version) | ||
| 368 | |||
| 369 | =item | ||
| 370 | |||
| 371 | B<locale_domain> - type: string, value: <your domain> (used in set_locale for B<Locale::Messages::textdomain>, default: 'application') | ||
| 372 | |||
| 373 | =item | ||
| 374 | |||
| 375 | B<find_app_mem_cycle> - type: int, values: 1/0 (1 - find memory cycle in post_run, used Devel::Cycle, default: 0) | ||
| 376 | |||
| 377 | =back | ||
| 378 | |||
| 379 | B<QBit::WebInterface options:> | ||
| 380 | |||
| 381 | =over | ||
| 382 | |||
| 383 | =item | ||
| 384 | |||
| 385 | B<error_dump_dir> - type: string, value: <your path for error dumps> | ||
| 386 | |||
| 387 | =item | ||
| 388 | |||
| 389 | B<salt> - type: string, value: <your salt> (used for generate csrf token) | ||
| 390 | |||
| 391 | =item | ||
| 392 | |||
| 393 | B<TemplateCachePath> - type: string, value: <your path for template cache> (default: "/tmp") | ||
| 394 | |||
| 395 | =item | ||
| 396 | |||
| 397 | B<show_timelog> - type: int, values: 1/0 (1 - view timelog in html footer, default: 0) | ||
| 398 | |||
| 399 | =item | ||
| 400 | |||
| 401 | B<TemplateIncludePaths> - type: array of a string: value: [<your path for templates>] | ||
| 402 | |||
| 403 | already used: | ||
| 404 | - <project_path>/templates # project_path = $self->get_option('ApplicationPath') | ||
| 405 | - <framework_path>/QBit/templates # framework_path = $self->get_option('FrameworkPath') | ||
| 406 | |||
| 407 | =back | ||
| 408 | |||
| 409 | B<QBit::WebInterface::Routing options:> | ||
| 410 | |||
| 411 | =over | ||
| 412 | |||
| 413 | =item | ||
| 414 | |||
| 415 | B<controller_class> - type: string, value: <your controller class> (default: B<QBit::WebInterface::Controller>) | ||
| 416 | |||
| 417 | =item | ||
| 418 | |||
| 419 | B<use_base_routing> - type: int, values: 1/0 (1 - also use routing from B<QBit::WebInterface::Controller>, 0 - only use routing from B<QBit::WebInterface::Routing>) | ||
| 420 | |||
| 421 | =back | ||
| 422 | |||
| 423 | B<Arguments:> | ||
| 424 | |||
| 425 | =over | ||
| 426 | |||
| 427 | =item | ||
| 428 | |||
| 429 | B<$filename> - Config name (type: string) | ||
| 430 | |||
| 431 | =back | ||
| 432 | |||
| 433 | B<Example:> | ||
| 434 | |||
| 435 | __PACKAGE__->use_config('Application.cfg'); # or __PACKAGE__->use_config('Application.json'); | ||
| 436 | |||
| 437 | # later in your code: | ||
| 438 | |||
| 439 | my preload_accessors = $app->get_option('preload_accessors'); | ||
| 440 | |||
| 441 | =cut | ||
| 442 | |||
| 443 | sub use_config { | ||
| 444 | my ($self, $filename) = @_; | ||
| 445 | |||
| 446 | $self->_push_pkg_opts($filename); | ||
| 447 | } | ||
| 448 | |||
| 449 | =head2 read_config | ||
| 450 | |||
| 451 | read config by path or name from folder "configs". | ||
| 452 | |||
| 453 | > tree ./Project | ||
| 454 | |||
| 455 | Project | ||
| 456 | ├── configs | ||
| 457 | │ └── Application.cfg | ||
| 458 | └── lib | ||
| 459 | └── Application.pm | ||
| 460 | |||
| 461 | B<Formats:> | ||
| 462 | |||
| 463 | =over | ||
| 464 | |||
| 465 | =item | ||
| 466 | |||
| 467 | B<cfg> - perl code | ||
| 468 | |||
| 469 | > cat ./configs/Application.cfg | ||
| 470 | |||
| 471 | preload_accessors => 1, | ||
| 472 | timelog_class => 'QBit::TimeLog::XS', | ||
| 473 | locale_domain => 'domain.local', | ||
| 474 | TemplateIncludePaths => ['${ApplicationPath}lib/QBit/templates'], | ||
| 475 | |||
| 476 | =item | ||
| 477 | |||
| 478 | B<json> - json format | ||
| 479 | |||
| 480 | > cat ./configs/Application.json | ||
| 481 | |||
| 482 | { | ||
| 483 | "preload_accessors" : 1, | ||
| 484 | "timelog_class" : "QBit::TimeLog::XS", | ||
| 485 | "locale_domain" : "domain.local", | ||
| 486 | "TemplateIncludePaths" : ["${ApplicationPath}lib/QBit/templates"] | ||
| 487 | } | ||
| 488 | |||
| 489 | =back | ||
| 490 | |||
| 491 | B<Arguments:> | ||
| 492 | |||
| 493 | =over | ||
| 494 | |||
| 495 | =item | ||
| 496 | |||
| 497 | B<$filename> - Config name (type: string) | ||
| 498 | |||
| 499 | =back | ||
| 500 | |||
| 501 | B<Return value:> Options (type: ref of a hash) | ||
| 502 | |||
| 503 | B<Example:> | ||
| 504 | |||
| 505 | my $config = $app->read_config('Application.cfg'); | ||
| 506 | |||
| 507 | =cut | ||
| 508 | |||
| 509 | sub read_config { | ||
| 510 | my ($self, $filename) = @_; | ||
| 511 | |||
| 512 | unless (-f $filename) { | ||
| 513 | foreach (qw(lib configs)) { | ||
| 514 | my $possible_file = $self->get_option('ApplicationPath') . "$_/$filename"; | ||
| 515 | |||
| 516 | if (-f $possible_file) { | ||
| 517 | $filename = $possible_file; | ||
| 518 | |||
| 519 | #TODO: use only configs | ||
| 520 | if ($_ eq 'lib') { | ||
| 521 | warn gettext('For configs, use the "configs" folder in the project root.'); | ||
| 522 | } | ||
| 523 | |||
| 524 | last; | ||
| 525 | } | ||
| 526 | } | ||
| 527 | } | ||
| 528 | |||
| 529 | my $config = {}; | ||
| 530 | |||
| 531 | try { | ||
| 532 | if ($filename =~ /\.cfg\z/) { | ||
| 533 | $config = {do $filename}; | ||
| 534 | } elsif ($filename =~ /\.json\z/) { | ||
| 535 | $config = from_json(readfile($filename)); | ||
| 536 | } else { | ||
| 537 | throw gettext('Unknown config format: %s', $filename); | ||
| 538 | } | ||
| 539 | } | ||
| 540 | catch { | ||
| 541 | my ($exception) = @_; | ||
| 542 | |||
| 543 | throw gettext('Read config file "%s" failed: %s', $filename, $exception->message); | ||
| 544 | }; | ||
| 545 | |||
| 546 | throw gettext('Config "%s" must be a hash') if ref($config) ne 'HASH'; | ||
| 547 | |||
| 548 | return $config; | ||
| 549 | } | ||
| 550 | |||
| 551 | =head2 get_option | ||
| 552 | |||
| 553 | Returns option value by name | ||
| 554 | |||
| 555 | B<Arguments:> | ||
| 556 | |||
| 557 | =over | ||
| 558 | |||
| 559 | =item | ||
| 560 | |||
| 561 | B<$name> - Option name (type: string) | ||
| 562 | |||
| 563 | =item | ||
| 564 | |||
| 565 | B<$default> - Default value | ||
| 566 | |||
| 567 | =back | ||
| 568 | |||
| 569 | B<Return value:> Option value | ||
| 570 | |||
| 571 | B<Example:> | ||
| 572 | |||
| 573 | my $salt = $app->get_option('salt', 's3cret'); | ||
| 574 | |||
| 575 | my $stash = $app->get_option('stash', {}); | ||
| 576 | |||
| 577 | =cut | ||
| 578 | |||
| 579 | sub get_option { | ||
| 580 | my ($self, $name, $default) = @_; | ||
| 581 | |||
| 582 | my $res = $self->{'__OPTIONS__'}{$name} // $default; | ||
| 583 | |||
| 584 | if (defined($res) && (!ref($res) || ref($res) eq 'ARRAY')) { | ||
| 585 | foreach my $str (ref($res) eq 'ARRAY' ? @$res : $res) { | ||
| 586 | while ($str =~ /^(.*?)(?:\$\{([\w\d_]+)\})(.*)$/) { | ||
| 587 | $str = ($1 || '') . ($self->get_option($2) || '') . ($3 || ''); | ||
| 588 | } | ||
| 589 | } | ||
| 590 | } | ||
| 591 | |||
| 592 | return $res; | ||
| 593 | } | ||
| 594 | |||
| 595 | =head2 set_option | ||
| 596 | |||
| 597 | Set option value by name. | ||
| 598 | |||
| 599 | B<Arguments:> | ||
| 600 | |||
| 601 | =over | ||
| 602 | |||
| 603 | =item | ||
| 604 | |||
| 605 | B<$name> - Option name (type: string) | ||
| 606 | |||
| 607 | =item | ||
| 608 | |||
| 609 | B<$value> - Option value | ||
| 610 | |||
| 611 | =back | ||
| 612 | |||
| 613 | B<Return value:> Option value | ||
| 614 | |||
| 615 | B<Example:> | ||
| 616 | |||
| 617 | $app->set_option('salt', 's3cret'); | ||
| 618 | |||
| 619 | $app->set_option('stash', {key => 'val'}); | ||
| 620 | |||
| 621 | =cut | ||
| 622 | |||
| 623 | sub set_option { | ||
| 624 | my ($self, $name, $value) = @_; | ||
| 625 | |||
| 626 | $self->{'__OPTIONS__'}{$name} = $value; | ||
| 627 | } | ||
| 628 | |||
| 629 | =head2 cur_user | ||
| 630 | |||
| 631 | set or get current user | ||
| 632 | |||
| 633 | B<Arguments:> | ||
| 634 | |||
| 635 | =over | ||
| 636 | |||
| 637 | =item | ||
| 638 | |||
| 639 | B<$user> - hash ref | ||
| 640 | |||
| 641 | =back | ||
| 642 | |||
| 643 | B<Return value:> hash ref | ||
| 644 | |||
| 645 | my $user = {id => 1}; | ||
| 646 | |||
| 647 | $cur_user = $app->cur_user($user); # set current user | ||
| 648 | |||
| 649 | # if use rbac | ||
| 650 | # {id => 1, roles => {3 => {id => 3, name => 'ROLE 3', description => 'ROLE 3'}}, rights => ['RIGHT1', 'RIGHT2']} | ||
| 651 | # or | ||
| 652 | # {id => 1} | ||
| 653 | |||
| 654 | $cur_user = $app->cur_user(); # return current user or {} | ||
| 655 | |||
| 656 | $app->cur_user({}); # remove current user | ||
| 657 | |||
| 658 | =cut | ||
| 659 | |||
| 660 | sub cur_user { | ||
| 661 | my ($self, $user) = @_; | ||
| 662 | |||
| 663 | my $cur_user = $self->{'__OPTIONS__'}{'cur_user'} // {}; | ||
| 664 | |||
| 665 | return $cur_user unless defined($user); | ||
| 666 | |||
| 667 | $self->revoke_cur_user_rights($cur_user->{'rights'} // []); | ||
| 668 | |||
| 669 | $self->set_option('cur_user', $user); | ||
| 670 | |||
| 671 | $self->_fix_cur_user($user); | ||
| 672 | |||
| 673 | return $user; | ||
| 674 | } | ||
| 675 | |||
| 676 | sub _fix_cur_user { | ||
| 677 | my ($self, $cur_user) = @_; | ||
| 678 | |||
| 679 | if (%$cur_user && $self->can('rbac')) { | ||
| 680 | $cur_user->{'roles'} = $self->rbac->get_cur_user_roles(); | ||
| 681 | $cur_user->{'rights'} = [ | ||
| 682 | map {$_->{'right'}} @{ | ||
| 683 | $self->rbac->get_roles_rights( | ||
| 684 | fields => {right => {distinct => ['right']}}, | ||
| 685 | role_id => [keys(%{$cur_user->{'roles'}})] | ||
| 686 | ) | ||
| 687 | } | ||
| 688 | ]; | ||
| 689 | |||
| 690 | $self->set_cur_user_rights($cur_user->{'rights'}); | ||
| 691 | } | ||
| 692 | } | ||
| 693 | |||
| 694 | =head2 set_cur_user_rights | ||
| 695 | |||
| 696 | set rights for current user | ||
| 697 | |||
| 698 | B<Arguments:> | ||
| 699 | |||
| 700 | =over | ||
| 701 | |||
| 702 | =item | ||
| 703 | |||
| 704 | B<$rights> - array ref | ||
| 705 | |||
| 706 | =back | ||
| 707 | |||
| 708 | $app->set_cur_user_rights([qw(RIGHT1 RIGHT2)]); | ||
| 709 | |||
| 710 | =cut | ||
| 711 | |||
| 712 | sub set_cur_user_rights { | ||
| 713 | my ($self, $rights) = @_; | ||
| 714 | |||
| 715 | $self->{'__CURRENT_USER_RIGHTS__'}{$_}++ foreach @$rights; | ||
| 716 | } | ||
| 717 | |||
| 718 | =head2 revoke_cur_user_rights | ||
| 719 | |||
| 720 | revoke rights for current user | ||
| 721 | |||
| 722 | B<Arguments:> | ||
| 723 | |||
| 724 | =over | ||
| 725 | |||
| 726 | =item | ||
| 727 | |||
| 728 | B<$rights> - array ref | ||
| 729 | |||
| 730 | =back | ||
| 731 | |||
| 732 | $app->revoke_cur_user_rights([qw(RIGHT1 RIGHT2)]); | ||
| 733 | |||
| 734 | =cut | ||
| 735 | |||
| 736 | sub revoke_cur_user_rights { | ||
| 737 | my ($self, $rights) = @_; | ||
| 738 | |||
| 739 | foreach (@$rights) { | ||
| 740 | delete($self->{'__CURRENT_USER_RIGHTS__'}{$_}) unless --$self->{'__CURRENT_USER_RIGHTS__'}{$_}; | ||
| 741 | } | ||
| 742 | } | ||
| 743 | |||
| 744 | =head2 refresh_rights | ||
| 745 | |||
| 746 | refresh rights for current user | ||
| 747 | |||
| 748 | my $cur_user_id = $app->cur_user()->{'id'}; | ||
| 749 | |||
| 750 | $app->rbac->set_user_role($cur_user_id, 3); # role_id = 3 | ||
| 751 | |||
| 752 | $app->refresh_rights(); | ||
| 753 | |||
| 754 | =cut | ||
| 755 | |||
| 756 | sub refresh_rights { | ||
| 757 | my ($self) = @_; | ||
| 758 | |||
| 759 | my $cur_user = $self->cur_user(); | ||
| 760 | |||
| 761 | $self->revoke_cur_user_rights($cur_user->{'rights'} // []); | ||
| 762 | |||
| 763 | $self->_fix_cur_user($cur_user); | ||
| 764 | |||
| 765 | return TRUE; | ||
| 766 | } | ||
| 767 | |||
| 768 | =head2 get_models | ||
| 769 | |||
| 770 | Returns all models. | ||
| 771 | |||
| 772 | B<No arguments.> | ||
| 773 | |||
| 774 | B<Return value:> $models - ref of a hash | ||
| 775 | |||
| 776 | B<Examples:> | ||
| 777 | |||
| 778 | my $models = $app->get_models(); | ||
| 779 | |||
| 780 | # $models = { | ||
| 781 | # users => 'Application::Model::Users', | ||
| 782 | # ... | ||
| 783 | # } | ||
| 784 | |||
| 785 | =cut | ||
| 786 | |||
| 787 | sub get_models { | ||
| 788 | my ($self) = @_; | ||
| 789 | |||
| 790 | my $models = {}; | ||
| 791 | |||
| 792 | package_merge_isa_data( | ||
| 793 | ref($self) || $self, $models, | ||
| 794 | sub { | ||
| 795 | my ($package, $res) = @_; | ||
| 796 | |||
| 797 | my $pkg_models = package_stash($package)->{'__MODELS__'} || {}; | ||
| 798 | $models->{$_} = $pkg_models->{$_} foreach keys(%$pkg_models); | ||
| 799 | }, | ||
| 800 | __PACKAGE__ | ||
| 801 | ); | ||
| 802 | |||
| 803 | return $models; | ||
| 804 | } | ||
| 805 | |||
| 806 | =head2 get_registered_rights | ||
| 807 | |||
| 808 | Returns all registered rights | ||
| 809 | |||
| 810 | B<No arguments.> | ||
| 811 | |||
| 812 | B<Return value:> ref of a hash | ||
| 813 | |||
| 814 | B<Example:> | ||
| 815 | |||
| 816 | my $registered_rights = $app->get_registered_rights(); | ||
| 817 | |||
| 818 | # $registered_rights = { | ||
| 819 | # view_all => { | ||
| 820 | # name => 'Right to view all elements', | ||
| 821 | # group => 'elemets' | ||
| 822 | # }, | ||
| 823 | # ... | ||
| 824 | # } | ||
| 825 | |||
| 826 | =cut | ||
| 827 | |||
| 828 | sub get_registered_rights { | ||
| 829 | my ($self) = @_; | ||
| 830 | |||
| 831 | my $rights = {}; | ||
| 832 | package_merge_isa_data( | ||
| 833 | ref($self), | ||
| 834 | $rights, | ||
| 835 | sub { | ||
| 836 | my ($ipackage, $res) = @_; | ||
| 837 | |||
| 838 | my $ipkg_stash = package_stash($ipackage); | ||
| 839 | $res->{'__RIGHTS__'} = {%{$res->{'__RIGHTS__'} || {}}, %{$ipkg_stash->{'__RIGHTS__'} || {}}}; | ||
| 840 | }, | ||
| 841 | __PACKAGE__ | ||
| 842 | ); | ||
| 843 | |||
| 844 | return $rights->{'__RIGHTS__'}; | ||
| 845 | } | ||
| 846 | |||
| 847 | sub get_registred_rights {&get_registered_rights;} | ||
| 848 | |||
| 849 | =head2 get_registered_right_groups | ||
| 850 | |||
| 851 | Returns all registered right groups. | ||
| 852 | |||
| 853 | B<No arguments.> | ||
| 854 | |||
| 855 | B<Return value:> $registered_right_groups - ref of a hash | ||
| 856 | |||
| 857 | B<Example:> | ||
| 858 | |||
| 859 | my $registered_right_groups = $app->get_registered_right_groups(); | ||
| 860 | |||
| 861 | # $registered_right_groups = { | ||
| 862 | # elements => 'Elements', | ||
| 863 | # } | ||
| 864 | |||
| 865 | =cut | ||
| 866 | |||
| 867 | sub get_registered_right_groups { | ||
| 868 | my ($self) = @_; | ||
| 869 | |||
| 870 | my $rights = {}; | ||
| 871 | package_merge_isa_data( | ||
| 872 | ref($self), | ||
| 873 | $rights, | ||
| 874 | sub { | ||
| 875 | my ($ipackage, $res) = @_; | ||
| 876 | |||
| 877 | my $ipkg_stash = package_stash($ipackage); | ||
| 878 | $res->{'__RIGHT_GROUPS__'} = | ||
| 879 | {%{$res->{'__RIGHT_GROUPS__'} || {}}, %{$ipkg_stash->{'__RIGHT_GROUPS__'} || {}}}; | ||
| 880 | }, | ||
| 881 | __PACKAGE__ | ||
| 882 | ); | ||
| 883 | |||
| 884 | return $rights->{'__RIGHT_GROUPS__'}; | ||
| 885 | } | ||
| 886 | |||
| 887 | sub get_registred_right_groups {&get_registered_right_groups;} | ||
| 888 | |||
| 889 | =head2 check_rights | ||
| 890 | |||
| 891 | Check rights for current user. | ||
| 892 | |||
| 893 | B<Arguments:> | ||
| 894 | |||
| 895 | =over | ||
| 896 | |||
| 897 | =item | ||
| 898 | |||
| 899 | B<@rights> - array of strings or array ref | ||
| 900 | |||
| 901 | =back | ||
| 902 | |||
| 903 | B<Return value:> boolean | ||
| 904 | |||
| 905 | B<Example:> | ||
| 906 | |||
| 907 | $app->check_rights('RIGHT1', 'RIGHT2'); # TRUE if has rights 'RIGHT1' and 'RIGHT2' | ||
| 908 | |||
| 909 | $app->check_rights(['RIGHT1', 'RIGHT2']); # TRUE if has rights 'RIGHT1' or 'RIGHT2' | ||
| 910 | |||
| 911 | =cut | ||
| 912 | |||
| 913 | sub check_rights { | ||
| 914 | my ($self, @rights) = @_; | ||
| 915 | |||
| 916 | return FALSE unless @rights; | ||
| 917 | |||
| 918 | foreach (@rights) { | ||
| 919 | return FALSE | ||
| 920 | unless ref($_) | ||
| 921 | ? scalar(grep($self->{'__CURRENT_USER_RIGHTS__'}{$_}, @$_)) | ||
| 922 | : $self->{'__CURRENT_USER_RIGHTS__'}{$_}; | ||
| 923 | } | ||
| 924 | |||
| 925 | return TRUE; | ||
| 926 | } | ||
| 927 | |||
| 928 | =head2 set_app_locale | ||
| 929 | |||
| 930 | Set locale for Application. | ||
| 931 | |||
| 932 | B<Arguments:> | ||
| 933 | |||
| 934 | =over | ||
| 935 | |||
| 936 | =item | ||
| 937 | |||
| 938 | B<$locale_id> - type: string, values: from config (key "locales") | ||
| 939 | |||
| 940 | =back | ||
| 941 | |||
| 942 | B<Example:> | ||
| 943 | |||
| 944 | $app->set_app_locale('ru'); | ||
| 945 | |||
| 946 | =cut | ||
| 947 | |||
| 948 | sub set_app_locale { | ||
| 949 | my ($self, $locale_id) = @_; | ||
| 950 | |||
| 951 | my $locale = $self->get_option('locales', {})->{$locale_id}; | ||
| 952 | throw gettext('Unknown locale') unless defined($locale); | ||
| 953 | throw gettext('Undefined locale code for locale "%s"', $locale_id) unless $locale->{'code'}; | ||
| 954 | |||
| 955 | set_locale( | ||
| 956 | project => $self->get_option('locale_domain', 'application'), | ||
| 957 | path => $self->get_option('ApplicationPath') . '/locale', | ||
| 958 | lang => $locale->{'code'}, | ||
| 959 | ); | ||
| 960 | |||
| 961 | $self->set_option(locale => $locale_id); | ||
| 962 | } | ||
| 963 | |||
| 964 | =head2 set_tmp_app_locale | ||
| 965 | |||
| 966 | Set temporary locale. | ||
| 967 | |||
| 968 | B<Arguments:> | ||
| 969 | |||
| 970 | =over | ||
| 971 | |||
| 972 | =item | ||
| 973 | |||
| 974 | B<$locale_id> - type: string, values: from config (key "locales") | ||
| 975 | |||
| 976 | =back | ||
| 977 | |||
| 978 | B<Return value:> $tmp_locale - object B<QBit::Application::_Utils::TmpLocale> | ||
| 979 | |||
| 980 | B<Example:> | ||
| 981 | |||
| 982 | my $tmp_locale = $app->set_tmp_app_locale('ru'); | ||
| 983 | |||
| 984 | #restore locale | ||
| 985 | undef($tmp_locale); | ||
| 986 | |||
| 987 | =cut | ||
| 988 | |||
| 989 | sub set_tmp_app_locale { | ||
| 990 | my ($self, $locale_id) = @_; | ||
| 991 | |||
| 992 | my $old_locale_id = $self->get_option('locale'); | ||
| 993 | $self->set_app_locale($locale_id); | ||
| 994 | |||
| 995 | return QBit::Application::_Utils::TmpLocale->new(app => $self, old_locale => $old_locale_id); | ||
| 996 | } | ||
| 997 | |||
| 998 | =head2 add_tmp_rights | ||
| 999 | |||
| 1000 | Add temporary rights. | ||
| 1001 | |||
| 1002 | B<Arguments:> | ||
| 1003 | |||
| 1004 | =over | ||
| 1005 | |||
| 1006 | =item | ||
| 1007 | |||
| 1008 | B<@rights> - Rights (type: array of a string) | ||
| 1009 | |||
| 1010 | =back | ||
| 1011 | |||
| 1012 | B<Return value:> $tmp_rights - object B<QBit::Application::_Utils::TmpRights> | ||
| 1013 | |||
| 1014 | B<Example:> | ||
| 1015 | |||
| 1016 | my $tmp_rights = $app->add_tmp_rights('view_all', 'edit_all'); | ||
| 1017 | |||
| 1018 | #restore rights | ||
| 1019 | undef($tmp_rights); | ||
| 1020 | |||
| 1021 | =cut | ||
| 1022 | |||
| 1023 | sub add_tmp_rights { | ||
| 1024 | my ($self, @rights) = @_; | ||
| 1025 | |||
| 1026 | return QBit::Application::_Utils::TmpRights->new(app => $self, rights => \@rights); | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | =head2 pre_run | ||
| 1030 | |||
| 1031 | Called before the request is processed. | ||
| 1032 | |||
| 1033 | B<It is done:> | ||
| 1034 | |||
| 1035 | =over | ||
| 1036 | |||
| 1037 | =item | ||
| 1038 | |||
| 1039 | Resets current user | ||
| 1040 | |||
| 1041 | =item | ||
| 1042 | |||
| 1043 | Refresh options | ||
| 1044 | |||
| 1045 | =item | ||
| 1046 | |||
| 1047 | Resets timelog | ||
| 1048 | |||
| 1049 | =item | ||
| 1050 | |||
| 1051 | Call "pre_run" for models | ||
| 1052 | |||
| 1053 | =back | ||
| 1054 | |||
| 1055 | B<No arguments.> | ||
| 1056 | |||
| 1057 | B<Example:> | ||
| 1058 | |||
| 1059 | $app->pre_run(); | ||
| 1060 | |||
| 1061 | =cut | ||
| 1062 | |||
| 1063 | sub pre_run { | ||
| 1064 | my ($self) = @_; | ||
| 1065 | |||
| 1066 | $self->{'__CURRENT_USER_RIGHTS__'} = {}; | ||
| 1067 | |||
| 1068 | $self->{'__OPTIONS__'} = clone($self->{'__ORIG_OPTIONS__'}); | ||
| 1069 | |||
| 1070 | unless (exists($self->{'__TIMELOG_CLASS__'})) { | ||
| 1071 | my $tl_package = $self->{'__TIMELOG_CLASS__'} = $self->get_option('timelog_class', 'QBit::TimeLog'); | ||
| 1072 | |||
| 1073 | $tl_package =~ s/::/\//g; | ||
| 1074 | $tl_package .= '.pm'; | ||
| 1075 | require $tl_package; | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | $self->{'timelog'} = $self->{'__TIMELOG_CLASS__'}->new(); | ||
| 1079 | $self->{'timelog'}->start(gettext('Total application run time')); | ||
| 1080 | |||
| 1081 | foreach (keys(%{$self->get_models()})) { | ||
| 1082 | $self->$_->pre_run() if exists($self->{$_}) && $self->{$_}->can('pre_run'); | ||
| 1083 | } | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | =head2 post_run | ||
| 1087 | |||
| 1088 | Called after the request is processed. | ||
| 1089 | |||
| 1090 | B<It is done:> | ||
| 1091 | |||
| 1092 | =over | ||
| 1093 | |||
| 1094 | =item | ||
| 1095 | |||
| 1096 | Call "post_run" for models | ||
| 1097 | |||
| 1098 | =item | ||
| 1099 | |||
| 1100 | Finish timelog | ||
| 1101 | |||
| 1102 | =item | ||
| 1103 | |||
| 1104 | Call "process_timelog" | ||
| 1105 | |||
| 1106 | =item | ||
| 1107 | |||
| 1108 | Find memory cycles and call "process_mem_cycles" if needed | ||
| 1109 | |||
| 1110 | =back | ||
| 1111 | |||
| 1112 | B<No arguments.> | ||
| 1113 | |||
| 1114 | B<Example:> | ||
| 1115 | |||
| 1116 | $app->post_run(); | ||
| 1117 | |||
| 1118 | =cut | ||
| 1119 | |||
| 1120 | sub post_run { | ||
| 1121 | my ($self) = @_; | ||
| 1122 | |||
| 1123 | foreach (keys(%{$self->get_models()})) { | ||
| 1124 | $self->$_->post_run() if exists($self->{$_}) && $self->{$_}->can('post_run'); | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | $self->timelog->finish(); | ||
| 1128 | $self->process_timelog($self->timelog); | ||
| 1129 | |||
| 1130 | if ($self->get_option('find_app_mem_cycle')) { | ||
| 1131 | if (eval {require 'Devel/Cycle.pm'}) { | ||
| 1132 | Devel::Cycle->import(); | ||
| 1133 | my @cycles; | ||
| 1134 | Devel::Cycle::find_cycle($self, sub {push(@cycles, shift)}); | ||
| 1135 | $self->process_mem_cycles(\@cycles) if @cycles; | ||
| 1136 | } else { | ||
| 1137 | l(gettext('Devel::Cycle is not installed')); | ||
| 1138 | } | ||
| 1139 | } | ||
| 1140 | } | ||
| 1141 | |||
| 1142 | =head2 process_mem_cycles | ||
| 1143 | |||
| 1144 | Process memory cycles | ||
| 1145 | |||
| 1146 | B<Arguments:> | ||
| 1147 | |||
| 1148 | =over | ||
| 1149 | |||
| 1150 | =item | ||
| 1151 | |||
| 1152 | B<$cycles> - Cycles. (result: B<Devel::Cycle::find_cycle>) | ||
| 1153 | |||
| 1154 | =back | ||
| 1155 | |||
| 1156 | B<Return value:> $text - info (type: string) | ||
| 1157 | |||
| 1158 | =cut | ||
| 1159 | |||
| 1160 | sub process_mem_cycles { | ||
| 1161 | my ($self, $cycles) = @_; | ||
| 1162 | |||
| 1163 | my $counter = 0; | ||
| 1164 | my $text = ''; | ||
| 1165 | foreach my $path (@$cycles) { | ||
| 1166 | $text .= gettext('Cycle (%s):', ++$counter) . "\n"; | ||
| 1167 | foreach (@$path) { | ||
| 1168 | my ($type, $index, $ref, $value, $is_weak) = @$_; | ||
| 1169 | $text .= gettext( | ||
| 1170 | "\t%30s => %-30s\n", | ||
| 1171 | ($is_weak ? 'w-> ' : '') . Devel::Cycle::_format_reference($type, $index, $ref, 0), | ||
| 1172 | Devel::Cycle::_format_reference(undef, undef, $value, 1) | ||
| 1173 | ); | ||
| 1174 | } | ||
| 1175 | $text .= "\n"; | ||
| 1176 | } | ||
| 1177 | |||
| 1178 | l($text); | ||
| 1179 | return $text; | ||
| 1180 | } | ||
| 1181 | |||
| 1182 | =head2 process_timelog | ||
| 1183 | |||
| 1184 | Process time log. Empty method. | ||
| 1185 | |||
| 1186 | B<No arguments.> | ||
| 1187 | |||
| 1188 | =cut | ||
| 1189 | |||
| 1190 | sub process_timelog { } | ||
| 1191 | |||
| 1192 | sub _push_pkg_opts { | ||
| 1193 | my ($self, $filename, $config) = @_; | ||
| 1194 | |||
| 1195 | my $pkg_stash = package_stash(ref($self) || $self); | ||
| 1196 | |||
| 1197 | $pkg_stash->{'__OPTIONS__'} = [] | ||
| 1198 | unless exists($pkg_stash->{'__OPTIONS__'}); | ||
| 1199 | |||
| 1200 | push( | ||
| 1201 | @{$pkg_stash->{'__OPTIONS__'}}, | ||
| 1202 | { | ||
| 1203 | filename => $filename, | ||
| 1204 | config => $config, | ||
| 1205 | } | ||
| 1206 | ); | ||
| 1207 | } | ||
| 1208 | |||
| 1209 | TRUE; |