Actualizar la asignación de permisos
Some checks are pending
Deploy Pruebas / deploy (push) Waiting to run

This commit is contained in:
Your Name
2024-09-02 09:58:39 -06:00
parent db3d0b5d39
commit 3886e3c55b

View File

@@ -1,16 +1,33 @@
<?php <?php
$ruta = "../"; $ruta = "../";
require_once "../include/bd_pdo.php"; require_once "../include/bd_pdo.php";
global $db; global $db, $pdo;
if(isset($_POST['lectura']))
$ver = $_POST['lectura']; try {
if(isset($_POST['editar'])) // Begin transaction
$editar = $_POST['editar']; $pdo->beginTransaction();
// Initialize arrays
$ver = $_POST['lectura'] ?? [];
$editar = $_POST['editar'] ?? [];
// Only proceed if $ver and $editar are not empty
if (empty($ver) && empty($editar)) {
throw new Exception("Both arrays are empty.");
}
$completo = [];
// Process $editar array
foreach ($editar as $edit) { foreach ($editar as $edit) {
$edit_separado = explode("_", $edit); $edit_separado = explode("_", $edit);
$completo[] = $edit_separado; $completo[] = $edit_separado;
} }
// Call the function to delete all permissions
$db->query("SELECT fd_permiso()"); $db->query("SELECT fd_permiso()");
// Process $ver array and merge with $completo
foreach ($ver as $lectura) { foreach ($ver as $lectura) {
$igual = false; $igual = false;
$ver_separado = explode("_", $lectura); $ver_separado = explode("_", $lectura);
@@ -20,17 +37,33 @@
break; break;
} }
} }
if(!$igual) if (!$igual) {
$completo[] = $ver_separado; $completo[] = $ver_separado;
} }
foreach($completo as $actual){ }
// Insert the combined data into the 'permiso' table
foreach ($completo as $actual) {
$db->insert('permiso', [ $db->insert('permiso', [
'pagina_id' => $actual['0'], 'pagina_id' => $actual[0],
'rol_id' => $actual['1'], 'rol_id' => $actual[1],
'permiso_tipo' => $actual['2'], 'permiso_tipo' => $actual[2],
]); ]);
} }
// Commit the transaction
$db->commit();
// Redirect to permisos.php
header("Location: ../permisos.php"); header("Location: ../permisos.php");
exit(); exit();
} catch (Exception $e) {
// Rollback the transaction on error
$pdo->rollback();
// Handle the error (for example, log it or show an error message)
echo "An error occurred: " . $e->getMessage();
exit();
}
?> ?>