| 1 | package Mojolicious::Plugin::RoutesAuthDBI::Admin; | ||
| 2 | use Mojo::Base 'Mojolicious::Controller'; | ||
| 3 | #~ use Mojo::Util qw(md5_sum); | ||
| 4 | |||
| 5 | my $pkg = __PACKAGE__; | ||
| 6 | my ($Init); | ||
| 7 | has [qw(plugin controller namespace prefix trust role_admin)]; | ||
| 8 | |||
| 9 | sub init {# from plugin! init Class vars | ||
| 10 | state $self = shift->SUPER::new(@_); | ||
| 11 | $Init = $self; | ||
| 12 | return $self; | ||
| 13 | } | ||
| 14 | |||
| 15 | |||
| 16 | sub index { | ||
| 17 | my $c = shift; | ||
| 18 | no warnings; | ||
| 19 | $c->render(format=>'txt', text=><<TXT) | ||
| 20 | $pkg | ||
| 21 | |||
| 22 | You are signed as: | ||
| 23 | @{[$c->dumper( $c->auth_user)]} | ||
| 24 | |||
| 25 | |||
| 26 | ADMIN ROUTES (this controller) | ||
| 27 | === | ||
| 28 | |||
| 29 | @{[map "$_->{request}\t\t$_->{descr}\n", $c->self_routes]} | ||
| 30 | |||
| 31 | TXT | ||
| 32 | and return | ||
| 33 | if $c->is_user_authenticated; | ||
| 34 | |||
| 35 | $c->render(format=>'txt', text=>__PACKAGE__."\n\nYou are not signed!!!\n\nTo sign in go to /sign/in/<login>/<pass>\n"); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub sign { | ||
| 39 | my $c = shift; | ||
| 40 | |||
| 41 | $c->authenticate($c->vars(qw'login pass')) | ||
| 42 | and $c->redirect_to("admin home") | ||
| 43 | #~ and $c->render(format=>'txt', text=>__PACKAGE__ . "\n\nSuccessfull signed! ".$c->dumper( $c->auth_user)) | ||
| 44 | and return; | ||
| 45 | |||
| 46 | |||
| 47 | $c->render(format=>'txt', text=>__PACKAGE__ . "\n\nBad sign! Try again"); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub signout { | ||
| 51 | my $c = shift; | ||
| 52 | |||
| 53 | $c->logout; | ||
| 54 | |||
| 55 | $c->render(format=>'txt', text=>__PACKAGE__ . "\n\nYou are exited!!!"); | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | sub out {# выход с редиректом | ||
| 60 | my $c = shift; | ||
| 61 | $c->logout; | ||
| 62 | $c->redirect_to($c->param('redirect') || '/'); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub users { | ||
| 66 | my $c = shift; | ||
| 67 | |||
| 68 | my $p = $Init->plugin->model('Profiles')->profiles; | ||
| 69 | $c->render(format=>'txt', text=><<TXT) | ||
| 70 | $pkg | ||
| 71 | |||
| 72 | Profiles(@{[scalar @$p]}) | ||
| 73 | === | ||
| 74 | |||
| 75 | @{[$c->dumper( $p)]} | ||
| 76 | TXT | ||
| 77 | } | ||
| 78 | |||
| 79 | sub new_user { | ||
| 80 | my $c = shift; | ||
| 81 | |||
| 82 | my ($login, $pass) = $c->vars(qw'login pass'); | ||
| 83 | |||
| 84 | my $r; | ||
| 85 | ($r = $Init->plugin->model('Profiles')->get_profile(undef, $login)) | ||
| 86 | and $c->render(format=>'txt', text=><<TXT) | ||
| 87 | $pkg | ||
| 88 | |||
| 89 | Profile/User already exists | ||
| 90 | === | ||
| 91 | |||
| 92 | @{[$c->dumper( $r)]} | ||
| 93 | TXT | ||
| 94 | and ($r->{not_new} = '!') | ||
| 95 | and return $r; | ||
| 96 | |||
| 97 | $r = $Init->plugin->model('Logins')->new_login($login, $pass); | ||
| 98 | |||
| 99 | my $p = $Init->plugin->model('Profiles')->new_profile([$login],); | ||
| 100 | |||
| 101 | $Init->plugin->model('Refs')->refer($p->{id} => $r->{id}); | ||
| 102 | |||
| 103 | @$p{qw(login pass)} = @$r{qw(login pass)}; | ||
| 104 | |||
| 105 | $c->render(format=>'txt', text=><<TXT); | ||
| 106 | $pkg | ||
| 107 | |||
| 108 | Success sign up new profile/user | ||
| 109 | === | ||
| 110 | |||
| 111 | @{[$c->dumper( $p)]} | ||
| 112 | TXT | ||
| 113 | return $p; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub trust_new_user { | ||
| 117 | my $c = shift; | ||
| 118 | |||
| 119 | my $u = $c->new_user; | ||
| 120 | |||
| 121 | # ROLE | ||
| 122 | my $rl = $Init->plugin->model('Roles')->get_role(undef, $Init->role_admin || $Init->controller) | ||
| 123 | || $Init->plugin->model('Roles')->new_role($Init->role_admin || $Init->controller); | ||
| 124 | |||
| 125 | # REF role->user | ||
| 126 | my $ru = $Init->plugin->model('Refs')->refer($rl->{id} => $u->{id}); | ||
| 127 | |||
| 128 | # CONTROLLER | ||
| 129 | my $cc = $Init->plugin->model('Controllers')->controller_ns($Init->{controller}, ($Init->{namespace}) x 2,) | ||
| 130 | || $Init->plugin->model('Controllers')->new_controller($Init->{controller}, 'admin actions'); | ||
| 131 | |||
| 132 | #Namespace | ||
| 133 | my $ns = $Init->plugin->model('Namespaces')->namespace(undef, $Init->{namespace},) | ||
| 134 | || $Init->plugin->model('Namespaces')->new_namespace($Init->{namespace}, 'plugin ns!', undef, undef,); | ||
| 135 | |||
| 136 | #ref namespace -> controller | ||
| 137 | my $nc = $Init->plugin->model('Refs')->refer($ns->{id}, $cc->{id}); | ||
| 138 | |||
| 139 | #REF namespace->role | ||
| 140 | my $cr = $Init->plugin->model('Refs')->refer($ns->{id}, $rl->{id}); | ||
| 141 | |||
| 142 | $c->render(format=>'txt', text=><<TXT); | ||
| 143 | $pkg | ||
| 144 | |||
| 145 | Success sign up new trust-admin-user with whole access to namespace=[$Init->{namespace}] | ||
| 146 | === | ||
| 147 | |||
| 148 | USER: | ||
| 149 | @{[$c->dumper( $u)]} | ||
| 150 | |||
| 151 | ROLE: | ||
| 152 | @{[$c->dumper( $rl)]} | ||
| 153 | |||
| 154 | CONTROLLER: | ||
| 155 | @{[$c->dumper( $cc)]} | ||
| 156 | |||
| 157 | NAMESPACE: | ||
| 158 | @{[$c->dumper( $ns)]} | ||
| 159 | |||
| 160 | TXT | ||
| 161 | } | ||
| 162 | |||
| 163 | sub new_role { | ||
| 164 | my $c = shift; | ||
| 165 | my ($name) = $c->vars('name'); | ||
| 166 | my $r = $Init->plugin->model('Roles')->get_role(undef, $name); | ||
| 167 | $c->render(format=>'txt', text=><<TXT) | ||
| 168 | $pkg | ||
| 169 | |||
| 170 | Role exists | ||
| 171 | === | ||
| 172 | |||
| 173 | @{[$c->dumper( $r)]} | ||
| 174 | |||
| 175 | TXT | ||
| 176 | and return $c | ||
| 177 | if $r; | ||
| 178 | $r = $Init->plugin->model('Roles')->new_role($name); | ||
| 179 | |||
| 180 | $c->render(format=>'txt', text=><<TXT); | ||
| 181 | $pkg | ||
| 182 | |||
| 183 | Success created role | ||
| 184 | === | ||
| 185 | |||
| 186 | @{[$c->dumper( $r)]} | ||
| 187 | |||
| 188 | TXT | ||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | sub user_roles { | ||
| 193 | my $c = shift; | ||
| 194 | my ($user) = $c->vars(qw'user login');# || $c->vars('login'); | ||
| 195 | my $u = $Init->plugin->model('Profiles')->get_profile($user =~ /\D/ ? (undef, $user) : ($user, undef,)); | ||
| 196 | |||
| 197 | $c->render(format=>'txt', text=><<TXT) | ||
| 198 | $pkg | ||
| 199 | |||
| 200 | No such profile.id/login [$user] | ||
| 201 | === | ||
| 202 | |||
| 203 | TXT | ||
| 204 | and return | ||
| 205 | unless $u; | ||
| 206 | |||
| 207 | my $r = $u->roles; | ||
| 208 | |||
| 209 | $c->render(format=>'txt', text=><<TXT); | ||
| 210 | $pkg | ||
| 211 | |||
| 212 | PROFILE+LOGIN | ||
| 213 | --- | ||
| 214 | @{[$c->dumper( $u)]} | ||
| 215 | |||
| 216 | List of profile/login roles (@{[scalar @$r]}) | ||
| 217 | === | ||
| 218 | |||
| 219 | ROLES | ||
| 220 | --- | ||
| 221 | @{[$c->dumper( $r)]} | ||
| 222 | |||
| 223 | TXT | ||
| 224 | |||
| 225 | } | ||
| 226 | |||
| 227 | sub roles { | ||
| 228 | my $c = shift; | ||
| 229 | my $r = $Init->plugin->model('Roles')->roles; | ||
| 230 | $c->render(format=>'txt', text=><<TXT); | ||
| 231 | $pkg | ||
| 232 | |||
| 233 | ROLES(@{[scalar @$r]}) | ||
| 234 | === | ||
| 235 | --- | ||
| 236 | @{[$c->dumper( $r)]} | ||
| 237 | |||
| 238 | TXT | ||
| 239 | } | ||
| 240 | |||
| 241 | sub new_role_user { | ||
| 242 | my $c = shift; | ||
| 243 | |||
| 244 | my ($role) = $c->vars('role'); | ||
| 245 | # ROLE | ||
| 246 | my $r = $Init->plugin->model('Roles')->get_role($role =~ /\D/ ? (undef, $role) : ($role, undef,)); | ||
| 247 | $c->render(format=>'txt', text=><<TXT) | ||
| 248 | $pkg | ||
| 249 | |||
| 250 | Can't create new role by only digits[$role] in name | ||
| 251 | === | ||
| 252 | |||
| 253 | TXT | ||
| 254 | and return | ||
| 255 | unless $r && $role =~ /\w/; | ||
| 256 | $r ||= $Init->plugin->model('Roles')->new_role($role); | ||
| 257 | |||
| 258 | my ($user) = $c->vars('user'); | ||
| 259 | my $u = $Init->plugin->model('Profiles')->get_profile($user =~ /\D/ ? (undef, $user) : ($user, undef,)); | ||
| 260 | |||
| 261 | $c->render(format=>'txt', text=><<TXT) | ||
| 262 | $pkg | ||
| 263 | |||
| 264 | No such profile.id/login [$user] | ||
| 265 | === | ||
| 266 | |||
| 267 | TXT | ||
| 268 | and return | ||
| 269 | unless $u; | ||
| 270 | |||
| 271 | my $ref = $Init->plugin->model('Refs')->refer($r->{id} => $u->{id}); | ||
| 272 | |||
| 273 | $c->render(format=>'txt', text=><<TXT); | ||
| 274 | $pkg | ||
| 275 | |||
| 276 | Success assign ROLE[$r->{name}] -> USER [@{[$c->dumper( $u) =~ s/\s+//gr]}] | ||
| 277 | === | ||
| 278 | |||
| 279 | @{[$c->dumper( $ref)]} | ||
| 280 | TXT | ||
| 281 | |||
| 282 | } | ||
| 283 | |||
| 284 | # доступ к контроллеру | ||
| 285 | sub new_role_controller { | ||
| 286 | my $c = shift; | ||
| 287 | |||
| 288 | my ($role) = $c->vars('role'); | ||
| 289 | # ROLE | ||
| 290 | my $r = $Init->plugin->model('Roles')->get_role($role =~ /\D/ ? (undef, $role) : ($role, undef,)); | ||
| 291 | $c->render(format=>'txt', text=><<TXT) | ||
| 292 | $pkg | ||
| 293 | |||
| 294 | Can't create new role by only digits[$role] in name | ||
| 295 | === | ||
| 296 | |||
| 297 | TXT | ||
| 298 | and return | ||
| 299 | unless $r && $role =~ /\w/; | ||
| 300 | $r ||= $Init->plugin->model('Roles')->new_role($role); | ||
| 301 | |||
| 302 | my ($ns, $controll) = $c->vars(qw'ns controll'); | ||
| 303 | my $cntr = $Init->plugin->model('Controllers')->controller_id_ns($controll =~ /\D/ ? (undef, $controll) : ($controll, undef), $ns && $ns =~ /\D/ ? (undef, $ns) : ($ns, undef), $ns); | ||
| 304 | |||
| 305 | $c->render(format=>'txt', text=><<TXT) | ||
| 306 | $pkg | ||
| 307 | |||
| 308 | No such controller [$ns::$controll] | ||
| 309 | === | ||
| 310 | |||
| 311 | TXT | ||
| 312 | and return | ||
| 313 | unless $cntr; | ||
| 314 | |||
| 315 | my $ref = $Init->plugin->model('Refs')->refer($cntr->{id} => $r->{id},); | ||
| 316 | $c->render(format=>'txt', text=><<TXT); | ||
| 317 | $pkg | ||
| 318 | |||
| 319 | Success assign access | ||
| 320 | |||
| 321 | CONTROLLER | ||
| 322 | === | ||
| 323 | @{[$c->dumper( $cntr) ]} | ||
| 324 | |||
| 325 | ROLE | ||
| 326 | === | ||
| 327 | @{[$c->dumper( $r)]} | ||
| 328 | |||
| 329 | REF | ||
| 330 | === | ||
| 331 | @{[$c->dumper( $ref)]} | ||
| 332 | TXT | ||
| 333 | } | ||
| 334 | |||
| 335 | sub del_role_user {# удалить связь пользователя с ролью | ||
| 336 | my $c = shift; | ||
| 337 | |||
| 338 | my ($role) = $c->vars('role'); | ||
| 339 | # ROLE | ||
| 340 | my $r = $Init->plugin->model('Roles')->get_role($role =~ /\D/ ? (undef, $role) : ($role, undef,)); | ||
| 341 | $c->render(format=>'txt', text=><<TXT) | ||
| 342 | $pkg | ||
| 343 | |||
| 344 | No such role [$role] | ||
| 345 | === | ||
| 346 | |||
| 347 | TXT | ||
| 348 | and return | ||
| 349 | unless $r; | ||
| 350 | |||
| 351 | my ($user) = $c->vars('user'); | ||
| 352 | my $u = $Init->plugin->model('Profiles')->get_profile($user =~ /\D/ ? (undef, $user) : ($user, undef,)); | ||
| 353 | |||
| 354 | $c->render(format=>'txt', text=><<TXT) | ||
| 355 | $pkg | ||
| 356 | |||
| 357 | No such profile.id/login [$user] | ||
| 358 | === | ||
| 359 | |||
| 360 | TXT | ||
| 361 | and return | ||
| 362 | unless $u; | ||
| 363 | |||
| 364 | my $ref = $Init->plugin->model('Refs')->del(undef, $r->{id}, $u->{id}); | ||
| 365 | $c->render(format=>'txt', text=><<TXT) | ||
| 366 | $pkg | ||
| 367 | |||
| 368 | Success delete ref ROLE[$r->{name}] -> USER[@{[$c->dumper( $u) =~ s/\s+//gr]}] | ||
| 369 | === | ||
| 370 | |||
| 371 | @{[$c->dumper( $ref)]} | ||
| 372 | TXT | ||
| 373 | and return | ||
| 374 | if $ref; | ||
| 375 | |||
| 376 | $c->render(format=>'txt', text=><<TXT) | ||
| 377 | $pkg | ||
| 378 | |||
| 379 | There is no ref ROLE[$r->{name}] -> USER[@{[$c->dumper( $u) =~ s/\s+//gr]}] | ||
| 380 | |||
| 381 | TXT | ||
| 382 | |||
| 383 | } | ||
| 384 | |||
| 385 | sub disable_role { | ||
| 386 | my $c = shift; | ||
| 387 | my $a = shift // 1; # 0-enable 1 - disable | ||
| 388 | my $k = {0=>'enable', 1=>'disable',}; | ||
| 389 | |||
| 390 | my ($role) = $c->vars('role'); | ||
| 391 | # ROLE | ||
| 392 | my $r = $Init->plugin->model('Roles')->dsbl_enbl($a, $role =~ /\D/ ? (undef, $role) : ($role, undef,)); | ||
| 393 | $c->render(format=>'txt', text=><<TXT) | ||
| 394 | $pkg | ||
| 395 | |||
| 396 | No such role [$role] | ||
| 397 | === | ||
| 398 | |||
| 399 | TXT | ||
| 400 | and return | ||
| 401 | unless $r; | ||
| 402 | |||
| 403 | $c->render(format=>'txt', text=><<TXT) | ||
| 404 | $pkg | ||
| 405 | |||
| 406 | Success @{[$k->{$a}]} role | ||
| 407 | === | ||
| 408 | |||
| 409 | @{[$c->dumper( $r)]} | ||
| 410 | |||
| 411 | TXT | ||
| 412 | } | ||
| 413 | |||
| 414 | sub enable_role {shift->disable_role(0);} | ||
| 415 | |||
| 416 | |||
| 417 | sub role_users {# все пользователи роли по запросу /myadmin/users/:role | ||
| 418 | my $c = shift; | ||
| 419 | |||
| 420 | my ($role) = $c->vars('role'); | ||
| 421 | # ROLE | ||
| 422 | my $r = $Init->plugin->model('Roles')->get_role($role =~ /\D/ ? (undef, $role) : ($role, undef,)); | ||
| 423 | $c->render(format=>'txt', text=><<TXT) | ||
| 424 | $pkg | ||
| 425 | |||
| 426 | No such role [$role] | ||
| 427 | === | ||
| 428 | |||
| 429 | TXT | ||
| 430 | and return | ||
| 431 | unless $r; | ||
| 432 | |||
| 433 | my $u = $Init->plugin->model('Roles')->profiles($r->{id}); | ||
| 434 | $c->render(format=>'txt', text=><<TXT); | ||
| 435 | $pkg | ||
| 436 | |||
| 437 | Profile/users(@{[scalar @$u]}) by role [$r->{name}] | ||
| 438 | === | ||
| 439 | |||
| 440 | @{[$c->dumper( $u)]} | ||
| 441 | TXT | ||
| 442 | } | ||
| 443 | |||
| 444 | sub role_routes {# все маршруты роли по запросу /myadmin/routes/:role | ||
| 445 | my $c = shift; | ||
| 446 | |||
| 447 | my ($role) = $c->vars('role'); | ||
| 448 | # ROLE | ||
| 449 | my $r = $Init->plugin->model('Roles')->get_role($role =~ /\D/ ? (undef, $role) : ($role, undef,)); | ||
| 450 | $c->render(format=>'txt', text=><<TXT) | ||
| 451 | $pkg | ||
| 452 | |||
| 453 | No such role [$role]! | ||
| 454 | |||
| 455 | TXT | ||
| 456 | and return | ||
| 457 | unless $r; | ||
| 458 | |||
| 459 | my $t = $Init->plugin->model('Routes')->routes_ref($r->{id}); | ||
| 460 | $c->render(format=>'txt', text=><<TXT); | ||
| 461 | $pkg | ||
| 462 | |||
| 463 | Total @{[scalar @$t]} routes by role [$r->{name}] | ||
| 464 | |||
| 465 | @{[$c->dumper( $t)]} | ||
| 466 | TXT | ||
| 467 | } | ||
| 468 | |||
| 469 | sub controllers { | ||
| 470 | my $c = shift; | ||
| 471 | my $list = $Init->plugin->model('Controllers')->controllers; | ||
| 472 | $c->render(format=>'txt', text=><<TXT); | ||
| 473 | $pkg | ||
| 474 | |||
| 475 | CONTROLLERS (@{[scalar @$list]}) | ||
| 476 | === | ||
| 477 | |||
| 478 | @{[$c->dumper( $list)]} | ||
| 479 | TXT | ||
| 480 | } | ||
| 481 | |||
| 482 | sub controll {# /controller/:ns/:controll | ||
| 483 | my $c = shift; | ||
| 484 | my ($ns, $controll) = $c->vars(qw(ns controll)); | ||
| 485 | my $r = $Init->plugin->model('Controllers')->controller_id_ns($controll =~ /\D/ ? (undef, $controll) : ($controll, undef), $ns && $ns =~ /\D/ ? (undef, $ns) : ($ns, undef), $ns); | ||
| 486 | $c->render(format=>'txt', text=><<TXT); | ||
| 487 | $pkg | ||
| 488 | |||
| 489 | CONTROLLER | ||
| 490 | === | ||
| 491 | |||
| 492 | @{[$c->dumper( $r)]} | ||
| 493 | TXT | ||
| 494 | } | ||
| 495 | |||
| 496 | sub new_controller { | ||
| 497 | my $c = shift; | ||
| 498 | #~ my $ns = $c->stash('ns') || $c->param('ns') || $c->stash('namespace') || $c->param('namespace'); | ||
| 499 | my ($ns) = $c->vars(qw'ns namespace');# || ($c->vars('namespace')); | ||
| 500 | my ($mod) = $c->vars(qw'module controll'); | ||
| 501 | my ($descr) = $c->vars(qw'descr'); | ||
| 502 | my $cn = $Init->plugin->model('Controllers')->controller_ns($mod, ($ns) x 2,); | ||
| 503 | $c->render(format=>'txt', text=><<TXT) | ||
| 504 | $pkg | ||
| 505 | |||
| 506 | Controller already exists | ||
| 507 | === | ||
| 508 | |||
| 509 | @{[$c->dumper( $cn)]} | ||
| 510 | TXT | ||
| 511 | and return | ||
| 512 | if $cn; | ||
| 513 | my $n = $c->new_namespace($ns) if $ns; | ||
| 514 | $cn = $Init->plugin->model('Controllers')->new_controller($mod, $descr); | ||
| 515 | $Init->plugin->model('Refs')->refer($n->{id}, $cn->{id}) | ||
| 516 | if $n; | ||
| 517 | |||
| 518 | $cn = $Init->plugin->model('Controllers')->controller_ns($mod, ($ns) x 2,); | ||
| 519 | |||
| 520 | $c->render(format=>'txt', text=><<TXT); | ||
| 521 | $pkg | ||
| 522 | |||
| 523 | Success create new controller | ||
| 524 | === | ||
| 525 | |||
| 526 | @{[$c->dumper( $cn)]} | ||
| 527 | TXT | ||
| 528 | return $cn; | ||
| 529 | } | ||
| 530 | |||
| 531 | sub namespaces { | ||
| 532 | my $c = shift; | ||
| 533 | my $list = $Init->plugin->model('Namespaces')->namespaces; | ||
| 534 | $c->render(format=>'txt', text=><<TXT); | ||
| 535 | $pkg | ||
| 536 | |||
| 537 | Namespaces (@{[scalar @$list]}) | ||
| 538 | === | ||
| 539 | |||
| 540 | @{[$c->dumper( $list)]} | ||
| 541 | TXT | ||
| 542 | } | ||
| 543 | |||
| 544 | sub new_namespace { | ||
| 545 | my $c = shift; | ||
| 546 | my ($ns) = $_[0] ? (shift) : $c->vars('ns'); | ||
| 547 | my ($descr) = $_[0] ? (shift) : $c->vars('descr'); | ||
| 548 | my ($app_ns) = $_[0] ? (shift) : $c->vars('app_ns'); | ||
| 549 | my ($interval_ts) = $_[0] ? (shift) : $c->vars('interval_ts'); | ||
| 550 | my $n = $Init->plugin->model('Namespaces')->namespace($ns =~ /\D/ ? (undef, $ns) : ($ns, undef,)); | ||
| 551 | $c->render(format=>'txt', text=><<TXT) | ||
| 552 | $pkg | ||
| 553 | |||
| 554 | Namespace already exists | ||
| 555 | === | ||
| 556 | |||
| 557 | @{[$c->dumper( $n)]} | ||
| 558 | TXT | ||
| 559 | and return $n | ||
| 560 | if $n; | ||
| 561 | $n = $Init->plugin->model('Namespaces')->new_namespace($ns, $descr, $app_ns, $interval_ts); | ||
| 562 | $c->render(format=>'txt', text=><<TXT); | ||
| 563 | $pkg | ||
| 564 | |||
| 565 | Success create new namespace | ||
| 566 | === | ||
| 567 | |||
| 568 | @{[$c->dumper( $n)]} | ||
| 569 | TXT | ||
| 570 | return $n; | ||
| 571 | |||
| 572 | } | ||
| 573 | |||
| 574 | sub actions { | ||
| 575 | my $c = shift; | ||
| 576 | my $list = $Init->plugin->model('Actions')->actions; | ||
| 577 | map { | ||
| 578 | $_->{routes} = $Init->plugin->model('Routes')->routes_action($_->{id}); | ||
| 579 | } @$list; | ||
| 580 | $c->render(format=>'txt', text=><<TXT); | ||
| 581 | $pkg | ||
| 582 | |||
| 583 | ACTIONS list (@{[scalar @$list]}) | ||
| 584 | === | ||
| 585 | |||
| 586 | @{[$c->dumper( $list )]} | ||
| 587 | TXT | ||
| 588 | } | ||
| 589 | |||
| 590 | sub routes { | ||
| 591 | my $c = shift; | ||
| 592 | my $list = $Init->plugin->model('Routes')->routes; | ||
| 593 | |||
| 594 | $c->render(format=>'txt', text=><<TXT); | ||
| 595 | $pkg | ||
| 596 | |||
| 597 | ROUTES (@{[scalar @$list]}) | ||
| 598 | === | ||
| 599 | |||
| 600 | @{[$c->dumper( $list )]} | ||
| 601 | TXT | ||
| 602 | } | ||
| 603 | |||
| 604 | sub new_route_ns {# показать список мест-имен | ||
| 605 | my $c = shift; | ||
| 606 | my $list = $Init->plugin->model('Namespaces')->namespaces; | ||
| 607 | $c->render(format=>'txt', text=><<TXT); | ||
| 608 | $pkg | ||
| 609 | |||
| 610 | 1. Для нового маршрута укажите имя namespace или его ID или undef. | ||
| 611 | Новый можно ввести. | ||
| 612 | |||
| 613 | Namespaces (@{[scalar @$list]}) | ||
| 614 | === | ||
| 615 | |||
| 616 | @{[$c->dumper( $list )]} | ||
| 617 | TXT | ||
| 618 | } | ||
| 619 | |||
| 620 | sub new_route_c {# показать список контроллеров | ||
| 621 | no warnings; | ||
| 622 | my $c = shift; | ||
| 623 | my ($ns) = $c->vars('ns'); | ||
| 624 | $ns = $Init->plugin->model('Namespaces')->namespace($ns && $ns =~ /\D/ ? (undef, $ns) : ($ns, undef,)) | ||
| 625 | || {namespace => $ns}; | ||
| 626 | my $list = $Init->plugin->model('Controllers')->controllers_ns_id(($ns->{id}) x 2); | ||
| 627 | $c->render(format=>'txt', text=><<TXT); | ||
| 628 | $pkg | ||
| 629 | |||
| 630 | 1. namespace = [@{[$c->dumper( $ns )]}] | ||
| 631 | |||
| 632 | Указать имя или ID контроллера или ввести новое имя | ||
| 633 | |||
| 634 | Если указать undef для последующей неявной привязкой через параметр маршрута - "to" | ||
| 635 | |||
| 636 | Controllers (@{[scalar @$list]}) | ||
| 637 | === | ||
| 638 | |||
| 639 | @{[$c->dumper( $list )]} | ||
| 640 | TXT | ||
| 641 | } | ||
| 642 | |||
| 643 | sub new_route_a {# показать список действий | ||
| 644 | no warnings; | ||
| 645 | my $c = shift; | ||
| 646 | my ($ns, $controll) = $c->vars(qw'ns controll'); | ||
| 647 | |||
| 648 | $ns = ($ns && $Init->plugin->model('Namespaces')->namespace($ns && $ns =~ /\D/ ? (undef, $ns) : ($ns, undef,))) | ||
| 649 | || {namespace => $ns}; | ||
| 650 | |||
| 651 | $controll = ($controll && $Init->plugin->model('Controllers')->controller_id_ns($controll =~ /\D/ ? (undef, $controll) : ($controll, undef,), $ns->{id}, $ns->{namespace}, $ns->{namespace},)) | ||
| 652 | || {controller=>$controll}; | ||
| 653 | |||
| 654 | my $list = $controll->{id} ? $Init->plugin->model('Actions')->actions_controller($controll->{id}) : ['контроллер пропускается']; | ||
| 655 | my $list2 = $Init->plugin->model('Actions')->actions_controller_null(); | ||
| 656 | $c->render(format=>'txt', text=><<TXT); | ||
| 657 | $pkg | ||
| 658 | |||
| 659 | 1. namespace = [@{[$c->dumper( $ns )]}] | ||
| 660 | 2. controller = [@{[$c->dumper( $controll )]}] | ||
| 661 | |||
| 662 | Указать имя или ID действия из списка или ввести новое имя действия | ||
| 663 | |||
| 664 | Или указать undef чтобы привязать маршрут к контроллеру или вообще неявно через параметр route"to", но тогда на следующем шаге обязательно указать параметр to=->имя метода в котроллере | ||
| 665 | |||
| 666 | Actions for selected controller (@{[scalar @$list]}) | ||
| 667 | === | ||
| 668 | @{[$c->dumper( $list )]} | ||
| 669 | |||
| 670 | Actions without controller (@{[scalar @$list2]}): | ||
| 671 | === | ||
| 672 | @{[$c->dumper( $list2 )]} | ||
| 673 | |||
| 674 | TXT | ||
| 675 | } | ||
| 676 | |||
| 677 | my @route_cols = qw(request host_re to name descr auth disable interval_ts); | ||
| 678 | sub new_route {# показать маршруты к действию | ||
| 679 | my $c = shift; | ||
| 680 | my ($ns, $controll, $act) = $c->vars(qw'ns controll act'); | ||
| 681 | |||
| 682 | $ns = ($ns && $Init->plugin->model('Namespaces')->namespace($ns&& $ns =~ /\D/ ? (undef, $ns) : ($ns, undef,))) | ||
| 683 | || {namespace => $ns}; | ||
| 684 | |||
| 685 | $controll = ($controll && $Init->plugin->model('Controllers')->controller_id_ns($controll =~ /\D/ ? (undef, $controll) : ($controll, undef,), $ns->{id}, $ns->{namespace}, $ns->{namespace},)) | ||
| 686 | || {controller=>$controll}; | ||
| 687 | |||
| 688 | $act = ($act && $Init->plugin->model('Actions')->action_controller($controll->{id}, $act =~ /\D/ ? (undef, $act) : ($act, undef,),)) | ||
| 689 | || ($act && $Init->plugin->model('Actions')->action_controller_null($act =~ /\D/ ? (undef, $act) : ($act, undef,),)) | ||
| 690 | || {action => $act}; | ||
| 691 | |||
| 692 | # Проверка на похожий $request ?? TODO | ||
| 693 | my $route = {}; | ||
| 694 | @$route{@route_cols, 'id'} = $c->vars(@route_cols, 'id',); | ||
| 695 | |||
| 696 | my @save = (); | ||
| 697 | ($route->{id} || ($route->{request} && $route->{name})) | ||
| 698 | && (@save = $c->route_save($ns, $controll, $act, $route)) | ||
| 699 | && $c->render(format=>'txt', text=><<TXT) | ||
| 700 | $pkg | ||
| 701 | |||
| 702 | Success done save! | ||
| 703 | |||
| 704 | Namespace: | ||
| 705 | === | ||
| 706 | @{[$c->dumper( $save[0] )]} | ||
| 707 | |||
| 708 | Controller: | ||
| 709 | === | ||
| 710 | @{[$c->dumper( $save[1] )]} | ||
| 711 | |||
| 712 | Action: | ||
| 713 | === | ||
| 714 | @{[$c->dumper( $save[2] )]} | ||
| 715 | |||
| 716 | Route: | ||
| 717 | === | ||
| 718 | @{[$c->dumper( $save[3] )]} | ||
| 719 | |||
| 720 | Refs: | ||
| 721 | === | ||
| 722 | @{[$c->dumper( $save[4] )]} | ||
| 723 | |||
| 724 | TXT | ||
| 725 | && return $c | ||
| 726 | ; | ||
| 727 | |||
| 728 | |||
| 729 | # маршруты действия | ||
| 730 | my $list = $act->{id} ? $Init->plugin->model('Routes')->routes_action($act->{id}) | ||
| 731 | : ['нет явного действия']; | ||
| 732 | # свободные маршруты | ||
| 733 | my $list2 = $Init->plugin->model('Routes')->routes_action_null; | ||
| 734 | |||
| 735 | no warnings; | ||
| 736 | $c->render(format=>'txt', text=><<TXT); | ||
| 737 | $pkg | ||
| 738 | |||
| 739 | 1. namespace = [@{[$c->dumper( $ns )]}] | ||
| 740 | 2. controller = [@{[$c->dumper( $controll )]}] | ||
| 741 | 3. action = [@{[$c->dumper( $act )]}] | ||
| 742 | |||
| 743 | Маршрут: | ||
| 744 | @{[$c->dumper( $route )]} | ||
| 745 | |||
| 746 | Указать параметры маршрута (?request=/x/y/:z&name=xyz&descr=...): | ||
| 747 | |||
| 748 | * request (request=GET POST /foo/:bar) | ||
| 749 | * name (name=foo_bar) | ||
| 750 | - host_re (regexp for HeaderCondition plugin) | ||
| 751 | - to (to=Foo->bar or to=->bar) | ||
| 752 | - descr (descr=пояснение такое) | ||
| 753 | - auth (auth=1) (auth='only') | ||
| 754 | - disable (disable=1) | ||
| 755 | - interval_ts (interval_ts=123) | ||
| 756 | |||
| 757 | Exists routes for selected action (@{[scalar @$list ]}) | ||
| 758 | === | ||
| 759 | @{[$c->dumper( $list )]} | ||
| 760 | |||
| 761 | Free routes (@{[scalar @$list2]}) | ||
| 762 | === | ||
| 763 | @{[$c->dumper( $list2 )]} | ||
| 764 | |||
| 765 | TXT | ||
| 766 | |||
| 767 | } | ||
| 768 | |||
| 769 | sub route_save { | ||
| 770 | my $c = shift; | ||
| 771 | my ($ns, $controll, $act, $route) = @_; | ||
| 772 | #~ local $Init->plugin->dbh->{AutoCommit} = 0; | ||
| 773 | $ns = $Init->plugin->model('Namespaces')->new_namespace(@$ns{qw(namespace descr app_ns interval_ts)}) | ||
| 774 | if $ns->{namespace} && ! $ns->{id}; | ||
| 775 | $controll = $Init->plugin->model('Controllers')->new_controller(@$controll{qw(controller descr)}) | ||
| 776 | if $controll->{controller} && !$controll->{id}; | ||
| 777 | $act = $Init->plugin->model('Actions')->new_action(@$act{qw(action callback descr)}) | ||
| 778 | if $act->{action} && !$act->{id}; | ||
| 779 | |||
| 780 | $route->{to} =~ s/->/#/ | ||
| 781 | if $route->{to}; | ||
| 782 | |||
| 783 | $route = $Init->plugin->model('Routes')->new_route(@$route{@route_cols}) | ||
| 784 | unless $route->{id}; | ||
| 785 | my $ref = [map { | ||
| 786 | $Init->plugin->model('Refs')->refer($$_[0]{id}, $$_[1]{id},) | ||
| 787 | if $$_[0]{id} && $$_[1]{id}; | ||
| 788 | } ([$ns, $controll], [$controll, $act], [ $act, $route,], $act->{id} ? () : [$controll, $route,])]; | ||
| 789 | #~ $Init->plugin->dbh->commit; | ||
| 790 | return ($ns, $controll, $act, $route, $ref); | ||
| 791 | |||
| 792 | } | ||
| 793 | |||
| 794 | sub vars {# получить из stash || param | ||
| 795 | my $c = shift; | ||
| 796 | |||
| 797 | return map { | ||
| 798 | my $var = $c->stash($_) || $c->param($_); | ||
| 799 | $var = undef if defined($var) && $var eq 'undef'; | ||
| 800 | $var; | ||
| 801 | } @_; | ||
| 802 | } | ||
| 803 | |||
| 804 | my @self_routes_cols = qw(request action name auth descr); | ||
| 805 | sub self_routes {# from plugin! | ||
| 806 | my $c = shift; | ||
| 807 | my $prefix = $Init->prefix; | ||
| 808 | my $trust = $Init->trust; | ||
| 809 | my $role_admin = $Init->role_admin; | ||
| 810 | |||
| 811 | my $t = <<TABLE; | ||
| 812 | #format | ||
| 813 | #<route path>\t<method>\t<route name>\t<need auth>\tDescription | ||
| 814 | /$prefix index admin home 1 Main page | ||
| 815 | # | ||
| 816 | # Namespaces, controllers, actions | ||
| 817 | # | ||
| 818 | /$prefix/namespaces namespaces $prefix namespaces 1 Namespaces list | ||
| 819 | /$prefix/namespace/new/:ns/:descr/:app_ns new_namespace $prefix new_namespace 1 Add new ns | ||
| 820 | /$prefix/controllers controllers $prefix controllers 1 Controllers list | ||
| 821 | /$prefix/controller/new/:ns/:module new_controller $prefix new_controller 1 Add new controller by :ns and :module | ||
| 822 | /$prefix/controller/:ns/:controll controll $prefix controller 1 View a controller (ID and name for NS and controller) | ||
| 823 | /$prefix/actions actions $prefix actions 1 Actions list | ||
| 824 | # | ||
| 825 | # Роли и доступ | ||
| 826 | # | ||
| 827 | /$prefix/role/new/:name new_role $prefix create role 1 Add new role by :name | ||
| 828 | /$prefix/role/del/:role/:user del_role_user $prefix del ref role->user 1 Delete ref :user -> :role by user.id|user.login and role.id|role.name. | ||
| 829 | /$prefix/role/dsbl/:role disable_role $prefix disable role->user 1 Disable :role by role.id|role.name. | ||
| 830 | /$prefix/role/enbl/:role enable_role $prefix enable role->user 1 Enable :role by role.id|role.name. | ||
| 831 | /$prefix/roles roles $prefix view roles 1 View roles table | ||
| 832 | /$prefix/roles/:user user_roles $prefix roles of user 1 View roles of :user by profile.id|login | ||
| 833 | /$prefix/role/user/:role/:user new_role_user $prefix create ref role->profile 1 Assign :user to :role by profile.id|login and role.id|role.name. | ||
| 834 | /$prefix/role/namespace/:role/:ns new_role_namespace $prefix create ref namespace->role 1 Assign :role to :ns by role.id|role.name and namespace.id|namespace.name. | ||
| 835 | /$prefix/role/controller/:role/:ns/:controll new_role_controller $prefix create ref controller->role 1 Assign :role to :controll by role.id|role.name and controller.id|controller.name+namespace(id|name). | ||
| 836 | # | ||
| 837 | # Последовательный ввод нового маршрута | ||
| 838 | # | ||
| 839 | /$prefix/route/new new_route_ns $prefix create route step ns 1 Step namespace | ||
| 840 | /$prefix/route/new/:ns new_route_c $prefix create route step controll 1 Step controller | ||
| 841 | /$prefix/route/new/:ns/:controll new_route_a $prefix create route step action 1 Step action | ||
| 842 | /$prefix/route/new/:ns/:controll/:act new_route $prefix create route step request 1 Step request. Params: request, name, auth, descr, .... | ||
| 843 | /$prefix/route/new/:ns/:controll/:act/:id new_route $prefix create route step exist route 1 Step by route id to assign to ns-controller-action | ||
| 844 | # | ||
| 845 | # Маршруты и доступ | ||
| 846 | # | ||
| 847 | /$prefix/routes routes $prefix view routes 1 View routes list | ||
| 848 | /$prefix/routes/:role role_routes $prefix routes of role 1 All routes of :role by id|name | ||
| 849 | /$prefix/route/:route/:role ref $prefix create ref route->role 1 Assign :route with :role by route.id and role.id|role.name | ||
| 850 | # | ||
| 851 | # Пользователи/профили | ||
| 852 | # | ||
| 853 | /$prefix/user/new new_user $prefix create user 1 Add new user by params: login,pass,... | ||
| 854 | /$prefix/user/new/:login/:pass new_user $prefix create user st 1 Add new user by :login & :pass | ||
| 855 | /$prefix/users users $prefix view users 1 View users table | ||
| 856 | /$prefix/users/:role role_users $prefix users of role 1 View users of :role by id|name | ||
| 857 | # | ||
| 858 | #get foo /sign/in sign signin form 0 Login&pass form | ||
| 859 | #post /sign/in sign signin params 0 Auth by params | ||
| 860 | /$prefix/sign/in/:login/:pass sign signin stash 0 Auth by stash | ||
| 861 | /$prefix/sign/out signout go away only Exit | ||
| 862 | /logout out logout only Exit and redirect | ||
| 863 | /reauth/:cookie auth_cookie auth-cookie 0 Relogin by cookie | ||
| 864 | # | ||
| 865 | |||
| 866 | /$prefix/$trust/$role_admin/new/:login/:pass trust_new_user $prefix/$trust !trust create user! 0 Add new user by :login & :pass and auto assign to role 'Admin' and assign to access this controller! | ||
| 867 | |||
| 868 | TABLE | ||
| 869 | |||
| 870 | |||
| 871 | my @r = (); | ||
| 872 | for my $line (grep /\S+/, split /\n/, $t) { | ||
| 873 | my $r = {}; | ||
| 874 | @$r{@self_routes_cols} = map($_ eq '' ? undef : $_, split /\t/, $line); | ||
| 875 | $r->{namespace} = $Init->{namespace}; | ||
| 876 | $r->{controller} = $Init->{controller}; | ||
| 877 | push @r, $r; | ||
| 878 | } | ||
| 879 | |||
| 880 | return @r; | ||
| 881 | } | ||
| 882 | |||
| 883 | sub auth_cookie {# action | ||
| 884 | my $c = shift; | ||
| 885 | my $json = $c->req->json; | ||
| 886 | |||
| 887 | my ($cookie) = ($json && $json->{cookie}) | ||
| 888 | || $c->vars('cookie'); | ||
| 889 | |||
| 890 | unless ($cookie) { | ||
| 891 | return $c->render(json=>{error=>"No cookie"}) | ||
| 892 | if $json; | ||
| 893 | |||
| 894 | return $c->render(text=>"Error: no cookie"); | ||
| 895 | |||
| 896 | } | ||
| 897 | |||
| 898 | my $profile = { %{$c->access->auth_cookie($c, $cookie) || {error=>"none profile by given cookie [$cookie]"}} }; | ||
| 899 | |||
| 900 | return $c->render(json=>{profile=>$profile}) | ||
| 901 | if $json; | ||
| 902 | |||
| 903 | return $c->render(text=>"Success signed by cookie\n".$c->dumper($profile)); | ||
| 904 | |||
| 905 | } | ||
| 906 | |||
| 907 | 1; | ||
| 908 | |||
| 909 | =pod | ||
| 910 | |||
| 911 | =encoding utf8 | ||
| 912 | |||
| 913 | =head1 Mojolicious::Plugin::RoutesAuthDBI::Admin | ||
| 914 | |||
| 915 | ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! ! | ||
| 916 | |||
| 917 | =head1 WARN | ||
| 918 | |||
| 919 | More or less complete! :) | ||
| 920 | |||
| 921 | =head1 NAME | ||
| 922 | |||
| 923 | Mojolicious::Plugin::RoutesAuthDBI::Admin - is a Mojolicious::Controller for manage admin operations on DBI tables: namespaces, controllers, actions, routes, roles, users. | ||
| 924 | |||
| 925 | =head1 DB DESIGN DIAGRAM | ||
| 926 | |||
| 927 | See L<https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/blob/master/Diagram.svg> | ||
| 928 | |||
| 929 | =head1 SYNOPSIS | ||
| 930 | |||
| 931 | $app->plugin('RoutesAuthDBI', | ||
| 932 | ... | ||
| 933 | admin => {< hashref options list below >}, | ||
| 934 | ... | ||
| 935 | ); | ||
| 936 | |||
| 937 | |||
| 938 | =head2 OPTIONS | ||
| 939 | |||
| 940 | =head3 namespace | ||
| 941 | |||
| 942 | Namespace (string). Defaults to 'Mojolicious::Plugin::RoutesAuthDBI'. | ||
| 943 | |||
| 944 | =head3 controller | ||
| 945 | |||
| 946 | Module controller name. Defaults to 'Admin'. | ||
| 947 | |||
| 948 | Both above options determining the loadable module controller as concatenation C<namespace>::C<controller>. | ||
| 949 | |||
| 950 | =head3 prefix | ||
| 951 | |||
| 952 | String. Is a prefix for admin urls of this module. Default as name of controller to low case. | ||
| 953 | |||
| 954 | =head3 trust | ||
| 955 | |||
| 956 | String. Is a url subprefix for trust admin urls of this module. See defaults below. | ||
| 957 | |||
| 958 | =head3 role_admin | ||
| 959 | |||
| 960 | String. Is a name of role for admonistrators. | ||
| 961 | |||
| 962 | =head3 tables | ||
| 963 | |||
| 964 | Hashref of any DB tables names. See L<Mojolicious::Plugin::RoutesAuthDBI::Schema#Default-variables-for-SQL-templates>. | ||
| 965 | |||
| 966 | =head2 Default options | ||
| 967 | |||
| 968 | admin = > { | ||
| 969 | namespace => 'Mojolicious::Plugin::RoutesAuthDBI', | ||
| 970 | controller => 'Admin', | ||
| 971 | prefix => 'admin', # lc(<module>) | ||
| 972 | trust => hmac_sha1_sum('admin', $app->secrets->[0]), | ||
| 973 | role_admin => 'administrators', | ||
| 974 | }, | ||
| 975 | |||
| 976 | Examples options: | ||
| 977 | |||
| 978 | admin = {}, # empty hashref sets defaults above | ||
| 979 | |||
| 980 | admin => undef, # disable admin controller | ||
| 981 | |||
| 982 | admin = > {prefix=>'myadmin', trust => 'foooobaaar'},# admin urls like: /myadmin/foooobaaar/ | ||
| 983 | |||
| 984 | |||
| 985 | |||
| 986 | =head1 METHODS NEEDS IN PLUGIN | ||
| 987 | |||
| 988 | =head2 self_routes() | ||
| 989 | |||
| 990 | Builtin to this admin controller routes. Return array of hashrefs routes records for apply route on app. Depends on conf options I<prefix> and I<trust>. | ||
| 991 | |||
| 992 | |||
| 993 | =head1 ROUTES | ||
| 994 | |||
| 995 | There are number of app routes on this controller. See in console C< $perl your-app.pl routes >. That routes will not apply then admin controller disabled. | ||
| 996 | |||
| 997 | =head1 SEE ALSO | ||
| 998 | |||
| 999 | L<Mojolicious::Plugin::RoutesAuthDBI> | ||
| 1000 | |||
| 1001 | =head1 AUTHOR | ||
| 1002 | |||
| 1003 | Михаил Че (Mikhail Che), C<< <mche [on] cpan.org> >> | ||
| 1004 | |||
| 1005 | =head1 BUGS / CONTRIBUTING | ||
| 1006 | |||
| 1007 | Please report any bugs or feature requests at L<https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/issues>. Pull requests also welcome. | ||
| 1008 | |||
| 1009 | =head1 COPYRIGHT | ||
| 1010 | |||
| 1011 | Copyright 2016 Mikhail Che. | ||
| 1012 | |||
| 1013 | This library is free software; you can redistribute it and/or modify | ||
| 1014 | it under the same terms as Perl itself. | ||
| 1015 | |||
| 1016 | =cut |