feat(secretary): add services permission resource + panel gating (phase A)

Secretaries could reach neither the services module (EntityContextResolver
does not recognise a secretary as clinic owner, so they resolved to
`unknown` → 403) nor had any toggle to grant it. Add `services` as a
first-class secretary permission resource, enforced end-to-end.

Backend
- DoctorSecretary::DEFAULT_PERMISSIONS: new `services` resource (default-deny).
- SecretaryAccessChecker::resolveOwnerEntity(): reusable owner (clinic/doctor)
  resolution from the secretary's active context, for controllers whose data
  is fetched by [entityType, entityId] and whose generic resolver is not
  secretary-aware.
- ClinicServiceController: resolveEntity() is now secretary-aware; every action
  (sections, items, tariffs — 13 total) guards with `services` view/create/
  update/delete via denyUnlessGranted, ahead of the subscription gate.

Frontend
- SecretaryPermissions type + MySecretariesPage + SecretariesPage: `services`
  section so owners can grant it.
- Sidebar (secretary branch): services / inventory / tags menu items gated by
  can(resource, 'view').
- RoleRoute: a secretary now needs the page's `permission` to open it (direct
  URL entry included); clinic-services, inventory, tags-settings routes accept
  secretary + permission gate.

Tests
- SecretaryResourceEnforcementTest: services denied-by-default, allowed-when-
  granted, create-denied-while-view-granted.
- Sidebar.test: secretary menu gating for services/inventory/tags.

Docs: secretary.md + clinic-services.md updated with the `services` resource
and the resolveOwnerEntity note.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-23 17:10:54 +03:30
co-authored by Claude Opus 4.8
parent 8a69ed8d91
commit 9d46577181
13 changed files with 431 additions and 6 deletions
+13 -5
View File
@@ -121,7 +121,11 @@ function PublicRoute({ children }: { children: React.ReactNode }) {
function RoleRoute({ roles, blockClinicScope, permission, children }: {
roles: string[];
blockClinicScope?: boolean;
/** [resource, action] — پزشکِ مهمان با داشتن این مجوز از blockClinicScope مستثنا می‌شود. */
/**
* [resource, action] — گیت مجوز صفحه.
* • پزشکِ مهمان در محیط کلینیک با داشتن این مجوز از blockClinicScope مستثنا می‌شود.
* • منشی فقط با داشتن این مجوز به صفحه دسترسی دارد (هم‌راستا با enforcement سمت API).
*/
permission?: [string, string];
children: React.ReactNode;
}) {
@@ -130,6 +134,10 @@ function RoleRoute({ roles, blockClinicScope, permission, children }: {
const { can } = usePermissions();
if (!primaryRole) return <div style={{ padding: 40, textAlign: 'center' }}>در حال بارگذاری...</div>;
if (!roles.includes(primaryRole)) return <Navigate to="/admin/dashboard" replace />;
// منشیِ بدون مجوزِ این صفحه نباید با ورود مستقیم URL هم بازش کند.
if (primaryRole === 'secretary' && permission && !can(permission[0], permission[1])) {
return <Navigate to="/admin/dashboard" replace />;
}
// پزشکِ مهمان در محیط کلینیک فقط تا جایی که کلینیک مجوز داده دسترسی دارد.
if (blockClinicScope && primaryRole === 'doctor' && context?.scope === 'clinic') {
if (!permission || !can(permission[0], permission[1])) {
@@ -243,14 +251,14 @@ export default function App() {
<Route path="staff" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><StaffPage /></RoleRoute>} />
<Route path="settings-menu" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><SettingsMenuPage /></RoleRoute>} />
<Route path="account-settings" element={<RoleRoute roles={['doctor', 'clinic', 'secretary']}><AccountSettingsPage /></RoleRoute>} />
<Route path="tags-settings" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><TagsSettingsPage /></RoleRoute>} />
<Route path="tags-settings" element={<RoleRoute roles={['doctor', 'clinic', 'secretary']} blockClinicScope permission={['tags', 'view']}><TagsSettingsPage /></RoleRoute>} />
<Route path="appointment-settings" element={<RoleRoute roles={['doctor']} blockClinicScope><AppointmentSettingsPage /></RoleRoute>} />
<Route path="subscription" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><SubscriptionPage /></RoleRoute>} />
<Route path="discounts" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><DiscountsPage /></RoleRoute>} />
<Route path="subscription/success" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><PaymentSuccessPage /></RoleRoute>} />
<Route path="clinic-services" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><ClinicServicesPage /></RoleRoute>} />
<Route path="clinic-services/:uuid" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><ServiceDetailPage /></RoleRoute>} />
<Route path="inventory" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><InventoryPage /></RoleRoute>} />
<Route path="clinic-services" element={<RoleRoute roles={['doctor', 'clinic', 'secretary']} blockClinicScope permission={['services', 'view']}><ClinicServicesPage /></RoleRoute>} />
<Route path="clinic-services/:uuid" element={<RoleRoute roles={['doctor', 'clinic', 'secretary']} blockClinicScope permission={['services', 'view']}><ServiceDetailPage /></RoleRoute>} />
<Route path="inventory" element={<RoleRoute roles={['doctor', 'clinic', 'secretary']} blockClinicScope permission={['inventory', 'view']}><InventoryPage /></RoleRoute>} />
<Route path="sms-wallet" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><SmsWalletPage /></RoleRoute>} />
<Route path="my-secretaries" element={<RoleRoute roles={['doctor', 'clinic']} blockClinicScope><MySecretariesPage /></RoleRoute>} />
<Route path="admin-subscription" element={<RoleRoute roles={['admin']}><AdminSubscriptionPage /></RoleRoute>} />
@@ -76,3 +76,44 @@ describe("Sidebar — خدمات در منوی اصلی (نه تنظیمات)",
);
});
});
describe("Sidebar — گِیت منوی منشی بر اساس مجوز", () => {
const setSecretary = (resources: Record<string, Record<string, boolean>>) =>
useAuthStore.setState({
primaryRole: "secretary",
dbUuid: "c1",
userName: "منشی",
availableContexts: [],
context: { scope: "clinic", permissions: { resources } },
} as any);
it("منشیِ بدون مجوز، سرویس‌ها/انبار/تگ‌ها را نمی‌بیند", () => {
setSecretary({ appointments: { view: true } });
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
expect(screen.queryByText("سرویس ها")).not.toBeInTheDocument();
expect(screen.queryByText("انبارداری")).not.toBeInTheDocument();
expect(screen.queryByText("تگ‌ها")).not.toBeInTheDocument();
});
it("با مجوز services.view، آیتم «سرویس ها» به /admin/clinic-services می‌رود", () => {
setSecretary({ services: { view: true } });
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
expect(screen.getByText("سرویس ها").closest("a")).toHaveAttribute(
"href",
"/admin/clinic-services",
);
});
it("با مجوز inventory.view و tags.view، همان آیتم‌ها نمایش داده می‌شوند", () => {
setSecretary({ inventory: { view: true }, tags: { view: true } });
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
expect(screen.getByText("انبارداری").closest("a")).toHaveAttribute(
"href",
"/admin/inventory",
);
expect(screen.getByText("تگ‌ها").closest("a")).toHaveAttribute(
"href",
"/admin/tags-settings",
);
});
});
@@ -401,6 +401,27 @@ function buildSections(
},
);
}
if (can("services", "view")) {
items.push({
to: "/admin/clinic-services",
icon: WrenchScrewdriverIcon,
label: "سرویس ها",
});
}
if (can("inventory", "view")) {
items.push({
to: "/admin/inventory",
icon: ArchiveBoxIcon,
label: "انبارداری",
});
}
if (can("tags", "view")) {
items.push({
to: "/admin/tags-settings",
icon: TagIcon,
label: "تگ‌ها",
});
}
return [
{
+11
View File
@@ -75,6 +75,7 @@ const EMPTY_PERMISSIONS: SecretaryPermissions = {
clinic_info: { view: false, update: false },
inventory: { view: false, create: false, update: false, delete: false },
tags: { view: false, create: false, update: false, delete: false },
services: { view: false, create: false, update: false, delete: false },
};
type PermSection = keyof SecretaryPermissions;
@@ -162,6 +163,16 @@ const PERMISSION_SECTIONS: {
{ key: "delete", label: "حذف تگ" },
],
},
{
key: "services",
title: "خدمات و تعرفه‌ها",
items: [
{ key: "view", label: "مشاهده خدمات" },
{ key: "create", label: "ایجاد خدمت" },
{ key: "update", label: "ویرایش خدمت" },
{ key: "delete", label: "حذف خدمت" },
],
},
];
function PermissionAccordions({
+10
View File
@@ -21,6 +21,7 @@ const DEFAULT_PERMISSIONS: SecretaryPermissions = {
clinic_info: { view: true, update: false },
inventory: { view: false, create: false, update: false, delete: false },
tags: { view: false, create: false, update: false, delete: false },
services: { view: false, create: false, update: false, delete: false },
};
type PermSection = keyof SecretaryPermissions;
@@ -96,6 +97,15 @@ const PERMISSION_LABELS: Record<PermSection, { label: string; actions: { key: st
{ key: 'delete', label: 'حذف' },
],
},
services: {
label: 'خدمات و تعرفه‌ها',
actions: [
{ key: 'view', label: 'مشاهده' },
{ key: 'create', label: 'ایجاد' },
{ key: 'update', label: 'ویرایش' },
{ key: 'delete', label: 'حذف' },
],
},
};
function PermissionsMatrix({
+6
View File
@@ -502,6 +502,12 @@ export interface SecretaryPermissions {
update: boolean;
delete: boolean;
};
services: {
view: boolean;
create: boolean;
update: boolean;
delete: boolean;
};
}
export interface Specialty {