feat(perms): permisología completa con 3 roles (Socio/Abogado/Usuario) + Panel de Control
ESQUEMA (migraciones 005 + 006)
* users: agrega role ENUM('socio','abogado','usuario'), apellido, activo, orden
* Seed: cabrego@aal-law.com como socio + 15 placeholders por apellido para
poblar el dropdown "Abogado Solicitante" hasta que los reales se logueen.
* tramites_involucrados: tabla N:M (tramite_id, user_id) con CASCADE delete.
AUTH (src/Auth.php)
* userRow() cachea el row del usuario actual por request.
* role(), isSocio(), isAbogado(), canCreate(), canDelete(), requireSocio().
* completeLogin() retorna bool: false si user.activo=0 → callback muestra
pantalla "cuenta deshabilitada" en lugar de loguear.
BOOTSTRAP (src/Bootstrap.php + src/Lookups.php)
* Bootstrap::inject(view) reemplaza readfile en los wrappers; inyecta
<script>window.DONNA={me,name,role,isSocio,isAbogado,canCreate,canDelete,
abogados,users}</script> antes de </head> para que el frontend tenga
contexto sin requests extra.
TRAMITES — visibilidad + involucrados
* api.php: list filtra por (socio | creador | abogado solicitante mapped via
users.apellido | involucrado) excepto socios que ven todo. Todas las
mutaciones validan canView(). create/delete validan rol. Persiste
involucrados vía syncInvolucrados().
* tramites.html: nueva columna "Involucrados" después de "Abogado", chip
picker multi-select en el modal, gear icon (oculto a non-socios), oculta
botones "+ Nuevo" y ✕ para usuarios sin permisos. Auto-incluye al
creador como involucrado por defecto.
* Calendario y demás apps consumen el API filtrado → heredan la
visibilidad sin código nuevo.
PANEL DE CONTROL (/admin/)
* index.php: requireSocio() + Bootstrap injection.
* api.php: list_users, create_user, update_user. Salvaguardas: el socio
no puede demoverse ni desactivarse a sí mismo.
* admin.html: tabla unificada de usuarios con filtros, modal CRUD,
resaltado de fila propia, placeholders identificados visualmente.
GEAR ICON
* Agregado en escrituras.html, tramites.html, calendario.html con
display:none por defecto; JS lo revela si window.DONNA.isSocio.
* Link a /admin/.
This commit is contained in:
+90
-1
@@ -78,6 +78,7 @@ function next_codigo(PDO $pdo, string $dept): string {
|
||||
|
||||
function hydrate(PDO $pdo, array $row): array {
|
||||
$tid = (int) $row['id'];
|
||||
|
||||
$obs = $pdo->prepare('SELECT id, texto, abogado, autor_email, fecha FROM tramites_obs WHERE tramite_id = :t ORDER BY fecha ASC');
|
||||
$obs->execute([':t' => $tid]);
|
||||
$row['obs'] = $obs->fetchAll();
|
||||
@@ -89,17 +90,79 @@ function hydrate(PDO $pdo, array $row): array {
|
||||
return $h;
|
||||
}, $hrs->fetchAll());
|
||||
|
||||
$inv = $pdo->prepare(
|
||||
'SELECT u.id, u.email, u.name, u.apellido
|
||||
FROM tramites_involucrados ti
|
||||
JOIN users u ON u.id = ti.user_id
|
||||
WHERE ti.tramite_id = :t
|
||||
ORDER BY u.name ASC, u.email ASC'
|
||||
);
|
||||
$inv->execute([':t' => $tid]);
|
||||
$row['involucrados'] = array_map(function ($u) {
|
||||
$u['id'] = (int)$u['id'];
|
||||
return $u;
|
||||
}, $inv->fetchAll());
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* ¿El usuario actual puede ver/editar este trámite?
|
||||
* Socio → siempre
|
||||
* Resto → si es creador, o abogado solicitante (apellido), o involucrado
|
||||
*/
|
||||
function canView(PDO $pdo, int $tid): bool {
|
||||
if (Auth::isSocio()) return true;
|
||||
$email = Auth::user()['email'];
|
||||
$uid = Auth::userId();
|
||||
$stmt = $pdo->prepare(
|
||||
"SELECT 1 FROM tramites_legales t
|
||||
LEFT JOIN users ua ON ua.apellido = t.abogado AND ua.activo = 1
|
||||
LEFT JOIN tramites_involucrados ti ON ti.tramite_id = t.id AND ti.user_id = :uid
|
||||
WHERE t.id = :tid
|
||||
AND (t.creado_por = :email OR ua.id = :uid2 OR ti.user_id IS NOT NULL)
|
||||
LIMIT 1"
|
||||
);
|
||||
$stmt->execute([':tid' => $tid, ':email' => $email, ':uid' => $uid, ':uid2' => $uid]);
|
||||
return $stmt->fetchColumn() !== false;
|
||||
}
|
||||
|
||||
/** Reemplaza la lista de involucrados del trámite por la lista dada (array de user_id). */
|
||||
function syncInvolucrados(PDO $pdo, int $tid, array $userIds, string $addedBy): void {
|
||||
$pdo->prepare('DELETE FROM tramites_involucrados WHERE tramite_id = :t')->execute([':t' => $tid]);
|
||||
if (empty($userIds)) return;
|
||||
$placeholders = implode(',', array_fill(0, count($userIds), '?'));
|
||||
$check = $pdo->prepare("SELECT id FROM users WHERE activo = 1 AND id IN ($placeholders)");
|
||||
$check->execute(array_map('intval', $userIds));
|
||||
$valid = array_map('intval', $check->fetchAll(PDO::FETCH_COLUMN));
|
||||
if (!$valid) return;
|
||||
$ins = $pdo->prepare('INSERT INTO tramites_involucrados (tramite_id, user_id, added_by) VALUES (?,?,?)');
|
||||
foreach ($valid as $uid) $ins->execute([$tid, $uid, $addedBy]);
|
||||
}
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$action = $_GET['action'] ?? '';
|
||||
$user = Auth::user();
|
||||
$myId = Auth::userId();
|
||||
$pdo = Db::pdo();
|
||||
|
||||
try {
|
||||
if ($method === 'GET' && $action === 'list') {
|
||||
$rows = $pdo->query('SELECT * FROM tramites_legales ORDER BY id DESC')->fetchAll();
|
||||
if (Auth::isSocio()) {
|
||||
$rows = $pdo->query('SELECT * FROM tramites_legales ORDER BY id DESC')->fetchAll();
|
||||
} else {
|
||||
$stmt = $pdo->prepare(
|
||||
"SELECT DISTINCT t.* FROM tramites_legales t
|
||||
LEFT JOIN users ua ON ua.apellido = t.abogado AND ua.activo = 1
|
||||
LEFT JOIN tramites_involucrados ti ON ti.tramite_id = t.id AND ti.user_id = :uid
|
||||
WHERE t.creado_por = :email
|
||||
OR ua.id = :uid2
|
||||
OR ti.user_id IS NOT NULL
|
||||
ORDER BY t.id DESC"
|
||||
);
|
||||
$stmt->execute([':email' => $user['email'], ':uid' => $myId, ':uid2' => $myId]);
|
||||
$rows = $stmt->fetchAll();
|
||||
}
|
||||
$grouped = ['adm' => [], 'corp' => [], 'mig' => [], 'lit' => []];
|
||||
foreach ($rows as $r) {
|
||||
$grouped[$r['departamento']][] = hydrate($pdo, $r);
|
||||
@@ -108,6 +171,8 @@ try {
|
||||
}
|
||||
|
||||
if ($method === 'POST' && $action === 'create') {
|
||||
if (!Auth::canCreate()) err('Tu rol no permite crear trámites. Solo Socios y Abogados pueden.', 403);
|
||||
|
||||
$b = body();
|
||||
$dept = dept_of($b);
|
||||
|
||||
@@ -156,6 +221,12 @@ try {
|
||||
':user' => $user['email'],
|
||||
]);
|
||||
$id = (int) $pdo->lastInsertId();
|
||||
|
||||
// Involucrados: si vienen del payload, se respetan; si no, se auto-incluye al creador
|
||||
$involIds = array_map('intval', (array)($b['involucrados'] ?? []));
|
||||
if ($myId > 0 && !in_array($myId, $involIds, true)) $involIds[] = $myId;
|
||||
syncInvolucrados($pdo, $id, $involIds, $user['email']);
|
||||
|
||||
$row = $pdo->prepare('SELECT * FROM tramites_legales WHERE id = :id');
|
||||
$row->execute([':id' => $id]);
|
||||
ok(hydrate($pdo, $row->fetch()));
|
||||
@@ -166,6 +237,8 @@ try {
|
||||
$id = (int) ($b['_id'] ?? 0);
|
||||
if (!$id) err('id requerido');
|
||||
|
||||
if (!canView($pdo, $id)) err('No tienes acceso a este trámite', 403);
|
||||
|
||||
$cur = $pdo->prepare('SELECT * FROM tramites_legales WHERE id = :id');
|
||||
$cur->execute([':id' => $id]);
|
||||
$before = $cur->fetch();
|
||||
@@ -212,6 +285,11 @@ try {
|
||||
':user' => $user['email'],
|
||||
':id' => $id,
|
||||
]);
|
||||
|
||||
if (array_key_exists('involucrados', $b) && is_array($b['involucrados'])) {
|
||||
syncInvolucrados($pdo, $id, array_map('intval', $b['involucrados']), $user['email']);
|
||||
}
|
||||
|
||||
$row = $pdo->prepare('SELECT * FROM tramites_legales WHERE id = :id');
|
||||
$row->execute([':id' => $id]);
|
||||
ok(hydrate($pdo, $row->fetch()));
|
||||
@@ -223,6 +301,8 @@ try {
|
||||
$val = (string) ($b['estatus'] ?? '');
|
||||
if (!$id) err('id requerido');
|
||||
|
||||
if (!canView($pdo, $id)) err('No tienes acceso a este trámite', 403);
|
||||
|
||||
$cur = $pdo->prepare('SELECT departamento FROM tramites_legales WHERE id = :id');
|
||||
$cur->execute([':id' => $id]);
|
||||
$dept = $cur->fetchColumn();
|
||||
@@ -242,6 +322,8 @@ try {
|
||||
if (!$id) err('id requerido');
|
||||
if ($texto === '') err('texto requerido');
|
||||
|
||||
if (!canView($pdo, $id)) err('No tienes acceso a este trámite', 403);
|
||||
|
||||
$pdo->prepare('INSERT INTO tramites_obs (tramite_id, texto, abogado, autor_email) VALUES (?,?,?,?)')
|
||||
->execute([$id, $texto, $abog, $user['email']]);
|
||||
|
||||
@@ -262,6 +344,8 @@ try {
|
||||
if (!$id) err('id requerido');
|
||||
if ($hrs <= 0) err('horas debe ser > 0');
|
||||
|
||||
if (!canView($pdo, $id)) err('No tienes acceso a este trámite', 403);
|
||||
|
||||
$pdo->prepare('INSERT INTO tramites_hours (tramite_id, abogado, hrs, descripcion, autor_email) VALUES (?,?,?,?,?)')
|
||||
->execute([$id, $abog, $hrs, $desc, $user['email']]);
|
||||
|
||||
@@ -274,8 +358,13 @@ try {
|
||||
}
|
||||
|
||||
if ($method === 'DELETE' && $action === 'delete') {
|
||||
if (!Auth::canDelete()) err('Tu rol no permite eliminar trámites. Solo Socios y Abogados pueden.', 403);
|
||||
|
||||
$id = (int) ($_GET['id'] ?? 0);
|
||||
if (!$id) err('id requerido');
|
||||
|
||||
if (!canView($pdo, $id)) err('No tienes acceso a este trámite', 403);
|
||||
|
||||
$pdo->prepare('DELETE FROM tramites_legales WHERE id = :id')->execute([':id' => $id]);
|
||||
ok(['deleted' => $id]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user