diff --git a/client/src/components/TeacherView/TeacherAccess.jsx b/client/src/components/TeacherView/TeacherAccess.jsx index 23ddd4f5..1a8f2fc1 100644 --- a/client/src/components/TeacherView/TeacherAccess.jsx +++ b/client/src/components/TeacherView/TeacherAccess.jsx @@ -1,3 +1,4 @@ +import { useEffect } from 'react'; import { useDataProvider, SimpleForm, TextInput, useNotify, Toolbar, SaveButton, useResetStore } from 'react-admin'; import { useObjectStore } from 'src/utils/storeUtil'; import Button from '@mui/material/Button'; @@ -10,18 +11,67 @@ import LockOutlinedIcon from '@mui/icons-material/LockOutlined'; import LogoutIcon from '@mui/icons-material/Logout'; import Paper from '@mui/material/Paper'; +// Teacher login lives only in this tab (sessionStorage) and expires after idle time, +// so the next teacher on a shared computer must enter her ID again. +export const TEACHER_IDLE_TIMEOUT_MS = 15 * 60 * 1000; +const LAST_ACTIVE_KEY = 'teacherLastActive'; +const ACTIVITY_EVENTS = ['mousedown', 'keydown', 'touchstart', 'scroll']; + +const getLastActive = () => { + try { + return Number(sessionStorage.getItem(LAST_ACTIVE_KEY)) || 0; + } catch { + return 0; + } +}; +const touchLastActive = () => { + try { + sessionStorage.setItem(LAST_ACTIVE_KEY, String(Date.now())); + } catch {} +}; +const clearLastActive = () => { + try { + sessionStorage.removeItem(LAST_ACTIVE_KEY); + } catch {} +}; +const isSessionActive = () => Date.now() - getLastActive() < TEACHER_IDLE_TIMEOUT_MS; + export const TeacherGuard = ({ children }) => { const dataProvider = useDataProvider(); const notify = useNotify(); const reset = useResetStore(); const { value: teacher, set: setTeacher, clear } = useObjectStore('teacher'); + const sessionActive = !!teacher && isSessionActive(); const handleLogout = () => { + clearLastActive(); clear(); reset(); }; - if (teacher) { + useEffect(() => { + if (teacher && !sessionActive) { + handleLogout(); + } + }, [teacher, sessionActive]); + + useEffect(() => { + if (!sessionActive) { + return; + } + ACTIVITY_EVENTS.forEach((event) => window.addEventListener(event, touchLastActive, { passive: true })); + const interval = setInterval(() => { + if (!isSessionActive()) { + handleLogout(); + } + }, 30 * 1000); + return () => { + ACTIVITY_EVENTS.forEach((event) => window.removeEventListener(event, touchLastActive)); + clearInterval(interval); + }; + }, [sessionActive]); + + if (sessionActive) { return ( { const teacher = result.data[0] || null; if (teacher) { + touchLastActive(); setTeacher(teacher); } else { notify('לא נמצאה מורה עם ת.ז. זו. יש לבדוק שהמספר נכון (כולל אפס בתחילתו), ואם הבעיה נמשכת לפנות להנהלת המוסד.', { type: 'warning' }); diff --git a/client/src/components/TeacherView/TeacherAccess.test.jsx b/client/src/components/TeacherView/TeacherAccess.test.jsx new file mode 100644 index 00000000..843c3d04 --- /dev/null +++ b/client/src/components/TeacherView/TeacherAccess.test.jsx @@ -0,0 +1,65 @@ +import { render, screen, act } from '@testing-library/react'; +import { AdminContext, memoryStore } from 'react-admin'; +import { TeacherGuard, TEACHER_IDLE_TIMEOUT_MS } from './TeacherAccess'; + +const teacher = { id: 7, name: 'כהן', tz: '123456789' }; + +const renderGuard = (store) => + render( + + +
תוכן מורה
+
+
, + ); + +beforeEach(() => { + sessionStorage.clear(); + jest.useFakeTimers(); +}); + +afterEach(() => { + jest.useRealTimers(); +}); + +test('shows login when teacher stored but tab session is new (tab was closed)', () => { + const store = memoryStore({ teacher }); + renderGuard(store); + expect(screen.queryByText('תוכן מורה')).toBeNull(); + expect(screen.getByText('כניסת מורה')).toBeTruthy(); + expect(store.getItem('teacher')).toBeFalsy(); +}); + +test('shows content when teacher stored and tab session is active', () => { + sessionStorage.setItem('teacherLastActive', String(Date.now())); + renderGuard(memoryStore({ teacher })); + expect(screen.getByText('תוכן מורה')).toBeTruthy(); +}); + +test('logs out after idle timeout', () => { + sessionStorage.setItem('teacherLastActive', String(Date.now())); + const store = memoryStore({ teacher }); + renderGuard(store); + expect(screen.getByText('תוכן מורה')).toBeTruthy(); + + act(() => { + jest.advanceTimersByTime(TEACHER_IDLE_TIMEOUT_MS + 60 * 1000); + }); + + expect(screen.queryByText('תוכן מורה')).toBeNull(); + expect(store.getItem('teacher')).toBeFalsy(); +}); + +test('activity keeps teacher logged in', () => { + sessionStorage.setItem('teacherLastActive', String(Date.now())); + renderGuard(memoryStore({ teacher })); + + for (let i = 0; i < 4; i++) { + act(() => { + jest.advanceTimersByTime(TEACHER_IDLE_TIMEOUT_MS / 2); + window.dispatchEvent(new Event('keydown')); + }); + } + + expect(screen.getByText('תוכן מורה')).toBeTruthy(); +}); diff --git a/client/src/roadmapFeatures.js b/client/src/roadmapFeatures.js index 48da429a..fe25b089 100644 --- a/client/src/roadmapFeatures.js +++ b/client/src/roadmapFeatures.js @@ -49,4 +49,5 @@ export default [ { html: 'דף חדש "אפשרויות שדרוג" בהסברים נוספים: כל היכולות שאפשר להפעיל בהרשאה, עם הסבר ומחיר', status: 'בוצע', statusColor: 'success' }, { html: 'התקנה כאפליקציה: אפשר להתקין את המערכת מהדפדפן (בכרום: סמל ההתקנה בשורת הכתובת) ולפתוח אותה בחלון נפרד, עם לוגו משלה בשולחן העבודה ובשורת המשימות', status: 'בוצע', statusColor: 'success' }, { html: 'כללי ניקוי נוכחות: הצגת ההרצה האחרונה (מתי, כמה נמחקו ומה דולג), כפתור "מי תימחק?" לתצוגה מקדימה בלי מחיקה, וסיכום מיד אחרי "הרץ עכשיו"', status: 'בוצע', statusColor: 'success' }, + { html: 'כניסת מורה: סגירת הלשונית או 15 דקות בלי פעילות מנתקות את המורה, כך שבמחשב משותף כל מורה מזינה ת.ז. מחדש', status: 'בוצע', statusColor: 'success' }, ].reverse();