Repository navigation
[KAN-66] Restrict staff routes and menus by system role #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hannng4416
wants to merge
8
commits into
main
Choose a base branch
from
codex/KAN-66-rbac
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9d00c64
[KAN-67] feat(rbac): define web permissions and route access matrix
Hieppham-1903 a0ee3e5
[KAN-69] feat(rbac): guard staff screens and authenticate API requests
Hieppham-1903 27b5000
[KAN-70] test(rbac): verify web permission matrix and safe landing pages
Hieppham-1903 5e0ef6d
[KAN-71] test(rbac): verify bearer transport and backend login contract
Hieppham-1903 b29ce95
[KAN-72] test(rbac): verify cross-role routes and handle server acces…
Hieppham-1903 e90e086
Merge branch 'main' into codex/KAN-66-rbac
huytran2005 e499fe6
[KAN-66] test(rbac): align protected routes and api mock with role-ba…
huytran2005 0f5a89d
[KAN-66] fix(rbac): address CodeRabbit review feedback on access cont…
huytran2005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| VITE_API_BASE_URL=http://localhost:8080/api | ||
| VITE_APP_TITLE=Cruise Activity and Service Management System | ||
| VITE_APP_VERSION=1.0.0 | ||
| VITE_API_BASE_URL=/api | ||
| VITE_APP_TITLE=Cruise Activity and Service Management System | ||
| VITE_APP_VERSION=1.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| VITE_API_BASE_URL=http://localhost:8080/api | ||
| VITE_APP_TITLE=Cruise Activity and Service Management System | ||
| VITE_APP_VERSION=1.0.0 | ||
| VITE_API_BASE_URL=/api | ||
| VITE_APP_TITLE=Cruise Activity and Service Management System | ||
| VITE_APP_VERSION=1.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # KAN-66 — Phân quyền giao diện | ||
|
|
||
| Phần web đồng bộ 8 role và permission với `Cruise-api` trong | ||
| `src/services/accessControl.ts`. Route guard, sidebar và command palette dùng | ||
| chung ma trận này. Backend vẫn là nơi quyết định quyền đối với từng API. | ||
|
|
||
| - Người dùng không đủ quyền vào URL trực tiếp được chuyển tới trang 403. | ||
| - Nhân viên POS vào `/` được chuyển tới `/pos`; các role khác có trang làm việc | ||
| tương ứng, tránh điều hướng lặp ở dashboard vận hành. | ||
| - Các thao tác đăng ký, đổi trạng thái và check-in hành khách bị khóa đối với | ||
| nhân viên chỉ có quyền đọc danh sách. | ||
| - Login gửi `usernameOrEmail`, lấy `accessToken`; request interceptor gửi Bearer | ||
| token. Response 401 xóa phiên và về login, 403 chuyển sang trang báo thiếu quyền, | ||
| kể cả khi service nghiệp vụ có fallback dữ liệu demo. | ||
| - `/api` là mặc định. Vite proxy tới đúng microservice ở localhost:8081–8084; | ||
| system health/info tới 8080. Production cần reverse proxy tương ứng hoặc cấu | ||
| hình `VITE_API_BASE_URL`. Gateway hiện tại chỉ có các endpoint giám sát hệ thống. | ||
| - Tài khoản demo chỉ bật ở development khi có | ||
| `VITE_ENABLE_DEMO_ACCOUNTS=true` hoặc trong test. Token demo không được API chấp nhận. | ||
|
|
||
| Kiểm tra local ngày 02/11/2026: 736 Vitest test (107 file) pass, bộ test script | ||
| Jules pass, lint exit 0 (còn warning của repo), TypeScript và production build pass. | ||
| Lệnh chạy: `npm ci`, `npx vitest run --maxWorkers=2`, `npm run test:jules`, | ||
| `npm run lint`, `npm run build`. | ||
|
|
||
| Chi tiết quyền API, bảng audit và giới hạn revocation nằm trong | ||
| `Cruise-api/docs/KAN-66-RBAC.md`. Chưa nghiệm thu staging hoặc chạy GitHub CI. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { beforeEach, describe, expect, it } from 'vitest'; | ||
| import { act, render, screen } from '@testing-library/react'; | ||
| import { MemoryRouter, Route, Routes } from 'react-router-dom'; | ||
| import { AdminAuthGuard } from './AdminAuthGuard'; | ||
| import { RouteAccessGuard } from './RouteAccessGuard'; | ||
| import { authService, type StaffRole } from '../../services/authService'; | ||
| import { ACCESS_DENIED_EVENT } from '../../services/api'; | ||
|
|
||
| function open(path: string, role?: StaffRole) { | ||
| if (role) authService.setSession('token', { id: 1, username: 'actor', fullName: 'Actor', role }); | ||
| render(<MemoryRouter initialEntries={[path]}><Routes> | ||
| <Route path="/login" element={<div>Login required</div>} /> | ||
| <Route element={<AdminAuthGuard />}><Route element={<RouteAccessGuard />}> | ||
| <Route path="/" element={<div>Operations dashboard</div>} /> | ||
| <Route path="/pos" element={<div>Cashier workspace</div>} /> | ||
| <Route path="/users" element={<div>Identity administration</div>} /> | ||
| <Route path="/excursions/:id/roster" element={<div>Shore roster</div>} /> | ||
| <Route path="/activities/:id/attendance" element={<div>Activity check-in</div>} /> | ||
| <Route path="/forbidden" element={<div>Forbidden</div>} /> | ||
| </Route></Route> | ||
| </Routes></MemoryRouter>); | ||
| } | ||
|
|
||
| describe('KAN-72 direct route authorization', () => { | ||
| beforeEach(() => { localStorage.clear(); sessionStorage.clear(); }); | ||
| it.each(['POS_STAFF', 'OPERATIONS_MANAGER', 'ACTIVITY_MANAGER', 'SHORE_EXCURSION_MANAGER', | ||
| 'ITINERARY_COORDINATOR', 'FINANCE_FRONT_DESK'] as StaffRole[])( | ||
| 'blocks %s from opening user administration by URL', role => { | ||
| open('/users', role); | ||
| expect(screen.getByText('Forbidden')).toBeInTheDocument(); | ||
| expect(screen.queryByText('Identity administration')).not.toBeInTheDocument(); | ||
| }, | ||
| ); | ||
| it('allows administrator user management', () => { | ||
| open('/users', 'SYSTEM_ADMIN'); | ||
| expect(screen.getByText('Identity administration')).toBeInTheDocument(); | ||
| }); | ||
| it('sends a cashier to the authorized landing page instead of an operations dashboard', () => { | ||
| open('/', 'POS_STAFF'); | ||
| expect(screen.getByText('Cashier workspace')).toBeInTheDocument(); | ||
| expect(screen.queryByText('Operations dashboard')).not.toBeInTheDocument(); | ||
| }); | ||
| it.each([ | ||
| ['/excursions/7/roster', 'SHORE_EXCURSION_MANAGER', 'Shore roster'], | ||
| ['/activities/7/attendance', 'ACTIVITY_MANAGER', 'Activity check-in'], | ||
| ] as const)('permits the owning role at %s', (path, role, content) => { | ||
| open(path, role); | ||
| expect(screen.getByText(content)).toBeInTheDocument(); | ||
| }); | ||
| it.each([ | ||
| ['/excursions/7/roster', 'ACTIVITY_MANAGER'], ['/activities/7/attendance', 'SHORE_EXCURSION_MANAGER'], | ||
| ] as const)('rejects cross-role deep link %s for %s', (path, role) => { | ||
| open(path, role); | ||
| expect(screen.getByText('Forbidden')).toBeInTheDocument(); | ||
| }); | ||
| it('requires login for an anonymous deep link', () => { | ||
| open('/users'); | ||
| expect(screen.getByText('Login required')).toBeInTheDocument(); | ||
| }); | ||
| it('does not accept a forged unknown staff role in local storage', () => { | ||
| open('/users', 'ADMIN' as StaffRole); | ||
| expect(screen.getByText('Login required')).toBeInTheDocument(); | ||
| }); | ||
| it('leaves an allowed page when the backend denies a mutation', () => { | ||
| open('/pos', 'POS_STAFF'); | ||
| act(() => window.dispatchEvent(new CustomEvent(ACCESS_DENIED_EVENT, { detail: { status: 403 } }))); | ||
| expect(screen.getByText('Forbidden')).toBeInTheDocument(); | ||
| expect(screen.queryByText('Cashier workspace')).not.toBeInTheDocument(); | ||
| }); | ||
| it('clears an expired session and returns to login on a backend 401', () => { | ||
| open('/pos', 'POS_STAFF'); | ||
| act(() => window.dispatchEvent(new CustomEvent(ACCESS_DENIED_EVENT, { detail: { status: 401 } }))); | ||
| expect(screen.getByText('Login required')).toBeInTheDocument(); | ||
| expect(authService.isAuthenticated()).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { Navigate, Outlet, useLocation } from 'react-router-dom'; | ||
| import { authService } from '../../services/authService'; | ||
| import { canAccessPath, homeForRole } from '../../services/accessControl'; | ||
|
|
||
| export function RouteAccessGuard() { | ||
| const { pathname } = useLocation(); | ||
| const role = authService.getCurrentUser()?.role; | ||
| if (canAccessPath(role, pathname)) return <Outlet />; | ||
| return <Navigate to={pathname === '/' ? homeForRole(role) : '/forbidden'} replace />; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Link } from 'react-router-dom'; | ||
| import { authService } from '../services/authService'; | ||
| import { homeForRole } from '../services/accessControl'; | ||
|
|
||
| export function ForbiddenPage() { | ||
| return ( | ||
| <section className="mx-auto max-w-xl p-6" aria-labelledby="forbidden-title"> | ||
| <p className="text-sm font-mono">403</p> | ||
| <h1 id="forbidden-title" className="text-2xl font-semibold mt-2">Bạn không có quyền truy cập</h1> | ||
| <p className="mt-3">Vai trò của bạn không được phép mở chức năng này. Liên hệ quản trị viên nếu cần thay đổi quyền.</p> | ||
| <Link className="inline-block mt-6 underline" to={homeForRole(authService.getCurrentUser()?.role)}> | ||
| Về trang làm việc | ||
| </Link> | ||
| </section> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.