From e3bd2c1c3b51407cd288330e03082a2cfecd5237 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 10 Mar 2023 15:08:56 +0900 Subject: [PATCH] Remove more _SESSION calls in classes, test updates Admin\EditBase now has ACL\Login class as mandatory class parameter Output\Form\Generate has loginAcl array parameter as mandatory --- 4dev/tests/ACL/CoreLibsACLLoginTest.php | 8 ++++++ www/admin/class_test.output.form.php | 4 +++ www/includes/edit_base.php | 2 +- www/includes/templates/admin/edit_body.tpl | 2 +- www/lib/CoreLibs/ACL/Login.php | 31 ++++++++++++++++++++++ www/lib/CoreLibs/Admin/EditBase.php | 31 +++++++++++----------- www/lib/CoreLibs/DB/IO.php | 4 +-- www/lib/CoreLibs/Debug/Logging.php | 20 +++++++------- www/lib/CoreLibs/Output/Form/Generate.php | 12 ++++++--- www/lib/CoreLibs/Template/SmartyExtend.php | 10 ++++--- 10 files changed, 87 insertions(+), 37 deletions(-) diff --git a/4dev/tests/ACL/CoreLibsACLLoginTest.php b/4dev/tests/ACL/CoreLibsACLLoginTest.php index 8b8b125d..bd55d223 100644 --- a/4dev/tests/ACL/CoreLibsACLLoginTest.php +++ b/4dev/tests/ACL/CoreLibsACLLoginTest.php @@ -7,6 +7,14 @@ namespace tests; use PHPUnit\Framework\TestCase; use PHPUnit\Framework\MockObject\MockObject; +/* +Not yet covered tests: +- loginGetLocale +- loginGetHeaderColor +- loginGetPages +- loginGetEuid +*/ + /** * Test class for ACL\Login * @coversDefaultClass \CoreLibs\ACL\Login diff --git a/www/admin/class_test.output.form.php b/www/admin/class_test.output.form.php index 47ec04cc..0a6d5c9d 100644 --- a/www/admin/class_test.output.form.php +++ b/www/admin/class_test.output.form.php @@ -75,6 +75,10 @@ $form = new CoreLibs\Output\Form\Generate( DB_CONFIG, $log, $l10n, + [ + 'base' => 10, + 'admin' => 0 + ], table_arrays: $table_arrays ); diff --git a/www/includes/edit_base.php b/www/includes/edit_base.php index 6834ab54..3b22b2b8 100644 --- a/www/includes/edit_base.php +++ b/www/includes/edit_base.php @@ -77,8 +77,8 @@ $edit_base = new CoreLibs\Admin\EditBase( DB_CONFIG, $log, $l10n, + $login, [ - 'default_acl_level' => DEFAULT_ACL_LEVEL, 'cache_id' => CACHE_ID, 'compile_id' => COMPILE_ID ] diff --git a/www/includes/templates/admin/edit_body.tpl b/www/includes/templates/admin/edit_body.tpl index 880aa115..9d178ed4 100644 --- a/www/includes/templates/admin/edit_body.tpl +++ b/www/includes/templates/admin/edit_body.tpl @@ -39,7 +39,7 @@ function pop(theURL, winName, features) {
- Hello {$USER_NAME|upper} [{$EUID}] from the group {$GROUP_NAME} with Access Level {$GROUP_LEVEL} + Hello {$USER_NAME|upper} [{$EUID}] from the group {$GROUP_NAME} with Access Level {$ACCESS_LEVEL} diff --git a/www/lib/CoreLibs/ACL/Login.php b/www/lib/CoreLibs/ACL/Login.php index 4d8d795a..d41dd33c 100644 --- a/www/lib/CoreLibs/ACL/Login.php +++ b/www/lib/CoreLibs/ACL/Login.php @@ -2464,6 +2464,37 @@ EOM; { return $this->locale; } + + /** + * return header color or null for not set + * + * @return string|null Header color in RGB hex with leading sharp + */ + public function loginGetHeaderColor(): ?string + { + return $_SESSION['HEADER_COLOR'] ?? null; + } + + /** + * Return the current loaded list of pages the user can access + * + * @return array + */ + public function loginGetPages(): array + { + + return $_SESSION['PAGES'] ?? []; + } + + /** + * Get the current set EUID (edit user id) + * + * @return string EUID as string + */ + public function loginGetEuid(): string + { + return $this->euid; + } } // __END__ diff --git a/www/lib/CoreLibs/Admin/EditBase.php b/www/lib/CoreLibs/Admin/EditBase.php index c96786c4..3933d535 100644 --- a/www/lib/CoreLibs/Admin/EditBase.php +++ b/www/lib/CoreLibs/Admin/EditBase.php @@ -35,6 +35,8 @@ class EditBase private $form; /** @var \CoreLibs\Debug\Logging */ public $log; + /** @var \CoreLibs\ACL\Login */ + public $login; /** * construct form generator @@ -42,15 +44,18 @@ class EditBase * @param array $db_config db config array, mandatory * @param \CoreLibs\Debug\Logging $log Logging class, null auto set * @param \CoreLibs\Language\L10n $l10n l10n language class, null auto set + * @param \CoreLibs\ACL\Login $login login class for ACL settings * @param array $options Various settings options */ public function __construct( array $db_config, \CoreLibs\Debug\Logging $log, \CoreLibs\Language\L10n $l10n, + \CoreLibs\ACL\Login $login, array $options ) { $this->log = $log; + $this->login = $login; // smarty template engine (extended Translation version) $this->smarty = new \CoreLibs\Template\SmartyExtend( $l10n, @@ -64,7 +69,8 @@ class EditBase $this->form = new \CoreLibs\Output\Form\Generate( $db_config, $log, - $l10n + $l10n, + $this->login->loginGetAcl() ); if ($this->form->mobile_phone) { echo "I am sorry, but this page cannot be viewed by a mobile phone"; @@ -274,23 +280,16 @@ class EditBase // MENU START // request some session vars - if (empty($_SESSION['HEADER_COLOR'])) { - $this->DATA['HEADER_COLOR'] = '#E0E2FF'; - } else { - $this->DATA['HEADER_COLOR'] = $_SESSION['HEADER_COLOR']; - } - $this->DATA['USER_NAME'] = $_SESSION['USER_NAME']; - $this->DATA['EUID'] = $_SESSION['EUID']; - $this->DATA['GROUP_NAME'] = $_SESSION['GROUP_NAME']; - $this->DATA['GROUP_LEVEL'] = $_SESSION['GROUP_ACL_LEVEL']; - $PAGES = $_SESSION['PAGES']; + $this->DATA['HEADER_COLOR'] = $this->login->loginGetHeaderColor() ?? '#E0E2FF'; + $this->DATA['USER_NAME'] = $this->login->loginGetAcl()['user_name'] ?? ''; + $this->DATA['EUID'] = $this->login->loginGetEuid(); + $this->DATA['GROUP_NAME'] = $this->login->loginGetAcl()['group_name'] ?? ''; + $this->DATA['ACCESS_LEVEL'] = $this->login->loginGetAcl()['base'] ?? ''; + // below is old and to removed when edit_body.tpl is updates + $this->DATA['GROUP_LEVEL'] = $this->DATA['ACCESS_LEVEL']; + $PAGES = $this->login->loginGetPages(); //$this->form->log->debug('menu', $this->form->log->prAr($PAGES)); - - // build nav from $PAGES ... - if (!isset($PAGES) || !is_array($PAGES)) { - $PAGES = []; - } $menuarray = []; foreach ($PAGES as $PAGE_CUID => $PAGE_DATA) { if ($PAGE_DATA['menu'] && $PAGE_DATA['online']) { diff --git a/www/lib/CoreLibs/DB/IO.php b/www/lib/CoreLibs/DB/IO.php index e593d227..9f878e96 100644 --- a/www/lib/CoreLibs/DB/IO.php +++ b/www/lib/CoreLibs/DB/IO.php @@ -405,9 +405,9 @@ class IO $db_debug_override ?? // from db config setting $db_config['db_debug'] ?? - // should be handled from outside + // [DEPRECATED] should be handled from outside $_SESSION['DB_DEBUG'] ?? - // globals should be deprecated + // [DEPRECATED] globals should be deprecated $GLOBALS['DB_DEBUG'] ?? false ); diff --git a/www/lib/CoreLibs/Debug/Logging.php b/www/lib/CoreLibs/Debug/Logging.php index bae2136f..de4e710e 100644 --- a/www/lib/CoreLibs/Debug/Logging.php +++ b/www/lib/CoreLibs/Debug/Logging.php @@ -251,22 +251,22 @@ class Logging 'debug', $this->options['debug_all'] ?? // for user login, should be handled outside like globals - $_SESSION['DEBUG_ALL'] ?? - $GLOBALS['DEBUG_ALL'] ?? + $_SESSION['DEBUG_ALL'] ?? // DEPRECATED + $GLOBALS['DEBUG_ALL'] ?? // DEPRECATED false ); $this->setLogLevelAll( 'print', $this->options['print_all'] ?? // for user login, should be handled outside like globals - $_SESSION['DEBUG_ALL'] ?? - $GLOBALS['PRINT_ALL'] ?? + $_SESSION['DEBUG_ALL'] ?? // DEPRECATED + $GLOBALS['PRINT_ALL'] ?? // DEPRECATED false ); $this->setLogLevelAll( 'echo', $this->options['echo_all'] ?? - $GLOBALS['ECHO_ALL'] ?? + $GLOBALS['ECHO_ALL'] ?? // DEPRECATED false ); @@ -274,32 +274,32 @@ class Logging // add file date is default on $this->setGetLogPrintFileDate( $this->options['print_file_date'] ?? - $GLOBALS['LOG_PRINT_FILE_DATE'] ?? + $GLOBALS['LOG_PRINT_FILE_DATE'] ?? // DEPRECATED true ); // all other logging file name flags are off $this->setLogPer( 'level', $this->options['per_level'] ?? - $GLOBALS['LOG_PER_LEVEL'] ?? + $GLOBALS['LOG_PER_LEVEL'] ?? // DEPRECATED false ); $this->setLogPer( 'class', $this->options['per_class'] ?? - $GLOBALS['LOG_PER_CLASS'] ?? + $GLOBALS['LOG_PER_CLASS'] ?? // DEPRECATED false ); $this->setLogPer( 'page', $this->options['per_page'] ?? - $GLOBALS['LOG_PER_PAGE'] ?? + $GLOBALS['LOG_PER_PAGE'] ?? // DEPRECATED false ); $this->setLogPer( 'run', $this->options['per_run'] ?? - $GLOBALS['LOG_PER_RUN'] ?? + $GLOBALS['LOG_PER_RUN'] ?? // DEPRECATED false ); // set log per date diff --git a/www/lib/CoreLibs/Output/Form/Generate.php b/www/lib/CoreLibs/Output/Form/Generate.php index 8014141b..65555bc3 100644 --- a/www/lib/CoreLibs/Output/Form/Generate.php +++ b/www/lib/CoreLibs/Output/Form/Generate.php @@ -277,6 +277,8 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO private $acl_admin = 0; /** @var array */ public $security_level; + /** @var array Login ACL */ + public $login_acl = []; // layout publics /** @var int */ public $table_width; @@ -308,6 +310,8 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO * @param array $db_config db config array, mandatory * @param \CoreLibs\Debug\Logging $log Logging class * @param \CoreLibs\Language\L10n $l10n l10n language class + * @param array $login_acl Login ACL array, + * at least base/admin should be set * @param array|null $table_arrays Override table array data * instead of try to load from * include file @@ -317,6 +321,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO array $db_config, \CoreLibs\Debug\Logging $log, \CoreLibs\Language\L10n $l10n, + array $login_acl, ?array $table_arrays = null, ) { // init logger if not set @@ -334,10 +339,10 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO $this->lang_dir = $locale['path']; // load config array // get table array definitions for current page name - + $this->login_acl = $login_acl; // security settings - $this->base_acl_level = (int)$_SESSION['BASE_ACL_LEVEL']; - $this->acl_admin = (int)$_SESSION['ADMIN']; + $this->base_acl_level = $this->login_acl['base'] ?? 0; + $this->acl_admin = $this->login_acl['admin'] ?? 0; // replace any non valid variable names and set my page name $this->my_page_name = str_replace( @@ -375,7 +380,6 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO $this->base_acl_level, $this->acl_admin ); - // $this->log->debug('SESSION FORM', 'sessin: ' . $this->log->prAr($_SESSION)); // here should be a check if the config_array is correct ... if (isset($config_array['show_fields']) && is_array($config_array['show_fields'])) { $this->field_array = $config_array['show_fields']; diff --git a/www/lib/CoreLibs/Template/SmartyExtend.php b/www/lib/CoreLibs/Template/SmartyExtend.php index 981c7fec..8fcbfda4 100644 --- a/www/lib/CoreLibs/Template/SmartyExtend.php +++ b/www/lib/CoreLibs/Template/SmartyExtend.php @@ -563,6 +563,7 @@ class SmartyExtend extends \Smarty * @param string|null $set_page_width PAGE_WIDTH * @param string|null $set_stylesheet STYLESHEET * @param string|null $set_javascript JAVASCRIPT + * @param string|null $set_user_name _SESSION['USER_NAME'] * @return void */ private function setSmartyVars( @@ -579,7 +580,8 @@ class SmartyExtend extends \Smarty ?string $set_admin_javascript = null, ?string $set_page_width = null, ?string $set_stylesheet = null, - ?string $set_javascript = null + ?string $set_javascript = null, + ?string $set_user_name = null, ): void { // trigger deprecation if ( @@ -594,7 +596,8 @@ class SmartyExtend extends \Smarty $admin_call === true && ( $set_admin_stylesheet === null || $set_admin_javascript === null || - $set_page_width === null + $set_page_width === null || + $set_user_name === null ) ) || ( @@ -623,6 +626,7 @@ class SmartyExtend extends \Smarty $set_page_width = $set_page_width ?? PAGE_WIDTH; $set_stylesheet = $set_stylesheet ?? STYLESHEET; $set_javascript = $set_javascript ?? JAVASCRIPT; + $set_user_name = $set_user_name ?? $_SESSION['USER_NAME'] ?? ''; // depreacte call globals cms on null 4mcs if ( $cms === null && @@ -731,7 +735,7 @@ class SmartyExtend extends \Smarty $this->DATA['JS_FLATPICKR'] = $this->JS_FLATPICKR; $this->DATA['JS_FILE_UPLOADER'] = $this->JS_FILE_UPLOADER; // user name - $this->DATA['USER_NAME'] = !empty($_SESSION['USER_NAME']) ? $_SESSION['USER_NAME'] : ''; + $this->DATA['USER_NAME'] = $set_user_name; // the template part to include into the body $this->DATA['TEMPLATE_NAME'] = $this->TEMPLATE_NAME; $this->DATA['CONTENT_INCLUDE'] = $this->CONTENT_INCLUDE;