Overview

Namespaces

  • App
    • Components
    • Interfaces
    • Lib
      • Files
      • Forms
        • Controls
      • Html
      • Repair
      • Statics
    • Model
    • Modules
      • BaseModule
        • Presenters
      • PartnersModule
        • Presenters
      • ProductModule
        • Presenters
      • SaleModule
        • Model
        • Presenters
      • SettingsModule
        • Model
        • Presenters
      • StoreModule
        • Model
        • Presenters
    • Presenters
    • Router
  • PHP

Classes

  • DateTime
  • SplFileInfo

Interfaces

  • ArrayAccess
  • Countable
  • Exception
  • Iterator
  • Traversable
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Download
  1: <?php
  2: 
  3: /**
  4:  * Project sberp
  5:  *
  6:  * @file Permission 
  7:  * @author Jaromír Polášek
  8:  * @version 0.6.7.ACL
  9:  * Encoding UTF-8
 10:  */
 11: 
 12: namespace App\Model;
 13: 
 14: use Nette,
 15:     Nette\Object,
 16:     App\Lib\Table,
 17:     App\Lib\Statics\Cons;
 18: 
 19: /**
 20:  * Vytváří a vyhledává oprávnění
 21:  */
 22: class Permission extends Object {
 23: 
 24:     /**
 25:      * Pole pravidel
 26:      * @var array
 27:      */
 28:     private $rules = array();
 29: 
 30:     /**
 31:      * Objekt rolí
 32:      * @var App\Model\Roles
 33:      */
 34:     protected $roles;
 35: 
 36:     /**
 37:      * Objekt zdrojů
 38:      * @var App\Model\Resources
 39:      */
 40:     protected $resources;
 41: 
 42:     /**
 43:      * setříděná tabulka obsahující oprávnění
 44:      * @var Nette\Database\Table\Selection 
 45:      */
 46:     private $source;
 47: 
 48:     /**
 49:      * Konstruktor volá modely Database, Roles a Resources. 
 50:      * Projde tabulku ACL azapíše pravidla
 51:      * @param \App\Model\Database $database
 52:      * @param \App\Model\Roles $roles
 53:      * @param \App\Model\Resources $resources
 54:      */
 55:     public function __construct(Database $database, Roles $roles, Resources $resources) {
 56:     $this->roles = $roles;
 57:     $this->resources = $resources;
 58:     $this->source = (new Table(Cons::TABLE_ACL, $database));
 59:     foreach ($this->source->data as $activeRow) {
 60:         $this->addPermision($activeRow);
 61:     }
 62:     }
 63: 
 64:     /**
 65:      * Vrátí True, pokud role existuje
 66:      * @param string $role Název Role
 67:      * @return boolean 
 68:      */
 69:     public function isRole($role) {
 70:     return $this->roles->hasRole($role);
 71:     }
 72: 
 73:     /**
 74:      * Vrátí true, pokud Zdroj existuje
 75:      * @param string $resource Název Zdroje
 76:      * @return boolean
 77:      */
 78:     public function isResource($resource) {
 79:     return $this->resources->hasResource($resource);
 80:     }
 81: 
 82:     /**
 83:      * Udělí roli oprávnění ke zdroji
 84:      * @param string $role
 85:      * @param string $resource
 86:      * @param string|array $privileges
 87:      * @return \App\Model\Permission
 88:      */
 89:     public function allow($role, $resource, $privileges) {
 90:     $this->setRule($role, $resource, $privileges, TRUE);
 91:     return $this;
 92:     }
 93: 
 94:     /**
 95:      * Odebere roli oprávnění ke zdroji
 96:      * @param string $role Název role
 97:      * @param string $resource Název zdroje
 98:      * @param string|array $privileges
 99:      * @return \App\Model\Permission
100:      */
101:     public function deny($role, $resource, $privileges) {
102:     $this->setRule($role, $resource, $privileges, FALSE);
103:     return $this;
104:     }
105: 
106:     protected function getRules() {
107:     return $this->rules;
108:     }
109: 
110:     /**
111:      * Nastaví pravidla Pro konkrétní zdroj a Roli 
112:      * @param string $role Název Role
113:      * @param string $resource Název zdroje
114:      * @param string|array $privileges Název Oprávnění
115:      * @throws Nette\InvalidStateException
116:      */
117:     protected function setRule($role, $resource, $privileges, $type) {
118:     if (!$this->isrole($role)) {
119:         throw new Nette\InvalidStateException("Role '$role' neexistuje.");
120:     }
121:     if (!$this->isResource($resource)) {
122:         throw new Nette\InvalidStateException("Zdroj '$resource' neexistuje.");
123:     }
124:     $rules = & $this->rule($resource, $role, TRUE);
125:     foreach (((is_array($privileges)) ? $privileges : array($privileges)) as $privilege) {
126:         $rules[$privilege] = $type;
127:     }
128:     }
129: 
130:     /**
131:      * Vrátí pravidlo, pokud existuje, pokud ne, vravcí nul. Pokud $add = true, pravidlo vytvoří
132:      * @param string $resource Název zdroje
133:      * @param string $role Název role
134:      * @param boolean $add True pro přidání
135:      * @return null|array
136:      */
137:     protected function &rule($resource, $role, $add = FALSE) {
138:     if ($add) {
139:         return $this->addRule($resource, $role);
140:     } else {
141:         return $this->getRule($resource, $role);
142:     }
143:     }
144: 
145:     /**
146:      * Priorita privilegií se přepisuje z hora dolů. 
147:      * Nejprve je vše zakázané, a pak jednotlivé práva povolí
148:      * @param Nette\Database\Table\ActiveRow $activeRow
149:      */
150:     private function addPermision($activeRow) {
151:     $privileges = $this->setPrivileges($activeRow);
152:     if (count($privileges)) {
153:         $this->allow(
154:             $activeRow[Cons::TABLE_ACL . '_' . Cons::TABLE_ROLES][Cons::COLUMN_PATH], $activeRow[Cons::TABLE_ACL . '_' . Cons::TABLE_RESOURCES][Cons::COLUMN_PATH], $privileges
155:         );
156:     }
157:     }
158: 
159:     /**
160:      * Vrací atributy (R,C,W,D) odpovídající operacím s databází 
161:      * (Select, Insert, Update, Delete)
162:      * @param Nette\Database\Table\ActiveRow $activeRow
163:      * @return array
164:      */
165:     private function setPrivileges($activeRow) {
166:     $privileges = array();
167:     (!$activeRow[Cons::DELETE])? : $privileges[] = Cons::DELETE;
168:     (!$activeRow[Cons::WRITE])? : $privileges[] = Cons::WRITE;
169:     (!$activeRow[Cons::CREATE])? : $privileges[] = Cons::CREATE;
170:     (!$activeRow[Cons::READ])? : $privileges[] = Cons::READ;
171:     return $privileges;
172:     }
173: 
174:     /**
175:      * Vytvoří a vrátí pravidlo
176:      * @param string $resource Název zdroje
177:      * @param string $role Název role
178:      * @return array
179:      */
180:     private function & addRule($resource, $role) {
181:     //$this->rules[$resource] = array();
182:     $visitor = & $this->rules[$resource];
183:     $visitor[$role] = array();
184:     return $visitor[$role];
185:     }
186: 
187:     /**
188:      * Vrátí pravidla, nebo null, pokud žádné není
189:      * @param string $resource Název zdroje
190:      * @param string $role  Název role
191:      * @return null|array
192:      */
193:     private function & getRule($resource, $role) {
194:     $null = NULL;
195:     if (!isset($this->rules[$resource])) {
196:         return $null;
197:     } else {
198:         $step = & $this->rules[$resource];
199:     }
200:     if (!isset($step[$role])) {
201:         return $null;
202:     }
203:     return $step[$role];
204:     }
205: }
206: 
sberp API API documentation generated by ApiGen