Advertisement
Guest User

Untitled

a guest
May 6th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.96 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controller\Component;
  4.  
  5. use Cake\Controller\Component;
  6. use Cake\ORM\TableRegistry;
  7.  
  8. class AclComponent extends Component {
  9.  
  10.     public $components = ['Auth'];
  11.  
  12.     public function initialize(array $config) {
  13.         $this->Aros = TableRegistry::get('Aros');
  14.         $this->Acos = TableRegistry::get('Acos');
  15.         $this->AcosAros = TableRegistry::get('AcosAros');
  16.     }
  17.  
  18.     public function check($aco, $aro_id = null) {
  19.         if ($aro_id === null) {
  20.             $aro_id = $this->Auth->user('aro_id');
  21.         }
  22.  
  23.         return $this->AcosAros->find('all', [
  24.             'conditions' => [
  25.                 'aro_id' => $aro_id,
  26.                 'Acos.name' => $aco
  27.             ],
  28.             'fields' => ['allowed']
  29.         ])->join([
  30.             'table' => 'acos',
  31.             'alias' => 'Acos',
  32.             'type' => 'INNER',
  33.             'conditions' => 'AcosAros.aco_id = Acos.id'
  34.         ])->first()->allowed === 1;
  35.     }
  36.  
  37.     public function multiCheck(array $acos, $aro_id = null, $type = 'AND') {
  38.  
  39.         if (!is_array($acos) || empty($acos)) {
  40.             throw new \Exception("Acos must be array!");
  41.         }
  42.  
  43.         if ($aro_id === null) {
  44.             $aro_id = $this->Auth->user('aro_id');
  45.         }
  46.  
  47.         $result = $this->AcosAros->find('all', [
  48.             'conditions' => [
  49.                 'aro_id' => $aro_id,
  50.                 'Acos.name IN' => $acos
  51.             ],
  52.             'fields' => ['allowed']
  53.         ])->join([
  54.             'table' => 'acos',
  55.             'alias' => 'Acos',
  56.             'type' => 'INNER',
  57.             'conditions' => 'AcosAros.aco_id = Acos.id'
  58.         ]);
  59.  
  60.         $result = $result->extract('allowed')->toArray();
  61.  
  62.         if (count($acos) !== count($result)) {
  63.             throw new \Exception("One or more parameters in acos field is not correct.");
  64.         }
  65.  
  66.         if ($type === 'AND') {
  67.             if (in_array(0, $result)) {
  68.                 return false;
  69.             } else {
  70.                 return true;
  71.             }
  72.         } else if ($type === 'OR') {
  73.             if (in_array(1, $result)) {
  74.                 return true;
  75.             } else {
  76.                 return false;
  77.             }
  78.         }
  79.     }
  80.  
  81.     public function insertAco($aco = null, $value = 0) {
  82.         if ($aco === null) {
  83.             throw new \Exception("Aco is not specified!");
  84.         }
  85.  
  86.         if ($value !== 0 && $value !== 1) {
  87.             throw new \Exception("Value must be 0 or 1");
  88.         }
  89.  
  90.         $aco = $this->Acos->save($this->Acos->newEntity(['name' => $aco]));
  91.         if (!$aco->id) {
  92.             throw new \Exception("Aco not added into 'acos' table.");
  93.         }
  94.  
  95.         $aros = $this->Aros->find('all', [
  96.             'fields' => ['id']
  97.         ]);
  98.  
  99.         $aros->each(function ($obj) use ($aco, $value) {
  100.             $this->AcosAros->save($this->AcosAros->newEntity([
  101.                 'aco_id' => $aco->id,
  102.                 'aro_id' => $obj->id,
  103.                 'allowed' => $value
  104.             ]));
  105.         });
  106.  
  107.  
  108.     }
  109.  
  110.     public function insertAro($aro = null, $value = 0) {
  111.         if ($aro === null) {
  112.             throw new \Exception("Aro is not specified!");
  113.         }
  114.  
  115.         if ($value !== 0 && $value !== 1) {
  116.             throw new \Exception("Value must be 0 or 1");
  117.         }
  118.  
  119.         $aro = $this->Aros->save($this->Aros->newEntity(['name' => $aro]));
  120.         if (!$aro) {
  121.             throw new \Exception("Aro not added into 'aros' table.");
  122.         }
  123.  
  124.         $acos = $this->Acos->find('all', [
  125.             'fields' => ['id']
  126.         ]);
  127.  
  128.         $acos->each(function ($obj) use ($aro, $value) {
  129.             $this->AcosAros->save($this->AcosAros->newEntity([
  130.                 'aco_id' => $obj->id,
  131.                 'aro_id' => $aro->id,
  132.                 'allowed' => $value
  133.             ]));
  134.         });
  135.     }
  136.  
  137.     private function _changeAco($aco, $aro_id, $allowed) {
  138.         if ($aco === null) {
  139.             throw new \Exception("Aco is not specified!");
  140.         }
  141.  
  142.         if ($aro_id === null) {
  143.             $aro_id = $this->Auth->user('aro_id');
  144.         }
  145.  
  146.         $acoAro = $this->AcosAros->find('all', [
  147.             'conditions' => [
  148.                 'Acos.name' => $aco,
  149.                 'aro_id' => $aro_id
  150.             ]
  151.         ])->join([
  152.             'table' => 'acos',
  153.             'alias' => 'Acos',
  154.             'type' => 'INNER',
  155.             'conditions' => 'AcosAros.aco_id = Acos.id'
  156.         ])->first();
  157.  
  158.         $acoAro->allowed = $allowed;
  159.  
  160.         $this->AcosAros->save($acoAro);
  161.     }
  162.  
  163.     public function allow($aco = null, $aro_id = null) {
  164.         $this->_changeAco($aco, $aro_id, 1);
  165.     }
  166.  
  167.     public function deny($aco = null, $aro_id = null) {
  168.         $this->_changeAco($aco, $aro_id, 0);
  169.     }
  170.  
  171.     public function _init() {
  172.         $acos = [
  173.             'home/index',
  174.             'examples/index',
  175.             'examples/bootbox',
  176.             'examples/dropzone',
  177.             'examples/documents',
  178.             'examples/file_upload',
  179.             'examples/delete_picture',
  180.             'examples/delete_file',
  181.             'examples/form_validation',
  182.             'examples/upload_docs',
  183.             'countries/index',
  184.             'countries/view',
  185.             'countries/add',
  186.             'countries/edit',
  187.             'countries/delete',
  188.             'countries/pdf',
  189.             'countries/excel',
  190.             'countries/html',
  191.             'configurations/index',
  192.             'configurations/view',
  193.             'configurations/add',
  194.             'configurations/add_dashboard_panel',
  195.             'configurations/edit_dashboard_panel',
  196.             'configurations/edit',
  197.             'configurations/delete',
  198.             'configurations/delete_dashboard_item',
  199.             'configurations/delete_dashboard_panel',
  200.             'languages/index',
  201.             'languages/view',
  202.             'languages/add',
  203.             'languages/edit',
  204.             'languages/delete',
  205.             'languages/pdf',
  206.             'languages/excel',
  207.             'languages/html',
  208.             'timezones/index',
  209.             'timezones/view',
  210.             'timezones/add',
  211.             'timezones/edit',
  212.             'timezones/delete',
  213.             'timezones/pdf',
  214.             'timezones/excel',
  215.             'timezones/html',
  216.             'users/index',
  217.             'users/view',
  218.             'users/add',
  219.             'users/edit',
  220.             'users/delete',
  221.             'users/login',
  222.             'users/logout',
  223.             'users/change_password',
  224.             'users/pdf',
  225.             'users/excel',
  226.             'users/html',
  227.             'users/lost_password',
  228.             'users/reset_password',
  229.             'users/upload_picture_with_ajax'
  230.         ];
  231.  
  232. //        foreach ($acos as $key => $value) {
  233. //            $aco = $this->Acos->save($this->Acos->newEntity(['name' => $value]));
  234. //        }
  235.  
  236. //        foreach ($aros as $key => $value) {
  237. //            $aro = $this->Aros->save($this->Aros->newEntity(['name' => $value]));
  238. //        }
  239.  
  240.         $aros = $this->Aros->find('all');
  241.         $acos = $this->Acos->find('all');
  242.  
  243.         foreach ($aros as $aroKey => $aroValue) {
  244.             foreach ($acos as $acoKey => $acoValue) {
  245.                 $acoAro = $this->AcosAros->save($this->AcosAros->newEntity(['aro_id' => $aroValue->id, 'aco_id' => $acoValue->id, 'allowed' => 1]));
  246.             }
  247.         }
  248.     }
  249.  
  250.     public function generate() {
  251.         $acos = $this->Acos->find('all');
  252. //        foreach($acos as $key => $value) {
  253. //            echo "[" . '<br>';
  254. //            echo "'id' => " . $value->id . ',<br>';
  255. //            echo "'name' => '" . $value->name . "'<br>";
  256. //            echo "]," . '<br>';
  257. //        }
  258.  
  259. //        foreach($acos as $key => $value) {
  260. //            echo "[" . '<br>';
  261. //            echo "'aco_id' => " . $value->id . ',<br>';
  262. //            echo "'aro_id' => " . 1 . ',<br>';
  263. //            echo "'allowed' => '" . 1 . "'<br>";
  264. //            echo "]," . '<br>';
  265. //        }
  266.     }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement