权限 ID 微调

This commit is contained in:
keqingmoe 2024-12-28 17:05:03 +08:00
parent 62cbd727a9
commit cbc4a018c0
2 changed files with 58 additions and 14 deletions

View File

@ -70,8 +70,8 @@ extern "C"
{
if (filter(user_id)) return 0;
auto status =
user_db->Put(leveldb::WriteOptions{}, mangle_user_id(user_id), generate_hash(password, iter_round));
auto hash = generate_hash(password, iter_round);
auto status = user_db->Put(leveldb::WriteOptions{}, mangle_user_id(user_id), hash);
if (!status.ok()) {
std::println(stderr, "Failed to set user password: {}", status.ToString());
return 0;
@ -99,7 +99,7 @@ extern "C"
auto batch = leveldb::WriteBatch{};
batch.Put(mangle_user_id(user_id), generate_hash(password, iter_round));
batch.Put(mangle_permission(user_id), "1");
batch.Put(mangle_permission(user_id), "2");
auto status = user_db->Write(leveldb::WriteOptions{}, &batch);
if (!status.ok()) {
std::println(stderr, "Failed to register: {}", status.ToString());
@ -150,11 +150,12 @@ extern "C"
return 1;
}
int set_admin_password_hash(const char* hash)
int set_admin_password(const char* password)
{
auto hash = generate_hash(password, iter_round);
auto status = user_db->Put(leveldb::WriteOptions{}, "admin_password_hash", hash);
if (!status.ok()) {
std::println(stderr, "Failed to set admin password hash: {}", status.ToString());
std::println(stderr, "Failed to set admin password: {}", status.ToString());
return 0;
}
return 1;
@ -165,14 +166,14 @@ extern "C"
auto value = std::string{};
auto status = user_db->Get(leveldb::ReadOptions{}, "admin_password_hash", &value);
if (!status.ok()) {
std::println(stderr, "Failed to login: {}", status.ToString());
std::println(stderr, "Failed to login admin: {}", status.ToString());
return 0;
}
*result = validate_password(password, value.data());
return 1;
}
int has_admin_password_hash(int* result)
int has_admin_password(int* result)
{
auto value = std::string{};
auto status = user_db->Get(leveldb::ReadOptions{}, "admin_password_hash", &value);
@ -181,7 +182,7 @@ extern "C"
} else if (status.IsNotFound()) {
*result = 0;
} else {
std::println(stderr, "Failed to check admin password hash existence: {}", status.ToString());
std::println(stderr, "Failed to check admin password existence: {}", status.ToString());
return 0;
}
return 1;

View File

@ -1,7 +1,14 @@
<template>
<v-card subtitle="占位符占位符占位符" title="用户信息面板" max-width="80%">
<v-card-item>
<v-chip class="ma-2" color="primary" label>
<v-icon icon="mdi-account-circle-outline" start></v-icon>
用户 ID: {{ userId }}
</v-chip>
<v-chip class="ma-2" color="pink" label @click="updateUserPermission">
<v-icon icon="mdi-human" start></v-icon>
权限: {{ userPermissionReadable }}
</v-chip>
</v-card-item>
<v-card-item>
<v-chip class="chips" color="orange" @click="logout">
@ -20,9 +27,6 @@
</v-expansion-panel>
</v-expansion-panels>
</v-card-item>
<v-card-item>
<Toy></Toy>
</v-card-item>
</v-card>
<v-dialog v-model="dialogShow" width="auto">
<v-card max-width="400" prepend-icon="mdi-update" :text="dialogText" :title="dialogTitle">
@ -36,10 +40,9 @@
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';
import { ref, computed, watch } from 'vue';
import { useAuthStore } from '@/store/auth';
import { jwtDecode, type JwtPayload } from 'jwt-decode';
import Toy from './Toy.vue';
import axios, { AxiosError } from 'axios';
import DeleteAccountDialog from './DeleteAccountDialog.vue';
import RepasswdDialog from './RepasswdDialog.vue';
@ -79,6 +82,46 @@ const userId = computed(() => {
return '';
});
type UserPermissionResponse = { success?: string, permission?: string, error?: string };
const queryPermission = async () => {
try {
const formData = new FormData;
formData.append("user_id", userId.value);
let res = await axios.post('/api/auth/permission', formData);
return res.data as UserPermissionResponse;
} catch (e) {
let ex = e as AxiosError;
return ex.response?.data as UserPermissionResponse;
}
}
const userPermission = ref('');
const updateUserPermission = async () => {
let res = await queryPermission();
if (res?.success) {
userPermission.value = res.permission as string;
} else {
userPermission.value = '';
}
}
watch(userId, updateUserPermission, { immediate: true });
const userPermissionReadable = computed(() => {
if (userPermission.value.length > 0) {
if (userPermission.value == '1') {
return '老师';
} else if (userPermission.value == '2') {
return '学生';
} else {
return '未知';
}
} else {
return '获取失败';
}
})
const logout = async () => {
authStore.clearToken();