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 repair 
  7:  * @author Jaromír Polášek
  8:  * @version 0.7.3.JIDASH
  9:  * Encoding UTF-8
 10:  */
 11: 
 12: namespace App\Lib\Repair;
 13: 
 14: use Nette\Object,
 15:     App\Lib\Table,
 16:     App\Lib\Tree,
 17:     App\Model\Database,
 18:     App\Lib\Statics\Cons;
 19: 
 20: /**
 21:  * Opravuje menu a implementuje zdroje do DB
 22:  */
 23: class Repair extends Object {
 24: 
 25:     private $database;
 26:     private $resources;
 27:     private $roles;
 28:     private $privileges;
 29: 
 30:     public function __construct(Database $database) {
 31:     $this->database = $database;
 32:     $this->resources = new Tree(Cons::TABLE_ACL . '_' . Cons::TABLE_RESOURCES, $this->database);
 33:     $this->roles = new Tree(Cons::TABLE_ACL . '_' . Cons::TABLE_ROLES, $this->database);
 34:     $this->privileges = new Table(Cons::TABLE_ACL, $this->database);
 35:     }
 36: 
 37:     public function repairSources() {
 38:     $this->rebuildTables();
 39:     $this->rebuildMenu();
 40:     $this->admin();
 41:     $this->user();
 42:     return true;
 43:     }
 44: 
 45:     /**
 46:      * zkontroluje a doplní zdroje tabulek
 47:      */
 48:     public function rebuildTables() {
 49:     $tables = (new Table(Cons::TABLE_TABLE, $this->database))->data;
 50:     $sectionRow = $this->section(Cons::RES_TABLES, 1);
 51:     foreach ($tables as $tableRow) {
 52:         $resourceTableRow = $this->section($tableRow[Cons::COLUMN_OBJECT], 2, $sectionRow[Cons::COLUMN_ID]);
 53:         $this->subsection($tableRow, $resourceTableRow);
 54:     }
 55:     }
 56: 
 57:     /**
 58:      * Zkontroluje a doplní zdroje menu
 59:      */
 60:     public function rebuildMenu() {
 61:     $menu = (new Tree(Cons::TABLE_MENU, $this->database))->data->where(Cons::COLUMN_DEPTH, 1);
 62:     $sectionRow = $this->section(Cons::RES_MENU, 1);
 63:     foreach ($menu as $menuRow) {
 64:         $itemRow = $this->section($menuRow[Cons::COLUMN_NAME], 2, $sectionRow[Cons::COLUMN_ID]);
 65:         $this->recurse($menuRow, $itemRow, Cons::TABLE_MENU);
 66:     }
 67:     }
 68: 
 69:     
 70:     /**
 71:      * Zabezpečí práva roli User
 72:      */
 73:     public function user() {
 74:     $role = $this->getRole('User');
 75:     $tableResource = $this->resources->copy->where(Cons::COLUMN_PATH . ' LIKE ? OR path LIKE ?', 'tables > users %', 'menu > %user% %');
 76:     foreach ($tableResource as $resource) {
 77:         if ($resource[Cons::COLUMN_PATH] === 'tables > users > state' || \Nette\Utils\Strings::startsWith($resource[Cons::COLUMN_PATH], 'menu')) {
 78:         $this->repairPrivilege($role[Cons::COLUMN_ID], $resource[Cons::COLUMN_ID]);     
 79:         } else {
 80:         $this->repairPrivilege($role[Cons::COLUMN_ID], $resource[Cons::COLUMN_ID], TRUE);       
 81:         }
 82:     }
 83:     }
 84: 
 85:     /**
 86:      * Zabezpečí všechna práva roli Admin
 87:      */
 88:     public function admin() {
 89:     $role = $this->getRole('Admin');
 90:     $tableResource = $this->resources->data;
 91:     foreach ($tableResource as $resource) {
 92:         if (\Nette\Utils\Strings::startsWith($resource[Cons::COLUMN_PATH], 'menu')){
 93:         $this->repairPrivilege($role[Cons::COLUMN_ID], $resource[Cons::COLUMN_ID], FALSE);      
 94:         }else {
 95:         $this->repairPrivilege($role[Cons::COLUMN_ID], $resource[Cons::COLUMN_ID], TRUE);       
 96:         }
 97:         
 98:     }
 99:     }
100:     
101:     /**
102:      * Rekurzivní přepis jednoho stromu do druhého
103:      * @param type $activeRow
104:      * @param type $parent
105:      * @param type $table
106:      */
107:     private function recurse($activeRow, $parent, $table) {
108:     $related = $activeRow->related($table . '.' . Cons::COLUMN_PARENT);
109:     $depth = $parent[Cons::COLUMN_DEPTH] + 1;
110:     foreach ($related as $relatedRow) {
111:         $resourceRow = $this->section($relatedRow[Cons::COLUMN_NAME], $depth, $parent[Cons::COLUMN_ID]);
112:         $this->recurse($relatedRow, $resourceRow, $table);
113:     }
114:     }
115: 
116:     /**
117:      * Projde Zdroje polí pro tabulku zadanou jako 
118:      * @param Nette\Database\Table\ActiveRow $activeRow řádek table
119:      */
120:     private function subsection($tableRow, $resourceTableRow) {
121:     $related = $tableRow->related(Cons::TABLE_TABLE . '_' . Cons::TABLE_FIELDS);
122:     foreach ($related as $activeRow) {
123:         $this->section(
124:             $activeRow[Cons::COLUMN_FIELD], 3, $resourceTableRow[Cons::COLUMN_ID]
125:         );
126:     }
127:     }
128: 
129:     /**
130:      * Vrátí sekci stromu podle sloupce 'name´ hloubky 'depth' a rodiče 'parent', 
131:      * pokud je zadán
132:      * @param string $section Jméno položky
133:      * @param integer $depth Úroveň zanoření
134:      * @param iteger $parent Id rodiče
135:      * @return Nette\Database\Table\ActiveRow
136:      */
137:     private function section($section, $depth, $parent = NULL) {
138:     return ($this->getSection($section, $depth, $parent))? :
139:         $this->setSection($section, $parent);
140:     }
141: 
142:     /**
143:      * Vrátí řádek zdroje z databáze
144:      * @param string $section Název sekce
145:      * @param integer $depth Úroveň zanoření
146:      * @param integer $parent ID rodiče
147:      * @return Nette\Database\Table\ActiveRow
148:      */
149:     private function getSection($section, $depth, $parent = NULL) {
150:     $ret = $this->resources->copy
151:         ->where(Cons::COLUMN_NAME, $section)
152:         ->where(Cons::COLUMN_DEPTH, $depth);
153:     ($parent === NULL) ? : $ret->where(Cons::COLUMN_PARENT, $parent);
154:     return $ret->fetch();
155:     }
156: 
157:     /**
158:      * přidá zdroj do databáze a vrátí jeho řádek
159:      * @param string $section Úroveň zanoření
160:      * @param integer $parent ID Rodiče
161:      * @return Nette\Database\Table\ActiveRow
162:      */
163:     private function setSection($section, $parent) {
164:     return $this->resources->insert(array(
165:             Cons::COLUMN_NAME => $section,
166:             Cons::COLUMN_PARENT => $parent
167:     ));
168:     }
169: 
170:     /**
171:      * Vrací ID zadané role
172:      * @param string $role
173:      * @return integer
174:      */
175:     public function getRole($role) {
176:     $query = $this->roles->copy->where(Cons::COLUMN_NAME . ' =?', $role)->fetch();
177:     if ($query === FALSE) {   
178:         $query = $this->roles->data->insert(array(
179:         Cons::COLUMN_NAME => $role,
180:         Cons::COLUMN_PARENT => $this->roles->copy->where(Cons::COLUMN_NAME . ' =?', Cons::ROOT)->fetch()->id
181:         ));
182:     }
183:     return $query;
184:     }
185: 
186:     /**
187:      * Vrací práva role
188:      * @param integer $role
189:      * @param integer $resource
190:      * @return type
191:      */
192:     private function getPrivilege($role, $resource) {
193:     $ret = $this->privileges->copy
194:         ->where(Cons::TABLE_ACL . '_' . Cons::TABLE_ROLES . '_' . Cons::COLUMN_ID, $role)
195:         ->where(Cons::TABLE_ACL . '_' . Cons::TABLE_RESOURCES . '_' . Cons::COLUMN_ID, $resource);
196:     return $ret->fetch();
197:     }
198:     /**
199:      * zkontroluje práva a případně je opraví
200:      * @param int $role Id Role
201:      * @param int $resource Id Zdoje
202:      * @param boolean $all TRUE = všechna práva
203:      */
204:     private function repairPrivilege($role, $resource, $all = FALSE) {
205:     $privilege = $this->getPrivilege($role, $resource);
206:     if ($privilege instanceof \Nette\Database\Table\ActiveRow) {
207:         if ($all === FALSE && $privilege[Cons::READ] != '1') {
208:         $privilege->update($this->defPriv($role, $resource, $all));
209:         }else if($all === TRUE){
210:         ($privilege[Cons::READ] == '1' && $privilege[Cons::CREATE] == '1' && $privilege[Cons::WRITE] == '1' && $privilege[Cons::DELETE] == '1')? :
211:                 $privilege->update($this->defPriv($role, $resource, $all));
212:         }     
213:     } else {
214:         $this->privileges->data->insert($this->defPriv($role, $resource, $all));
215:     }
216:     }
217:     /**
218:      * Vrací výchozí nastavení práv
219:      * @param int $role Id Role
220:      * @param int $resource Id Zdroje
221:      * @param boolean $all
222:      * @return array
223:      */
224:     private function defPriv($role, $resource, $all = FALSE) {
225:     return array(
226:         Cons::TABLE_ACL . '_' . Cons::TABLE_ROLES . '_' . Cons::COLUMN_ID => $role,
227:         Cons::TABLE_ACL . '_' . Cons::TABLE_RESOURCES . '_' . Cons::COLUMN_ID => $resource,
228:         Cons::CREATE => ($all) ? 1 : 0,
229:         Cons::WRITE => ($all) ? 1 : 0,
230:         Cons::DELETE => ($all) ? 1 : 0,
231:         Cons::READ => 1,
232:         Cons::COLUMN_C_ID => 1,
233:         Cons::COLUMN_W_ID => 1,
234:     );
235:     }
236: }
237: 
sberp API API documentation generated by ApiGen