Compare commits

...

6 Commits

8 changed files with 1138 additions and 850 deletions

View File

@@ -1 +1 @@
9.21.0 9.23.0

View File

@@ -69,6 +69,7 @@ declare(strict_types=1);
namespace CoreLibs\ACL; namespace CoreLibs\ACL;
use CoreLibs\Security\Password; use CoreLibs\Security\Password;
use CoreLibs\Create\Uids;
use CoreLibs\Convert\Json; use CoreLibs\Convert\Json;
class Login class Login
@@ -77,6 +78,8 @@ class Login
private ?int $euid; private ?int $euid;
/** @var ?string the user cuid (note will be super seeded with uuid v4 later) */ /** @var ?string the user cuid (note will be super seeded with uuid v4 later) */
private ?string $ecuid; private ?string $ecuid;
/** @var ?string UUIDv4, will superseed the ecuid and replace euid as login id */
private ?string $ecuuid;
/** @var string _GET/_POST loginUserId parameter for non password login */ /** @var string _GET/_POST loginUserId parameter for non password login */
private string $login_user_id = ''; private string $login_user_id = '';
/** @var string source, either _GET or _POST or empty */ /** @var string source, either _GET or _POST or empty */
@@ -195,6 +198,12 @@ class Login
/** @var bool */ /** @var bool */
private bool $login_is_ajax_page = false; private bool $login_is_ajax_page = false;
// logging
/** @var array<string> list of allowed types for edit log write */
private const WRITE_TYPES = ['BINARY', 'BZIP2', 'LZIP', 'STRING', 'SERIAL', 'JSON'];
/** @var array<string> list of available write types for log */
private array $write_types_available = [];
// settings // settings
/** @var array<string,mixed> options */ /** @var array<string,mixed> options */
private array $options = []; private array $options = [];
@@ -363,9 +372,6 @@ class Login
], ],
]; ];
// init default ACL list array
$_SESSION['DEFAULT_ACL_LIST'] = [];
$_SESSION['DEFAULT_ACL_LIST_TYPE'] = [];
// read the current edit_access_right list into an array // read the current edit_access_right list into an array
$q = "SELECT level, type, name FROM edit_access_right " $q = "SELECT level, type, name FROM edit_access_right "
. "WHERE level >= 0 ORDER BY level"; . "WHERE level >= 0 ORDER BY level";
@@ -378,8 +384,12 @@ class Login
$this->default_acl_list_type[(string)$res['type']] = (int)$res['level']; $this->default_acl_list_type[(string)$res['type']] = (int)$res['level'];
} }
// write that into the session // write that into the session
$_SESSION['DEFAULT_ACL_LIST'] = $this->default_acl_list; $this->session->setMany([
$_SESSION['DEFAULT_ACL_LIST_TYPE'] = $this->default_acl_list_type; 'DEFAULT_ACL_LIST' => $this->default_acl_list,
'DEFAULT_ACL_LIST_TYPE' => $this->default_acl_list_type,
]);
$this->loginSetEditLogWriteTypeAvailable();
// this will be deprecated // this will be deprecated
if ($this->options['auto_login'] === true) { if ($this->options['auto_login'] === true) {
@@ -569,7 +579,7 @@ class Login
// set path // set path
$options['locale_path'] = BASE . INCLUDES . LOCALE; $options['locale_path'] = BASE . INCLUDES . LOCALE;
} }
$_SESSION['LOCALE_PATH'] = $options['locale_path']; $this->session->set('LOCALE_PATH', $options['locale_path']);
// LANG: LOCALE // LANG: LOCALE
if (empty($options['site_locale'])) { if (empty($options['site_locale'])) {
trigger_error( trigger_error(
@@ -604,7 +614,7 @@ class Login
$options['set_domain'] = str_replace(DIRECTORY_SEPARATOR, '', CONTENT_PATH); $options['set_domain'] = str_replace(DIRECTORY_SEPARATOR, '', CONTENT_PATH);
} }
} }
$_SESSION['DEFAULT_DOMAIN'] = $options['site_domain']; $this->session->set('DEFAULT_DOMAIN', $options['site_domain']);
// LANG: ENCODING // LANG: ENCODING
if (empty($options['site_encoding'])) { if (empty($options['site_encoding'])) {
trigger_error( trigger_error(
@@ -759,7 +769,7 @@ class Login
} }
// have to get the global stuff here for setting it later // have to get the global stuff here for setting it later
// we have to get the themes in here too // we have to get the themes in here too
$q = "SELECT eu.edit_user_id, eu.cuid, eu.username, eu.password, " $q = "SELECT eu.edit_user_id, eu.cuid, eu.cuuid, eu.username, eu.password, "
. "eu.edit_group_id, " . "eu.edit_group_id, "
. "eg.name AS edit_group_name, eu.admin, " . "eg.name AS edit_group_name, eu.admin, "
// additinal acl lists // additinal acl lists
@@ -890,8 +900,14 @@ class Login
} }
// normal user processing // normal user processing
// set class var and session var // set class var and session var
$_SESSION['EUID'] = $this->euid = (int)$res['edit_user_id']; $this->euid = (int)$res['edit_user_id'];
$_SESSION['ECUID'] = $this->ecuid = (string)$res['cuid']; $this->ecuid = (string)$res['cuid'];
$this->ecuuid = (string)$res['cuuid'];
$this->session->setMany([
'EUID' => $this->euid,
'ECUID' => $this->ecuid,
'ECUUID' => $this->ecuuid,
]);
// check if user is okay // check if user is okay
$this->loginCheckPermissions(); $this->loginCheckPermissions();
if ($this->login_error == 0) { if ($this->login_error == 0) {
@@ -904,27 +920,39 @@ class Login
. "WHERE edit_user_id = " . $this->euid; . "WHERE edit_user_id = " . $this->euid;
$this->db->dbExec($q); $this->db->dbExec($q);
} }
$locale = $res['locale'] ?? 'en';
$encoding = $res['encoding'] ?? 'UTF-8';
$this->session->setMany([
// now set all session vars and read page permissions // now set all session vars and read page permissions
$_SESSION['DEBUG_ALL'] = $this->db->dbBoolean($res['debug']); 'DEBUG_ALL' => $this->db->dbBoolean($res['debug']),
$_SESSION['DB_DEBUG'] = $this->db->dbBoolean($res['db_debug']); 'DB_DEBUG' => $this->db->dbBoolean($res['db_debug']),
// general info for user logged in // general info for user logged in
$_SESSION['USER_NAME'] = $res['username']; 'USER_NAME' => $res['username'],
$_SESSION['ADMIN'] = $res['admin']; 'ADMIN' => $res['admin'],
$_SESSION['GROUP_NAME'] = $res['edit_group_name']; 'GROUP_NAME' => $res['edit_group_name'],
$_SESSION['USER_ACL_LEVEL'] = $res['user_level']; 'USER_ACL_LEVEL' => $res['user_level'],
$_SESSION['USER_ACL_TYPE'] = $res['user_type']; 'USER_ACL_TYPE' => $res['user_type'],
$_SESSION['USER_ADDITIONAL_ACL'] = Json::jsonConvertToArray($res['user_additional_acl']); 'USER_ADDITIONAL_ACL' => Json::jsonConvertToArray($res['user_additional_acl']),
$_SESSION['GROUP_ACL_LEVEL'] = $res['group_level']; 'GROUP_ACL_LEVEL' => $res['group_level'],
$_SESSION['GROUP_ACL_TYPE'] = $res['group_type']; 'GROUP_ACL_TYPE' => $res['group_type'],
$_SESSION['GROUP_ADDITIONAL_ACL'] = Json::jsonConvertToArray($res['group_additional_acl']); 'GROUP_ADDITIONAL_ACL' => Json::jsonConvertToArray($res['group_additional_acl']),
// deprecated TEMPLATE setting // deprecated TEMPLATE setting
$_SESSION['TEMPLATE'] = $res['template'] ? $res['template'] : ''; 'TEMPLATE' => $res['template'] ? $res['template'] : '',
$_SESSION['HEADER_COLOR'] = !empty($res['second_header_color']) ? 'HEADER_COLOR' => !empty($res['second_header_color']) ?
$res['second_header_color'] : $res['second_header_color'] :
$res['first_header_color']; $res['first_header_color'],
// LANGUAGE/LOCALE/ENCODING:
'LANG' => $locale,
'DEFAULT_CHARSET' => $encoding,
'DEFAULT_LOCALE' => $locale . '.' . strtoupper($encoding),
'DEFAULT_LANG' => $locale . '_' . strtolower(str_replace('-', '', $encoding))
]);
// missing # before, this is for legacy data, will be deprecated // missing # before, this is for legacy data, will be deprecated
if (preg_match("/^[\dA-Fa-f]{6,8}$/", $_SESSION['HEADER_COLOR'])) { if (
$_SESSION['HEADER_COLOR'] = '#' . $_SESSION['HEADER_COLOR']; !empty($this->session->get('HEADER_COLOR')) &&
preg_match("/^[\dA-Fa-f]{6,8}$/", $this->session->get('HEADER_COLOR'))
) {
$this->session->set('HEADER_COLOR', '#' . $this->session->get('HEADER_COLOR'));
} }
// TODO: make sure that header color is valid: // TODO: make sure that header color is valid:
// # + 6 hex // # + 6 hex
@@ -933,13 +961,6 @@ class Login
// rgb: nnn.n for each // rgb: nnn.n for each
// hsl: nnn.n for first, nnn.n% for 2nd, 3rd // hsl: nnn.n for first, nnn.n% for 2nd, 3rd
// Check\Colors::validateColor() // Check\Colors::validateColor()
// LANGUAGE/LOCALE/ENCODING:
$_SESSION['LANG'] = $res['locale'] ?? 'en';
$_SESSION['DEFAULT_CHARSET'] = $res['encoding'] ?? 'UTF-8';
$_SESSION['DEFAULT_LOCALE'] = $_SESSION['LANG']
. '.' . strtoupper($_SESSION['DEFAULT_CHARSET']);
$_SESSION['DEFAULT_LANG'] = $_SESSION['LANG'] . '_'
. strtolower(str_replace('-', '', $_SESSION['DEFAULT_CHARSET']));
// reset any login error count for this user // reset any login error count for this user
if ($res['login_error_count'] > 0) { if ($res['login_error_count'] > 0) {
$q = "UPDATE edit_user " $q = "UPDATE edit_user "
@@ -1029,8 +1050,10 @@ class Login
]; ];
} }
// write back the pages data to the output array // write back the pages data to the output array
$_SESSION['PAGES'] = $pages; $this->session->setMany([
$_SESSION['PAGES_ACL_LEVEL'] = $pages_acl; 'PAGES' => $pages,
'PAGES_ACL_LEVEL' => $pages_acl,
]);
// load the edit_access user rights // load the edit_access user rights
$q = "SELECT ea.edit_access_id, level, type, ea.name, " $q = "SELECT ea.edit_access_id, level, type, ea.name, "
. "ea.color, ea.uid, edit_default, ea.additional_acl " . "ea.color, ea.uid, edit_default, ea.additional_acl "
@@ -1042,6 +1065,7 @@ class Login
$unit_access = []; $unit_access = [];
$eauid = []; $eauid = [];
$unit_acl = []; $unit_acl = [];
$unit_uid = [];
while (is_array($res = $this->db->dbReturn($q))) { while (is_array($res = $this->db->dbReturn($q))) {
// read edit access data fields and drop them into the unit access array // read edit access data fields and drop them into the unit access array
$q_sub = "SELECT name, value " $q_sub = "SELECT name, value "
@@ -1065,16 +1089,19 @@ class Login
]; ];
// set the default unit // set the default unit
if ($res['edit_default']) { if ($res['edit_default']) {
$_SESSION['UNIT_DEFAULT'] = (int)$res['edit_access_id']; $this->session->set('UNIT_DEFAULT', (int)$res['edit_access_id']);
} }
$_SESSION['UNIT_UID'][$res['uid']] = (int)$res['edit_access_id']; $unit_uid[$res['uid']] = (int)$res['edit_access_id'];
// sub arrays for simple access // sub arrays for simple access
array_push($eauid, $res['edit_access_id']); array_push($eauid, $res['edit_access_id']);
$unit_acl[$res['edit_access_id']] = $res['level']; $unit_acl[$res['edit_access_id']] = $res['level'];
} }
$_SESSION['UNIT'] = $unit_access; $this->session->setMany([
$_SESSION['UNIT_ACL_LEVEL'] = $unit_acl; 'UNIT_UID' => $unit_uid,
$_SESSION['EAID'] = $eauid; 'UNIT' => $unit_access,
'UNIT_ACL_LEVEL' => $unit_acl,
'EAID' => $eauid,
]);
} // user has permission to THIS page } // user has permission to THIS page
} // user was not enabled or other login error } // user was not enabled or other login error
if ($this->login_error && is_array($res)) { if ($this->login_error && is_array($res)) {
@@ -1137,6 +1164,7 @@ class Login
$this->acl['group_name'] = $_SESSION['GROUP_NAME']; $this->acl['group_name'] = $_SESSION['GROUP_NAME'];
// edit user cuid // edit user cuid
$this->acl['ecuid'] = $_SESSION['ECUID']; $this->acl['ecuid'] = $_SESSION['ECUID'];
$this->acl['ecuuid'] = $_SESSION['ECUUID'];
// set additional acl // set additional acl
$this->acl['additional_acl'] = [ $this->acl['additional_acl'] = [
'user' => $_SESSION['USER_ADDITIONAL_ACL'], 'user' => $_SESSION['USER_ADDITIONAL_ACL'],
@@ -1169,7 +1197,7 @@ class Login
$this->acl['base'] = (int)$_SESSION['USER_ACL_LEVEL']; $this->acl['base'] = (int)$_SESSION['USER_ACL_LEVEL'];
} }
} }
$_SESSION['BASE_ACL_LEVEL'] = $this->acl['base']; $this->session->set('BASE_ACL_LEVEL', $this->acl['base']);
// set the current page acl // set the current page acl
// start with base acl // start with base acl
@@ -1430,7 +1458,7 @@ class Login
$data = 'Illegal user for password change: ' . $this->pw_username; $data = 'Illegal user for password change: ' . $this->pw_username;
} }
// log this password change attempt // log this password change attempt
$this->writeLog($event, $data, $this->login_error, $this->pw_username); $this->writeEditLog($event, $data, $this->login_error, $this->pw_username);
} }
/** /**
@@ -1571,7 +1599,7 @@ class Login
$username = $res['username']; $username = $res['username'];
} }
} // if euid is set, get username (or try) } // if euid is set, get username (or try)
$this->writeLog($event, '', $this->login_error, $username); $this->writeEditLog($event, '', $this->login_error, $username);
} // write log under certain settings } // write log under certain settings
// now close DB connection // now close DB connection
// $this->error_msg = $this->_login(); // $this->error_msg = $this->_login();
@@ -1727,6 +1755,8 @@ HTML;
} }
} }
// MARK: LOGGING
/** /**
* writes detailed data into the edit user log table (keep log what user does) * writes detailed data into the edit user log table (keep log what user does)
* *
@@ -1736,7 +1766,7 @@ HTML;
* @param string $username login user username * @param string $username login user username
* @return void has no return * @return void has no return
*/ */
private function writeLog( private function writeEditLog(
string $event, string $event,
string $data, string $data,
string|int $error = '', string|int $error = '',
@@ -1754,50 +1784,191 @@ HTML;
'_GET' => $_GET, '_GET' => $_GET,
'_POST' => $_POST, '_POST' => $_POST,
'_FILES' => $_FILES, '_FILES' => $_FILES,
'error' => $this->login_error 'error' => $this->login_error,
'data' => $data,
]; ];
$data_binary = $this->db->dbEscapeBytea((string)bzcompress(serialize($_data_binary))); $_action_set = [
// SQL querie for log entry 'action' => $this->action,
$q = "INSERT INTO edit_log " 'action_id' => $this->username,
. "(username, password, euid, event_date, event, error, data, data_binary, page, " 'action_flag' => (string)$this->login_error,
. "ip, user_agent, referer, script_name, query_string, server_name, http_host, " 'action_value' => (string)$this->permission_okay,
. "http_accept, http_accept_charset, http_accept_encoding, session_id, " ];
. "action, action_id, action_yes, action_flag, action_menu, action_loaded, "
. "action_value, action_error) " $this->writeLog($event, $_data_binary, $_action_set, $error, $username);
. "VALUES ('" . $this->db->dbEscapeString($username) . "', 'PASSWORD', " }
. ($this->euid ? $this->euid : 'NULL') . ", "
. "NOW(), '" . $this->db->dbEscapeString($event) . "', " /**
. "'" . $this->db->dbEscapeString((string)$error) . "', " * writes all action vars plus other info into edit_log table
. "'" . $this->db->dbEscapeString($data) . "', '" . $data_binary . "', " * this is for public class
. "'" . $this->page_name . "', "; *
foreach ( * phpcs:disable Generic.Files.LineLength
* @param string $event [default=''] any kind of event description,
* @param string|array<mixed> $data [default=''] any kind of data related to that event
* @param array{action?:?string,action_id?:null|string|int,action_sub_id?:null|string|int,action_yes?:null|string|int|bool,action_flag?:?string,action_menu?:?string,action_loaded?:?string,action_value?:?string,action_type?:?string,action_error?:?string} $action_set [default=[]] action set names
* @param string|int $error error id (mostly an int)
* @param string $write_type [default=JSON] write type can be
* JSON, STRING/SERIEAL, BINARY/BZIP or ZLIB
* @param string|null $db_schema [default=null] override target schema
* @return void
* phpcs:enable Generic.Files.LineLength
*/
public function writeLog(
string $event = '',
string|array $data = '',
array $action_set = [],
string|int $error = '',
string $username = '',
string $write_type = 'JSON',
?string $db_schema = null
): void {
$data_binary = '';
$data_write = '';
// check if write type is valid, if not fallback to JSON
if (!in_array(strtoupper($write_type), $this->write_types_available)) {
$this->log->warning('Write type not in allowed array, fallback to JSON', context:[
"write_type" => $write_type,
"write_list" => $this->write_types_available,
]);
$write_type = 'JSON';
}
switch ($write_type) {
case 'BINARY':
case 'BZIP':
$data_binary = $this->db->dbEscapeBytea((string)bzcompress(serialize($data)));
$data_write = Json::jsonConvertArrayTo([
'type' => 'BZIP',
'message' => 'see bzip compressed data_binary field'
]);
break;
case 'ZLIB':
$data_binary = $this->db->dbEscapeBytea((string)gzcompress(serialize($data)));
$data_write = Json::jsonConvertArrayTo([
'type' => 'ZLIB',
'message' => 'see zlib compressed data_binary field'
]);
break;
case 'STRING':
case 'SERIAL':
$data_binary = $this->db->dbEscapeBytea(Json::jsonConvertArrayTo([
'type' => 'SERIAL',
'message' => 'see serial string data field'
]));
$data_write = serialize($data);
break;
case 'JSON':
$data_binary = $this->db->dbEscapeBytea(Json::jsonConvertArrayTo([
'type' => 'JSON',
'message' => 'see json string data field'
]));
// must be converted to array
if (!is_array($data)) {
$data = ["data" => $data];
}
$data_write = Json::jsonConvertArrayTo($data);
break;
default:
$this->log->alert('Invalid type for data compression was set', context:[
"write_type" => $write_type
]);
break;
}
/** @var string $DB_SCHEMA check schema */
$DB_SCHEMA = 'public';
if ($db_schema !== null) {
$DB_SCHEMA = $db_schema;
} elseif (!empty($this->db->dbGetSchema())) {
$DB_SCHEMA = $this->db->dbGetSchema();
}
$q = <<<SQL
INSERT INTO {DB_SCHEMA}.edit_log (
username, euid, ecuid, ecuuid, event_date, event, error, data, data_binary, page,
ip, user_agent, referer, script_name, query_string, server_name, http_host,
http_accept, http_accept_charset, http_accept_encoding, session_id,
action, action_id, action_sub_id, action_yes, action_flag, action_menu, action_loaded,
action_value, action_type, action_error
) VALUES (
$1, $2, $3, $4, NOW(), $5, $6, $7, $8, $9,
$10, $11, $12, $13, $14, $15, $16,
$17, $18, $19, $20,
$21, $22, $23, $24, $25, $26, $27,
$28, $29, $30
)
SQL;
$this->db->dbExecParams(
str_replace(
['{DB_SCHEMA}'],
[$DB_SCHEMA],
$q
),
[ [
'REMOTE_ADDR', 'HTTP_USER_AGENT', 'HTTP_REFERER', 'SCRIPT_FILENAME', // row 1
'QUERY_STRING', 'SERVER_NAME', 'HTTP_HOST', 'HTTP_ACCEPT', empty($username) ? $this->session->get('USER_NAME') ?? '' : $username,
'HTTP_ACCEPT_CHARSET', 'HTTP_ACCEPT_ENCODING' is_numeric($this->session->get('EUID')) ?
] as $server_code $this->session->get('EUID') : null,
) { is_string($this->session->get('ECUID')) ?
if (array_key_exists($server_code, $_SERVER)) { $this->session->get('ECUID') : null,
$q .= "'" . $this->db->dbEscapeString($_SERVER[$server_code]) . "', "; !empty($this->session->get('ECUUID')) && Uids::validateUuuidv4($this->session->get('ECUUID')) ?
} else { $this->session->get('ECUUID') : null,
$q .= "NULL, "; (string)$event,
(string)$error,
$data_write,
$data_binary,
(string)$this->page_name,
// row 2
$_SERVER["REMOTE_ADDR"] ?? null,
$_SERVER['HTTP_USER_AGENT'] ?? null,
$_SERVER['HTTP_REFERER'] ?? null,
$_SERVER['SCRIPT_FILENAME'] ?? null,
$_SERVER['QUERY_STRING'] ?? null,
$_SERVER['SERVER_NAME'] ?? null,
$_SERVER['HTTP_HOST'] ?? null,
// row 3
$_SERVER['HTTP_ACCEPT'] ?? null,
$_SERVER['HTTP_ACCEPT_CHARSET'] ?? null,
$_SERVER['HTTP_ACCEPT_ENCODING'] ?? null,
$this->session->getSessionId() !== '' ?
$this->session->getSessionId() : null,
// row 4
$action_set['action'] ?? null,
$action_set['action_id'] ?? null,
$action_set['action_sub_id'] ?? null,
$action_set['action_yes'] ?? null,
$action_set['action_flag'] ?? null,
$action_set['action_menu'] ?? null,
$action_set['action_loaded'] ?? null,
$action_set['action_value'] ?? null,
$action_set['action_type'] ?? null,
$action_set['action_error'] ?? null,
],
'NULL'
);
} }
/**
* set the write types that are allowed
*
* @return void
*/
private function loginSetEditLogWriteTypeAvailable()
{
// check what edit log data write types are allowed
$this->write_types_available = self::WRITE_TYPES;
if (!function_exists('bzcompress')) {
$this->write_types_available = array_diff($this->write_types_available, ['BINARY', 'BZIP']);
}
if (!function_exists('gzcompress')) {
$this->write_types_available = array_diff($this->write_types_available, ['LZIP']);
} }
$q .= "'" . $this->session->getSessionId() . "', ";
$q .= "'" . $this->db->dbEscapeString($this->action) . "', ";
$q .= "'" . $this->db->dbEscapeString($this->username) . "', ";
$q .= "NULL, ";
$q .= "'" . $this->db->dbEscapeString((string)$this->login_error) . "', ";
$q .= "NULL, NULL, ";
$q .= "'" . $this->db->dbEscapeString((string)$this->permission_okay) . "', ";
$q .= "NULL)";
$this->db->dbExec($q, 'NULL');
} }
// ************************************************************************* // *************************************************************************
// **** PUBLIC INTERNAL // **** PUBLIC INTERNAL
// ************************************************************************* // *************************************************************************
// MARK: LOGIN CALL
/** /**
* Main call that needs to be run to actaully check for login * Main call that needs to be run to actaully check for login
* If this is not called, no login checks are done, unless the class * If this is not called, no login checks are done, unless the class
@@ -1866,9 +2037,10 @@ HTML;
} }
} }
// if there is none, there is none, saves me POST/GET check // if there is none, there is none, saves me POST/GET check
$this->euid = array_key_exists('EUID', $_SESSION) ? (int)$_SESSION['EUID'] : 0; $this->euid = (int)($this->session->get('EUID') ?? 0);
// TODO: allow load from cuid // TODO: allow load from cuid
// $this->ecuid = array_key_exists('ECUID', $_SESSION) ? (string)$_SESSION['ECUID'] : ''; // $this->ecuid = (string)($this->session->get('ECUID') ?? '');
// $this->ecuuid = (string)($this->session->get('ECUUID') ?? '');
// get login vars, are so, can't be changed // get login vars, are so, can't be changed
// prepare // prepare
// pass on vars to Object vars // pass on vars to Object vars
@@ -1949,6 +2121,8 @@ HTML;
$this->loginSetAcl(); $this->loginSetAcl();
} }
// MARK: setters/getters
/** /**
* Returns current set login_html content * Returns current set login_html content
* *
@@ -2119,6 +2293,7 @@ HTML;
// unset euid // unset euid
$this->euid = null; $this->euid = null;
$this->ecuid = null; $this->ecuid = null;
$this->ecuuid = null;
// then prints the login screen again // then prints the login screen again
$this->permission_okay = false; $this->permission_okay = false;
} }
@@ -2136,12 +2311,12 @@ HTML;
if (empty($this->euid)) { if (empty($this->euid)) {
return $this->permission_okay; return $this->permission_okay;
} }
// euid must match ecuid // euid must match ecuid and ecuuid
// bail for previous wrong page match, eg if method is called twice // bail for previous wrong page match, eg if method is called twice
if ($this->login_error == 103) { if ($this->login_error == 103) {
return $this->permission_okay; return $this->permission_okay;
} }
$q = "SELECT ep.filename, eu.cuid, " $q = "SELECT ep.filename, eu.cuid, eu.cuuid, "
// base lock flags // base lock flags
. "eu.deleted, eu.enabled, eu.locked, " . "eu.deleted, eu.enabled, eu.locked, "
// date based lock // date based lock
@@ -2208,7 +2383,12 @@ HTML;
$this->login_error = 103; $this->login_error = 103;
} }
// set ECUID // set ECUID
$_SESSION['ECUID'] = $this->ecuid = (string)$res['cuid']; $this->ecuid = (string)$res['cuid'];
$this->ecuuid = (string)$res['cuuid'];
$this->session->setMany([
'ECUID' => $this->ecuid,
'ECUUID' => $this->ecuuid,
]);
// if called from public, so we can check if the permissions are ok // if called from public, so we can check if the permissions are ok
return $this->permission_okay; return $this->permission_okay;
} }
@@ -2354,13 +2534,12 @@ HTML;
{ {
if ( if (
$edit_access_id !== null && $edit_access_id !== null &&
isset($_SESSION['UNIT']) && is_array($this->session->get('UNIT')) &&
is_array($_SESSION['UNIT']) && !array_key_exists($edit_access_id, $this->session->get('UNIT'))
!array_key_exists($edit_access_id, $_SESSION['UNIT'])
) { ) {
$edit_access_id = null; $edit_access_id = null;
if (is_numeric($_SESSION['UNIT_DEFAULT'])) { if (is_numeric($this->session->get('UNIT_DEFAULT'))) {
$edit_access_id = (int)$_SESSION['UNIT_DEFAULT']; $edit_access_id = (int)$this->session->get('UNIT_DEFAULT');
} }
} }
return $edit_access_id; return $edit_access_id;
@@ -2491,7 +2670,7 @@ HTML;
*/ */
public function loginGetHeaderColor(): ?string public function loginGetHeaderColor(): ?string
{ {
return $_SESSION['HEADER_COLOR'] ?? null; return $this->session->get('HEADER_COLOR');
} }
/** /**
@@ -2502,7 +2681,7 @@ HTML;
public function loginGetPages(): array public function loginGetPages(): array
{ {
return $_SESSION['PAGES'] ?? []; return $this->session->get('PAGES');
} }
/** /**
@@ -2520,10 +2699,20 @@ HTML;
* *
* @return string ECUID as string * @return string ECUID as string
*/ */
public function loginGetEcid(): string public function loginGetEcuid(): string
{ {
return (string)$this->ecuid; return (string)$this->ecuid;
} }
/**
* Get the current set ECUUID (edit user cuuid)
*
* @return string ECUUID as string
*/
public function loginGetEcuuid(): string
{
return (string)$this->ecuuid;
}
} }
// __END__ // __END__

