From 5faae8796d4aecbe27c066ff9a22ed96f87e646f Mon Sep 17 00:00:00 2001 From: Dream Hunter Date: Sat, 27 Jul 2024 22:04:18 +0800 Subject: [PATCH] feat: add ADMIN_USER_ROLE for user access admin panel (#363) --- CHANGELOG.md | 5 +++++ frontend/package.json | 2 +- frontend/src/api/index.js | 1 + frontend/src/store/index.js | 8 +++++++- frontend/src/views/Admin.vue | 7 ++++--- frontend/src/views/Header.vue | 11 +++++++--- frontend/src/views/admin/Account.vue | 8 ++------ frontend/src/views/admin/Mails.vue | 12 +---------- frontend/src/views/admin/MailsUnknow.vue | 14 +------------ frontend/src/views/admin/Maintenance.vue | 8 +------- frontend/src/views/admin/Statistics.vue | 5 ----- frontend/src/views/user/UserBar.vue | 3 ++- vitepress-docs/docs/en/cli.md | 1 + vitepress-docs/docs/zh/guide/cli/worker.md | 2 ++ .../zh/guide/feature/admin-user-management.md | 2 -- vitepress-docs/docs/zh/guide/feature/admin.md | 8 ++++++-- worker/src/constants.ts | 2 +- worker/src/types.d.ts | 1 + worker/src/user_api/settings.ts | 16 +++++++++++++++ worker/src/worker.ts | 20 +++++++++++++++++++ worker/wrangler.toml.template | 1 + 21 files changed, 81 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ac47cad..1b543844 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # CHANGE LOG +## main(v0.7.0) + +- Docs: Update new-address-api.md (#360) +- feat: worker 增加 `ADMIN_USER_ROLE` 配置, 用于配置管理员用户角色,此角色的用户可访问 admin 管理页面 (#363) + ## v0.6.1 - pages github actions && 修复清理邮件天数为 0 不生效 by @tqjason (#355) diff --git a/frontend/package.json b/frontend/package.json index e511351c..dded1e25 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "cloudflare_temp_email", - "version": "0.6.1", + "version": "0.7.0", "private": true, "type": "module", "scripts": { diff --git a/frontend/src/api/index.js b/frontend/src/api/index.js index 15cfb965..3e7ccd80 100644 --- a/frontend/src/api/index.js +++ b/frontend/src/api/index.js @@ -22,6 +22,7 @@ const apiFetch = async (path, options = {}) => { data: options.body || null, headers: { 'x-user-token': userJwt.value, + 'x-user-access-token': userSettings.value.access_token, 'x-custom-auth': auth.value, 'x-admin-auth': adminAuth.value, 'Authorization': `Bearer ${jwt.value}`, diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js index 43e9c555..377506d0 100644 --- a/frontend/src/store/index.js +++ b/frontend/src/store/index.js @@ -1,4 +1,4 @@ -import { ref } from "vue"; +import { computed, ref } from "vue"; import { createGlobalState, useStorage, useDark, useToggle, useLocalStorage } from '@vueuse/core' export const useGlobalState = createGlobalState( @@ -74,9 +74,14 @@ export const useGlobalState = createGlobalState( user_email: '', /** @type {number} */ user_id: 0, + /** @type {boolean} */ + is_admin: false, + /** @type {string | null} */ + access_token: null, /** @type {null | {domains: string[] | undefined | null, role: string, prefix: string | undefined | null}} */ user_role: null, }); + const showAdminPage = computed(() => !!adminAuth.value || userSettings.value.is_admin); const telegramApp = ref(window.Telegram?.WebApp || {}); const isTelegram = ref(!!window.Telegram?.WebApp?.initData); return { @@ -108,6 +113,7 @@ export const useGlobalState = createGlobalState( useSideMargin, telegramApp, isTelegram, + showAdminPage, } }, ) diff --git a/frontend/src/views/Admin.vue b/frontend/src/views/Admin.vue index c50498fd..74acd1db 100644 --- a/frontend/src/views/Admin.vue +++ b/frontend/src/views/Admin.vue @@ -21,7 +21,8 @@ import Telegram from './admin/Telegram.vue'; import Webhook from './admin/Webhook.vue'; const { - adminAuth, showAdminAuth, adminTab, loading, globalTabplacement + adminAuth, showAdminAuth, adminTab, loading, + globalTabplacement, showAdminPage } = useGlobalState() const message = useMessage() @@ -81,7 +82,7 @@ const { t } = useI18n({ }); onMounted(async () => { - if (!adminAuth.value) { + if (!showAdminPage.value) { showAdminAuth.value = true; return; } @@ -100,7 +101,7 @@ onMounted(async () => { - + diff --git a/frontend/src/views/Header.vue b/frontend/src/views/Header.vue index 23fe788c..de5b75f9 100644 --- a/frontend/src/views/Header.vue +++ b/frontend/src/views/Header.vue @@ -17,8 +17,8 @@ import { getRouterPathWithLang } from '../utils' const message = useMessage() const { - toggleDark, isDark, isTelegram, - showAuth, adminAuth, auth, loading, openSettings + toggleDark, isDark, isTelegram, showAdminPage, + showAuth, auth, loading, openSettings } = useGlobalState() const route = useRoute() const router = useRouter() @@ -134,7 +134,7 @@ const menuOptions = computed(() => [ icon: () => h(NIcon, { component: AdminPanelSettingsFilled }), } ), - show: !!adminAuth.value, + show: showAdminPage.value, key: "admin" }, { @@ -223,6 +223,11 @@ const logoClick = async () => { onMounted(async () => { await api.getOpenSettings(message); + try { + await api.getUserSettings(message); + } catch (error) { + console.error(error); + } }); diff --git a/frontend/src/views/admin/Account.vue b/frontend/src/views/admin/Account.vue index 8329faed..8eb077f4 100644 --- a/frontend/src/views/admin/Account.vue +++ b/frontend/src/views/admin/Account.vue @@ -9,8 +9,8 @@ import { NButton, NMenu } from 'naive-ui'; import { MenuFilled } from '@vicons/material' const { - adminAuth, showAdminAuth, loading, - adminTab, adminMailTabAddress, adminSendBoxTabAddress + showAdminAuth, loading, adminTab, + adminMailTabAddress, adminSendBoxTabAddress } = useGlobalState() const message = useMessage() @@ -252,10 +252,6 @@ watch([page, pageSize], async () => { }) onMounted(async () => { - if (!adminAuth.value) { - showAdminAuth.value = true; - return; - } await fetchData() }) diff --git a/frontend/src/views/admin/Mails.vue b/frontend/src/views/admin/Mails.vue index 33b808e3..1c64d5d1 100644 --- a/frontend/src/views/admin/Mails.vue +++ b/frontend/src/views/admin/Mails.vue @@ -6,10 +6,7 @@ import { useGlobalState } from '../../store' import { api } from '../../api' import MailBox from '../../components/MailBox.vue'; -const { - adminAuth, showAdminAuth, - adminMailTabAddress -} = useGlobalState() +const { adminMailTabAddress } = useGlobalState() const { t } = useI18n({ messages: { @@ -48,13 +45,6 @@ const fetchMailData = async (limit, offset) => { const deleteMail = async (curMailId) => { await api.fetch(`/admin/mails/${curMailId}`, { method: 'DELETE' }); }; - -onMounted(async () => { - if (!adminAuth.value) { - showAdminAuth.value = true; - return; - } -})