dvc890 commited on
Commit
3339f8f
·
verified ·
1 Parent(s): b4dc55a

Create components/SensitiveContentProtection.tsx

Browse files
components/SensitiveContentProtection.tsx ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState } from 'react';
2
+ import { useSecurity } from '../contexts/SecurityContext';
3
+ import { api } from '../services/api';
4
+ import { Lock, Loader2, Eye, EyeOff } from 'lucide-react';
5
+
6
+ interface Props {
7
+ children: React.ReactNode;
8
+ enabled?: boolean; // Whether protection is enabled for this user role
9
+ }
10
+
11
+ export const SensitiveContentProtection: React.FC<Props> = ({ children, enabled = true }) => {
12
+ const { isSensitiveUnlocked, unlockSensitiveContent } = useSecurity();
13
+ const [password, setPassword] = useState('');
14
+ const [loading, setLoading] = useState(false);
15
+ const [error, setError] = useState('');
16
+ const [showPassword, setShowPassword] = useState(false);
17
+
18
+ // If protection is disabled (e.g. for Students/Admins if configured), just show content
19
+ if (!enabled || isSensitiveUnlocked) {
20
+ return <>{children}</>;
21
+ }
22
+
23
+ const handleSubmit = async (e: React.FormEvent) => {
24
+ e.preventDefault();
25
+ if (!password) return;
26
+
27
+ setLoading(true);
28
+ setError('');
29
+
30
+ try {
31
+ const isValid = await api.auth.verifyPassword(password);
32
+ if (isValid) {
33
+ unlockSensitiveContent();
34
+ } else {
35
+ setError('密码错误,请重试');
36
+ }
37
+ } catch (err) {
38
+ setError('验证失败,请稍后重试');
39
+ } finally {
40
+ setLoading(false);
41
+ }
42
+ };
43
+
44
+ return (
45
+ <div className="relative w-full h-full min-h-[calc(100vh-100px)]">
46
+ {/* Blurred Content Background */}
47
+ {/* We render children to create the visual "content behind blur" effect.
48
+ Pointer events are disabled to prevent interaction.
49
+ A heavy blur and opacity layer is applied. */}
50
+ <div
51
+ className="absolute inset-0 filter blur-lg opacity-40 pointer-events-none select-none overflow-hidden bg-white"
52
+ aria-hidden="true"
53
+ >
54
+ {children}
55
+ </div>
56
+
57
+ {/* Overlay */}
58
+ <div className="absolute inset-0 z-50 flex items-center justify-center bg-white/40 backdrop-blur-md">
59
+ <div className="bg-white p-8 rounded-2xl shadow-2xl w-full max-w-md border border-gray-100 mx-4">
60
+ <div className="flex flex-col items-center mb-6">
61
+ <div className="p-4 bg-blue-50 rounded-full mb-4 ring-4 ring-blue-50/50">
62
+ <Lock className="w-8 h-8 text-blue-600" />
63
+ </div>
64
+ <h2 className="text-xl font-bold text-gray-900">敏感内容保护</h2>
65
+ <p className="text-gray-500 text-sm mt-2 text-center leading-relaxed">
66
+ 为了保护学生隐私与数据安全<br/>
67
+ 查看 <span className="font-medium text-gray-700">成绩管理</span> 与 <span className="font-medium text-gray-700">报表统计</span> 需验证身份
68
+ </p>
69
+ </div>
70
+
71
+ <form onSubmit={handleSubmit} className="space-y-5">
72
+ <div className="space-y-1">
73
+ <label className="text-xs font-medium text-gray-500 ml-1">登录密码</label>
74
+ <div className="relative">
75
+ <input
76
+ type={showPassword ? "text" : "password"}
77
+ value={password}
78
+ onChange={(e) => setPassword(e.target.value)}
79
+ className="w-full px-4 py-3 border border-gray-200 rounded-xl focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 outline-none transition-all bg-gray-50 focus:bg-white text-gray-800"
80
+ placeholder="请输入当前账号密码"
81
+ autoFocus
82
+ />
83
+ <button
84
+ type="button"
85
+ onClick={() => setShowPassword(!showPassword)}
86
+ className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600 p-1 rounded-md hover:bg-gray-100 transition-colors"
87
+ >
88
+ {showPassword ? <EyeOff size={18} /> : <Eye size={18} />}
89
+ </button>
90
+ </div>
91
+ </div>
92
+
93
+ {error && (
94
+ <div className="text-red-500 text-sm bg-red-50 p-3 rounded-lg flex items-center justify-center font-medium animate-in fade-in slide-in-from-top-1">
95
+ {error}
96
+ </div>
97
+ )}
98
+
99
+ <button
100
+ type="submit"
101
+ disabled={loading || !password}
102
+ className="w-full py-3.5 bg-blue-600 hover:bg-blue-700 text-white rounded-xl font-medium transition-all shadow-lg shadow-blue-600/20 hover:shadow-blue-600/30 flex items-center justify-center disabled:opacity-50 disabled:cursor-not-allowed disabled:shadow-none active:scale-[0.98]"
103
+ >
104
+ {loading ? (
105
+ <>
106
+ <Loader2 className="w-5 h-5 animate-spin mr-2" />
107
+ 验证中...
108
+ </>
109
+ ) : (
110
+ '验证并解锁'
111
+ )}
112
+ </button>
113
+ </form>
114
+ </div>
115
+ </div>
116
+ </div>
117
+ );
118
+ };