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 Roles 
  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\Tree,
 17:     App\Lib\Statics\Cons;
 18: 
 19: /**
 20:  * Objek Rolí
 21:  */
 22: class Roles extends Object {
 23: 
 24:     /**
 25:      * Pole Rolí s předky a potomky
 26:      * @var array
 27:      */
 28:     private $roles = array();
 29: 
 30:     /**
 31:      * setříděná tabulka s rolemi
 32:      * @var Nette\Database\Table\Selection 
 33:      */
 34:     private $source;
 35: 
 36:     /**
 37:      * Konstruktor načte tabulku Rolí z database
 38:      * @param \App\Model\Database $database
 39:      */
 40:     public function __construct(Database $database) {
 41:     $this->source = (new Tree(Cons::TABLE_ACL . '_' . Cons::TABLE_ROLES, $database))
 42:         ->data
 43:         ->where(Cons::COLUMN_PARENT . ' NOT', NULL);
 44:     foreach ($this->source as $activeRow) {
 45:         $parent = $activeRow->ref(Cons::COLUMN_PARENT)[Cons::COLUMN_PATH];
 46:         $this->addRole($activeRow[Cons::COLUMN_PATH], ($parent === NULL || $parent === '') ? NULL : $parent);
 47:     }
 48:     }
 49: 
 50:     /**
 51:      * Přidá Doli do $roles a Databáze. Kontroluje existenci rodiče 
 52:      * @param string $role
 53:      * @param integer $parent
 54:      * @param string $parentName
 55:      */
 56:     public function setRole($role, $parent = NULL, $parentName = NULL) {
 57:     $data = array(Cons::COLUMN_NAME => $role);
 58:     if ($parent !== NULL) {
 59:         $parent = $this->source->findById($parent);
 60:         $data[Cons::COLUMN_PARENT] = $parent[Cons::COLUMN_ID];
 61:         $parentName = $parent[Cons::COLUMN_PATH];
 62:     }
 63:     $this->addRole($role, $parentName);
 64:     $this->source->insert($data);
 65:     }
 66: 
 67:     /**
 68:      * Vrací TRUE pokud je zadaná role v seznamu.
 69:      * @param  string $role Název Role
 70:      * @return bool
 71:      */
 72:     public function hasRole($role) {
 73:     $this->checkRole($role, FALSE);
 74:     return isset($this->roles[$role]);
 75:     }
 76: 
 77:     /**
 78:      * Vrácí pole všech Rolí
 79:      * @return array Pole rolí
 80:      */
 81:     public function getRoles() {
 82:     return $this->roles;
 83:     }
 84: 
 85:     /**
 86:      * Přidá do seznamu Roli s jedinečným identifikátorem 
 87:      * @param  string $role Role (unique)
 88:      * @param  string $parent Název předka (musí již existovat)
 89:      * @return \App\Model\Roles
 90:      * @throws Nette\InvalidArgumentException
 91:      * @throws Nette\InvalidStateException
 92:      */
 93:     private function addRole($role, $parent = NULL) {
 94:     $this->checkRole($role, FALSE);
 95:     if (isset($this->roles[$role])) {
 96:         throw new Nette\InvalidStateException("Role '$role' již v seznamu existuje.");
 97:     }
 98:     if ($parent !== NULL) {
 99:         $this->checkRole($parent);
100:         $this->roles[$parent]['childs'][] = $role;
101:     }
102:     $this->roles[$role] = array('parent' => $parent, 'childs' => array());
103:     return $this;
104:     }
105: 
106:     /**
107:      * Zkontroluje, zda je zdroj validní a existuje
108:      * @param  string
109:      * @param  bool
110:      * @throws Nette\InvalidStateException
111:      * @return void
112:      */
113:     private function checkRole($role, $need = TRUE) {
114:     if (!is_string($role) || $role === '') {
115:         throw new Nette\InvalidArgumentException('Role musí být řetězec a nesmí být prázdný.');
116:     } elseif ($need && !isset($this->roles[$role])) {
117:         throw new Nette\InvalidStateException("Role '$role' neexistuje.");
118:     }
119:     }
120: }
121: 
sberp API API documentation generated by ApiGen