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
+9 -8
View File
@@ -10,7 +10,7 @@ import time
from datetime import datetime, timedelta, timezone
from dataclasses import dataclass
from sqlalchemy import select
from sqlalchemy import delete, select
from sqlalchemy.orm import Session
from euniv.client import (
@@ -580,15 +580,16 @@ def _live(db: Session, employee_id: str, client_factory, read):
def revoke_credential(db: Session, employee_id: str) -> None:
"""从本平台删除账号,并停掉定时任务和已经开始的学习。"""
from euniv.models import FinishedCourse
with _credential_lock(employee_id):
account = db.get(Account, employee_id)
if account is None:
return
db.refresh(account)
account.credential_cipher = ""
account.password_cipher = ""
account.sessions_valid_after = datetime.now(timezone.utc)
db.commit()
if account is not None:
db.refresh(account)
db.execute(delete(FinishedCourse).where(FinishedCourse.employee_id == employee_id))
db.delete(account)
db.commit()
from euniv.autostudy import stop_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")
assert account is not None
assert account.credential_cipher
again = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"})
assert again.status_code == 200
assert calls["login"] == 1
assert client.post("/api/revoke").status_code == 200
assert client.get("/api/session").status_code == 401
account.study_schedule_enabled = True
db.commit()
with SessionLocal() as db:
account = db.get(Account, "6650000146")
assert account is not None
assert account.credential_cipher == ""
assert account.password_cipher == ""
assert account.study_schedule_enabled is True
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:
+43 -25
View File
@@ -1,6 +1,6 @@
<script setup lang="ts">
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 { isDark, toggleTheme } from "../shared/theme";
import { navOptions, type PageKey } from "./navigation";
@@ -11,10 +11,11 @@ import TrainingsView from "../features/trainings/TrainingsView.vue";
const props = defineProps<{ employeeId: string }>();
const emit = defineEmits<{ logout: []; revoke: [] }>();
const dialog = useDialog();
const page = ref<PageKey>(pageFromHash());
const collapsed = ref(window.innerWidth < 800);
const holdOpen = ref(false);
const leaveOpen = ref(false);
const leaveChoice = ref<"logout" | "revoke" | null>(null);
const pages = new Set<PageKey>(["home", "history", "exams", "trainings"]);
function hashName() {
@@ -73,28 +74,21 @@ onUnmounted(() => {
window.removeEventListener("hashchange", onHashChange);
});
const logoutOptions = [
{ label: "退出当前系统", key: "logout" },
{ label: "注销当前账号", key: "revoke" },
];
function onMenu(key: string) {
if (key === "home" || key === "history" || key === "exams" || key === "trainings") show(key);
}
function onLogout(key: string) {
if (key !== "revoke") {
emit("logout");
return;
}
dialog.warning({
title: "注销当前账号",
content: "注销后所有设备都会退出,正在进行的学习会停止,已保存的密码和登录凭证也会清除。",
positiveText: "注销",
negativeText: "取消",
positiveButtonProps: { type: "error" },
onPositiveClick: () => emit("revoke"),
});
function openLeave() {
leaveChoice.value = null;
leaveOpen.value = true;
}
function confirmLeave() {
const choice = leaveChoice.value;
if (!choice) return;
leaveOpen.value = false;
if (choice === "revoke") emit("revoke");
else emit("logout");
}
</script>
@@ -139,11 +133,9 @@ function onLogout(key: string) {
</template>
{{ isDark ? "切换到日间" : "切换到夜间" }}
</n-tooltip>
<n-dropdown trigger="click" placement="top-end" :options="logoutOptions" @select="onLogout">
<n-button class="logout-btn" quaternary circle size="small" aria-label="退出">
<template #icon><n-icon><LogOutOutline /></n-icon></template>
</n-button>
</n-dropdown>
<n-button class="logout-btn" quaternary circle size="small" aria-label="退出" @click="openLeave">
<template #icon><n-icon><LogOutOutline /></n-icon></template>
</n-button>
</div>
</div>
</n-layout-sider>
@@ -153,5 +145,31 @@ function onLogout(key: string) {
<ExamsView v-else-if="page === 'exams'" />
<TrainingsView v-else />
</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>
</template>
+13
View File
@@ -1644,6 +1644,19 @@ select {
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 {
position: relative;
}