Skip to content

Commit

Permalink
Merge pull request #98 from mrysav/delete-lab-config
Browse files Browse the repository at this point in the history
LabConfig V2 UI updates: delete lab, apply pending migrations
  • Loading branch information
mrysav authored Sep 27, 2024
2 parents 1c8ada3 + 851fd66 commit 81ee1b6
Show file tree
Hide file tree
Showing 19 changed files with 282 additions and 65 deletions.
4 changes: 4 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"8000": {
"label": "mkdocs",
"onAutoForward": "notify"
},
"8080": {
"label": "phpmyadmin",
"onAutoForward": "notify"
}
},
"forwardPorts": [
Expand Down
8 changes: 8 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ services:
environment:
MYSQL_ROOT_PASSWORD: blis123

phpmyadmin:
image: phpmyadmin
restart: unless-stopped
ports:
- 8080:80
environment:
PMA_HOST: db

volumes:
blis-data:
2 changes: 1 addition & 1 deletion htdocs/config/v2/delete_backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

$backup->destroy();

$_SESSION["BACKUP_FLASH"] = "Backup deleted successfully.";
$_SESSION["FLASH"] = "Backup deleted successfully.";

header("Location: lab_config_backups.php?id=$lab_config_id");

Expand Down
131 changes: 131 additions & 0 deletions htdocs/config/v2/delete_lab_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
#
# (c) C4G, Santosh Vempala, Ruban Monu and Amol Shintre
# Download a lab backup, with access control.
#

require_once(__DIR__."/../../users/accesslist.php");
require_once(__DIR__."/../../includes/db_lib.php");
require_once(__DIR__."/../../includes/platform_lib.php");
require_once(__DIR__."/../../includes/user_lib.php");

$current_user_id = $_SESSION['user_id'];
$current_user = get_user_by_id($current_user_id);

if (!isset($_REQUEST["lab_config_id"])) {
$_SESSION["FLASH"] = "Error deleting lab: no lab specified.";
header("Location: /lab_config_index.php");
exit;
}

$lab_config_id = $_REQUEST['lab_config_id'];
$lab_db_name_query = "SELECT lab_config_id, name, db_name FROM lab_config WHERE lab_config_id = '$lab_config_id';";
$lab = query_associative_one($lab_db_name_query);
$lab_config_name = $lab["name"];

$unauthorized = true;

// You can only delete a lab if you are a superadmin or country director
if (is_super_admin($current_user) || is_country_dir($current_user)) {
$unauthorized = false;
}

if ($unauthorized) {
header('HTTP/1.1 401 Unauthorized', true, 401);
header("Location: /home.php");
exit;
}

$confirmed = isset($_GET["confirm"]) && $_GET["confirm"] == "true";

global $log;

$err = false;
if ($confirmed) {
$log->info("Deleting lab [ID $lab_config_id] database: " . $lab["db_name"]);

try {
// in db_lib.php
delete_lab_config($lab_config_id);
} catch (Exception $e) {
$log->error("Could not delete database: ". $e);
$err = true;
}

try {
$langdata_dir = realpath("../../../local/langdata_$lab_config_id");
if ($langdata_dir) {
$log->info("Deleting folder: $langdata_dir");
PlatformLib::removeDirectory($langdata_dir);
} else {
$log->info("Lab [ID $lab_config_id] does not have a langdata folder, so skipping delete.");
}
} catch (Exception $e) {
$log->error("Could not delete langdata folder: ". $e);
$err = true;
}

if (!$err) {
$_SESSION["FLASH"] = "Lab deleted successfully.";
} else {
$_SESSION["FLASH"] = "There was a problem deleting the lab. Please see the logs for details.";
}

header("Location: lab_config_index.php");
exit;
}

require_once(__DIR__."/../../includes/header.php");
LangUtil::setPageId("lab_config_home");

?>

<style type="text/css">
.section {
padding: 0.5rem 1rem;
}

.section-head {
margin-top: 0rem;
}

form input[type="submit"] {
float: right;
font-weight: bold;
padding: 5px 10px;
color: red;
}

.cancel-ok-buttons {
margin: 0 auto;
max-width: 400px;
}

.cancel {
font-weight: bold;
}
</style>

<div id="confirm-delete-lab" class="section">
<h3 class="section-head">Delete Lab</h3>

<div class="text-center">
<p>Are you sure you want to delete <b><?php echo($lab_config_name); ?></b>?</p>

<p><b>This cannot be undone.</b> ALL data about this lab will be deleted forever!</p>

<div class="cancel-ok-buttons">
<form action="delete_lab_config.php" method="GET">
<a class="cancel" href="lab_config_index.php">Cancel</a>

<input type="hidden" name="lab_config_id" value="<?php echo($lab_config_id);?>" />
<input type="hidden" name="confirm" value="true" />
<input type="submit" value="Delete" />
</form>
</div>
</div>
</div>

<?php
require_once(__DIR__."/../../includes/footer.php");
?>
20 changes: 4 additions & 16 deletions htdocs/config/v2/lab_config_backup_header.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,14 @@ function render_tab($tab_name, $tab_link, $tab_label) {
margin: 0.5rem 1rem;
}

.backup-flash {
background-color: lightgreen;
margin: 0.5rem;
padding: 1rem;
font-size: large;
}

a.delete-backup {
color: red;
}

#pending-migrations {
border: 1px solid darkblue;
background-color: lightblue;
}
</style>

