Skip to content

Commit a2c53a5

Browse files
Matěj Račinskýdg
authored andcommitted
Added usage in appliaction via config.neon and factory class. (#388)
1 parent cd9563d commit a2c53a5

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

cs/access-control.texy

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,89 @@ $acl->isAllowed('mary', 'backend'); // TRUE
383383
\--
384384

385385

386+
Použití v aplikaci
387+
=================================
388+
389+
Permission můžeme nakonfigurovat v souboru `config.neon` následujícím způsobem:
390+
391+
/--
392+
services:
393+
acl:
394+
class: Nette\Security\Permission
395+
setup:
396+
- addRole(admin)
397+
- addRole(guest)
398+
399+
- addResource(backend)
400+
401+
- allow(admin, backend)
402+
- deny(guest, backend)
403+
404+
# případ A: role admin má menší váhu než role guest
405+
- addRole(john, [admin, guest])
406+
407+
# případ B: role admin má větší váhu než guest
408+
- addRole(mary, [guest, admin])
409+
\--
410+
411+
a v presenterech pak můžete ověřit práva například v metodě startup:
412+
413+
414+
/--php
415+
416+
protected function startup()
417+
{
418+
parent::startup();
419+
if (!$this->getUser()->isAllowed('backend')) {
420+
throw new Nette\Application\ForbiddenRequestException;
421+
}
422+
}
423+
\--
424+
425+
Alternativou k nastavení v souboru `config.neon` je vytvoření továrny, která nám Permission nastaví. Ta pak může vypadat například následovně:
426+
427+
/--php
428+
<?php
429+
430+
namespace App\Model;
431+
432+
use Nette;
433+
434+
class AuthorizatorFactory extends Nette\Object
435+
{
436+
/** @return Nette\Security\Permission */
437+
public static function create()
438+
{
439+
$acl = new Nette\Security\Permission;
440+
//pokud chceme, můžeme role a zdroje načíst z databáze
441+
$acl->addRole('admin');
442+
$acl->addRole('guest');
443+
444+
$acl->addResource('backend');
445+
446+
$acl->allow('admin', 'backend');
447+
$acl->deny('guest', 'backend');
448+
449+
// případ A: role admin má menší váhu než role guest
450+
$acl->addRole('john', array('admin', 'guest'));
451+
$acl->isAllowed('john', 'backend'); // FALSE
452+
453+
// případ B: role admin má větší váhu než guest
454+
$acl->addRole('mary', array('guest', 'admin'));
455+
$acl->isAllowed('mary', 'backend'); // TRUE
456+
457+
return $acl;
458+
}
459+
}
460+
\--
461+
462+
Tovární metodu použijeme jako továrnu pro Permission:
463+
464+
/--
465+
services:
466+
- App\Model\AuthorizatorFactory::create #říkáme, že třídu Permission vytvoříme metodou create třídy AuthorizatorFactory
467+
\--
468+
386469

387470
Více nezávislých přihlášení v aplikaci
388471
======================================

en/access-control.texy

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,89 @@ $acl->isAllowed('mary', 'backend'); // TRUE
379379
\--
380380

381381

382+
Usage in application
383+
====================
384+
385+
We can configure Permission in the `config.neon` like this:
386+
387+
/--
388+
services:
389+
acl:
390+
class: Nette\Security\Permission
391+
setup:
392+
- addRole(admin)
393+
- addRole(guest)
394+
395+
- addResource(backend)
396+
397+
- allow(admin, backend)
398+
- deny(guest, backend)
399+
400+
# example A: role admin has lower weight than role guest
401+
- addRole(john, [admin, guest])
402+
403+
# example B: role admin has greater weight than role guest
404+
- addRole(mary, [guest, admin])
405+
\--
406+
407+
and then we can verify privileges in Presenter e.g. in the startup method:
408+
409+
410+
/--php
411+
412+
protected function startup()
413+
{
414+
parent::startup();
415+
if (!$this->getUser()->isAllowed('backend')) {
416+
throw new Nette\Application\ForbiddenRequestException;
417+
}
418+
}
419+
\--
420+
421+
Following solution is alternative to the previous one. We create factory service, where we can setup Permission:
422+
423+
/--php
424+
<?php
425+
426+
namespace App\Model;
427+
428+
use Nette;
429+
430+
class AuthorizatorFactory extends Nette\Object
431+
{
432+
/** @return Nette\Security\Permission */
433+
public static function create()
434+
{
435+
$acl = new Nette\Security\Permission;
436+
//if we want, we can load roles from database
437+
$acl->addRole('admin');
438+
$acl->addRole('guest');
439+
440+
$acl->addResource('backend');
441+
442+
$acl->allow('admin', 'backend');
443+
$acl->deny('guest', 'backend');
444+
445+
// example A: role admin has lower weight than role guest
446+
$acl->addRole('john', array('admin', 'guest'));
447+
$acl->isAllowed('john', 'backend'); // FALSE
448+
449+
// example B: role admin has greater weight than role guest
450+
$acl->addRole('mary', array('guest', 'admin'));
451+
$acl->isAllowed('mary', 'backend'); // TRUE
452+
453+
return $acl;
454+
}
455+
}
456+
\--
457+
458+
Then we have to register factory to the `config.neon` and use it as factory for Permission:
459+
460+
/--
461+
acl: App\Model\AuthorizatorFactory::create #here we specify, that AuthorizationFactory will be factory for Permission
462+
\--
463+
464+
382465
Multiple authentications in the application
383466
===========================================
384467

0 commit comments

Comments
 (0)