Skip to content

Commit

Permalink
Rename auth_hash to auth_key in user table
Browse files Browse the repository at this point in the history
  • Loading branch information
juneszh committed Sep 9, 2022
1 parent 6355904 commit a571140
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down Expand Up @@ -281,7 +281,7 @@ private static function createTable()
"NOT NULL",
"DEFAULT '1'",
],
'auth_hash' => [
'auth_key' => [
"VARCHAR(32)",
"NOT NULL",
"DEFAULT ''",
Expand All @@ -293,7 +293,7 @@ private static function createTable()
],
'PRIMARY KEY (<id>)',
'UNIQUE INDEX <account> (<account>)',
'INDEX <auth_hash> (<auth_hash>)',
'INDEX <auth_key> (<auth_key>)',
], [
"ENGINE" => "InnoDB",
"DEFAULT CHARSET" => "utf8mb4",
Expand All @@ -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(),
],
]);

Expand Down
8 changes: 4 additions & 4 deletions src/Admin/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions src/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}
});
Expand Down
18 changes: 9 additions & 9 deletions src/Admin/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a571140

Please sign in to comment.