diff --git a/src/Controller/Component/Navigation/Individuals.php b/src/Controller/Component/Navigation/Individuals.php index e592e13..f2ed784 100644 --- a/src/Controller/Component/Navigation/Individuals.php +++ b/src/Controller/Component/Navigation/Individuals.php @@ -5,4 +5,21 @@ require_once(APP . 'Controller' . DS . 'Component' . DS . 'Navigation' . DS . 'b class IndividualsNavigation extends BaseNavigation { + public function addLinks() + { + $controller = 'Individuals'; + if (empty($this->viewVars['canEdit'])) { + $this->bcf->removeLink($controller, 'view', $controller, 'edit'); + $this->bcf->removeLink($controller, 'edit', $controller, 'edit'); + } + } + + public function addActions() + { + $controller = 'Individuals'; + if (empty($this->viewVars['canDelete'])) { + $this->bcf->removeAction($controller, 'view', $controller, 'delete'); + $this->bcf->removeAction($controller, 'edit', $controller, 'delete'); + } + } } diff --git a/src/Controller/Component/NavigationComponent.php b/src/Controller/Component/NavigationComponent.php index 452d575..301c308 100644 --- a/src/Controller/Component/NavigationComponent.php +++ b/src/Controller/Component/NavigationComponent.php @@ -402,6 +402,20 @@ class BreadcrumbFactory } } + public function removeAction(string $sourceController, string $sourceAction, string $targetController, string $targetAction) + { + $routeSourceConfig = $this->getRouteConfig($sourceController, $sourceAction, true); + if (!empty($routeSourceConfig['actions'])) { + foreach ($routeSourceConfig['actions'] as $i => $routeConfig) { + if ($routeConfig['controller'] == $targetController && $routeConfig['action'] == $targetAction) { + unset($routeSourceConfig['actions'][$i]); + $this->endpoints[$sourceController][$sourceAction]['actions'] = $routeSourceConfig['actions']; + break; + } + } + } + } + public function getRouteConfig($controller, $action, $fullRoute = false) { $routeConfig = $this->get($controller, $action); diff --git a/src/Controller/IndividualsController.php b/src/Controller/IndividualsController.php index 46e52e8..8702891 100644 --- a/src/Controller/IndividualsController.php +++ b/src/Controller/IndividualsController.php @@ -69,6 +69,7 @@ class IndividualsController extends AppController return $responsePayload; } $this->set('canEdit', $this->canEdit($id)); + $this->set('canDelete', $this->canDelete($id)); } public function edit($id) @@ -89,6 +90,8 @@ class IndividualsController extends AppController if (!empty($responsePayload)) { return $responsePayload; } + $this->set('canEdit', $this->canEdit($id)); + $this->set('canDelete', $this->canDelete($id)); $this->render('add'); } @@ -155,4 +158,17 @@ class IndividualsController extends AppController } return false; } + + private function canDelete($indId): bool + { + $associatedUsersCount = $this->Individuals->Users->find()->select(['id'])->where(['individual_id' => $indId])->count(); + if ($associatedUsersCount > 0) { + return false; + } + $currentUser = $this->ACL->getUser(); + if ($currentUser['role']['perm_admin']) { + return true; + } + return false; + } }