From b7a7b6ba8a54eefa2a150c83e382c605ecf8a404 Mon Sep 17 00:00:00 2001 From: samapraku Date: Tue, 13 Oct 2020 15:55:34 +0200 Subject: [PATCH 1/2] Redirect to login page if not logged in --- src/bb-di.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/bb-di.php b/src/bb-di.php index f6a74de07..c7a089e4d 100644 --- a/src/bb-di.php +++ b/src/bb-di.php @@ -168,17 +168,37 @@ $di['twig'] = $di->factory(function () use ($di) { $di['is_client_logged'] = function() use($di) { if(!$di['auth']->isClientLoggedIn()) { - $url = $di['url']->link('login'); - header("Location: $url"); - throw new Exception('Client is not logged in'); + $api_str = '/api/'; + $url = $di['request']->getQuery('_url'); + if (strncasecmp($url, $api_str , strlen($api_str)) === 0) { + //Throw Exception if api request + throw new Exception('Client is not logged in'); + } + else { + //Redirect to login page if browser request + $login_url = $di['url']->link('login'); + header("Location: $login_url"); + } + } return true; }; $di['is_admin_logged'] = function() use($di) { + if(!$di['auth']->isAdminLoggedIn()) { + $api_str = '/api/'; + $url = $di['request']->getQuery('_url'); + if (strncasecmp($url, $api_str , strlen($api_str)) === 0) { + //Throw Exception if api request throw new Exception('Admin is not logged in'); } + else { + //Redirect to login page if browser request + $login_url = $di['url']->adminLink('staff/login'); + header("Location: $login_url"); + } + } return true; }; From 04876cb2863e50769fbcac645156b8abbb0d999d Mon Sep 17 00:00:00 2001 From: samapraku Date: Wed, 14 Oct 2020 17:11:38 +0200 Subject: [PATCH 2/2] add only admin or client api at a time --- src/bb-modules/System/Service.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/bb-modules/System/Service.php b/src/bb-modules/System/Service.php index 75f40543e..711600cc4 100644 --- a/src/bb-modules/System/Service.php +++ b/src/bb-modules/System/Service.php @@ -285,6 +285,7 @@ class Service public function renderString($tpl, $try_render, $vars) { $twig = $this->di['twig']; + //add client api if _client_id is set if(isset($vars['_client_id'])) { $identity = $this->di['db']->load('Client', $vars['_client_id']); if($identity instanceof \Model_Client) { @@ -296,12 +297,14 @@ class Service } } } - + else{ + // attempt adding admin api to twig try { $twig->addGlobal('admin', $this->di['api_admin']); } catch(\Exception $e) { //skip if admin is not logged in } + } try { $template = $twig->load($tpl); @@ -1582,4 +1585,4 @@ class Service $this->di['session']->delete('pending_messages'); return true; } -} +} \ No newline at end of file