View File

@@ -31,6 +31,7 @@ declare(strict_types=1);
namespace CoreLibs\Admin; namespace CoreLibs\Admin;
use CoreLibs\Create\Uids;
use CoreLibs\Convert\Json; use CoreLibs\Convert\Json;
class Backend class Backend
@@ -258,6 +259,27 @@ class Backend
} }
} }
/**
* return all the action data, if not set, sets entry to null
*
* @return array{action:?string,action_id:null|string|int,action_sub_id:null|string|int,action_yes:null|string|int|bool,action_flag:?string,action_menu:?string,action_loaded:?string,action_value:?string,action_type:?string,action_error:?string}
*/
public function adbGetActionSet(): array
{
return [
'action' => $this->action ?? null,
'action_id' => $this->action_id ?? null,
'action_sub_id' => $this->action_sub_id ?? null,
'action_yes' => $this->action_yes ?? null,
'action_flag' => $this->action_flag ?? null,
'action_menu' => $this->action_menu ?? null,
'action_loaded' => $this->action_loaded ?? null,
'action_value' => $this->action_value ?? null,
'action_type' => $this->action_type ?? null,
'action_error' => $this->action_error ?? null,
];
}
/** /**
* writes all action vars plus other info into edit_log table * writes all action vars plus other info into edit_log table
* *
@@ -267,6 +289,7 @@ class Backend
* JSON, STRING/SERIEAL, BINARY/BZIP or ZLIB * JSON, STRING/SERIEAL, BINARY/BZIP or ZLIB
* @param string|null $db_schema [default=null] override target schema * @param string|null $db_schema [default=null] override target schema
* @return void * @return void
* @deprecated Use $login->writeLog() and set action_set from ->adbGetActionSet()
*/ */
public function adbEditLog( public function adbEditLog(
string $event = '', string $event = '',
@@ -335,17 +358,17 @@ class Backend
} }
$q = <<<SQL $q = <<<SQL
INSERT INTO {DB_SCHEMA}.edit_log ( INSERT INTO {DB_SCHEMA}.edit_log (
euid, event_date, event, data, data_binary, page, username, euid, ecuid, ecuuid, event_date, event, error, data, data_binary, page,
ip, user_agent, referer, script_name, query_string, server_name, http_host, ip, user_agent, referer, script_name, query_string, server_name, http_host,
http_accept, http_accept_charset, http_accept_encoding, session_id, http_accept, http_accept_charset, http_accept_encoding, session_id,
action, action_id, action_yes, action_flag, action_menu, action_loaded, action, action_id, action_sub_id, action_yes, action_flag, action_menu, action_loaded,
action_value, action_type, action_error action_value, action_type, action_error
) VALUES ( ) VALUES (
$1, NOW(), $2, $3, $4, $5, $1, $2, $3, $4, NOW(), $5, $6, $7, $8, $9,
$6, $7, $8, $9, $10, $11, $12, $10, $11, $12, $13, $14, $15, $16,
$13, $14, $15, $16, $17, $18, $19, $20,
$17, $18, $19, $20, $21, $22, $21, $22, $23, $24, $25, $26, $27,
$23, $24, $25 $28, $29, $30
) )
SQL; SQL;
$this->db->dbExecParams( $this->db->dbExecParams(
@@ -356,9 +379,15 @@ class Backend
), ),
[ [
// row 1 // row 1
isset($_SESSION['EUID']) && is_numeric($_SESSION['EUID']) ? '',
$_SESSION['EUID'] : null, is_numeric($this->session->get('EUID')) ?
$this->session->get('EUID') : null,
is_string($this->session->get('ECUID')) ?
$this->session->get('ECUID') : null,
!empty($this->session->get('ECUUID')) && Uids::validateUuuidv4($this->session->get('ECUID')) ?
$this->session->get('ECUID') : null,
(string)$event, (string)$event,
'',
$data_write, $data_write,
$data_binary, $data_binary,
(string)$this->page_name, (string)$this->page_name,
@@ -374,11 +403,12 @@ class Backend
$_SERVER['HTTP_ACCEPT'] ?? '', $_SERVER['HTTP_ACCEPT'] ?? '',
$_SERVER['HTTP_ACCEPT_CHARSET'] ?? '', $_SERVER['HTTP_ACCEPT_CHARSET'] ?? '',
$_SERVER['HTTP_ACCEPT_ENCODING'] ?? '', $_SERVER['HTTP_ACCEPT_ENCODING'] ?? '',
$this->session->getSessionId() !== false ? $this->session->getSessionId() !== '' ?
$this->session->getSessionId() : null, $this->session->getSessionId() : null,
// row 4 // row 4
$this->action ?? '', $this->action ?? '',
$this->action_id ?? '', $this->action_id ?? '',
$this->action_sub_id ?? '',
$this->action_yes ?? '', $this->action_yes ?? '',
$this->action_flag ?? '', $this->action_flag ?? '',
$this->action_menu ?? '', $this->action_menu ?? '',
@@ -438,7 +468,7 @@ class Backend
} }
// get the session pages array // get the session pages array
$PAGES = $_SESSION['PAGES'] ?? null; $PAGES = $this->session->get('PAGES');
if (!isset($PAGES) || !is_array($PAGES)) { if (!isset($PAGES) || !is_array($PAGES)) {
$PAGES = []; $PAGES = [];
} }

