-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermisos.html
More file actions
316 lines (273 loc) · 8.27 KB
/
permisos.html
File metadata and controls
316 lines (273 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Gestión de Permisos por Rol</title>
<style>
:root{
--bg:#f3f5f7;
--card:#ffffff;
--text:#1f2a37;
--muted:#6b7280;
--primary:#2c7be5;
--line:#e5e7eb;
--danger:#b91c1c;
--dangerBg:#fef2f2;
--dangerLine:#fecaca;
}
body{
margin:0;
font-family: Arial, sans-serif;
background:var(--bg);
color:var(--text);
padding:24px;
}
.top{
display:flex;
align-items:center;
justify-content:space-between;
gap:12px;
margin-bottom:16px;
}
h1{
margin:0;
font-size:34px;
font-weight:800;
letter-spacing:-.3px;
}
.actions{
display:flex;
gap:10px;
align-items:center;
}
button{
padding:10px 14px;
border:none;
background:var(--primary);
color:#fff;
border-radius:10px;
cursor:pointer;
font-weight:800;
box-shadow:0 2px 10px rgba(0,0,0,.08);
}
button.secondary{ background:#111827; }
button:disabled{ opacity:.6; cursor:not-allowed; }
.card{
background:var(--card);
border:1px solid var(--line);
border-radius:14px;
overflow:hidden;
box-shadow:0 4px 18px rgba(0,0,0,.05);
}
.statusbar{
display:flex;
gap:12px;
align-items:center;
padding:12px 16px;
border-bottom:1px solid var(--line);
background:#fbfdff;
flex-wrap:wrap;
}
.badge{
display:inline-flex;
align-items:center;
gap:8px;
padding:7px 11px;
border-radius:999px;
border:1px solid var(--line);
font-size:13px;
color:var(--muted);
background:#fff;
font-weight:700;
}
.dot{ width:8px;height:8px;border-radius:999px;background:var(--primary); display:inline-block; }
table{ width:100%; border-collapse:collapse; }
th,td{
padding:14px 12px;
border-bottom:1px solid var(--line);
text-align:center;
font-size:14px;
}
th{
background:var(--primary);
color:#fff;
font-weight:900;
}
td:first-child, th:first-child{
text-align:left;
padding-left:16px;
font-weight:800;
}
tr:hover td{ background:#f9fafb; }
input[type="checkbox"]{
transform:scale(1.15);
cursor:pointer;
}
.msg{
padding:18px 16px;
color:var(--muted);
font-weight:700;
display:none;
}
.error{
display:none;
color:var(--danger);
background:var(--dangerBg);
border-top:1px solid var(--dangerLine);
padding:12px 16px;
white-space:pre-wrap;
font-weight:700;
}
</style>
</head>
<body>
<div class="top">
<h1>Gestión de Permisos por Rol</h1>
<div class="actions">
<button class="secondary" id="btnReload">Recargar</button>
<button onclick="window.location.href='https://cacg1022.github.io/ODONTOLOGIA/index.html'">Volver</button>
</div>
</div>
<div class="card">
<div class="statusbar">
<span class="badge"><span class="dot"></span> Usuario: <b id="uEmail">—</b></span>
<span class="badge">Rol: <b id="uRole">—</b></span>
<span class="badge">Tabla: <b>modulos_permisos</b></span>
</div>
<table>
<thead>
<tr>
<th>Módulo</th>
<th>Admin</th>
<th>Doctor</th>
<th>Auxiliar</th>
<th>Paciente</th>
</tr>
</thead>
<tbody id="tablaPermisos"></tbody>
</table>
<div id="emptyMsg" class="msg">
No se encontraron módulos en <b>modulos_permisos</b>.
</div>
<div id="errBox" class="error"></div>
</div>
<script type="module">
import { createClient } from "https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm";
// ✅ MISMAS credenciales que en tu index.html
const supabase = createClient(
"https://dmcfrmpqlkrqbpjhrfyo.supabase.co",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImRtY2ZybXBxbGtycWJwamhyZnlvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjI5MzU0MDIsImV4cCI6MjA3ODUxMTQwMn0.YY_6t_lCTs5mK-a_eEdBFa6l9_NBM3B4YDtWOXayCBE"
);
const LOGIN_URL = "https://cacg1022.github.io/ODONTOLOGIA/login.html";
const elEmail = document.getElementById("uEmail");
const elRole = document.getElementById("uRole");
const tbody = document.getElementById("tablaPermisos");
const emptyMsg= document.getElementById("emptyMsg");
const errBox = document.getElementById("errBox");
const btnReload = document.getElementById("btnReload");
function showError(msg){
errBox.style.display = "block";
errBox.textContent = msg;
console.error(msg);
}
function clearError(){
errBox.style.display = "none";
errBox.textContent = "";
}
async function validarAdminYMostrarUsuario(){
clearError();
const { data: { session }, error: eSession } = await supabase.auth.getSession();
if (eSession){
showError("Error getSession():\n" + JSON.stringify(eSession, null, 2));
return null;
}
if (!session){
window.location.href = LOGIN_URL;
return null;
}
elEmail.textContent = session.user?.email || session.user?.id || "Usuario autenticado";
const { data: perfil, error } = await supabase
.from("profiles")
.select("role")
.eq("id", session.user.id)
.single();
if (error){
showError(
"No pude leer tu rol en profiles.\n" +
"Si tienes RLS en profiles, revisa tu policy.\n\n" +
JSON.stringify(error, null, 2)
);
return null;
}
const rolActual = perfil?.role || "user";
elRole.textContent = rolActual;
if (rolActual !== "admin"){
alert("Acceso denegado: solo ADMIN.");
window.location.href = "https://cacg1022.github.io/ODONTOLOGIA/index.html";
return null;
}
return rolActual;
}
async function cargarPermisos(){
clearError();
tbody.innerHTML = "";
emptyMsg.style.display = "none";
// ✅ OJO: tus columnas son admin/doctor/auxiliar/paciente
const { data, error } = await supabase
.from("modulos_permisos")
.select("id, modulo, label, admin, doctor, auxiliar, paciente")
.order("label", { ascending: true });
if (error){
showError(
"Error leyendo modulos_permisos.\n\n" +
JSON.stringify(error, null, 2)
);
return;
}
if (!data || data.length === 0){
emptyMsg.style.display = "block";
return;
}
data.forEach(m => {
const tr = document.createElement("tr");
tr.innerHTML = `
<td>${m.label} <span style="color:#9ca3af;font-weight:700;">(${m.modulo})</span></td>
<td><input type="checkbox" ${m.admin ? "checked":""} data-id="${m.id}" data-col="admin"></td>
<td><input type="checkbox" ${m.doctor ? "checked":""} data-id="${m.id}" data-col="doctor"></td>
<td><input type="checkbox" ${m.auxiliar ? "checked":""} data-id="${m.id}" data-col="auxiliar"></td>
<td><input type="checkbox" ${m.paciente ? "checked":""} data-id="${m.id}" data-col="paciente"></td>
`;
tbody.appendChild(tr);
});
// Listener único para toda la tabla
tbody.querySelectorAll('input[type="checkbox"]').forEach(chk => {
chk.addEventListener("change", async (e) => {
const id = e.target.dataset.id;
const col = e.target.dataset.col;
const val = e.target.checked;
const { error: upErr } = await supabase
.from("modulos_permisos")
.update({ [col]: val })
.eq("id", id);
if (upErr){
showError(
"Error actualizando permiso.\n" +
"Si luego activas RLS en modulos_permisos, crea policy de UPDATE para admin.\n\n" +
JSON.stringify(upErr, null, 2)
);
}
});
});
}
async function init(){
const ok = await validarAdminYMostrarUsuario();
if (!ok) return;
await cargarPermisos();
}
btnReload.addEventListener("click", init);
// Iniciar
init();
</script>
</body>
</html>