<div class="tab-bar">
Expand All @@ -93,12 +90,3 @@ function render_tab($tab_name, $tab_link, $tab_label) {
| <?php render_tab("settings", "lab_config_backup_settings.php?id=$lab_config_id", "Settings"); ?>
| <?php render_tab("upload", "lab_config_backup_upload.php?id=$lab_config_id", "Upload Backup"); ?>
</div>

<?php
# This is used for rendering ephemeral messages on this page.
# To use, set $_SESSION['BACKUP_FLASH'] on another page and then redirect to this one.
if (isset($_SESSION['BACKUP_FLASH']) && $_SESSION['BACKUP_FLASH'] != '') {
echo "<div class=\"backup-flash\">".$_SESSION['BACKUP_FLASH']."</div>";
$_SESSION['BACKUP_FLASH'] = '';
}
?>
6 changes: 3 additions & 3 deletions htdocs/config/v2/lab_config_backup_restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@

if ($restore_successful) {
if ($migrations_successful) {
$_SESSION["BACKUP_FLASH"] = "Backup restored successfully.";
$_SESSION["FLASH"] = "Backup restored successfully.";
} else {
$_SESSION["BACKUP_FLASH"] = "Backup database was restored, "
$_SESSION["FLASH"] = "Backup database was restored, "
. "but could not be migrated to the new BLIS version. "
. "Please check the logs for details.";
}
} else {
$_SESSION["BACKUP_FLASH"] = "Failed to restore backup.";
$_SESSION["FLASH"] = "Failed to restore backup.";
}

header("Location: lab_config_backups.php?id=$lab_config_id");
Expand Down
2 changes: 1 addition & 1 deletion htdocs/config/v2/lab_config_backup_upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
$perm_tmp_path = "/tmp/blis/" . basename($tmp_path);

if (!move_uploaded_file($tmp_path, $perm_tmp_path)) {
$_SESSION["BACKUP_FLASH"] = "Failed to upload $filename.";
$_SESSION["FLASH"] = "Failed to upload $filename.";
header("Location: lab_config_backup_upload.php?id=$lab_config_id");
return;
}
Expand Down
14 changes: 14 additions & 0 deletions htdocs/config/v2/lab_config_backups.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#

require_once(__DIR__."/../../users/accesslist.php");
require_once(__DIR__."/../../includes/migrations.php");
require_once(__DIR__."/../../includes/user_lib.php");
require_once(__DIR__."/lib/backup.php");

Expand Down Expand Up @@ -52,6 +53,9 @@
// TODO: switch this to its own table, maybe...
$settings_encryption_enabled = KeyMgmt::read_enc_setting() != "0";

$migrator = new LabDatabaseMigrator($lab['db_name']);
$has_pending_migrations = $migrator->pending_migrations();

?>

<?php
Expand All @@ -69,6 +73,16 @@
});
</script>

<?php
if ($has_pending_migrations) {
?>
<div class="section" id="pending-migrations">
There are database migrations pending. Please <a href="lab_config_backups_apply_migrations.php?id=<?php echo($lab_config_id);?>">click here</a> to apply them.
</div>
<?php
}
?>

<div id="create-backup" class="section">
<h3 class="section-head">Create New Backup</h3>

Expand Down
55 changes: 55 additions & 0 deletions htdocs/config/v2/lab_config_backups_apply_migrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
#
# (c) C4G, Santosh Vempala, Ruban Monu and Amol Shintre
# Lists currently accessible lab configurations with options to modify/add
# Check whether to redirect to lab configuration page
# Called when the lab admin has only one lab under him/her
#

