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
This commit is contained in:
Clemens Schwaighofer
2023-03-10 15:08:56 +09:00
parent 90a8c5540f
commit e3bd2c1c3b
10 changed files with 87 additions and 37 deletions

View File

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

View File

@@ -75,6 +75,10 @@ $form = new CoreLibs\Output\Form\Generate(
DB_CONFIG,
$log,
$l10n,
[
'base' => 10,
'admin' => 0
],
table_arrays: $table_arrays
);

View File

@@ -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
]

View File

@@ -39,7 +39,7 @@ function pop(theURL, winName, features) {
<form method="post">
<tr>
<td bgcolor="{$HEADER_COLOR}" class="normal">
Hello <b>{$USER_NAME|upper}</b> [{$EUID}] from the group <b>{$GROUP_NAME}</b> with Access Level <b>{$GROUP_LEVEL}</b>
Hello <b>{$USER_NAME|upper}</b> [{$EUID}] from the group <b>{$GROUP_NAME}</b> with Access Level <b>{$ACCESS_LEVEL}</b>
</td>
<td bgcolor="{$HEADER_COLOR}" class="normal" align="right">
<input type="submit" name="login_logout" value="Logout">

View File

@@ -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<mixed>
*/
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__

View File

@@ -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<mixed> $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<string,mixed> $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']) {

View File

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

View File

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

View File

@@ -277,6 +277,8 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
private $acl_admin = 0;
/** @var array<mixed> */
public $security_level;
/** @var array<string,mixed> 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<mixed> $db_config db config array, mandatory
* @param \CoreLibs\Debug\Logging $log Logging class
* @param \CoreLibs\Language\L10n $l10n l10n language class
* @param array<string,mixed> $login_acl Login ACL array,
* at least base/admin should be set
* @param array<mixed>|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'];

View File

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