HosipLan

Untitled

Aug 5th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.45 KB | None | 0 0
  1. class AclModel extends Object
  2. {
  3.     /**
  4.      * Put in to array parents of specific role
  5.      *
  6.      * @param integer ID of parent role
  7.      * @param string Key name of parent role
  8.      */
  9.     public function getParentRole($parent_id, $parent_key, &$roles) {
  10.         $sql = dibi::query('SELECT id, key_name
  11.                                FROM ['.TABLE_ROLES.']
  12.                                WHERE %and;', array('parent_id' => $parent_id));
  13.         $rows = $sql->fetchAll();
  14.         if (count($sql)) {
  15.             foreach ($rows as $row) {
  16.                 $roles[] = array('key_name' => $row->key_name, 'parent_key' => $parent_key);
  17.                 $this->getParentRole($row->id, $row->key_name, $roles);
  18.             }
  19.         }
  20.     }
  21.     /**
  22.      * Return all roles hierarchically ordered
  23.      *
  24.      * @return  array
  25.      */
  26.     public function getRoles() {
  27.         $roles = array();
  28.         $this->getParentRole(NULL, NULL, $roles);
  29.         return $roles;
  30.     }
  31.  
  32.     /**
  33.      * Put in to array parents of specific resource
  34.      *
  35.      * @param integer ID of parent resource
  36.      * @param string Key name of parent resource
  37.      * @param array Array of all resource
  38.      */
  39.     public function getParentResource($parent_id, $parent_key, &$resources) {
  40.         $sql = dibi::query('SELECT id, key_name
  41.                                FROM ['.TABLE_RESOURCES.']
  42.                                WHERE %and;', array('parent_id' => $parent_id));
  43.         $rows = $sql->fetchAll();
  44.         if (count($sql)) {
  45.             foreach ($rows as $row) {
  46.                 $resources[] = array('key_name' => $row->key_name, 'parent_key' => $parent_key);
  47.                 $this->getParentResource($row->id, $row->key_name, $resources);
  48.             }
  49.         }
  50.     }
  51.     /**
  52.      * Return all resources hierarchically ordered
  53.      *
  54.      * @return  array
  55.      */
  56.     public function getResources() {
  57.         $resources = array();
  58.         $this->getParentResource(NULL, NULL, $resources);
  59.         return $resources;
  60.     }
  61.  
  62.     /**
  63.      * Return all rules of permissions
  64.      *
  65.      * @return  object
  66.      */
  67.     public function getRules() {
  68.          $sql = dibi::query('
  69.            SELECT
  70.                a.access as access,
  71.                ro.key_name as role,
  72.                re.key_name as resource,
  73.                p.key_name as privilege
  74.                FROM ['.TABLE_ACL.'] a
  75.                JOIN ['.TABLE_ROLES.'] ro ON (a.role_id = ro.id)
  76.                LEFT JOIN ['.TABLE_RESOURCES.'] re ON (a.resource_id = re.id)
  77.                LEFT JOIN ['.TABLE_PRIVILEGES.'] p ON (a.privilege_id = p.id)
  78.                ORDER BY a.id ASC
  79.        ');
  80.          $sql->setType('access', Dibi::BOOL);
  81.          return $sql->fetchAll();
  82.     }
  83.  
  84.  
  85.     public function createPermission()
  86.     {
  87.     $permission = new Nette\Security\Permission();
  88.  
  89.         foreach($this->->getRoles() as $role) {
  90.             $permission->addRole($role['key_name'], $role['parent_key']);
  91.     }
  92.  
  93.         foreach($this->getResources() as $resource) {
  94.             $permission->addResource($resource['key_name'], $resource['parent_key']);
  95.     }
  96.  
  97.         foreach($this->getRules() as $rule) {
  98.             $permission->{$rule->access ? 'allow' : 'deny'}($rule->role, $rule->resource, $rule->privilege);
  99.     }
  100.  
  101.     return $permission;
  102.     }
  103. }
  104.  
  105.  
  106.  
  107.  
  108. # konfig
  109.  
  110.  
  111. services:
  112.     aclFactory:
  113.         class: Acl
  114.         arguments: [@database]
  115.    
  116.     authorizator:
  117.         factory: [@aclFactory, 'createPermission']
Advertisement
Add Comment
Please, Sign In to add comment