feat: 退出时选择离开系统或删除账号

This commit is contained in:
Nixevol
2026-11-18 10:16:29 +08:00
parent bb23521e9b
commit 3670a76dfe
4 changed files with 88 additions and 40 deletions
+7 -6
View File
@@ -10,7 +10,7 @@ import time
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from dataclasses import dataclass from dataclasses import dataclass
from sqlalchemy import select from sqlalchemy import delete, select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from euniv.client import ( from euniv.client import (
@@ -580,14 +580,15 @@ def _live(db: Session, employee_id: str, client_factory, read):
def revoke_credential(db: Session, employee_id: str) -> None: def revoke_credential(db: Session, employee_id: str) -> None:
"""从本平台删除账号,并停掉定时任务和已经开始的学习。"""
from euniv.models import FinishedCourse
with _credential_lock(employee_id): with _credential_lock(employee_id):
account = db.get(Account, employee_id) account = db.get(Account, employee_id)
if account is None: if account is not None:
return
db.refresh(account) db.refresh(account)
account.credential_cipher = "" db.execute(delete(FinishedCourse).where(FinishedCourse.employee_id == employee_id))
account.password_cipher = "" db.delete(account)
account.sessions_valid_after = datetime.now(timezone.utc)
db.commit() db.commit()
from euniv.autostudy import stop_account from euniv.autostudy import stop_account
from euniv.study_push import close_account from euniv.study_push import close_account
+23 -7
View File
@@ -352,16 +352,32 @@ def test_browser_session_logout_and_revoke(monkeypatch) -> None:
account = db.get(Account, "6650000146") account = db.get(Account, "6650000146")
assert account is not None assert account is not None
assert account.credential_cipher assert account.credential_cipher
again = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}) account.study_schedule_enabled = True
assert again.status_code == 200 db.commit()
assert calls["login"] == 1
assert client.post("/api/revoke").status_code == 200
assert client.get("/api/session").status_code == 401
with SessionLocal() as db: with SessionLocal() as db:
account = db.get(Account, "6650000146") account = db.get(Account, "6650000146")
assert account is not None assert account is not None
assert account.credential_cipher == "" assert account.study_schedule_enabled is True
assert account.password_cipher == "" again = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"})
assert again.status_code == 200
assert calls["login"] == 1
from euniv.autostudy import _wanted
from euniv.models import FinishedCourse
_wanted["6650000146"] = object()
with SessionLocal() as db:
db.add(FinishedCourse(employee_id="6650000146", knowledge_id="course-1"))
db.commit()
assert client.post("/api/revoke").status_code == 200
assert client.get("/api/session").status_code == 401
assert "6650000146" not in _wanted
with SessionLocal() as db:
from sqlalchemy import select
account = db.get(Account, "6650000146")
assert account is None
left = db.scalars(select(FinishedCourse).where(FinishedCourse.employee_id == "6650000146")).all()
assert left == []
def test_logout_keeps_the_other_browser_until_revoke(monkeypatch) -> None: def test_logout_keeps_the_other_browser_until_revoke(monkeypatch) -> None:
+40 -22
View File
@@ -1,6 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { onMounted, onUnmounted, ref } from "vue"; import { onMounted, onUnmounted, ref } from "vue";
import { NButton, NDropdown, NIcon, NLayout, NLayoutSider, NMenu, NTooltip, useDialog } from "naive-ui"; import { NButton, NIcon, NLayout, NLayoutSider, NMenu, NModal, NRadio, NRadioGroup, NTooltip } from "naive-ui";
import { LogOutOutline, MoonOutline, PersonOutline, SunnyOutline } from "@vicons/ionicons5"; import { LogOutOutline, MoonOutline, PersonOutline, SunnyOutline } from "@vicons/ionicons5";
import { isDark, toggleTheme } from "../shared/theme"; import { isDark, toggleTheme } from "../shared/theme";
import { navOptions, type PageKey } from "./navigation"; import { navOptions, type PageKey } from "./navigation";
@@ -11,10 +11,11 @@ import TrainingsView from "../features/trainings/TrainingsView.vue";
const props = defineProps<{ employeeId: string }>(); const props = defineProps<{ employeeId: string }>();
const emit = defineEmits<{ logout: []; revoke: [] }>(); const emit = defineEmits<{ logout: []; revoke: [] }>();
const dialog = useDialog();
const page = ref<PageKey>(pageFromHash()); const page = ref<PageKey>(pageFromHash());
const collapsed = ref(window.innerWidth < 800); const collapsed = ref(window.innerWidth < 800);
const holdOpen = ref(false); const holdOpen = ref(false);
const leaveOpen = ref(false);
const leaveChoice = ref<"logout" | "revoke" | null>(null);
const pages = new Set<PageKey>(["home", "history", "exams", "trainings"]); const pages = new Set<PageKey>(["home", "history", "exams", "trainings"]);
function hashName() { function hashName() {
@@ -73,28 +74,21 @@ onUnmounted(() => {
window.removeEventListener("hashchange", onHashChange); window.removeEventListener("hashchange", onHashChange);
}); });
const logoutOptions = [
{ label: "退出当前系统", key: "logout" },
{ label: "注销当前账号", key: "revoke" },
];
function onMenu(key: string) { function onMenu(key: string) {
if (key === "home" || key === "history" || key === "exams" || key === "trainings") show(key); if (key === "home" || key === "history" || key === "exams" || key === "trainings") show(key);
} }
function onLogout(key: string) { function openLeave() {
if (key !== "revoke") { leaveChoice.value = null;
emit("logout"); leaveOpen.value = true;
return;
} }
dialog.warning({
title: "注销当前账号", function confirmLeave() {
content: "注销后所有设备都会退出,正在进行的学习会停止,已保存的密码和登录凭证也会清除。", const choice = leaveChoice.value;
positiveText: "注销", if (!choice) return;
negativeText: "取消", leaveOpen.value = false;
positiveButtonProps: { type: "error" }, if (choice === "revoke") emit("revoke");
onPositiveClick: () => emit("revoke"), else emit("logout");
});
} }
</script> </script>
@@ -139,11 +133,9 @@ function onLogout(key: string) {
</template> </template>
{{ isDark ? "切换到日间" : "切换到夜间" }} {{ isDark ? "切换到日间" : "切换到夜间" }}
</n-tooltip> </n-tooltip>
<n-dropdown trigger="click" placement="top-end" :options="logoutOptions" @select="onLogout"> <n-button class="logout-btn" quaternary circle size="small" aria-label="退出" @click="openLeave">
<n-button class="logout-btn" quaternary circle size="small" aria-label="退出">
<template #icon><n-icon><LogOutOutline /></n-icon></template> <template #icon><n-icon><LogOutOutline /></n-icon></template>
</n-button> </n-button>
</n-dropdown>
</div> </div>
</div> </div>
</n-layout-sider> </n-layout-sider>
@@ -153,5 +145,31 @@ function onLogout(key: string) {
<ExamsView v-else-if="page === 'exams'" /> <ExamsView v-else-if="page === 'exams'" />
<TrainingsView v-else /> <TrainingsView v-else />
</n-layout> </n-layout>
<n-modal
v-model:show="leaveOpen"
preset="card"
title="退出"
:style="{ width: 'min(440px, calc(100vw - 32px))' }"
:mask-closable="false"
>
<n-radio-group v-model:value="leaveChoice" class="leave-choices">
<n-radio value="logout">退出系统</n-radio>
<n-radio value="revoke">注销账号</n-radio>
</n-radio-group>
<p v-if="leaveChoice === 'logout'" class="leave-note">定时计划和已开始的任务不会受影响。</p>
<p v-else-if="leaveChoice === 'revoke'" class="leave-note">会从平台删除这个账号,定时任务和已开始的任务会一并删除。</p>
<template #footer>
<div class="modal-actions">
<n-button @click="leaveOpen = false">取消</n-button>
<n-button
:type="leaveChoice === 'revoke' ? 'error' : 'primary'"
:disabled="leaveChoice === null"
@click="confirmLeave"
>
{{ leaveChoice === "revoke" ? "注销账号" : "退出系统" }}
</n-button>
</div>
</template>
</n-modal>
</n-layout> </n-layout>
</template> </template>
+13
View File
@@ -1644,6 +1644,19 @@ select {
gap: 8px; gap: 8px;
} }
.leave-choices {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 12px;
}
.leave-note {
margin: 16px 0 0;
color: var(--ink);
line-height: 1.6;
}
.app-root { .app-root {
position: relative; position: relative;
} }