View File

@@ -15,19 +15,27 @@ namespace CoreLibs\Create;
class Session class Session
{ {
/** @var string current session name */
private string $session_name = '';
/** @var string current session id */
private string $session_id = '';
/** @var bool flag auto write close */
private bool $auto_write_close = false;
/** /**
* init a session, if array is empty or array does not have session_name set * init a session, if array is empty or array does not have session_name set
* then no auto init is run * then no auto init is run
* *
* @param string $session_name if set and not empty, will start session * @param string $session_name if set and not empty, will start session
*/ */
public function __construct(string $session_name = '') public function __construct(string $session_name, bool $auto_write_close = false)
{ {
if (!empty($session_name)) { $this->initSession($session_name);
$this->startSession($session_name); $this->auto_write_close = $auto_write_close;
}
} }
// MARK: private methods
/** /**
* Start session * Start session
* startSession should be called for complete check * startSession should be called for complete check
@@ -36,36 +44,32 @@ class Session
* *
* @return void * @return void
*/ */
protected function startSessionCall(): void private function startSessionCall(): void
{ {
session_start(); session_start();
} }
/** /**
* check if we are in CLI, we set this, so we can mock this * get current set session id or false if none started
* Not this is just a wrapper for the static System::checkCLI call
* *
* @return bool True if we are in a CLI enviroment, or false for everything else * @return string|false
*/ */
public function checkCliStatus(): bool public function getSessionIdCall(): string|false
{ {
return \CoreLibs\Get\System::checkCLI(); return session_id();
} }
/** /**
* Set session name call. If not valid session name, will return false * automatically closes a session if the auto write close flag is set
* *
* @param string $session_name A valid string for session name * @return bool
* @return bool True if session name is valid,
* False if not
*/ */
public function setSessionName(string $session_name): bool private function closeSessionCall(): bool
{ {
if (!$this->checkValidSessionName($session_name)) { if ($this->auto_write_close) {
return false; return $this->writeClose();
} }
session_name($session_name); return false;
return true;
} }
/** /**
@@ -94,15 +98,34 @@ class Session
} }
/** /**
* start session with given session name if set * validate _SESSION key, must be valid variable
*
* @param int|float|string $key
* @return true
*/
private function checkValidSessionEntryKey(int|float|string $key): true
{
if (!is_string($key) || is_numeric($key)) {
throw new \UnexpectedValueException(
'[SESSION] Given key for _SESSION is not a valid value for a varaible: ' . $key,
1
);
}
return true;
}
// MARK: init session (on class start)
/**
* stinitart session with given session name if set
* aborts on command line or if sessions are not enabled * aborts on command line or if sessions are not enabled
* also aborts if session cannot be started * also aborts if session cannot be started
* On sucess returns the session id * On sucess returns the session id
* *
* @param string|null $session_name * @param string $session_name
* @return string|bool * @return void
*/ */
public function startSession(?string $session_name = null): string|bool private function initSession(string $session_name): void
{ {
// we can't start sessions on command line // we can't start sessions on command line
if ($this->checkCliStatus()) { if ($this->checkCliStatus()) {
@@ -115,39 +138,85 @@ class Session
// session_status // session_status
// initial the session if there is no session running already // initial the session if there is no session running already
if (!$this->checkActiveSession()) { if (!$this->checkActiveSession()) {
// if session name is emtpy, check if there is a global set
// this is a deprecated fallback
$session_name = $session_name ?? $GLOBALS['SET_SESSION_NAME'] ?? '';
// DEPRECTED: constant SET_SESSION_NAME is no longer used
// if set, set special session name
if (!empty($session_name)) {
// invalid session name, abort // invalid session name, abort
if (!$this->checkValidSessionName($session_name)) { if (!$this->checkValidSessionName($session_name)) {
throw new \UnexpectedValueException('[SESSION] Invalid session name: ' . $session_name, 3); throw new \UnexpectedValueException('[SESSION] Invalid session name: ' . $this->session_name, 3);
}
$this->setSessionName($session_name);
} }
// set session name
$this->session_name = $session_name;
session_name($this->session_name);
// start session // start session
$this->startSessionCall(); $this->startSessionCall();
} // if we faild to start the session
// if we still have no active session
if (!$this->checkActiveSession()) { if (!$this->checkActiveSession()) {
throw new \RuntimeException('[SESSION] Failed to activate session', 4); throw new \RuntimeException('[SESSION] Failed to activate session', 5);
} }
if (false === ($session_id = $this->getSessionId())) { } elseif ($session_name != $this->getSessionName()) {
throw new \UnexpectedValueException('[SESSION] getSessionId did not return a session id', 5); throw new \UnexpectedValueException(
'[SESSION] Another session exists with a different name: ' . $this->getSessionName(),
4
);
} }
return $session_id; // check session id
if (false === ($session_id = $this->getSessionIdCall())) {
throw new \UnexpectedValueException('[SESSION] getSessionId did not return a session id', 6);
}
// set session id
$this->session_id = $session_id;
// if flagged auto close, write close session
if ($this->auto_write_close) {
$this->writeClose();
}
}
// MARK: public set/get status
/**
* start session, will only run after initSession
*
* @return bool True if started, False if alrady running
*/
public function restartSession(): bool
{
if (!$this->checkActiveSession()) {
if (empty($this->session_name)) {
throw new \RuntimeException('[SESSION] Cannot restart session without a session name', 1);
}
$this->startSessionCall();
return true;
}
return false;
} }
/** /**
* get current set session id or false if none started * current set session id
* *
* @return string|bool * @return string
*/ */
public function getSessionId(): string|bool public function getSessionId(): string
{ {
return session_id(); return $this->session_id;
}
/**
* set the auto write close flag
*
* @param bool $flag
* @return void
*/
public function setAutoWriteClose(bool $flag): void
{
$this->auto_write_close = $flag;
}
/**
* return the auto write close flag
*
* @return bool
*/
public function checkAutoWriteClose(): bool
{
return $this->auto_write_close;
} }
/** /**
@@ -175,6 +244,34 @@ class Session
} }
} }
/**
* check if we are in CLI, we set this, so we can mock this
* Not this is just a wrapper for the static System::checkCLI call
*
* @return bool True if we are in a CLI enviroment, or false for everything else
*/
public function checkCliStatus(): bool
{
return \CoreLibs\Get\System::checkCLI();
}
/**
* get session status
* PHP_SESSION_DISABLED if sessions are disabled.
* PHP_SESSION_NONE if sessions are enabled, but none exists.
* PHP_SESSION_ACTIVE if sessions are enabled, and one exists.
*
* https://www.php.net/manual/en/function.session-status.php
*
* @return int See possible return int values above
*/
public function getSessionStatus(): int
{
return session_status();
}
// MARK: write close session
/** /**
* unlock the session file, so concurrent AJAX requests can be done * unlock the session file, so concurrent AJAX requests can be done
* NOTE: after this has been called, no changes in _SESSION will be stored * NOTE: after this has been called, no changes in _SESSION will be stored
@@ -188,17 +285,24 @@ class Session
return session_write_close(); return session_write_close();
} }
// MARK: session close and clean up
/** /**
* Proper destroy a session * Proper destroy a session
* - unset the _SESSION array * - unset the _SESSION array
* - unset cookie if cookie on and we have not strict mode * - unset cookie if cookie on and we have not strict mode
* - unset session_name and session_id internal vars
* - destroy session * - destroy session
* *
* @return bool * @return bool True on successful session destroy
*/ */
public function sessionDestroy(): bool public function sessionDestroy(): bool
{ {
$_SESSION = []; // abort to false if not unsetable
if (!session_unset()) {
return false;
}
$this->clear();
if ( if (
ini_get('session.use_cookies') && ini_get('session.use_cookies') &&
!ini_get('session.use_strict_mode') !ini_get('session.use_strict_mode')
@@ -218,68 +322,92 @@ class Session
$params['httponly'] $params['httponly']
); );
} }
// unset internal vars
$this->session_name = '';
$this->session_id = '';
return session_destroy(); return session_destroy();
} }
/** // MARK: _SESSION set/unset methods
* get session status
* PHP_SESSION_DISABLED if sessions are disabled.
* PHP_SESSION_NONE if sessions are enabled, but none exists.
* PHP_SESSION_ACTIVE if sessions are enabled, and one exists.
*
* https://www.php.net/manual/en/function.session-status.php
*
* @return int See possible return int values above
*/
public function getSessionStatus(): int
{
return session_status();
}
// _SESSION set/unset methods
/** /**
* unset all _SESSION entries * unset all _SESSION entries
* *
* @return void * @return void
*/ */
public function unsetAllS(): void public function clear(): void
{ {
foreach (array_keys($_SESSION ?? []) as $name) { $this->restartSession();
unset($_SESSION[$name]); if (!session_unset()) {
throw new \RuntimeException('[SESSION] Cannot unset session vars', 1);
} }
if (!empty($_SESSION)) {
$_SESSION = [];
}
$this->closeSessionCall();
} }
/** /**
* set _SESSION entry 'name' with any value * set _SESSION entry 'name' with any value
* *
* @param string|int $name array name in _SESSION * @param string $name array name in _SESSION
* @param mixed $value value to set (can be anything) * @param mixed $value value to set (can be anything)
* @return void * @return void
*/ */
public function setS(string|int $name, mixed $value): void public function set(string $name, mixed $value): void
{ {
$this->checkValidSessionEntryKey($name);
$this->restartSession();
$_SESSION[$name] = $value; $_SESSION[$name] = $value;
$this->closeSessionCall();
}
/**
* set many session entries in one set
*
* @param array<string,mixed> $set key is the key in the _SESSION, value is any data to set
* @return void
*/
public function setMany(array $set): void
{
$this->restartSession();
// skip any that are not valid
foreach ($set as $key => $value) {
$this->checkValidSessionEntryKey($key);
$_SESSION[$key] = $value;
}
$this->closeSessionCall();
} }
/** /**
* get _SESSION 'name' entry or empty string if not set * get _SESSION 'name' entry or empty string if not set
* *
* @param string|int $name value key to get from _SESSION * @param string $name value key to get from _SESSION
* @return mixed value stored in _SESSION * @return mixed value stored in _SESSION, if not found set to null
*/ */
public function getS(string|int $name): mixed public function get(string $name): mixed
{ {
return $_SESSION[$name] ?? ''; return $_SESSION[$name] ?? null;
}
/**
* get multiple session entries
*
* @param array<string> $set
* @return array<string,mixed>
*/
public function getMany(array $set): array
{
return array_intersect_key($_SESSION, array_flip($set));
} }
/** /**
* Check if a name is set in the _SESSION array * Check if a name is set in the _SESSION array
* *
* @param string|int $name Name to check for * @param string $name Name to check for
* @return bool True for set, False fornot set * @return bool True for set, False fornot set
*/ */
public function issetS(string|int $name): bool public function isset(string $name): bool
{ {
return isset($_SESSION[$name]); return isset($_SESSION[$name]);
} }
@@ -287,67 +415,35 @@ class Session
/** /**
* unset one _SESSION entry 'name' if exists * unset one _SESSION entry 'name' if exists
* *
* @param string|int $name _SESSION key name to remove * @param string $name _SESSION key name to remove
* @return void * @return void
*/ */
public function unsetS(string|int $name): void public function unset(string $name): void
{ {
if (isset($_SESSION[$name])) { if (!isset($_SESSION[$name])) {
return;
}
$this->restartSession();
unset($_SESSION[$name]); unset($_SESSION[$name]);
$this->closeSessionCall();
} }
}
// set/get below
// ->var = value;
/** /**
* Undocumented function * reset many session entry
* *
* @param string|int $name * @param array<string> $set list of session keys to reset
* @param mixed $value
* @return void * @return void
*/ */
public function __set(string|int $name, mixed $value): void public function unsetMany(array $set): void
{ {
$_SESSION[$name] = $value; $this->restartSession();
foreach ($set as $key) {
if (!isset($_SESSION[$key])) {
continue;
} }
unset($_SESSION[$key]);
/**
* Undocumented function
*
* @param string|int $name
* @return mixed If name is not found, it will return null
*/
public function __get(string|int $name): mixed
{
if (isset($_SESSION[$name])) {
return $_SESSION[$name];
}
return null;
}
/**
* Undocumented function
*
* @param string|int $name
* @return bool
*/
public function __isset(string|int $name): bool
{
return isset($_SESSION[$name]);
}
/**
* Undocumented function
*
* @param string|int $name
* @return void
*/
public function __unset(string|int $name): void
{
if (isset($_SESSION[$name])) {
unset($_SESSION[$name]);
} }
$this->closeSessionCall();
} }
} }

