diff options
Diffstat (limited to 'lib/Minz')
| -rw-r--r-- | lib/Minz/Extension.php | 6 | ||||
| -rw-r--r-- | lib/Minz/Log.php | 7 | ||||
| -rw-r--r-- | lib/Minz/ModelPdo.php | 2 | ||||
| -rw-r--r-- | lib/Minz/User.php | 27 |
4 files changed, 32 insertions, 10 deletions
diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index a35d7d8b6..a280676c1 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -155,7 +155,7 @@ abstract class Minz_Extension { $file_name_url = urlencode("{$dir}/static/{$filename}"); $mtime = @filemtime("{$this->path}/static/{$filename}"); } else { - $username = Minz_Session::param('currentUser'); + $username = Minz_User::name(); $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}"; $file_name_url = urlencode("{$username}/{$this->config_key}/{$this->getName()}/{$filename}"); $mtime = @filemtime($path); @@ -343,7 +343,7 @@ abstract class Minz_Extension { } public function saveFile(string $filename, string $content) { - $username = Minz_Session::param('currentUser'); + $username = Minz_User::name(); $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}"; if (!file_exists($path)) { @@ -354,7 +354,7 @@ abstract class Minz_Extension { } public function removeFile(string $filename) { - $username = Minz_Session::param('currentUser'); + $username = Minz_User::name(); $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}"; if (file_exists($path)) { diff --git a/lib/Minz/Log.php b/lib/Minz/Log.php index 43f3c9d6d..117f231ac 100644 --- a/lib/Minz/Log.php +++ b/lib/Minz/Log.php @@ -31,14 +31,9 @@ class Minz_Log { } if (! ($env === 'silent' || ($env === 'production' && ($level >= LOG_NOTICE)))) { - $username = Minz_Session::param('currentUser', ''); - if ($username == '') { - $username = '_'; - } + $username = Minz_User::name() ?? Minz_User::INTERNAL_USER; if ($file_name == null) { $file_name = join_path(USERS_PATH, $username, LOG_FILENAME); - } else { - $username = '_'; } switch ($level) { diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php index 3bcf87247..b8c1ad89b 100644 --- a/lib/Minz/ModelPdo.php +++ b/lib/Minz/ModelPdo.php @@ -103,7 +103,7 @@ class Minz_ModelPdo { */ public function __construct($currentUser = null, $currentPdo = null) { if ($currentUser === null) { - $currentUser = Minz_Session::param('currentUser'); + $currentUser = Minz_User::name(); } if ($currentPdo !== null) { $this->pdo = $currentPdo; diff --git a/lib/Minz/User.php b/lib/Minz/User.php new file mode 100644 index 000000000..252584e83 --- /dev/null +++ b/lib/Minz/User.php @@ -0,0 +1,27 @@ +<?php + +/** + * The Minz_User class handles the user information. + */ +final class Minz_User { + + public const INTERNAL_USER = '_'; + + public const CURRENT_USER = 'currentUser'; + + /** + * @return string the name of the current user, or null if there is none + */ + public static function name(): ?string { + $currentUser = trim(Minz_Session::param(Minz_User::CURRENT_USER, '')); + return $currentUser === '' ? null : $currentUser; + } + + /** + * @param string $name the name of the new user. Set to empty string to clear the user. + */ + public static function change(string $name = ''): void { + $name = trim($name); + Minz_Session::_param(Minz_User::CURRENT_USER, $name === '' ? false : $name); + } +} |
