Admin: RBAC faza 2 M2 - konsola ról gymowych z grantów (nowa strona ról, modal członkostwa na PUT roleKeys, testy e2e i unit)
This commit is contained in:
@@ -11,6 +11,7 @@ import GymBoard from './pages/gyms/GymBoard';
|
||||
import GymGalleryPage from './pages/gyms/GymGalleryPage';
|
||||
import GymEquipmentPage from './pages/gyms/GymEquipmentPage';
|
||||
import GymAnnouncementsPage from './pages/gyms/GymAnnouncementsPage';
|
||||
import GymRolesPage from './pages/gyms/GymRolesPage';
|
||||
import GymLogsPage from './pages/gyms/GymLogsPage';
|
||||
import UsersList from './pages/users/UsersList';
|
||||
import UserForm from './pages/users/UserForm';
|
||||
@@ -126,6 +127,14 @@ export default function App() {
|
||||
</Guard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/gyms/:id/roles"
|
||||
element={
|
||||
<Guard perm="gyms:manage">
|
||||
<GymRolesPage />
|
||||
</Guard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/gyms/:id/logs"
|
||||
element={
|
||||
|
||||
@@ -9,6 +9,7 @@ import type {
|
||||
GymBoardData,
|
||||
GymKind,
|
||||
GymRole,
|
||||
GymRoleDef,
|
||||
User,
|
||||
} from './types';
|
||||
|
||||
@@ -64,6 +65,15 @@ export function useGymMembers(
|
||||
});
|
||||
}
|
||||
|
||||
/** Katalog ról jednej siłowni (GET /admin/gyms/:gymId/roles): systemowe role gymowe + custom tej siłowni. */
|
||||
export function useGymRoles(gymId: string | undefined) {
|
||||
return useQuery({
|
||||
queryKey: ['gym-roles', gymId],
|
||||
queryFn: () => api<{ roles: GymRoleDef[] }>(`/admin/gyms/${gymId}/roles`),
|
||||
enabled: !!gymId,
|
||||
});
|
||||
}
|
||||
|
||||
/** Wnioski i zaproszenia powiązane z jedną siłownią (GET /admin/gyms/:id/related). */
|
||||
export function useGymRelated(gymId: string | undefined) {
|
||||
return useQuery({
|
||||
|
||||
@@ -42,7 +42,7 @@ export interface User {
|
||||
/** Data ostatniego udanego logowania (login, nie odświeżanie tokenu); null = nigdy. */
|
||||
lastLoginAt?: string | null;
|
||||
createdAt: string;
|
||||
memberships?: { gymId: string; roles: GymRole[]; gym: Gym }[];
|
||||
memberships?: { gymId: string; roles: GymRole[]; roleKeys: string[]; gym: Gym }[];
|
||||
}
|
||||
|
||||
/** Rola platformowa (RBAC) — odpowiedź /admin/roles. */
|
||||
@@ -237,11 +237,23 @@ export interface AdminMemberUser {
|
||||
export interface AdminGymMemberItem {
|
||||
membershipId: string;
|
||||
roles: string[];
|
||||
/** Klucze grantów ról gymowych (RBAC faza 2): GYM_OWNER/GYM_TRAINER + custom. */
|
||||
roleKeys: string[];
|
||||
joinedAt: string;
|
||||
checkedInNow: boolean;
|
||||
user: AdminMemberUser;
|
||||
}
|
||||
|
||||
/** Rola gymowa (katalog jednej siłowni) — odpowiedź GET /admin/gyms/:gymId/roles. */
|
||||
export interface GymRoleDef {
|
||||
id: string;
|
||||
key: string;
|
||||
name: string;
|
||||
permissions: string[];
|
||||
isSystem: boolean;
|
||||
grantCount: number;
|
||||
}
|
||||
|
||||
export interface AdminGymMemberPage {
|
||||
members: AdminGymMemberItem[];
|
||||
total: number;
|
||||
|
||||
@@ -1,22 +1,32 @@
|
||||
import { useState } from 'react';
|
||||
import { useMemo, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { api } from '../api/client';
|
||||
import { useGymRoles } from '../api/queries';
|
||||
import { apiErrorMessage } from '../lib/errors';
|
||||
import type { GymRole } from '../api/types';
|
||||
import { GymRolePicker } from './GymRolePicker';
|
||||
import { GYM_ROLE_META } from '../lib/roles';
|
||||
import { Button, ErrorText, Field, Modal, Muted, Textarea } from './ui';
|
||||
import { GYM_ROLE_ORDER } from '../lib/roles';
|
||||
|
||||
const GYM_OWNER_KEY = 'GYM_OWNER';
|
||||
const GYM_TRAINER_KEY = 'GYM_TRAINER';
|
||||
|
||||
/** Etykieta roli: systemowa z tłumaczeniem legacy, custom z nazwą zdefiniowaną przez admina. */
|
||||
function roleLabel(key: string, name: string, t: (k: string) => string): string {
|
||||
if (key === GYM_OWNER_KEY) return t(`common:${GYM_ROLE_META.OWNER.labelKey}`);
|
||||
if (key === GYM_TRAINER_KEY) return t(`common:${GYM_ROLE_META.TRAINER.labelKey}`);
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wspólne menu członkostwa (konsola siłowni + profil usera): pełna lista ról (PATCH)
|
||||
* oraz wyrzucenie z siłowni z wymagalnym powodem (DELETE + audyt membership.deleted).
|
||||
* Wspólne menu członkostwa (konsola siłowni + profil usera): przydzielanie ról
|
||||
* z grantów (PUT /admin/gyms/:gymId/members/:userId/roles — RBAC faza 2) oraz
|
||||
* wyrzucenie z siłowni z wymagalnym powodem (DELETE + audyt membership.deleted).
|
||||
*/
|
||||
export function MembershipActionsModal({
|
||||
userId,
|
||||
titleName,
|
||||
gymId,
|
||||
roles: initialRoles,
|
||||
roleKeys: initialRoleKeys,
|
||||
showProfileLink = true,
|
||||
onClose,
|
||||
onChanged,
|
||||
@@ -24,27 +34,37 @@ export function MembershipActionsModal({
|
||||
userId: string;
|
||||
titleName: string;
|
||||
gymId: string;
|
||||
roles: string[];
|
||||
/** Klucze grantów ról gymowych członka (GET members / list users). */
|
||||
roleKeys: string[];
|
||||
showProfileLink?: boolean;
|
||||
onClose: () => void;
|
||||
onChanged: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation('gyms');
|
||||
const [roles, setRoles] = useState<GymRole[]>(
|
||||
GYM_ROLE_ORDER.filter((r) => initialRoles.includes(r)),
|
||||
);
|
||||
const { data: catalog } = useGymRoles(gymId);
|
||||
const [selected, setSelected] = useState<string[]>(initialRoleKeys);
|
||||
const [reason, setReason] = useState('');
|
||||
const [busy, setBusy] = useState<'' | 'roles' | 'remove'>('');
|
||||
const [error, setError] = useState('');
|
||||
|
||||
// katalog: systemowe role gymowe tej siłowni + role własne (GYM_OWNER już w katalogu)
|
||||
const roleOptions = useMemo(() => {
|
||||
const defs = (catalog?.roles ?? []).filter((r) =>
|
||||
r.isSystem ? r.key === GYM_OWNER_KEY || r.key === GYM_TRAINER_KEY : true,
|
||||
);
|
||||
return defs.map((r) => ({ key: r.key, label: roleLabel(r.key, r.name, t) }));
|
||||
}, [catalog, t]);
|
||||
|
||||
const toggle = (key: string) =>
|
||||
setSelected((ks) => (ks.includes(key) ? ks.filter((k) => k !== key) : [...ks, key]));
|
||||
|
||||
const saveRoles = async () => {
|
||||
if (roles.length === 0) return;
|
||||
setError('');
|
||||
setBusy('roles');
|
||||
try {
|
||||
await api(`/admin/users/${userId}/memberships/${gymId}`, {
|
||||
method: 'PATCH',
|
||||
body: { roles },
|
||||
await api(`/admin/gyms/${gymId}/members/${userId}/roles`, {
|
||||
method: 'PUT',
|
||||
body: { roleKeys: selected },
|
||||
});
|
||||
onChanged();
|
||||
onClose();
|
||||
@@ -91,18 +111,26 @@ export function MembershipActionsModal({
|
||||
<div className="mb-2 text-[10.5px] font-bold uppercase tracking-[0.11em] text-dim">
|
||||
{t('memberRolesTitle')}
|
||||
</div>
|
||||
<GymRolePicker
|
||||
value={roles}
|
||||
onChange={setRoles}
|
||||
labelPrefix={titleName}
|
||||
showExplain={false}
|
||||
/>
|
||||
<div className="flex flex-col gap-1 rounded-xl border border-line bg-input/40 p-3">
|
||||
{roleOptions.length === 0 && <Muted className="text-[0.85rem]">{t('rolesLoading')}</Muted>}
|
||||
{roleOptions.map((o) => (
|
||||
<label key={o.key} className="flex items-center gap-2.5 text-[0.9rem]">
|
||||
<input
|
||||
type="checkbox"
|
||||
className="h-4 w-4"
|
||||
checked={selected.includes(o.key)}
|
||||
onChange={() => toggle(o.key)}
|
||||
/>
|
||||
<span className="min-w-0">
|
||||
<span>{o.label}</span>
|
||||
<span className="block font-mono text-[0.7rem] text-dim">{o.key}</span>
|
||||
</span>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
<p className="mt-1.5 text-[0.78rem] text-dim">{t('memberRolesNote')}</p>
|
||||
<div className="mt-3">
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => void saveRoles()}
|
||||
disabled={busy !== '' || roles.length === 0}
|
||||
>
|
||||
<Button size="sm" onClick={() => void saveRoles()} disabled={busy !== ''}>
|
||||
{busy === 'roles' ? t('common:saving') : t('saveRoles')}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
"hint": "Gym audit"
|
||||
},
|
||||
"groupLogs": "Logs",
|
||||
"groupRoles": "Roles & permissions",
|
||||
"logsPage": {
|
||||
"title": "Gym logs",
|
||||
"subtitle": "Operations on the gym, its members, claims and invitations",
|
||||
@@ -90,6 +91,39 @@
|
||||
"memberMenuDesc": "Manage roles or remove this member from the gym.",
|
||||
"memberProfileLink": "Open profile in panel",
|
||||
"memberRolesTitle": "Gym roles",
|
||||
"memberRolesNote": "Checked roles are gym permissions (grants); every member also has the base MEMBER access.",
|
||||
"rolesLoading": "Loading role catalog…",
|
||||
"rolesTitle": "Gym roles: {{name}}",
|
||||
"rolesIntro": "System roles (owner, trainer) and this gym's own roles with a chosen set of gym permissions.",
|
||||
"rolesNew": "New role",
|
||||
"rolesEditTitle": "Edit role",
|
||||
"rolesNameLabel": "Role name",
|
||||
"rolesNamePh": "e.g. Reception",
|
||||
"rolesPermsTitle": "Permissions",
|
||||
"rolesDelegateNote": "gym:delegate (assigning roles) is pinned to the owner role only and never lands in custom roles.",
|
||||
"rolesSystemBadge": "System",
|
||||
"rolesEditAria": "Edit role {{name}}",
|
||||
"rolesRemoveAria": "Delete role {{name}}",
|
||||
"rolesGrantCount_one": "{{count}} assignment",
|
||||
"rolesGrantCount_other": "{{count}} assignments",
|
||||
"rolesEmpty": "No roles",
|
||||
"rolesDeleteTitle": "Delete this role?",
|
||||
"rolesDeleteDesc": "Role “{{name}}” will be deleted permanently.",
|
||||
"rolesDeleteBlockedSystem": "System roles are managed via permissions only",
|
||||
"rolesDeleteBlockedAssigned": "Remove this role from all members first",
|
||||
"gymPerms": {
|
||||
"gym_edit": "Edit gym settings",
|
||||
"gym_members": "Manage members and invitations",
|
||||
"gym_delegate": "Assign roles",
|
||||
"gym_equipment": "Equipment and its photos",
|
||||
"gym_gallery": "Photo gallery",
|
||||
"gym_announcements": "Board announcements",
|
||||
"gym_exceptions": "Opening-hours exceptions",
|
||||
"gym_contact": "Member messages",
|
||||
"gym_stats": "Gym stats and logs",
|
||||
"gym_delete": "Delete the gym",
|
||||
"trainer_clients": "Trainer care (trainees)"
|
||||
},
|
||||
"saveRoles": "Save roles",
|
||||
"removeMemberTitle": "Remove from gym",
|
||||
"removeMemberDesc": "The membership is removed immediately. The reason is stored in the audit log.",
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
"hint": "Audyt siłowni"
|
||||
},
|
||||
"groupLogs": "Logi",
|
||||
"groupRoles": "Role i uprawnienia",
|
||||
"logsPage": {
|
||||
"title": "Logi siłowni",
|
||||
"subtitle": "Operacje na siłowni, członkach, wnioskach i zaproszeniach",
|
||||
@@ -90,6 +91,41 @@
|
||||
"memberMenuDesc": "Zarządzaj rolami albo usuń członkostwo w tej siłowni.",
|
||||
"memberProfileLink": "Otwórz profil w panelu",
|
||||
"memberRolesTitle": "Role w siłowni",
|
||||
"memberRolesNote": "Zaznaczone role to uprawnienia gymowe (granty); każdy członek ma też dostęp bazowy MEMBER.",
|
||||
"rolesLoading": "Ładowanie katalogu ról…",
|
||||
"rolesTitle": "Role siłowni: {{name}}",
|
||||
"rolesIntro": "Role systemowe (właściciel, trener) i role własne tej siłowni z wybranym zestawem uprawnień gymowych.",
|
||||
"rolesNew": "Nowa rola",
|
||||
"rolesEditTitle": "Edytuj rolę",
|
||||
"rolesNameLabel": "Nazwa roli",
|
||||
"rolesNamePh": "np. Recepcja",
|
||||
"rolesPermsTitle": "Uprawnienia",
|
||||
"rolesDelegateNote": "gym:delegate (przypisywanie ról) jest przypięte wyłącznie do roli właściciela i nie trafia do ról własnych.",
|
||||
"rolesSystemBadge": "Systemowa",
|
||||
"rolesEditAria": "Edytuj rolę {{name}}",
|
||||
"rolesRemoveAria": "Usuń rolę {{name}}",
|
||||
"rolesGrantCount_one": "{{count}} przypisanie",
|
||||
"rolesGrantCount_few": "{{count}} przypisania",
|
||||
"rolesGrantCount_many": "{{count}} przypisań",
|
||||
"rolesGrantCount_other": "{{count}} przypisania",
|
||||
"rolesEmpty": "Brak ról",
|
||||
"rolesDeleteTitle": "Usunąć rolę?",
|
||||
"rolesDeleteDesc": "Rola „{{name}}” zostanie usunięta na stałe.",
|
||||
"rolesDeleteBlockedSystem": "Rolę systemową można tylko edytować z poziomu uprawnień",
|
||||
"rolesDeleteBlockedAssigned": "Najpierw odbierz tę rolę wszystkim członkom",
|
||||
"gymPerms": {
|
||||
"gym_edit": "Edycja ustawień siłowni",
|
||||
"gym_members": "Zarządzanie członkami i zaproszeniami",
|
||||
"gym_delegate": "Przypisywanie ról",
|
||||
"gym_equipment": "Urządzenia i ich zdjęcia",
|
||||
"gym_gallery": "Galeria zdjęć",
|
||||
"gym_announcements": "Ogłoszenia na tablicy",
|
||||
"gym_exceptions": "Wyjątki godzin otwarcia",
|
||||
"gym_contact": "Wiadomości od członków",
|
||||
"gym_stats": "Statystyki i logi siłowni",
|
||||
"gym_delete": "Usunięcie siłowni",
|
||||
"trainer_clients": "Opieka trenerska (podopieczni)"
|
||||
},
|
||||
"saveRoles": "Zapisz role",
|
||||
"removeMemberTitle": "Wyrzuć z siłowni",
|
||||
"removeMemberDesc": "Członkostwo zostanie usunięte natychmiast. Powód trafi do logów audytu.",
|
||||
|
||||
@@ -383,6 +383,7 @@ export default function GymDetail() {
|
||||
: []),
|
||||
{ label: t('groupEquipment'), to: `/gyms/${gym.id}/equipment` },
|
||||
{ label: t('groupAnnouncements'), to: `/gyms/${gym.id}/announcements` },
|
||||
{ label: t('groupRoles'), to: `/gyms/${gym.id}/roles` },
|
||||
{ label: t('groupLogs'), to: `/gyms/${gym.id}/logs` },
|
||||
{
|
||||
label: t('deleteGym'),
|
||||
@@ -683,7 +684,7 @@ export default function GymDetail() {
|
||||
userId={manage.user.id}
|
||||
titleName={memberLabel(manage)}
|
||||
gymId={gym.id}
|
||||
roles={manage.roles}
|
||||
roleKeys={manage.roleKeys}
|
||||
onClose={() => setManage(null)}
|
||||
onChanged={() => void invalidate()}
|
||||
/>
|
||||
|
||||
286
admin/src/pages/gyms/GymRolesPage.tsx
Normal file
286
admin/src/pages/gyms/GymRolesPage.tsx
Normal file
@@ -0,0 +1,286 @@
|
||||
import { useState } from 'react';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { KeyRound, Pencil, Plus, ShieldCheck, Trash2 } from 'lucide-react';
|
||||
import { api } from '../../api/client';
|
||||
import type { GymRoleDef } from '../../api/types';
|
||||
import {
|
||||
Badge,
|
||||
Button,
|
||||
Card,
|
||||
ConfirmDialog,
|
||||
EmptyState,
|
||||
ErrorText,
|
||||
Field,
|
||||
Input,
|
||||
Loading,
|
||||
Modal,
|
||||
Muted,
|
||||
} from '../../components/ui';
|
||||
import { useTopbar } from '../../topbar';
|
||||
|
||||
/** Katalog uprawnień gymowych (faza 2 RBAC) — kolejność zgodna z katalogiem API. */
|
||||
export const GYM_PERMISSION_KEYS = [
|
||||
'gym:edit',
|
||||
'gym:members',
|
||||
'gym:delegate',
|
||||
'gym:equipment',
|
||||
'gym:gallery',
|
||||
'gym:announcements',
|
||||
'gym:exceptions',
|
||||
'gym:contact',
|
||||
'gym:stats',
|
||||
'gym:delete',
|
||||
'trainer:clients',
|
||||
];
|
||||
|
||||
/** Klucz i18n uprawnienia gymowego: dwukropek zastąpiony podkreśleniem. */
|
||||
function gpermKey(perm: string): string {
|
||||
return `gymPerms.${perm.replace(':', '_')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Role siłowni (RBAC faza 2): katalog ról tej siłowni (systemowe GYM_OWNER/GYM_TRAINER
|
||||
* + role własne), kreator/edycja ról custom z checkboxami uprawnień gymowych
|
||||
* (`gym:delegate` tylko w GYM_OWNER) i usuwanie ról bez przypisań.
|
||||
*/
|
||||
export default function GymRolesPage() {
|
||||
const { t } = useTranslation('gyms');
|
||||
const { id } = useParams();
|
||||
const qc = useQueryClient();
|
||||
const { data: gym } = useQuery({
|
||||
queryKey: ['gym', id],
|
||||
queryFn: () => api<{ id: string; name: string }>(`/admin/gyms/${id}`),
|
||||
enabled: !!id,
|
||||
});
|
||||
const { data, isLoading, error } = useQuery({
|
||||
queryKey: ['gym-roles', id],
|
||||
queryFn: () => api<{ roles: GymRoleDef[] }>(`/admin/gyms/${id}/roles`),
|
||||
enabled: !!id,
|
||||
});
|
||||
const roles = data?.roles ?? [];
|
||||
|
||||
const [formOpen, setFormOpen] = useState(false);
|
||||
const [editing, setEditing] = useState<GymRoleDef | null>(null);
|
||||
const [name, setName] = useState('');
|
||||
const [perms, setPerms] = useState<string[]>([]);
|
||||
const [formError, setFormError] = useState<string | null>(null);
|
||||
const [deleting, setDeleting] = useState<GymRoleDef | null>(null);
|
||||
|
||||
const invalidate = () => {
|
||||
qc.invalidateQueries({ queryKey: ['gym-roles', id] });
|
||||
qc.invalidateQueries({ queryKey: ['gym-members'] });
|
||||
};
|
||||
|
||||
const save = useMutation({
|
||||
mutationFn: async () => {
|
||||
if (editing) {
|
||||
await api(`/admin/gyms/${id}/roles/${editing.id}`, {
|
||||
method: 'PATCH',
|
||||
body: { name: name.trim(), permissions: perms },
|
||||
});
|
||||
} else {
|
||||
await api(`/admin/gyms/${id}/roles`, {
|
||||
method: 'POST',
|
||||
body: { name: name.trim(), permissions: perms },
|
||||
});
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
invalidate();
|
||||
setFormOpen(false);
|
||||
setEditing(null);
|
||||
setFormError(null);
|
||||
},
|
||||
onError: (e) => setFormError((e as Error).message),
|
||||
});
|
||||
|
||||
const remove = useMutation({
|
||||
mutationFn: (roleId: string) => api(`/admin/gyms/${id}/roles/${roleId}`, { method: 'DELETE' }),
|
||||
onSuccess: () => {
|
||||
invalidate();
|
||||
setDeleting(null);
|
||||
},
|
||||
onError: (e) => {
|
||||
setDeleting(null);
|
||||
setFormError((e as Error).message);
|
||||
},
|
||||
});
|
||||
|
||||
function openCreate() {
|
||||
setEditing(null);
|
||||
setName('');
|
||||
setPerms([]);
|
||||
setFormError(null);
|
||||
setFormOpen(true);
|
||||
}
|
||||
|
||||
function openEdit(role: GymRoleDef) {
|
||||
setEditing(role);
|
||||
setName(role.name);
|
||||
setPerms(role.permissions);
|
||||
setFormError(null);
|
||||
setFormOpen(true);
|
||||
}
|
||||
|
||||
const toggle = (perm: string) =>
|
||||
setPerms((ps) => (ps.includes(perm) ? ps.filter((p) => p !== perm) : [...ps, perm]));
|
||||
|
||||
useTopbar({
|
||||
title: t('rolesTitle', { name: gym?.name ?? '' }),
|
||||
back: `/gyms/${id}`,
|
||||
actions: [{ label: t('rolesNew'), onClick: openCreate, icon: Plus }],
|
||||
});
|
||||
|
||||
const system = editing?.isSystem;
|
||||
const pinnedDelegate = system && editing?.key === 'GYM_OWNER';
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<Muted className="block">{t('rolesIntro')}</Muted>
|
||||
|
||||
{isLoading && <Loading />}
|
||||
{error && (
|
||||
<ErrorText>
|
||||
{t('common:errorPrefix')}
|
||||
{(error as Error).message}
|
||||
</ErrorText>
|
||||
)}
|
||||
|
||||
{formError && !formOpen && (
|
||||
<div>
|
||||
<ErrorText>{formError}</ErrorText>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
{roles.map((r) => (
|
||||
<Card key={r.id} className="mb-0">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<span className="text-[1.05rem] font-semibold">{r.name}</span>
|
||||
{r.isSystem && <Badge color="blue">{t('rolesSystemBadge')}</Badge>}
|
||||
</div>
|
||||
<div className="mt-0.5 font-mono text-[0.75rem] text-dim">{r.key}</div>
|
||||
<div className="mt-1.5 flex flex-wrap gap-1">
|
||||
{r.permissions.map((p) => (
|
||||
<Badge key={p} color="gray">
|
||||
{t(gpermKey(p), { defaultValue: p })}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex shrink-0 items-center gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => openEdit(r)}
|
||||
aria-label={t('rolesEditAria', { name: r.name })}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="text-danger hover:text-danger"
|
||||
onClick={() => setDeleting(r)}
|
||||
disabled={r.isSystem || r.grantCount > 0}
|
||||
title={
|
||||
r.isSystem
|
||||
? t('rolesDeleteBlockedSystem')
|
||||
: r.grantCount > 0
|
||||
? t('rolesDeleteBlockedAssigned')
|
||||
: undefined
|
||||
}
|
||||
aria-label={t('rolesRemoveAria', { name: r.name })}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-2 text-[0.8rem] text-dim">
|
||||
{t('rolesGrantCount', { count: r.grantCount })}
|
||||
</div>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{!isLoading && !error && roles.length === 0 && (
|
||||
<Card>
|
||||
<EmptyState icon={KeyRound} title={t('rolesEmpty')} description={t('rolesIntro')} />
|
||||
</Card>
|
||||
)}
|
||||
|
||||
<Modal
|
||||
open={formOpen}
|
||||
onClose={() => setFormOpen(false)}
|
||||
title={editing ? t('rolesEditTitle') : t('rolesNew')}
|
||||
>
|
||||
<div className="flex flex-col gap-3.5">
|
||||
<Field label={t('rolesNameLabel')}>
|
||||
<Input
|
||||
aria-label={t('rolesNameLabel')}
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
placeholder={t('rolesNamePh')}
|
||||
maxLength={60}
|
||||
/>
|
||||
</Field>
|
||||
<div>
|
||||
<div className="mb-1.5 text-[0.8rem] font-bold uppercase tracking-[0.08em] text-dim">
|
||||
{t('rolesPermsTitle')}
|
||||
</div>
|
||||
<div className="flex flex-col gap-1 rounded-xl border border-line bg-input/40 p-3">
|
||||
{GYM_PERMISSION_KEYS.map((p) => {
|
||||
const pinned = p === 'gym:delegate';
|
||||
return (
|
||||
<label key={p} className="flex items-center gap-2.5 text-[0.9rem]">
|
||||
<input
|
||||
type="checkbox"
|
||||
className="h-4 w-4"
|
||||
checked={perms.includes(p)}
|
||||
disabled={pinned}
|
||||
onChange={() => toggle(p)}
|
||||
/>
|
||||
<span className="min-w-0">
|
||||
<span className="flex items-center gap-1.5">
|
||||
{t(gpermKey(p), { defaultValue: p })}
|
||||
{pinned && pinnedDelegate && (
|
||||
<ShieldCheck className="h-3.5 w-3.5 text-accent-text" />
|
||||
)}
|
||||
</span>
|
||||
<span className="block font-mono text-[0.7rem] text-dim">{p}</span>
|
||||
</span>
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<p className="mt-1.5 text-[0.78rem] text-dim">{t('rolesDelegateNote')}</p>
|
||||
</div>
|
||||
{formError && <ErrorText>{formError}</ErrorText>}
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button variant="secondary" onClick={() => setFormOpen(false)}>
|
||||
{t('common:cancel')}
|
||||
</Button>
|
||||
<Button onClick={() => save.mutate()} disabled={save.isPending || !name.trim()}>
|
||||
{editing ? t('common:save') : t('common:add')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
<ConfirmDialog
|
||||
open={deleting != null}
|
||||
onClose={() => setDeleting(null)}
|
||||
onConfirm={() => deleting && remove.mutate(deleting.id)}
|
||||
title={t('rolesDeleteTitle')}
|
||||
description={t('rolesDeleteDesc', { name: deleting?.name ?? '' })}
|
||||
confirmLabel={t('common:remove')}
|
||||
danger
|
||||
busy={remove.isPending}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -89,9 +89,11 @@ export default function UserDetail() {
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [removeError, setRemoveError] = useState('');
|
||||
const [restriction, setRestriction] = useState<'SUSPENDED' | 'BANNED' | null>(null);
|
||||
const [manage, setManage] = useState<{ gymId: string; gymName: string; roles: string[] } | null>(
|
||||
null,
|
||||
);
|
||||
const [manage, setManage] = useState<{
|
||||
gymId: string;
|
||||
gymName: string;
|
||||
roleKeys: string[];
|
||||
} | null>(null);
|
||||
|
||||
// Konto z rolą platformową (staff) wolno ruszać tylko posiadaczowi roles:manage.
|
||||
const staffLocked = (user?.platformRoles?.length ?? 0) > 0 && !canRoles;
|
||||
@@ -254,7 +256,11 @@ export default function UserDetail() {
|
||||
staffLocked
|
||||
? undefined
|
||||
: () =>
|
||||
setManage({ gymId: m.gymId, gymName: m.gym.name, roles: m.roles })
|
||||
setManage({
|
||||
gymId: m.gymId,
|
||||
gymName: m.gym.name,
|
||||
roleKeys: m.roleKeys,
|
||||
})
|
||||
}
|
||||
className={cn(
|
||||
'border-b border-line/60 transition-colors last:border-b-0',
|
||||
@@ -367,7 +373,7 @@ export default function UserDetail() {
|
||||
userId={user.id}
|
||||
titleName={manage.gymName}
|
||||
gymId={manage.gymId}
|
||||
roles={manage.roles}
|
||||
roleKeys={manage.roleKeys}
|
||||
showProfileLink={false}
|
||||
onClose={() => setManage(null)}
|
||||
onChanged={() => void invalidateUsers()}
|
||||
|
||||
Reference in New Issue
Block a user