Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controller\Component;
- use Cake\Controller\Component;
- use Cake\ORM\TableRegistry;
- class AclComponent extends Component {
- public $components = ['Auth'];
- public function initialize(array $config) {
- $this->Aros = TableRegistry::get('Aros');
- $this->Acos = TableRegistry::get('Acos');
- $this->AcosAros = TableRegistry::get('AcosAros');
- }
- public function check($aco, $aro_id = null) {
- if ($aro_id === null) {
- $aro_id = $this->Auth->user('aro_id');
- }
- return $this->AcosAros->find('all', [
- 'conditions' => [
- 'aro_id' => $aro_id,
- 'Acos.name' => $aco
- ],
- 'fields' => ['allowed']
- ])->join([
- 'table' => 'acos',
- 'alias' => 'Acos',
- 'type' => 'INNER',
- 'conditions' => 'AcosAros.aco_id = Acos.id'
- ])->first()->allowed === 1;
- }
- public function multiCheck(array $acos, $aro_id = null, $type = 'AND') {
- if (!is_array($acos) || empty($acos)) {
- throw new \Exception("Acos must be array!");
- }
- if ($aro_id === null) {
- $aro_id = $this->Auth->user('aro_id');
- }
- $result = $this->AcosAros->find('all', [
- 'conditions' => [
- 'aro_id' => $aro_id,
- 'Acos.name IN' => $acos
- ],
- 'fields' => ['allowed']
- ])->join([
- 'table' => 'acos',
- 'alias' => 'Acos',
- 'type' => 'INNER',
- 'conditions' => 'AcosAros.aco_id = Acos.id'
- ]);
- $result = $result->extract('allowed')->toArray();
- if (count($acos) !== count($result)) {
- throw new \Exception("One or more parameters in acos field is not correct.");
- }
- if ($type === 'AND') {
- if (in_array(0, $result)) {
- return false;
- } else {
- return true;
- }
- } else if ($type === 'OR') {
- if (in_array(1, $result)) {
- return true;
- } else {
- return false;
- }
- }
- }
- public function insertAco($aco = null, $value = 0) {
- if ($aco === null) {
- throw new \Exception("Aco is not specified!");
- }
- if ($value !== 0 && $value !== 1) {
- throw new \Exception("Value must be 0 or 1");
- }
- $aco = $this->Acos->save($this->Acos->newEntity(['name' => $aco]));
- if (!$aco->id) {
- throw new \Exception("Aco not added into 'acos' table.");
- }
- $aros = $this->Aros->find('all', [
- 'fields' => ['id']
- ]);
- $aros->each(function ($obj) use ($aco, $value) {
- $this->AcosAros->save($this->AcosAros->newEntity([
- 'aco_id' => $aco->id,
- 'aro_id' => $obj->id,
- 'allowed' => $value
- ]));
- });
- }
- public function insertAro($aro = null, $value = 0) {
- if ($aro === null) {
- throw new \Exception("Aro is not specified!");
- }
- if ($value !== 0 && $value !== 1) {
- throw new \Exception("Value must be 0 or 1");
- }
- $aro = $this->Aros->save($this->Aros->newEntity(['name' => $aro]));
- if (!$aro) {
- throw new \Exception("Aro not added into 'aros' table.");
- }
- $acos = $this->Acos->find('all', [
- 'fields' => ['id']
- ]);
- $acos->each(function ($obj) use ($aro, $value) {
- $this->AcosAros->save($this->AcosAros->newEntity([
- 'aco_id' => $obj->id,
- 'aro_id' => $aro->id,
- 'allowed' => $value
- ]));
- });
- }
- private function _changeAco($aco, $aro_id, $allowed) {
- if ($aco === null) {
- throw new \Exception("Aco is not specified!");
- }
- if ($aro_id === null) {
- $aro_id = $this->Auth->user('aro_id');
- }
- $acoAro = $this->AcosAros->find('all', [
- 'conditions' => [
- 'Acos.name' => $aco,
- 'aro_id' => $aro_id
- ]
- ])->join([
- 'table' => 'acos',
- 'alias' => 'Acos',
- 'type' => 'INNER',
- 'conditions' => 'AcosAros.aco_id = Acos.id'
- ])->first();
- $acoAro->allowed = $allowed;
- $this->AcosAros->save($acoAro);
- }
- public function allow($aco = null, $aro_id = null) {
- $this->_changeAco($aco, $aro_id, 1);
- }
- public function deny($aco = null, $aro_id = null) {
- $this->_changeAco($aco, $aro_id, 0);
- }
- public function _init() {
- $acos = [
- 'home/index',
- 'examples/index',
- 'examples/bootbox',
- 'examples/dropzone',
- 'examples/documents',
- 'examples/file_upload',
- 'examples/delete_picture',
- 'examples/delete_file',
- 'examples/form_validation',
- 'examples/upload_docs',
- 'countries/index',
- 'countries/view',
- 'countries/add',
- 'countries/edit',
- 'countries/delete',
- 'countries/pdf',
- 'countries/excel',
- 'countries/html',
- 'configurations/index',
- 'configurations/view',
- 'configurations/add',
- 'configurations/add_dashboard_panel',
- 'configurations/edit_dashboard_panel',
- 'configurations/edit',
- 'configurations/delete',
- 'configurations/delete_dashboard_item',
- 'configurations/delete_dashboard_panel',
- 'languages/index',
- 'languages/view',
- 'languages/add',
- 'languages/edit',
- 'languages/delete',
- 'languages/pdf',
- 'languages/excel',
- 'languages/html',
- 'timezones/index',
- 'timezones/view',
- 'timezones/add',
- 'timezones/edit',
- 'timezones/delete',
- 'timezones/pdf',
- 'timezones/excel',
- 'timezones/html',
- 'users/index',
- 'users/view',
- 'users/add',
- 'users/edit',
- 'users/delete',
- 'users/login',
- 'users/logout',
- 'users/change_password',
- 'users/pdf',
- 'users/excel',
- 'users/html',
- 'users/lost_password',
- 'users/reset_password',
- 'users/upload_picture_with_ajax'
- ];
- // foreach ($acos as $key => $value) {
- // $aco = $this->Acos->save($this->Acos->newEntity(['name' => $value]));
- // }
- // foreach ($aros as $key => $value) {
- // $aro = $this->Aros->save($this->Aros->newEntity(['name' => $value]));
- // }
- $aros = $this->Aros->find('all');
- $acos = $this->Acos->find('all');
- foreach ($aros as $aroKey => $aroValue) {
- foreach ($acos as $acoKey => $acoValue) {
- $acoAro = $this->AcosAros->save($this->AcosAros->newEntity(['aro_id' => $aroValue->id, 'aco_id' => $acoValue->id, 'allowed' => 1]));
- }
- }
- }
- public function generate() {
- $acos = $this->Acos->find('all');
- // foreach($acos as $key => $value) {
- // echo "[" . '<br>';
- // echo "'id' => " . $value->id . ',<br>';
- // echo "'name' => '" . $value->name . "'<br>";
- // echo "]," . '<br>';
- // }
- // foreach($acos as $key => $value) {
- // echo "[" . '<br>';
- // echo "'aco_id' => " . $value->id . ',<br>';
- // echo "'aro_id' => " . 1 . ',<br>';
- // echo "'allowed' => '" . 1 . "'<br>";
- // echo "]," . '<br>';
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement