feat: version 3.1.0 -- Acceso por modulo y por usuario
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Save } from 'lucide-react';
|
||||
import { DetailPopup } from '../../components/DetailPopup.jsx';
|
||||
import { Button } from '../../components/Button.jsx';
|
||||
import { useUi } from '../../context/ui-context.jsx';
|
||||
import { getUserModules, setUserModules } from '../../services/users.js';
|
||||
import { MODULE_LABEL_KEYS } from '../../utils/modules.js';
|
||||
|
||||
// Acceso por modulo de un usuario (solo superadmin). Sin marcar = el usuario no ve el modulo.
|
||||
export function UserModulesDialog({ user, onClose }) {
|
||||
const { t, pushToast } = useUi();
|
||||
const [all, setAll] = useState([]);
|
||||
const [initial, setInitial] = useState([]);
|
||||
const [selected, setSelected] = useState([]);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!user) return;
|
||||
getUserModules(user.id).then((r) => { setAll(r.all); setInitial(r.modules); setSelected(r.modules); }).catch(() => {});
|
||||
}, [user?.id]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
const dirty = all.some((m) => initial.includes(m) !== selected.includes(m));
|
||||
|
||||
function toggle(module) {
|
||||
setSelected((s) => (s.includes(module) ? s.filter((m) => m !== module) : [...s, module]));
|
||||
}
|
||||
|
||||
async function save() {
|
||||
if (!dirty) return onClose();
|
||||
setSaving(true);
|
||||
try {
|
||||
await setUserModules(user.id, selected);
|
||||
pushToast({ tone: 'success', message: t('user_modules_saved') });
|
||||
onClose();
|
||||
} catch (err) {
|
||||
pushToast({ tone: 'error', message: err.message });
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<DetailPopup open={!!user} title={`${t('user_modules')} — ${user?.name || ''}`} onClose={onClose} width={420} height={420}>
|
||||
<p className="text-muted" style={{ marginTop: 0 }}>{t('user_modules_hint')}</p>
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 10, marginBottom: 16 }}>
|
||||
{all.map((m) => (
|
||||
<label key={m} style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
||||
<input type="checkbox" checked={selected.includes(m)} onChange={() => toggle(m)} />
|
||||
{t(MODULE_LABEL_KEYS[m])}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
<div style={{ display: 'flex', gap: 8 }}>
|
||||
<Button variant="accent" icon={Save} onClick={save} disabled={saving || !dirty}>{t('common_save')}</Button>
|
||||
<Button variant="standard" onClick={onClose}>{t('common_cancel')}</Button>
|
||||
</div>
|
||||
</DetailPopup>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState } from 'react';
|
||||
import { Plus, Save, UserX, UserCheck, Check, X as XIcon, Mail } from 'lucide-react';
|
||||
import { Plus, Save, UserX, UserCheck, Check, X as XIcon, Mail, LayoutGrid } from 'lucide-react';
|
||||
import { useUi } from '../../context/ui-context.jsx';
|
||||
import { useAuth } from '../../context/auth-context.jsx';
|
||||
import { useAsync, useAction } from '../../hooks/useAsync.js';
|
||||
@@ -12,6 +12,7 @@ import { Field, FieldRow } from '../../components/Field.jsx';
|
||||
import { ComboBox } from '../../components/ComboBox.jsx';
|
||||
import { Badge } from '../../components/Badge.jsx';
|
||||
import { DetailPopup } from '../../components/DetailPopup.jsx';
|
||||
import { UserModulesDialog } from './UserModulesDialog.jsx';
|
||||
import { DraftNotice } from '../../components/DraftNotice.jsx';
|
||||
import { DraftMark, useGuardedClose } from '../../components/NavigationGuard.jsx';
|
||||
import { useDraftForm, useUserDrafts, hasDraftFor, useReopenDraft } from '../../hooks/useDraftForm.js';
|
||||
@@ -34,6 +35,8 @@ export default function UsersPanel() {
|
||||
const { data: deptTree } = useAsync(listDepartments, []);
|
||||
|
||||
const canManage = ['admin', 'superadmin'].includes(currentUser?.roleCode);
|
||||
const isSuperadmin = currentUser?.roleCode === 'superadmin';
|
||||
const [modulesUser, setModulesUser] = useState(null);
|
||||
|
||||
const [search, setSearch] = useState('');
|
||||
const [editing, setEditing] = useState(false);
|
||||
@@ -179,6 +182,7 @@ export default function UsersPanel() {
|
||||
}
|
||||
return [
|
||||
{ label: t('common_edit'), onClick: () => startEdit(u) },
|
||||
...(isSuperadmin && u.role_code !== 'superadmin' ? [{ label: t('user_modules'), icon: LayoutGrid, onClick: () => setModulesUser(u) }] : []),
|
||||
u.is_active
|
||||
? { label: t('common_inactive'), icon: UserX, danger: true, onClick: () => toggleActive(u) }
|
||||
: { label: t('common_active'), icon: UserCheck, onClick: () => toggleActive(u) },
|
||||
@@ -186,6 +190,8 @@ export default function UsersPanel() {
|
||||
} : undefined}
|
||||
/>
|
||||
|
||||
{modulesUser && <UserModulesDialog user={modulesUser} onClose={() => setModulesUser(null)} />}
|
||||
|
||||
<DetailPopup open={editing} title={selected ? t('common_edit') : t('user_new')} onClose={() => guardedClose(draft, () => setEditing(false))} width={560} height={500}>
|
||||
<DraftNotice draft={draft} />
|
||||
<FieldRow>
|
||||
|
||||
Reference in New Issue
Block a user