Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • SecurityExtension
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the Nette Framework (http://nette.org)
 5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
 6:  */
 7: 
 8: namespace Nette\Bridges\SecurityDI;
 9: 
10: use Nette;
11: 
12: 
13: /**
14:  * Security extension for Nette DI.
15:  *
16:  * @author     David Grudl
17:  */
18: class SecurityExtension extends Nette\DI\CompilerExtension
19: {
20:     public $defaults = array(
21:         'debugger' => TRUE,
22:         'users' => array(), // of [user => password] or [user => ['password' => password, 'roles' => [role]]]
23:         'roles' => array(), // of [role => parents]
24:         'resources' => array(), // of [resource => parents]
25:     );
26: 
27:     /** @var bool */
28:     private $debugMode;
29: 
30: 
31:     public function __construct($debugMode = FALSE)
32:     {
33:         $this->debugMode = $debugMode;
34:     }
35: 
36: 
37:     public function loadConfiguration()
38:     {
39:         $config = $this->validateConfig($this->defaults);
40:         $container = $this->getContainerBuilder();
41: 
42:         $container->addDefinition($this->prefix('userStorage'))
43:             ->setClass('Nette\Security\IUserStorage')
44:             ->setFactory('Nette\Http\UserStorage');
45: 
46:         $user = $container->addDefinition($this->prefix('user'))
47:             ->setClass('Nette\Security\User');
48: 
49:         if ($this->debugMode && $config['debugger']) {
50:             $user->addSetup('@Tracy\Bar::addPanel', array(
51:                 new Nette\DI\Statement('Nette\Bridges\SecurityTracy\UserPanel')
52:             ));
53:         }
54: 
55:         if ($config['users']) {
56:             $usersList = $usersRoles = array();
57:             foreach ($config['users'] as $username => $data) {
58:                 $data = is_array($data) ? $data : array('password' => $data);
59:                 $this->validateConfig(array('password' => NULL, 'roles' => NULL), $data, $this->prefix("security.users.$username"));
60:                 $usersList[$username] = $data['password'];
61:                 $usersRoles[$username] = isset($data['roles']) ? $data['roles'] : NULL;
62:             }
63: 
64:             $container->addDefinition($this->prefix('authenticator'))
65:                 ->setClass('Nette\Security\IAuthenticator')
66:                 ->setFactory('Nette\Security\SimpleAuthenticator', array($usersList, $usersRoles));
67: 
68:             if ($this->name === 'security') {
69:                 $container->addAlias('nette.authenticator', $this->prefix('authenticator'));
70:             }
71:         }
72: 
73:         if ($config['roles'] || $config['resources']) {
74:             $authorizator = $container->addDefinition($this->prefix('authorizator'))
75:                 ->setClass('Nette\Security\IAuthorizator')
76:                 ->setFactory('Nette\Security\Permission');
77: 
78:             foreach ($config['roles'] as $role => $parents) {
79:                 $authorizator->addSetup('addRole', array($role, $parents));
80:             }
81:             foreach ($config['resources'] as $resource => $parents) {
82:                 $authorizator->addSetup('addResource', array($resource, $parents));
83:             }
84: 
85:             if ($this->name === 'security') {
86:                 $container->addAlias('nette.authorizator', $this->prefix('authorizator'));
87:             }
88:         }
89: 
90:         if ($this->name === 'security') {
91:             $container->addAlias('user', $this->prefix('user'));
92:             $container->addAlias('nette.userStorage', $this->prefix('userStorage'));
93:         }
94:     }
95: 
96: }
97: 
Nette 2.3.1 API API documentation generated by ApiGen 2.8.0