检查点:保留通知授权开发状态,开始用户分组隔离
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-25 21:19:28 +08:00
1 parent deba430f2f
commit 9be7a6f990
22 files changed
+1024 -7

No files matched your search

@@ -20,6 +20,7 @@ const lazySettings = loader => defineAsyncComponent({
})
const SecuritySettings = lazySettings(() => import('../SecuritySettings/SecuritySettings.vue'))
const ThirdPartyAuthorization = lazySettings(() => import('../ThirdPartyAuthorization/ThirdPartyAuthorization.vue'))
const SystemNotification = lazySettings(() => import('../SystemNotification/SystemNotification.vue'))
const ConfigSettings = lazySettings(() => import('../ConfigSettings/ConfigSettings.vue'))
const GroupSettings = lazySettings(() => import('../GroupSettings/GroupSettings.vue'))
const RuleSettings = lazySettings(() => import('../RuleSettings/RuleSettings.vue'))
@@ -50,6 +51,7 @@ export function usePage() {
const settingsMenu = computed(() => [
{ key: 'security', label: lang.security, hint: '账号与登录保护', icon: Lock, component: SecuritySettings, capability: 'password.change_own' },
{ key: 'third-party', label: '邮件客户端', hint: '当前账号协议授权', icon: Connection, component: ThirdPartyAuthorization, capability: 'user.read' },
{ key: 'system-notification', label: '系统通知发信', hint: '逐账号授权,仅发信', icon: Connection, component: SystemNotification, capability: 'user.read' },
{ key: 'config', label: '配置管理', hint: '服务与运行配置', icon: Setting, component: ConfigSettings, capability: 'config.read' },
{ key: 'group', label: lang.group_settings, hint: '邮箱分组管理', icon: FolderOpened, component: GroupSettings, capability: 'user.read' },
{ key: 'rule', label: lang.rule_setting, hint: '邮件处理规则', icon: Filter, component: RuleSettings, capability: 'user.read' },
@@ -0,0 +1,61 @@
.notification-settings {
display: flex;
flex-direction: column;
gap: 18px;
padding: 20px;
color: var(--el-text-color-primary);
}
.notification-header,
.notification-heading,
.notification-actions,
.notification-limits {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 16px;
}
.notification-header,
.notification-heading {
justify-content: space-between;
}
.notification-settings h2,
.notification-settings h3 {
margin: 0 0 10px;
}
.notification-header p,
.notification-hint {
margin: 6px 0 14px;
color: var(--el-text-color-secondary);
font-size: 13px;
line-height: 1.7;
}
.notification-panel {
min-height: 80px;
padding: 18px;
border: 1px solid var(--el-border-color-light);
border-radius: 10px;
background: var(--el-bg-color);
}
.notification-actions {
margin-bottom: 22px;
}
.notification-warning {
color: var(--el-color-warning-dark-2);
}
.notification-panel code {
overflow-wrap: anywhere;
user-select: all;
}
.notification-panel .el-pagination,
.notification-secret {
margin-top: 16px;
}
@@ -0,0 +1,171 @@
import { computed, onMounted, onUnmounted, reactive, ref } from 'vue'
import { ElMessageBox, ElNotification } from 'element-plus'
import { http } from '@/utils/axios'
import { useGlobalStatusStore } from '@/stores/useGlobalStatusStore'
import { isRecentAuthenticationCancelled, requestWithRecentAuthentication } from '@/utils/recentAuthentication'
export function usePage() {
const globalStatus = useGlobalStatusStore()
const manager = computed(() => globalStatus.hasCapability('notification.manage'))
const loading = ref(false)
const submitting = ref(false)
const error = ref('')
const status = ref(null)
const accounts = ref([])
const page = ref(1)
const total = ref(0)
const selectedID = ref(Number(globalStatus.userInfos?.id || 0))
const form = reactive({ enabled: false, minute_limit: 10, day_limit: 100 })
const secret = ref('')
const secretAccount = ref('')
const secretVisible = ref(false)
const seconds = ref(0)
let secretTimer = null
let requestSequence = 0
let accountsSequence = 0
let disposed = false
const canManage = computed(() => manager.value && Boolean(status.value?.can_manage))
const stateText = computed(() => {
if (!status.value?.enabled) return '未开启'
if (!status.value.account_available) return '账号不可用'
return status.value.configured ? '已开启,可发信' : '已开启,待生成凭据'
})
const dirty = computed(() => status.value && (
form.enabled !== status.value.enabled || form.minute_limit !== status.value.minute_limit || form.day_limit !== status.value.day_limit
))
const dataOf = result => {
if (result?.errorNo !== 0) throw new Error(result?.errorMsg || '操作失败')
return result.data
}
const showError = cause => {
if (isRecentAuthenticationCancelled(cause) || cause === 'cancel' || cause === 'close') return
ElNotification({ title: '操作失败', message: cause?.message || '请稍后重试', type: 'error' })
}
const clearSecret = () => {
if (secretTimer) clearInterval(secretTimer)
secretTimer = null
secret.value = ''
secretAccount.value = ''
seconds.value = 0
secretVisible.value = false
}
const loadStatus = async () => {
const sequence = ++requestSequence
loading.value = true
error.value = ''
try {
const data = dataOf(await http.get('/api/v1/system/notification/status', { params: { user_id: selectedID.value || undefined } }))
if (disposed || sequence !== requestSequence) return
status.value = data
Object.assign(form, { enabled: data.enabled, minute_limit: data.minute_limit, day_limit: data.day_limit })
} catch (cause) {
if (disposed || sequence !== requestSequence) return
status.value = null
error.value = cause?.message || '读取通知发信状态失败'
} finally {
if (!disposed && sequence === requestSequence) loading.value = false
}
}
const loadAccounts = async () => {
if (!manager.value) return
const sequence = ++accountsSequence
try {
const data = dataOf(await http.get('/api/v1/system/notification/accounts', { params: { page: page.value } }))
if (disposed || sequence !== accountsSequence) return
accounts.value = data.list || []
total.value = data.total || 0
} catch (cause) {
if (!disposed && sequence === accountsSequence) {
accounts.value = []
showError(cause)
}
}
}
const beforeLeave = async () => {
if (submitting.value) return false
if (!dirty.value) return true
try {
await ElMessageBox.confirm('通知授权尚未保存,是否放弃本次修改?', '离开确认', {
confirmButtonText: '放弃修改', cancelButtonText: '继续编辑', type: 'warning'
})
return true
} catch { return false }
}
const selectAccount = async row => {
if (loading.value || !(await beforeLeave())) return
clearSecret()
status.value = null
selectedID.value = row.user_id
await loadStatus()
}
const refresh = async () => {
if (!(await beforeLeave())) return
await Promise.all([loadStatus(), loadAccounts()])
}
const save = async () => {
if (!canManage.value || !status.value || submitting.value) return
const target = status.value
const input = { user_id: target.user_id, version: target.version, ...form }
if (input.day_limit < input.minute_limit) {
showError(new Error('每日限额不能小于每分钟限额'))
return
}
try {
await ElMessageBox.confirm(
input.enabled ? `保存账号「${target.account}」的通知发信授权与限额?` : `关闭账号「${target.account}」的通知发信后,旧凭据将永久撤销;普通收发信不受影响。`,
'确认通知授权', { confirmButtonText: '确认保存', cancelButtonText: '取消', type: 'warning' }
)
submitting.value = true
dataOf(await requestWithRecentAuthentication(() => http.post('/api/v1/system/notification/authorization', input)))
clearSecret()
ElNotification({ title: '已保存', message: '账号通知授权已更新', type: 'success' })
await Promise.all([loadStatus(), loadAccounts()])
} catch (cause) {
showError(cause)
} finally { submitting.value = false }
}
const rotate = async () => {
if (!canManage.value || !status.value?.enabled || submitting.value || dirty.value) return
const target = status.value
try {
await ElMessageBox.confirm(`为「${target.account}」生成新凭据?旧通知凭据和旧连接将失效,普通邮箱凭据不变。`, '确认重置凭据', {
confirmButtonText: '生成新凭据', cancelButtonText: '取消', type: 'warning'
})
submitting.value = true
const data = dataOf(await requestWithRecentAuthentication(() => http.post('/api/v1/system/notification/credential', {
user_id: target.user_id, version: target.version
})))
if (disposed) return
clearSecret()
secret.value = data.code
secretAccount.value = target.account
seconds.value = 60
secretVisible.value = true
secretTimer = setInterval(() => {
seconds.value -= 1
if (seconds.value <= 0) clearSecret()
}, 1000)
await Promise.all([loadStatus(), loadAccounts()])
} catch (cause) { showError(cause) }
finally { submitting.value = false }
}
const copySecret = async () => {
if (!secret.value) return
try {
await navigator.clipboard.writeText(secret.value)
ElNotification({ title: '已复制', message: '请安全交付给账号使用者,不要放入日志或代码仓库', type: 'success' })
} catch { showError(new Error('复制失败,请手动复制授权码')) }
}
onMounted(() => { loadStatus(); loadAccounts() })
onUnmounted(() => { disposed = true; requestSequence += 1; accountsSequence += 1; clearSecret() })
return {
manager, loading, submitting, error, status, accounts, page, total, form,
secret, secretAccount, secretVisible, seconds, canManage, stateText, dirty,
loadAccounts, selectAccount, refresh, save, rotate, clearSecret, copySecret, beforeLeave
}
}
@@ -0,0 +1,123 @@
<template>
<section class="notification-settings" aria-label="系统通知发信">
<header class="notification-header">
<div>
<h2>系统通知发信</h2>
<p>按账号授权的 SMTP 发信服务,与普通邮箱登录和收发信独立。</p>
</div>
<el-button :disabled="submitting || loading" @click="refresh">刷新状态</el-button>
</header>
<el-alert
title="默认关闭,逐账号授权"
:description="manager ? '只有管理范围内的账号可以授权。管理员修改授权前需要身份确认;关闭时永久撤销旧凭据。' : '普通用户只能查看自己的状态和连接参数。需要开启或重置凭据时,请联系管理员。'"
type="info"
show-icon
:closable="false"
/>
<div v-if="manager" class="notification-panel">
<h3>账号授权管理</h3>
<el-table :data="accounts" stripe>
<el-table-column prop="account" label="账号" min-width="110" />
<el-table-column prop="name" label="名称" min-width="90" />
<el-table-column label="通知状态" min-width="140">
<template #default="{ row }">
<el-tag :type="row.enabled && row.configured ? 'success' : 'info'">
{{ row.disabled ? '账号已禁用' : !row.enabled ? '未开启' : row.configured ? '已开启' : '待生成凭据' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="操作" width="90">
<template #default="{ row }">
<el-button link type="primary" :disabled="submitting || loading" @click="selectAccount(row)">管理</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
v-model:current-page="page"
:page-size="20"
:total="total"
layout="prev, pager, next"
:disabled="submitting"
@current-change="loadAccounts"
/>
</div>
<el-alert v-if="error" :title="error" type="error" :closable="false" show-icon />
<div v-loading="loading" class="notification-panel" aria-live="polite">
<template v-if="status">
<div class="notification-heading">
<h3>账号:{{ status.account }}</h3>
<el-tag :type="status.enabled && status.configured ? 'success' : 'info'">{{ stateText }}</el-tag>
</div>
<p v-if="!status.account_available" class="notification-warning">账号已禁用或尚未修改临时密码,不能生成或使用通知凭据。</p>
<el-form v-if="canManage" label-position="top" :disabled="submitting || loading" @submit.prevent="save">
<el-form-item label="允许该账号通过 SMTP 发送系统通知">
<el-switch v-model="form.enabled" active-text="开启" inactive-text="关闭" />
</el-form-item>
<div class="notification-limits">
<el-form-item label="每分钟收件人限额">
<el-input-number v-model="form.minute_limit" :min="1" :max="100" :precision="0" />
</el-form-item>
<el-form-item label="每日收件人限额(UTC)">
<el-input-number v-model="form.day_limit" :min="1" :max="10000" :precision="0" />
</el-form-item>
</div>
<div class="notification-actions">
<el-button type="primary" :loading="submitting" :disabled="!dirty" native-type="submit">保存授权</el-button>
<el-button
:disabled="!status.enabled || !status.account_available || dirty || submitting"
@click="rotate"
>{{ status.configured ? '重置发信凭据' : '生成发信凭据' }}</el-button>
</div>
</el-form>
<el-descriptions title="使用状态" :column="1" border>
<el-descriptions-item label="本分钟用量">{{ status.minute_used }} / {{ status.minute_limit }} 个收件人</el-descriptions-item>
<el-descriptions-item label="今日用量(UTC)">{{ status.day_used }} / {{ status.day_limit }} 个收件人</el-descriptions-item>
<el-descriptions-item label="单封限制">最多 {{ status.max_recipients }} 个收件人,最大 {{ status.max_message_bytes / 1048576 }} MiB</el-descriptions-item>
</el-descriptions>
<p class="notification-hint">配额按提交的收件人数计算,失败和重试也计数;更换凭据或重启不会清空配额。</p>
<el-descriptions title="应用 SMTP 配置" :column="1" border>
<el-descriptions-item label="服务器">{{ status.host }}</el-descriptions-item>
<el-descriptions-item label="隐式 TLS 端口">{{ status.tls_port || '未启用' }}</el-descriptions-item>
<el-descriptions-item label="STARTTLS 端口">{{ status.starttls_port || '未启用' }}</el-descriptions-item>
<el-descriptions-item label="SMTP 用户名"><code>{{ status.username }}</code></el-descriptions-item>
<el-descriptions-item label="SMTP 密码">管理员生成的独立发信凭据,不是网页登录密码或普通客户端授权码</el-descriptions-item>
<el-descriptions-item label="发件邮箱"><code>{{ status.sender }}</code></el-descriptions-item>
<el-descriptions-item label="发件名称">{{ status.sender_name }}(服务端固定)</el-descriptions-item>
</el-descriptions>
<p class="notification-hint">必须启用加密和证书校验。服务器名需正确解析且由证书覆盖;此处显示配置,不代表公网连通性已验收。</p>
<el-alert title="仅发信,不提供收件箱" description="通知地址拒收邮件;通知凭据不能用于网页登录、IMAP 或 POP3。普通邮箱及其原有凭据不受影响。" type="warning" :closable="false" show-icon />
</template>
<el-empty v-else-if="!loading && !error" description="尚未读取账号状态" />
</div>
<el-dialog v-model="secretVisible" title="通知发信凭据(仅显示一次)" width="min(560px, 92vw)" destroy-on-close @closed="clearSecret">
<p>账号:{{ secretAccount }}。请安全保存并交付给该账号的使用者。</p>
<el-alert :title="`${seconds} 秒后自动隐藏;关闭后无法再次查看,只能重新生成。`" type="warning" :closable="false" show-icon />
<el-input :model-value="secret" readonly aria-label="新通知发信凭据" class="notification-secret" />
<template #footer>
<el-button @click="copySecret">复制凭据</el-button>
<el-button type="primary" @click="clearSecret">已保存,关闭</el-button>
</template>
</el-dialog>
</section>
</template>
<script setup>
import { usePage } from './SystemNotification.js'
const {
manager, loading, submitting, error, status, accounts, page, total, form,
secret, secretAccount, secretVisible, seconds, canManage, stateText, dirty,
loadAccounts, selectAccount, refresh, save, rotate, clearSecret, copySecret, beforeLeave
} = usePage()
defineExpose({ beforeLeave })
</script>
<style scoped src="./SystemNotification.css"></style>