权限 ID 微调
This commit is contained in:
parent
62cbd727a9
commit
cbc4a018c0
@ -70,8 +70,8 @@ extern "C"
|
|||||||
{
|
{
|
||||||
if (filter(user_id)) return 0;
|
if (filter(user_id)) return 0;
|
||||||
|
|
||||||
auto status =
|
auto hash = generate_hash(password, iter_round);
|
||||||
user_db->Put(leveldb::WriteOptions{}, mangle_user_id(user_id), generate_hash(password, iter_round));
|
auto status = user_db->Put(leveldb::WriteOptions{}, mangle_user_id(user_id), hash);
|
||||||
if (!status.ok()) {
|
if (!status.ok()) {
|
||||||
std::println(stderr, "Failed to set user password: {}", status.ToString());
|
std::println(stderr, "Failed to set user password: {}", status.ToString());
|
||||||
return 0;
|
return 0;
|
||||||
@ -99,7 +99,7 @@ extern "C"
|
|||||||
|
|
||||||
auto batch = leveldb::WriteBatch{};
|
auto batch = leveldb::WriteBatch{};
|
||||||
batch.Put(mangle_user_id(user_id), generate_hash(password, iter_round));
|
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);
|
auto status = user_db->Write(leveldb::WriteOptions{}, &batch);
|
||||||
if (!status.ok()) {
|
if (!status.ok()) {
|
||||||
std::println(stderr, "Failed to register: {}", status.ToString());
|
std::println(stderr, "Failed to register: {}", status.ToString());
|
||||||
@ -150,11 +150,12 @@ extern "C"
|
|||||||
return 1;
|
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);
|
auto status = user_db->Put(leveldb::WriteOptions{}, "admin_password_hash", hash);
|
||||||
if (!status.ok()) {
|
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 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -165,14 +166,14 @@ extern "C"
|
|||||||
auto value = std::string{};
|
auto value = std::string{};
|
||||||
auto status = user_db->Get(leveldb::ReadOptions{}, "admin_password_hash", &value);
|
auto status = user_db->Get(leveldb::ReadOptions{}, "admin_password_hash", &value);
|
||||||
if (!status.ok()) {
|
if (!status.ok()) {
|
||||||
std::println(stderr, "Failed to login: {}", status.ToString());
|
std::println(stderr, "Failed to login admin: {}", status.ToString());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
*result = validate_password(password, value.data());
|
*result = validate_password(password, value.data());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int has_admin_password_hash(int* result)
|
int has_admin_password(int* result)
|
||||||
{
|
{
|
||||||
auto value = std::string{};
|
auto value = std::string{};
|
||||||
auto status = user_db->Get(leveldb::ReadOptions{}, "admin_password_hash", &value);
|
auto status = user_db->Get(leveldb::ReadOptions{}, "admin_password_hash", &value);
|
||||||
@ -181,7 +182,7 @@ extern "C"
|
|||||||
} else if (status.IsNotFound()) {
|
} else if (status.IsNotFound()) {
|
||||||
*result = 0;
|
*result = 0;
|
||||||
} else {
|
} 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 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1,7 +1,14 @@
|
|||||||
<template>
|
<template>
|
||||||
<v-card subtitle="占位符占位符占位符" title="用户信息面板" max-width="80%">
|
<v-card subtitle="占位符占位符占位符" title="用户信息面板" max-width="80%">
|
||||||
<v-card-item>
|
<v-card-item>
|
||||||
用户 ID: {{ userId }}
|
<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-card-item>
|
<v-card-item>
|
||||||
<v-chip class="chips" color="orange" @click="logout">
|
<v-chip class="chips" color="orange" @click="logout">
|
||||||
@ -20,9 +27,6 @@
|
|||||||
</v-expansion-panel>
|
</v-expansion-panel>
|
||||||
</v-expansion-panels>
|
</v-expansion-panels>
|
||||||
</v-card-item>
|
</v-card-item>
|
||||||
<v-card-item>
|
|
||||||
<Toy></Toy>
|
|
||||||
</v-card-item>
|
|
||||||
</v-card>
|
</v-card>
|
||||||
<v-dialog v-model="dialogShow" width="auto">
|
<v-dialog v-model="dialogShow" width="auto">
|
||||||
<v-card max-width="400" prepend-icon="mdi-update" :text="dialogText" :title="dialogTitle">
|
<v-card max-width="400" prepend-icon="mdi-update" :text="dialogText" :title="dialogTitle">
|
||||||
@ -36,10 +40,9 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed } from 'vue';
|
import { ref, computed, watch } from 'vue';
|
||||||
import { useAuthStore } from '@/store/auth';
|
import { useAuthStore } from '@/store/auth';
|
||||||
import { jwtDecode, type JwtPayload } from 'jwt-decode';
|
import { jwtDecode, type JwtPayload } from 'jwt-decode';
|
||||||
import Toy from './Toy.vue';
|
|
||||||
import axios, { AxiosError } from 'axios';
|
import axios, { AxiosError } from 'axios';
|
||||||
import DeleteAccountDialog from './DeleteAccountDialog.vue';
|
import DeleteAccountDialog from './DeleteAccountDialog.vue';
|
||||||
import RepasswdDialog from './RepasswdDialog.vue';
|
import RepasswdDialog from './RepasswdDialog.vue';
|
||||||
@ -79,6 +82,46 @@ const userId = computed(() => {
|
|||||||
return '';
|
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 () => {
|
const logout = async () => {
|
||||||
authStore.clearToken();
|
authStore.clearToken();
|
||||||
|
Loading…
Reference in New Issue
Block a user