chg: [navigation:individuals] Only show edit and deletion buttons if users are allowed to do it

refacto/CRUDComponent
Sami Mokaddem 2023-09-06 09:48:16 +02:00
parent 3514f8bd7c
commit 7377e77204
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
3 changed files with 47 additions and 0 deletions

View File

@ -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');
}
}
}

View File

@ -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);

View File

@ -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;
}
}