From d35a6745054977b1398e368c3cc78c414bbdd45d Mon Sep 17 00:00:00 2001 From: iglocska Date: Wed, 24 Aug 2022 11:39:56 +0200 Subject: [PATCH] chg: [navigation] added keycloak self management - also some changes to the navigation system --- src/Controller/Component/Navigation/Users.php | 17 +++++++++++++++++ src/Controller/Component/Navigation/base.php | 1 + .../Component/NavigationComponent.php | 12 ++++++++++++ .../layouts/header/header-breadcrumb.php | 6 +++++- 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Controller/Component/Navigation/Users.php b/src/Controller/Component/Navigation/Users.php index 21fb76b..0b2030a 100644 --- a/src/Controller/Component/Navigation/Users.php +++ b/src/Controller/Component/Navigation/Users.php @@ -1,6 +1,7 @@ bcf; $request = $this->request; $passedData = $this->request->getParam('pass'); + $currentUserId = $this->currentUserId; $currentUser = $this->currentUser; $this->bcf->addLink('Users', 'view', 'UserSettings', 'index', function ($config) use ($bcf, $request, $passedData, $currentUser) { if (!empty($passedData[0])) { @@ -69,6 +71,21 @@ class UsersNavigation extends BaseNavigation } return []; }); + if ( + !empty(Configure::read('keycloak.enabled')) && + !empty(Configure::read('keycloak.provider.baseUrl')) && + !empty(Configure::read('keycloak.provider.realm')) && + $currentUserId == $passedData[0] + ) { + $url = sprintf( + '%s/realms/%s/account', + Configure::read('keycloak.provider.baseUrl'), + Configure::read('keycloak.provider.realm') + ); + foreach (['edit', 'view', 'settings'] as $sourceAction) { + $this->bcf->addCustomLink('Users', $sourceAction, $url, __('Manage KeyCloak Account')); + } + } $this->bcf->addLink('Users', 'settings', 'Users', 'view', function ($config) use ($bcf, $request, $passedData) { if (!empty($passedData[0])) { diff --git a/src/Controller/Component/Navigation/base.php b/src/Controller/Component/Navigation/base.php index 30f904a..daf39ce 100644 --- a/src/Controller/Component/Navigation/base.php +++ b/src/Controller/Component/Navigation/base.php @@ -12,6 +12,7 @@ class BaseNavigation { $this->bcf = $bcf; $this->request = $request; + $this->currentUserId = $this->request->getAttribute('identity')->getIdentifier(); $this->viewVars = $viewVars; } diff --git a/src/Controller/Component/NavigationComponent.php b/src/Controller/Component/NavigationComponent.php index 9df48be..86da134 100644 --- a/src/Controller/Component/NavigationComponent.php +++ b/src/Controller/Component/NavigationComponent.php @@ -346,6 +346,18 @@ class BreadcrumbFactory $this->endpoints[$sourceController][$sourceAction]['links'] = $links; } + public function addCustomLink(string $sourceController, string $sourceAction, string $targetUrl, string $label, $overrides = []) + { + $routeSourceConfig = $this->getRouteConfig($sourceController, $sourceAction, true); + $links = array_merge($routeSourceConfig['links'] ?? [], [[ + 'url' => $targetUrl, + 'icon' => 'link', + 'label' => $label, + 'route_path' => 'foo:bar' + ]]); + $this->endpoints[$sourceController][$sourceAction]['links'] = $links; + } + public function addAction(string $sourceController, string $sourceAction, string $targetController, string $targetAction, $overrides = []) { $routeSourceConfig = $this->getRouteConfig($sourceController, $sourceAction, true); diff --git a/templates/element/layouts/header/header-breadcrumb.php b/templates/element/layouts/header/header-breadcrumb.php index 9ac92fa..4f9560e 100644 --- a/templates/element/layouts/header/header-breadcrumb.php +++ b/templates/element/layouts/header/header-breadcrumb.php @@ -49,7 +49,11 @@ if (!empty($breadcrumb)) { if (!empty($lastCrumb['links'])) { // dd($lastCrumb['links']); foreach ($lastCrumb['links'] as $i => $linkEntry) { - $active = $linkEntry['route_path'] == $lastCrumb['route_path']; + if (empty($linkEntry['route_path'])) { + $active = false; + } else { + $active = $linkEntry['route_path'] == $lastCrumb['route_path']; + } if (!empty($linkEntry['url_vars'])) { $linkEntry['url'] = $this->DataFromPath->buildStringFromDataPath($linkEntry['url'], $entity, $linkEntry['url_vars']); }