View File

@@ -2,7 +2,7 @@
/* /*
* sets a form token in the _SESSION variable * sets a form token in the _SESSION variable
* session must be started for this to work * session must be started and running for this to work
*/ */
declare(strict_types=1); declare(strict_types=1);

View File

@@ -244,6 +244,7 @@ final class CoreLibsACLLoginTest extends TestCase
[ [
'EUID' => 1, 'EUID' => 1,
'ECUID' => 'abc', 'ECUID' => 'abc',
'ECUUID' => '1233456-1234-1234-1234-123456789012',
], ],
2, 2,
[], [],
@@ -262,6 +263,7 @@ final class CoreLibsACLLoginTest extends TestCase
[ [
'EUID' => 1, 'EUID' => 1,
'ECUID' => 'abc', 'ECUID' => 'abc',
'ECUUID' => '1233456-1234-1234-1234-123456789012',
'USER_NAME' => '', 'USER_NAME' => '',
'GROUP_NAME' => '', 'GROUP_NAME' => '',
'ADMIN' => 1, 'ADMIN' => 1,
@@ -1087,9 +1089,9 @@ final class CoreLibsACLLoginTest extends TestCase
/** @var \CoreLibs\Create\Session&MockObject */ /** @var \CoreLibs\Create\Session&MockObject */
$session_mock = $this->createPartialMock( $session_mock = $this->createPartialMock(
\CoreLibs\Create\Session::class, \CoreLibs\Create\Session::class,
['startSession', 'checkActiveSession', 'sessionDestroy'] ['getSessionId', 'checkActiveSession', 'sessionDestroy']
); );
$session_mock->method('startSession')->willReturn('ACLLOGINTEST12'); $session_mock->method('getSessionId')->willReturn('ACLLOGINTEST12');
$session_mock->method('checkActiveSession')->willReturn(true); $session_mock->method('checkActiveSession')->willReturn(true);
$session_mock->method('sessionDestroy')->will( $session_mock->method('sessionDestroy')->will(
$this->returnCallback(function () { $this->returnCallback(function () {
@@ -1790,9 +1792,9 @@ final class CoreLibsACLLoginTest extends TestCase
/** @var \CoreLibs\Create\Session&MockObject */ /** @var \CoreLibs\Create\Session&MockObject */
$session_mock = $this->createPartialMock( $session_mock = $this->createPartialMock(
\CoreLibs\Create\Session::class, \CoreLibs\Create\Session::class,
['startSession', 'checkActiveSession', 'sessionDestroy'] ['getSessionId', 'checkActiveSession', 'sessionDestroy']
); );
$session_mock->method('startSession')->willReturn('ACLLOGINTEST34'); $session_mock->method('getSessionId')->willReturn('ACLLOGINTEST34');
$session_mock->method('checkActiveSession')->willReturn(true); $session_mock->method('checkActiveSession')->willReturn(true);
$session_mock->method('sessionDestroy')->will( $session_mock->method('sessionDestroy')->will(
$this->returnCallback(function () { $this->returnCallback(function () {
@@ -1904,9 +1906,9 @@ final class CoreLibsACLLoginTest extends TestCase
/** @var \CoreLibs\Create\Session&MockObject */ /** @var \CoreLibs\Create\Session&MockObject */
$session_mock = $this->createPartialMock( $session_mock = $this->createPartialMock(
\CoreLibs\Create\Session::class, \CoreLibs\Create\Session::class,
['startSession', 'checkActiveSession', 'sessionDestroy'] ['getSessionId', 'checkActiveSession', 'sessionDestroy']
); );
$session_mock->method('startSession')->willReturn('ACLLOGINTEST34'); $session_mock->method('getSessionId')->willReturn('ACLLOGINTEST34');
$session_mock->method('checkActiveSession')->willReturn(true); $session_mock->method('checkActiveSession')->willReturn(true);
$session_mock->method('sessionDestroy')->will( $session_mock->method('sessionDestroy')->will(
$this->returnCallback(function () { $this->returnCallback(function () {
@@ -1992,9 +1994,9 @@ final class CoreLibsACLLoginTest extends TestCase
/** @var \CoreLibs\Create\Session&MockObject */ /** @var \CoreLibs\Create\Session&MockObject */
$session_mock = $this->createPartialMock( $session_mock = $this->createPartialMock(
\CoreLibs\Create\Session::class, \CoreLibs\Create\Session::class,
['startSession', 'checkActiveSession', 'sessionDestroy'] ['getSessionId', 'checkActiveSession', 'sessionDestroy']
); );
$session_mock->method('startSession')->willReturn('ACLLOGINTEST34'); $session_mock->method('getSessionId')->willReturn('ACLLOGINTEST34');
$session_mock->method('checkActiveSession')->willReturn(true); $session_mock->method('checkActiveSession')->willReturn(true);
$session_mock->method('sessionDestroy')->will( $session_mock->method('sessionDestroy')->will(
$this->returnCallback(function () { $this->returnCallback(function () {
@@ -2088,9 +2090,9 @@ final class CoreLibsACLLoginTest extends TestCase
/** @var \CoreLibs\Create\Session&MockObject */ /** @var \CoreLibs\Create\Session&MockObject */
$session_mock = $this->createPartialMock( $session_mock = $this->createPartialMock(
\CoreLibs\Create\Session::class, \CoreLibs\Create\Session::class,
['startSession', 'checkActiveSession', 'sessionDestroy'] ['getSessionId', 'checkActiveSession', 'sessionDestroy']
); );
$session_mock->method('startSession')->willReturn('ACLLOGINTEST34'); $session_mock->method('getSessionId')->willReturn('ACLLOGINTEST34');
$session_mock->method('checkActiveSession')->willReturn(true); $session_mock->method('checkActiveSession')->willReturn(true);
$session_mock->method('sessionDestroy')->will( $session_mock->method('sessionDestroy')->will(
$this->returnCallback(function () { $this->returnCallback(function () {

View File

@@ -32,6 +32,7 @@ BEGIN
IF TG_OP = 'INSERT' THEN IF TG_OP = 'INSERT' THEN
NEW.date_created := 'now'; NEW.date_created := 'now';
NEW.cuid := random_string(random_length); NEW.cuid := random_string(random_length);
NEW.cuuid := gen_random_uuid();
ELSIF TG_OP = 'UPDATE' THEN ELSIF TG_OP = 'UPDATE' THEN
NEW.date_updated := 'now'; NEW.date_updated := 'now';
END IF; END IF;
@@ -305,6 +306,7 @@ CREATE TABLE temp_files (
-- DROP TABLE edit_generic; -- DROP TABLE edit_generic;
CREATE TABLE edit_generic ( CREATE TABLE edit_generic (
cuid VARCHAR, cuid VARCHAR,
cuuid UUID,
date_created TIMESTAMP WITHOUT TIME ZONE DEFAULT clock_timestamp(), date_created TIMESTAMP WITHOUT TIME ZONE DEFAULT clock_timestamp(),
date_updated TIMESTAMP WITHOUT TIME ZONE date_updated TIMESTAMP WITHOUT TIME ZONE
); );
@@ -652,6 +654,8 @@ COMMENT ON COLUMN edit_user.additional_acl IS 'Additional Access Control List st
CREATE TABLE edit_log ( CREATE TABLE edit_log (
edit_log_id SERIAL PRIMARY KEY, edit_log_id SERIAL PRIMARY KEY,
euid INT, -- this is a foreign key, but I don't nedd to reference to it euid INT, -- this is a foreign key, but I don't nedd to reference to it
ecuid VARCHAR,
ecuuid UUID,
FOREIGN KEY (euid) REFERENCES edit_user (edit_user_id) MATCH FULL ON UPDATE CASCADE ON DELETE SET NULL, FOREIGN KEY (euid) REFERENCES edit_user (edit_user_id) MATCH FULL ON UPDATE CASCADE ON DELETE SET NULL,
username VARCHAR, username VARCHAR,
password VARCHAR, password VARCHAR,
@@ -664,6 +668,7 @@ CREATE TABLE edit_log (
page VARCHAR, page VARCHAR,
action VARCHAR, action VARCHAR,
action_id VARCHAR, action_id VARCHAR,
action_sub_id VARCHAR,
action_yes VARCHAR, action_yes VARCHAR,
action_flag VARCHAR, action_flag VARCHAR,
action_menu VARCHAR, action_menu VARCHAR,

View File

@@ -22,7 +22,6 @@ final class CoreLibsCreateSessionTest extends TestCase
public function sessionProvider(): array public function sessionProvider(): array
{ {
// 0: session name as parameter or for GLOBAL value // 0: session name as parameter or for GLOBAL value
// 1: type p: parameter, g: global, d: php.ini default
// 2: mock data as array // 2: mock data as array
// checkCliStatus: true/false, // checkCliStatus: true/false,
// getSessionStatus: PHP_SESSION_DISABLED for abort, // getSessionStatus: PHP_SESSION_DISABLED for abort,
@@ -31,13 +30,10 @@ final class CoreLibsCreateSessionTest extends TestCase
// checkActiveSession: true/false, [1st call, 2nd call] // checkActiveSession: true/false, [1st call, 2nd call]
// getSessionId: string or false // getSessionId: string or false
// 3: exepcted name (session)] // 3: exepcted name (session)]
// 4: Exception thrown on error // 4: auto write close flag
// 5: exception code, null for none
// 6: expected error string
return [ return [
'session parameter' => [ 'session parameter' => [
'sessionNameParameter', 'sessionNameParameter',
'p',
[ [
'checkCliStatus' => false, 'checkCliStatus' => false,
'getSessionStatus' => PHP_SESSION_NONE, 'getSessionStatus' => PHP_SESSION_NONE,
@@ -47,12 +43,9 @@ final class CoreLibsCreateSessionTest extends TestCase
], ],
'sessionNameParameter', 'sessionNameParameter',
null, null,
null,
'',
], ],
'session globals' => [ 'session globals' => [
'sessionNameGlobals', 'sessionNameGlobals',
'g',
[ [
'checkCliStatus' => false, 'checkCliStatus' => false,
'getSessionStatus' => PHP_SESSION_NONE, 'getSessionStatus' => PHP_SESSION_NONE,
@@ -61,13 +54,10 @@ final class CoreLibsCreateSessionTest extends TestCase
'getSessionId' => '1234abcd4567' 'getSessionId' => '1234abcd4567'
], ],
'sessionNameGlobals', 'sessionNameGlobals',
null, false,
null,
'',
], ],
'session name default' => [ 'auto write close' => [
'', 'sessionNameAutoWriteClose',
'd',
[ [
'checkCliStatus' => false, 'checkCliStatus' => false,
'getSessionStatus' => PHP_SESSION_NONE, 'getSessionStatus' => PHP_SESSION_NONE,
@@ -75,109 +65,8 @@ final class CoreLibsCreateSessionTest extends TestCase
'checkActiveSession' => [false, true], 'checkActiveSession' => [false, true],
'getSessionId' => '1234abcd4567' 'getSessionId' => '1234abcd4567'
], ],
'', 'sessionNameAutoWriteClose',
null, true,
null,
'',
],
// error checks
// 1: we are in cli
'on cli error' => [
'',
'd',
[
'checkCliStatus' => true,
'getSessionStatus' => PHP_SESSION_NONE,
'setSessionName' => true,
'checkActiveSession' => [false, true],
'getSessionId' => '1234abcd4567'
],
'',
'RuntimeException',
1,
'[SESSION] No sessions in php cli'
],
// 2: session disabled
'session disabled error' => [
'',
'd',
[
'checkCliStatus' => false,
'getSessionStatus' => PHP_SESSION_DISABLED,
'setSessionName' => true,
'checkActiveSession' => [false, true],
'getSessionId' => '1234abcd4567'
],
'',
'RuntimeException',
2,
'[SESSION] Sessions are disabled'
],
// 3: invalid session name: string
'invalid name chars error' => [
'1invalid$session#;',
'p',
[
'checkCliStatus' => false,
'getSessionStatus' => PHP_SESSION_NONE,
'setSessionName' => false,
'checkActiveSession' => [false, true],
'getSessionId' => '1234abcd4567'
],
'',
'UnexpectedValueException',
3,
'[SESSION] Invalid session name: 1invalid$session#;'
],
// 3: invalid session name: only numbers
'invalid name numbers only error' => [
'123',
'p',
[
'checkCliStatus' => false,
'getSessionStatus' => PHP_SESSION_NONE,
'setSessionName' => false,
'checkActiveSession' => [false, true],
'getSessionId' => '1234abcd4567'
],
'',
'UnexpectedValueException',
3,
'[SESSION] Invalid session name: 123'
],
// 3: invalid session name: invalid name short
// 3: invalid session name: too long (128)
// 4: failed to start session (2nd false on check active session)
'invalid name numbers only error' => [
'',
'd',
[
'checkCliStatus' => false,
'getSessionStatus' => PHP_SESSION_NONE,
'setSessionName' => true,
'checkActiveSession' => [false, false],
'getSessionId' => '1234abcd4567'
],
'',
'RuntimeException',
4,
'[SESSION] Failed to activate session'
],
// 5: get session id return false
'invalid name numbers only error' => [
'',
'd',
[
'checkCliStatus' => false,
'getSessionStatus' => PHP_SESSION_NONE,
'setSessionName' => true,
'checkActiveSession' => [false, true],
'getSessionId' => false
],
'',
'UnexpectedValueException',
5,
'[SESSION] getSessionId did not return a session id'
], ],
]; ];
} }
@@ -190,32 +79,23 @@ final class CoreLibsCreateSessionTest extends TestCase
* @testdox startSession $input name for $type will be $expected (error: $expected_error) [$_dataName] * @testdox startSession $input name for $type will be $expected (error: $expected_error) [$_dataName]
* *
* @param string $input * @param string $input
* @param string $type
* @param array<mixed> $mock_data * @param array<mixed> $mock_data
* @param string $expected * @param string $expected
* @param string|null $exception
* @param string $expected_error
* @return void * @return void
*/ */
public function testStartSession( public function testStartSession(
string $input, string $input,
string $type,
array $mock_data, array $mock_data,
string $expected, string $expected,
?string $exception, ?bool $auto_write_close,
?int $exception_code,
string $expected_error
): void { ): void {
// override expected
if ($type == 'd') {
$expected = ini_get('session.name');
}
/** @var \CoreLibs\Create\Session&MockObject $session_mock */ /** @var \CoreLibs\Create\Session&MockObject $session_mock */
$session_mock = $this->createPartialMock( $session_mock = $this->createPartialMock(
\CoreLibs\Create\Session::class, \CoreLibs\Create\Session::class,
[ [
'checkCliStatus', 'getSessionStatus', 'checkActiveSession', 'checkCliStatus',
'setSessionName', 'startSessionCall', 'getSessionId', 'getSessionStatus', 'checkActiveSession',
'getSessionId',
'getSessionName' 'getSessionName'
] ]
); );
@@ -234,12 +114,8 @@ final class CoreLibsCreateSessionTest extends TestCase
$mock_data['checkActiveSession'][0], $mock_data['checkActiveSession'][0],
$mock_data['checkActiveSession'][1], $mock_data['checkActiveSession'][1],
); );
// dummy set for session name
$session_mock->method('setSessionName')->with($input)->willReturn($mock_data['setSessionName']);
// set session name & return bsed on request data // set session name & return bsed on request data
$session_mock->method('getSessionName')->willReturn($expected); $session_mock->method('getSessionName')->willReturn($expected);
// will not return anything
$session_mock->method('startSessionCall');
// in test case only return string // in test case only return string
// false: will return false // false: will return false
$session_mock->method('getSessionId')->willReturn($mock_data['getSessionId']); $session_mock->method('getSessionId')->willReturn($mock_data['getSessionId']);
@@ -247,25 +123,7 @@ final class CoreLibsCreateSessionTest extends TestCase
// regex for session id // regex for session id
$ression_id_regex = "/^\w+$/"; $ression_id_regex = "/^\w+$/";
if ($exception !== null) { $session_id = $session_mock->getSessionId();
$this->expectException($exception);
$this->expectExceptionCode($exception_code);
}
unset($GLOBALS['SET_SESSION_NAME']);
$session_id = '';
switch ($type) {
case 'p':
$session_id = $session_mock->startSession($input);
break;
case 'g':
$GLOBALS['SET_SESSION_NAME'] = $input;
$session_id = $session_mock->startSession();
break;
case 'd':
$session_id = $session_mock->startSession();
break;
}
// asert checks // asert checks
if (!empty($session_id)) { if (!empty($session_id)) {
$this->assertMatchesRegularExpression( $this->assertMatchesRegularExpression(
@@ -284,6 +142,73 @@ final class CoreLibsCreateSessionTest extends TestCase
} }
} }
/**
* Undocumented function
*
* @return array
*/
public function providerSessionException(): array
{
return [
'not cli' => [
'TEST_EXCEPTION',
\RuntimeException::class,
1,
'/^\[SESSION\] No sessions in php cli$/',
],
/* 'session disabled ' => [
'TEST_EXCEPTION',
\RuntimeException::class,
2,
'/^\[SESSION\] Sessions are disabled/'
],
'invalid session name' => [
'--#as^-292p-',
\UnexpectedValueException::class,
3,
'/^\[SESSION\] Invalid session name: /'
],
'failed to activate session' => [
'TEST_EXCEPTION',
\RuntimeException::class,
4,
'/^\[SESSION\] Failed to activate session/'
],
'not a valid session id returned' => [
\UnexpectedValueException::class,
5,
'/^\[SESSION\] getSessionId did not return a session id/'
], */
];
}
/**
* exception checks
*
* @covers ::initSession
* @dataProvider providerSessionException
* @testdox create session $session_name with exception $exception ($exception_code) [$_dataName]
*
* @param string $session_name
* @param string $exception
* @param int $exception_code
* @param string $expected_error
* @return void
*/
public function testSessionException(
string $session_name,
string $exception,
int $exception_code,
string $expected_error,
): void {
//
// throws only on new Object creation
$this->expectException($exception);
$this->expectExceptionCode($exception_code);
$this->expectExceptionMessageMatches($expected_error);
new \CoreLibs\Create\Session($session_name);
}
/** /**
* provider for session name check * provider for session name check
* *
@@ -347,109 +272,147 @@ final class CoreLibsCreateSessionTest extends TestCase
* *
* @return array * @return array
*/ */
public function sessionDataProvider(): array public function providerSessionData(): array
{ {
return [ return [
'test' => [ 'test' => [
'foo', 'foo',
'bar', 'bar',
'bar', 'bar',
null,
], ],
'int key test' => [ 'int key test' => [
123, 123,
'bar', 'bar',
'bar', 'bar',
\UnexpectedValueException::class
], ],
// more complex value tests // more complex value tests
'array values' => [ 'array values' => [
'array', 'array',
[1, 2, 3], [1, 2, 3],
[1, 2, 3], [1, 2, 3],
null,
] ]
]; ];
} }
// NOTE: with auto start session, we cannot test this in the command line
/** /**
* method call test * method call test
* *
* @covers ::setS * @covers ::set
* @covers ::getS * @covers ::get
* @covers ::issetS * @covers ::isset
* @covers ::unsetS * @covers ::unset
* @dataProvider sessionDataProvider * @dataProvider providerSessionData
* @testdox setS/getS/issetS/unsetS $name with $input is $expected [$_dataName] * @testdox set/get/isset/unset $name with $input is $expected ($exception) [$_dataName]
* *
* @param string|int $name * @param string|int $name
* @param mixed $input * @param mixed $input
* @param mixed $expected * @param mixed $expected
* @param ?mixed $exception
* @return void * @return void
*/ */
public function testMethodSetGet($name, $input, $expected): void public function testMethodSetGet($name, $input, $expected, $exception): void
{ {
$session = new \CoreLibs\Create\Session(); if (\CoreLibs\Get\System::checkCLI()) {
$session->setS($name, $input); $this->markTestSkipped('Cannot run testMethodSetGet in CLI');
}
$session = new \CoreLibs\Create\Session('TEST_METHOD');
if ($expected !== null) {
$this->expectException($exception);
}
$session->set($name, $input);
$this->assertEquals( $this->assertEquals(
$expected, $expected,
$session->getS($name), $session->get($name),
'method set assert' 'method set assert'
); );
// isset true // isset true
$this->assertTrue( $this->assertTrue(
$session->issetS($name), $session->isset($name),
'method isset assert ok' 'method isset assert ok'
); );
$session->unsetS($name); $session->unset($name);
$this->assertEquals( $this->assertEquals(
'', '',
$session->getS($name), $session->get($name),
'method unset assert' 'method unset assert'
); );
// iset false // isset false
$this->assertFalse( $this->assertFalse(
$session->issetS($name), $session->isset($name),
'method isset assert false' 'method isset assert false'
); );
} }
/** /**
* magic call test * Undocumented function
* *
* @covers ::__set * @return array
* @covers ::__get */
* @covers ::__isset public function providerSessionDataMany(): array
* @covers ::__unset {
* @dataProvider sessionDataProvider return [
* @testdox __set/__get/__iseet/__unset $name with $input is $expected [$_dataName] 'valid set' => [
[
'foo 1' => 'bar 1',
'foo 2' => 'bar 1',
],
[
'foo 1' => 'bar 1',
'foo 2' => 'bar 1',
],
null,
],
'invalid entry' => [
[
'foo 1' => 'bar 1',
123 => 'bar 1',
],
[
'foo 1' => 'bar 1',
],
\UnexpectedValueException::class
]
];
}
/**
* Undocumented function
* *
* @param string|int $name * @covers ::setMany
* @param mixed $input * @covers ::getMany
* @param mixed $expected * @dataProvider providerSessionDataMany
* @testdox setMany/getMany/unsetMany $set is $expected ($exception) [$_dataName]
*
* @param array<string|int,mixed> $set
* @param array<string,mixed> $expected
* @param ?mixed $exception
* @return void * @return void
*/ */
public function testMagicSetGet($name, $input, $expected): void public function testMany($set, $expected, $exception): void
{ {
$session = new \CoreLibs\Create\Session(); if (\CoreLibs\Get\System::checkCLI()) {
$session->$name = $input; $this->markTestSkipped('Cannot run testMethodSetGet in CLI');
}
$session = new \CoreLibs\Create\Session('TEST_METHOD');
if ($expected !== null) {
$this->expectException($exception);
}
$session->setMany($set);
$this->assertEquals( $this->assertEquals(
$expected, $expected,
$session->$name, $session->getMany(array_keys($set)),
'magic set assert' 'set many failed'
); );
// isset true $session->unsetMany(array_keys($set));
$this->assertTrue(
isset($session->$name),
'magic isset assert ok'
);
unset($session->$name);
$this->assertEquals( $this->assertEquals(
'', [],
$session->$name, $session->getMany(array_keys($set)),
'magic unset assert' 'unset many failed'
);
// isset true
$this->assertFalse(
isset($session->$name),
'magic isset assert false'
); );
} }
@@ -463,27 +426,30 @@ final class CoreLibsCreateSessionTest extends TestCase
*/ */
public function testUnsetAll(): void public function testUnsetAll(): void
{ {
if (\CoreLibs\Get\System::checkCLI()) {
$this->markTestSkipped('Cannot run testUnsetAll in CLI');
}
$test_values = [ $test_values = [
'foo' => 'abc', 'foo' => 'abc',
'bar' => '123' 'bar' => '123'
]; ];
$session = new \CoreLibs\Create\Session(); $session = new \CoreLibs\Create\Session('TEST_UNSET');
foreach ($test_values as $name => $value) { foreach ($test_values as $name => $value) {
$session->setS($name, $value); $session->set($name, $value);
// confirm set // confirm set
$this->assertEquals( $this->assertEquals(
$value, $value,
$session->getS($name), $session->get($name),
'set assert: ' . $name 'set assert: ' . $name
); );
} }
// unset all // unset all
$session->unsetAllS(); $session->clear();
// check unset // check unset
foreach (array_keys($test_values) as $name) { foreach (array_keys($test_values) as $name) {
$this->assertEquals( $this->assertEquals(
'', '',
$session->getS($name), $session->get($name),
'unsert assert: ' . $name 'unsert assert: ' . $name
); );
} }