From a571140d51539085f196c819dfb385204942228e Mon Sep 17 00:00:00 2001 From: juneszh Date: Fri, 9 Sep 2022 17:19:58 +0800 Subject: [PATCH] Rename auth_hash to auth_key in user table --- src/Admin.php | 8 ++++---- src/Admin/Auth.php | 8 ++++---- src/Admin/Controller.php | 4 ++-- src/Admin/Model.php | 18 +++++++++--------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Admin.php b/src/Admin.php index 2b6b253..8b7a7ea 100644 --- a/src/Admin.php +++ b/src/Admin.php @@ -205,7 +205,7 @@ private static function insertConfig() private static function createTable() { $alightAccount = 'alight'; - $alightPassword = Utility::uid(16); + $alightPassword = Utility::randomHex(16); $db = Database::init(); $roleCreate = $db->create('admin_role', [ @@ -281,7 +281,7 @@ private static function createTable() "NOT NULL", "DEFAULT '1'", ], - 'auth_hash' => [ + 'auth_key' => [ "VARCHAR(32)", "NOT NULL", "DEFAULT ''", @@ -293,7 +293,7 @@ private static function createTable() ], 'PRIMARY KEY ()', 'UNIQUE INDEX ()', - 'INDEX ()', + 'INDEX ()', ], [ "ENGINE" => "InnoDB", "DEFAULT CHARSET" => "utf8mb4", @@ -308,7 +308,7 @@ private static function createTable() 'password' => password_hash($alightPassword, PASSWORD_DEFAULT), 'name' => 'Alight', 'role_id' => 1, - 'auth_hash' => Utility::uid(), + 'auth_key' => Utility::randomHex(), ], ]); diff --git a/src/Admin/Auth.php b/src/Admin/Auth.php index 5ecec7c..8faddd7 100644 --- a/src/Admin/Auth.php +++ b/src/Admin/Auth.php @@ -75,14 +75,14 @@ public static function getUserId(): int } if ($auth && $session) { - $userId = Model::getUserIdByHash($auth); + $userId = Model::getUserIdByKey($auth); if ($userId) { $cache = Cache::init(); $cacheKey = 'admin_user_auth_' . $userId; $authInfo = $cache->get($cacheKey); if ($authInfo && $authInfo['session'] == $session) { $userInfo = Model::getUserInfo($userId); - if ($userInfo['status'] == 1 && ($authInfo['auth'] ?? '') == $userInfo['auth_hash']) { + if ($userInfo['status'] == 1 && ($authInfo['auth'] ?? '') == $userInfo['auth_key']) { return (int) $userId; } } @@ -109,8 +109,8 @@ public static function store(int $userId, bool $renew = false) $session = trim(strip_tags($_COOKIE['admin_session'] ?? '')); } else { $userInfo = Model::getUserInfo($userId); - $auth = $userInfo['auth_hash']; - $session = Utility::uid(); + $auth = $userInfo['auth_key']; + $session = Utility::randomHex(); } $authInfo = [ diff --git a/src/Admin/Controller.php b/src/Admin/Controller.php index e302226..366a1a9 100755 --- a/src/Admin/Controller.php +++ b/src/Admin/Controller.php @@ -203,7 +203,7 @@ public static function captcha() $builder = new \Gregwar\Captcha\CaptchaBuilder(null, $phraseBuilder); $code = $builder->build(130, 40)->getPhrase(); - $captchaHash = Utility::uid(); + $captchaHash = Utility::randomHex(); $cache = Cache::init(); $cache->set('admin_captcha_' . $captchaHash, $code, 300); @@ -344,7 +344,7 @@ public static function userForm() Form::render('admin_user', function ($action, &$return) { if ($action == 'filter') { if (in_array(Request::$data['_form'], ['add', 'password', 'my_password'])) { - $return['auth_hash'] = Utility::uid(); + $return['auth_key'] = Utility::randomHex(); } } }); diff --git a/src/Admin/Model.php b/src/Admin/Model.php index 69739b6..f2ecc39 100755 --- a/src/Admin/Model.php +++ b/src/Admin/Model.php @@ -61,7 +61,9 @@ public static function getRoleEnumList(?array $filter = [], ?string $enumKey = n /** * Get user enum list * - * @param array $filter + * @param null|array $filter + * @param null|string $enumKey + * @param null|string $enumValue * @return array * @throws Exception * @throws ErrorException @@ -70,7 +72,7 @@ public static function getRoleEnumList(?array $filter = [], ?string $enumKey = n * @throws InvalidArgumentException * @throws PDOException */ - public static function getUserEnumList(array $filter = []): array + public static function getUserEnumList(?array $filter = [], ?string $enumKey = null, ?string $enumValue = null): array { $cache = Cache::init(); $cacheKey = 'admin_user_enum_list'; @@ -84,9 +86,7 @@ public static function getUserEnumList(array $filter = []): array $cache->set($cacheKey, $result, 86400); } - if ($filter) { - $result = Utility::arrayFilter($result, $filter); - } + $result = Utility::arrayFilter($result, $filter, $enumKey, $enumValue); return $result; } @@ -123,24 +123,24 @@ public static function getUserIdByAccount(string $account): int /** * Get user id by auth hash * - * @param string $hash + * @param string $key * @return int * @throws Exception * @throws ErrorException * @throws ExceptionInvalidArgumentException * @throws ExceptionInvalidArgumentException */ - public static function getUserIdByHash(string $hash): int + public static function getUserIdByKey(string $key): int { $cache = Cache::init(); - $cacheKey = 'admin_user_id_by_hash_' . $hash; + $cacheKey = 'admin_user_id_by_key_' . $key; if ($cache->has($cacheKey)) { return $cache->get($cacheKey); } $db = Database::init(); - $result = $db->get('admin_user', 'id', ['auth_hash' => $hash]); + $result = $db->get('admin_user', 'id', ['auth_key' => $key]); if ($result) { $cache->set($cacheKey, (int) $result, 3600);