Vai al contenuto

Middleware - Esempio pratico

Questo capitolo finale mostra un esempio pratico di come potrebbe apparire una semplice applicazione, nella quale avremo una schermata di autenticazione (login), un CRUD, e 3 moduli finti.

Per lo schema di questa applicazione le "mie condizioni" (perché ognuno ha le proprie) sono:

  • schermata principale Hello splash senza sicurezza, accesso aperto
  • schermata di login -> middleware "MyAppLogin"
  • schermata module_x -> middleware "MyAppAuth", se solo autenticato -> ok
  • schermata CRUD -> middleware "MyAppAuthRole" con validazione granulare del ruolo. Autenticato + ruolo
  • schermata CRUD (edit) -> middleware "MyAppAuthEdit" con validazione granulare del ruolo + CSRF

Possiamo osservare come uso 3 tipi di Mw. Una struttura che valida semplicemente se sei autenticato (MyAppAuth) a se sono validato e ho un ruolo specifico (MyAppRole)

Le definizioni delle route potrebbero essere come le seguenti:

[
  { "name": "index",          "url": "/",                            "action": "views/index.html",                         "method": "GET" },
  { "name": "main",           "url": "/main",                        "action": "controllers/main.prg",                     "method": "GET",  "middleware": "MyAppAuth" },
  { "name": "sys.login" ,     "url": "/login",                       "action": "controllers/login.prg",                    "method": "GET" },
  { "name": "sys.logout",     "url": "/logout",                      "action": "controllers/logout.prg",                   "method": "GET",  "middleware": "MyAppAuth" },
  { "name": "sys.auth",       "url": "/auth",                        "action": "controllers/auth.prg" ,                    "method": "POST", "middleware": "MyAppLogin" },
  { "name": "module_a",       "url": "/module_a",                    "action": "views/masters/modules/module_a.view.html", "method": "GET",  "middleware": "MyAppAuth" },
  { "name": "module_b",       "url": "/module_b",                    "action": "views/masters/modules/module_b.view.html", "method": "GET",  "middleware": "MyAppAuth" },
  { "name": "module_c",       "url": "/module_c",                    "action": "views/masters/modules/module_c.view.html", "method": "GET",  "middleware": "MyAppAuth" },
  { "name": "customer.search","url": "/customer/search",             "action": "controllers/masters/search@customer.prg",  "method": "GET",  "middleware": "MyAppAuthRole", "scope": "customers" },
  { "name": "customer.create","url": "/customer/create",             "action": "controllers/masters/create@customer.prg",  "method": "GET",  "middleware": "MyAppAuthRole", "scope": "customers:create" },
  { "name": "customer.store", "url": "/customer/store",              "action": "controllers/masters/store@customer.prg",   "method": "POST", "middleware": "MyAppAuthEdit", "scope": "customers:create" },
  { "name": "customer.show",  "url": "/customer/:id",                "action": "controllers/masters/show@customer.prg",    "method": "GET",  "middleware": "MyAppAuthRole", "scope": "customers" },
  { "name": "customer.edit",  "url": "/customer/:id([0-9]+)/edit",   "action": "controllers/masters/edit@customer.prg",    "method": "GET",  "middleware": "MyAppAuthRole", "scope": "customers:edit" },
  { "name": "customer.update","url": "/customer/:id([0-9]+)/edit",   "action": "controllers/masters/update@customer.prg",  "method": "POST", "middleware": "MyAppAuthEdit", "scope": "customers:edit" },
  { "name": "customer.delete","url": "/customer/:id([0-9]+)/delete", "action": "controllers/masters/delete@customer.prg",  "method": "POST", "middleware": "MyAppAuthEdit", "scope": "customers:delete" }

]