require_once(__DIR__."/../../users/accesslist.php");
require_once(__DIR__."/../../includes/migrations.php");
require_once(__DIR__."/../../includes/user_lib.php");

$current_user_id = $_SESSION['user_id'];
$current_user = get_user_by_id($current_user_id);
$lab_config_id = $_REQUEST['id'];

DbUtil::switchToGlobal();

$lab_db_name_query = "SELECT lab_config_id, name, db_name FROM lab_config WHERE lab_config_id = '$lab_config_id';";
$lab = query_associative_one($lab_db_name_query);
db_change($lab['db_name']);

$lab_config_name = $lab["name"];

$unauthorized = true;

if (is_super_admin($current_user) || is_country_dir($current_user)) {
$unauthorized = false;
}

if ($unauthorized) {
// If the user is not a super admin or country director, they should only
// be able to access data for their own lab, and only if they are an admin.
if ($lab_config_id == $current_user->labConfigId && is_admin($current_user)) {
$unauthorized = false;
}
}

if ($unauthorized) {
header('HTTP/1.1 401 Unauthorized', true, 401);
header("Location: /home.php");
exit;
}

$migrator = new LabDatabaseMigrator($lab['db_name']);
$migrations_successful = $migrator->apply_migrations();

if ($migrations_successful) {
$_SESSION["FLASH"] = "Migrations applied successfully.";
} else {
$_SESSION["FLASH"] = "There were errors while applying migrations.";
}

header("Location: lab_config_backups.php?id=$lab_config_id");
exit;
30 changes: 19 additions & 11 deletions htdocs/config/v2/lab_config_index.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
border-bottom: 1px solid #555;
}

a.delete {
color: red;
}

</style>

<div class="tab-bar">
Expand Down Expand Up @@ -75,6 +79,8 @@
<?php echo LangUtil::$generalTerms['LAB_MGR']; ?>
</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
Expand All @@ -98,17 +104,19 @@
<?php echo get_username_by_id($lab_config->adminUserId); ?>
</td>
<td>
<div>
<a href="/config/v2/lab_config_backups.php?id=<?php echo $lab_config->id; ?>">
<?php echo "Manage Lab Backups"; ?>
</a>
</div>
<hr>
<div>
<a href="/exportLabConfiguration.php?id=<?php echo $lab_config->id; ?>">
<?php echo "Export Lab Configuration"; ?>
</a>
</div>
<a href="/config/v2/lab_config_backups.php?id=<?php echo $lab_config->id; ?>">
<?php echo "Manage Backups"; ?>
</a>
</td>
<td>
<a href="/exportLabConfiguration.php?id=<?php echo $lab_config->id; ?>">
<?php echo "Export Configuration"; ?>
</a>
</td>
<td>
<a class="delete" href="/config/v2/delete_lab_config.php?lab_config_id=<?php echo $lab_config->id; ?>">
<?php echo "Delete Lab"; ?>
</a>
</td>
</tr>
<?php
Expand Down
2 changes: 1 addition & 1 deletion htdocs/config/v2/update_backup_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
KeyMgmt::write_enc_setting(0);
}

$_SESSION['BACKUP_FLASH'] = "Settings updated successfully.";
$_SESSION['FLASH'] = "Settings updated successfully.";

header("Location: lab_config_backup_settings.php?id=$lab_config_id");
6 changes: 3 additions & 3 deletions htdocs/config/v2/upload_backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
$newpath = __DIR__."/../../../files/$relpath";

if (!rename($uploaded_tmp_path, $newpath)) {
$_SESSION["BACKUP_FLASH"] = "Failed to upload $uploaded_filename.";
$_SESSION["FLASH"] = "Failed to upload $uploaded_filename.";
header("Location: $lab_config_backups_path");
return;
}
Expand All @@ -60,9 +60,9 @@
DbUtil::switchToLabConfig($lab_config_id);
Backup::insert($lab_config_id, $uploaded_filename, $relpath);

$_SESSION["BACKUP_FLASH"] = "Successfully uploaded $uploaded_filename";
$_SESSION["FLASH"] = "Successfully uploaded $uploaded_filename";
} catch (Exception $e) {
$_SESSION["BACKUP_FLASH"] = "Failed to upload $uploaded_filename:" . $e->getMessage();
$_SESSION["FLASH"] = "Failed to upload $uploaded_filename:" . $e->getMessage();
}

header("Location: $lab_config_backups_path");
Expand Down
Loading

0 comments on commit 81ee1b6

Please sign in to comment.