Commit fa8a0822 authored by Celine Noirot's avatar Celine Noirot

purge work, bug when user hasn't right, always ok but files still exist

parent bb5f3953
......@@ -858,13 +858,29 @@ class tx_nG6_eid {
print json_encode(tx_nG6_db::filter_list_retention_data_info($filter_size,$max_retention_date,$create_users, $without_laboratories_id,$with_laboratories_id));
} elseif ($type == 'delete_purge_data') {
//TODO check if user is still logged
//TODO recuperer user password et faire comme pour pi1
if ($user_id == ""){
print json_encode("Error: reconnect");
} else {
//Delete data link to the list of purge demand
$purge_ids = trim(t3lib_div::_GP('purge_demand'));
print json_encode(tx_nG6_pi6::delete_purge_demand(explode(',',$purge_ids)));
$data_folder = trim(t3lib_div::_GP('data_folder'));
$user_login = trim(t3lib_div::_GP('user_login'));
$user_pwd = trim(t3lib_div::_GP('user_pwd'));
//EXPECT : delete_purge_demand($demand_ids,$user_login, $user_pwd, $data_folder)
// check login and password
$res = 0;
if (isset($user_login) && isset($user_pwd) && $user_login != 'undefined' && $user_pwd != 'undefined') {
$connection = ssh2_connect('127.0.0.1', 22);
if (!$connection) { $res = 3; }
if (!ssh2_auth_password($connection, $user_login, $user_pwd)) { $res=2;}
} else {
$res = 3;
}
if ($res == 0) {
$res = tx_nG6_pi6::delete_purge_demand($purge_ids,$user_login, $user_pwd, $data_folder);
}
print $res;
}
} elseif ($type == 'extend_rentention_date') {
//Extend the retention date for the
......
......@@ -366,12 +366,12 @@ class tx_nG6_db {
return $purge_demand_id;
}
function get_purge_demand_from_id($values){
function get_purge_demand_from_id($values_str){
#select all demand id not processed
$queryParts=array(
'SELECT' => ' uid as demand_id,project_id,purge_size,mail_sent_date,processed_date,demand_state,analyze_ids,run_ids',
'FROM' => 'tx_nG6_purge_demand',
'WHERE' => 'uid IN ('.implode(",",$values).')',
'WHERE' => 'uid IN ('.$values_str.')',
'GROUPBY' => '',
'ORDERBY' => '',
'LIMIT' => ''
......@@ -1928,6 +1928,46 @@ function get_project_runs($project_id, $orderby='', $limit='') {
return $max_date;
}
/**
* Purge an run from database project
*
* @param array $a_id the analyze ids
* @param string $user_login the user login
* @param string $user_pwd the user password
* @param string $data_folder the data folder
* @return 0=>everything ok, 1=>user right problem, 2=>wrong authentification, 3=>connection error
*/
function purge_run($r_id, $user_login, $user_pwd, $data_folder) {
$res = 0;
// First select the analyse
$my_run = tx_nG6_db::select_run($r_id);
$storage_size = $my_run['storage_size'];
$datas = array(
'purged_date' => time(),
'data_state' => "purged",
'purged_size' => $storage_size,
'storage_size' => 0,
);
$res = tx_nG6_utils::purge_directory($user_login, $user_pwd, $data_folder.$my_run['directory']);
if ($res == 0 ){ //delete file ok
if ( $my_run['data_state'] != "purged"){
$res_query = $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_nG6_run', 'uid='.$r_id, $datas );
if ($res_query == False){
$res = 5;
}
}
}else { //error delete file
$res = 4;
}
return $res;
}
/*
* Analysis functions
*------------------------------------------------------------*/
......@@ -2248,6 +2288,45 @@ function get_project_runs($project_id, $orderby='', $limit='') {
return $res;
}
/**
* Purge an analyze and results from database project
*
* @param array $a_id the analyze ids
* @param string $user_login the user login
* @param string $user_pwd the user password
* @param string $data_folder the data folder
* @return 0=>everything ok, 1=>user right problem, 2=>wrong authentification, 3=>connection error
*/
function purge_analysis($a_id, $user_login, $user_pwd, $data_folder) {
$res = 0;
// First select the analyse
$my_analysis = tx_nG6_db::select_analyse($a_id);
$storage_size = $my_analysis['storage_size'];
$datas = array(
'purged_date' => time(),
'data_state' => "purged",
'purged_size' => $storage_size,
'storage_size' => 0,
);
$res = tx_nG6_utils::purge_directory($user_login, $user_pwd, $data_folder.$my_analysis['directory']);
if ($res == 0 ){ //delete file ok
if ( $my_analysis['data_state'] != "purged"){
$res_query = $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_nG6_analyze', 'uid='.$a_id, $datas );
if ($res_query == False){
$res = 5;
}
}
}else { //error delete file
$res = 4;
}
return $res;
}
/*
......
......@@ -29,6 +29,8 @@
require_once(PATH_t3lib.'class.t3lib_div.php');
require_once(t3lib_extMgm::extPath('saltedpasswords').'/classes/salts/class.tx_saltedpasswords_salts_factory.php');
set_include_path(get_include_path() . '/phpseclib');
include('Net/SSH2.php');
class tx_nG6_utils {
......@@ -102,6 +104,7 @@ class tx_nG6_utils {
if ($directory != 'undefined' && $directory != '') {
// And process the directories structure
//TODO BUG NE RETOURNE PAS D'ERREUR si pas supprimer
if (!ssh2_exec($connection, 'rm -rf '.$directory."/")) {
return 1;
}
......@@ -109,6 +112,48 @@ class tx_nG6_utils {
return 0;
}
/**
* Purge directories
*
* @param string $user_login
* @param string $user_pwd
* @param string $directory
* @return 0=>everything ok, 1=>user right problem, 2=>wrong authentification, 3=>connection error
*/
function purge_directory($user_login, $user_pwd, $directory) {
// First try to connect the specified user using ssh
$connection = ssh2_connect('127.0.0.1', 22);
if (!$connection) return 3;
if (!ssh2_auth_password($connection, $user_login, $user_pwd)) return 2;
if ($directory != 'undefined' && $directory != '') {
// And process the directories structure, remove all files except .html ans .png
//TO DEBUG
/*
$stream = ssh2_exec($connection, 'ls '.$directory.'/* | grep -v -e ".html$" -e ".png$" | xargs rm -f');
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
error_log( stream_get_contents($stream_out) . $stream);
error_log( 'ls '.$directory.'/* | grep -v -e ".html$" -e ".png$" | xargs rm -f');
if ($stream === False) {
return 4;
}*//*
$ssh = new Net_SSH2('127.0.0.1');
if (!$ssh->login($user_login, $user_pwd)) {
return 2;
}
$val_return = $ssh->exec( 'ls '.$directory.'/* | grep -v -e ".html$" -e ".png$" | xargs rm -f');
if ($val_return != 0 ) {
return 4;
}*/
}
return 4;
}
/**
* Return the string representation of a byte
*
......
......@@ -23,6 +23,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<input type="hidden" id="data_folder" value="{$data_folder}" />
<input type="hidden" id="usergroup" value="{$group_info.uid}" />
<input type="hidden" id="server_url" value="{$server_url}" />
<input type="hidden" id="server_name" value="{$server_name}" />
<input type="hidden" id="purge_delay" value="{$ng6_purge_delay}" />
<div class="sub-content sc-top">
<div class="ng6-content-header-left administration">
......
......@@ -92,6 +92,7 @@ class tx_nG6_pi6 extends tslib_pibase {
$smarty->assign('group_list', $group_list);
$smarty->assign('data_folder', $this->conf["data"]);
$smarty->assign('server_url', $this->conf['server_url']);
$smarty->assign('server_name', $this->conf["server_name"]);
$distribution = tx_nG6_db::select_projects_repartition('create_user', 'title');
$smarty->assign('distribution', $distribution);
......@@ -224,40 +225,39 @@ Files impacted of:
return("Done extend");
}
function delete_purge_demand($demand_ids){
function delete_purge_demand($demand_ids,$user_login, $user_pwd, $data_folder){
$date= time();
$all_demands=tx_nG6_db::get_purge_demand_from_id($demand_ids);
$all_runs=array();
$all_analyses=array();
foreach($all_demands as $res){
if ($res["state"] != "deleted")
{
$all_analyses=array_merge($all_analyses,explode(',',$res["analyze_ids"]));
$all_runs=array_merge($all_runs,explode(',',$res["run_ids"]));
$directories=array();
$res = 0 ;
foreach($all_demands as $demand){
if ($demand["state"] != "deleted"){
foreach(explode(',',$demand["analyze_ids"]) as $a_id){
$res = tx_nG6_db::purge_analysis($a_id, $user_login, $user_pwd, $data_folder);
if ($res != 0) {
break;
}
}
if ($res == 0) {
foreach(explode(',',$demand["run_ids"]) as $r_id){
$res = tx_nG6_db::purge_run($r_id, $user_login, $user_pwd, $data_folder);
if ($res != 0) {
break;
}
}
}
else {
break;
}
}
}
//purged_size = storage_size !!!
//purge_files
//set purge size BUG
tx_nG6_db::update_field('tx_nG6_run', $all_runs, "purged_size", "`storage_size`");
//set store size
tx_nG6_db::update_field('tx_nG6_run', $all_runs, array("data_state", "purged_date", "storage_size"),
array("purged",$date, 0),
array("purged_size"));
tx_nG6_db::update_field('tx_nG6_analyze',$all_analyses, array("purged_size","data_state", "purged_date", "storage_size"),
array("storage_size","purged",$date, 0),
array("purged_size"));
tx_nG6_db::update_field('tx_nG6_purge_demand',$demand_ids,
# finally set purge demand as purged
if ($res == 0){
tx_nG6_db::update_field('tx_nG6_purge_demand',explode(',',$demand_ids),
array("processed_date","demand_state"),
array($date,"deleted"));
return("Done delete, res : ".$res);
//todo , est ce necessaire de controler si nb run supprimé == nb run en entrée
array($date,"purged"));
}
return $res;
}
}
......
......@@ -394,27 +394,73 @@ $(function () {
// Delete data after selecting list of ids demand
$("#delete_data_from_list").click(function(){
console.log("IIIIIIIIIIICCCCCCCCCCCCCCCCCCCC");
$("#modal-label-tmpl").html("Delete");
$("#modal-body-tmpl").html("Are you sure you want to delete those data?");
$("#modal-foot-tmpl").html('<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">No</button>' +
'<button id="modal-btn-yes" class="btn btn-primary">Yes</button>');
$("#ng6modal").modal();
var demands = [];
$("#modal-btn-yes").click( function() {
var del_auth_html = [
'<div class="tx-nG6-pi1-login">',
' This functionality is only available for ng6 unix user on the server. <br /> <br /> ',
' <form class="form-horizontal" id="tx-nG6-pi1-login-form" >',
' <div class="input-group" style="margin-left:50px">',
' <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>',
' <input class="form-control" style="width:200px" type="text" id="user_login_val" name="user_login_val" placeholder="Username"/>',
' </div><br/>',
' <div class="input-group" style="margin-left:50px">',
' <span class="input-group-addon"><i class="glyphicon glyphicon-wrench"></i></span>',
' <input class="form-control" style="width:200px" type="password" id="user_pwd_val" name="user_pwd_val" placeholder="Password"/>',
' </div>',
' </form>',
'</div>'
].join('');
$("#modal-label-tmpl").html("Purge");
$("#modal-body-tmpl").html(del_auth_html);
$("#modal-foot-tmpl").html('<button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="glyphicon glyphicon-remove"></i> Close</button>' +
'<button id="modal-btn-purge" class="btn btn-primary"><i class="glyphicon glyphicon-trash"></i> Delete</button>');
$("#ng6modal").modal();
var demands = [];
$("#modal-btn-purge").click( function() {
$(':checked[id^=chk_demand').each(function(){
demand_id=$(this).val();
demands.push(demand_id);
console.log("chk : " + demand_id);
});
var val_url = "index.php?eID=tx_nG6&type=delete_purge_data&purge_demand=" + demands.join(',');
val_url += "&user_login=" + $("#user_login_val").val() + "&user_pwd=" + $("#user_pwd_val").val();
val_url += "&user_id="+$("#user_id").val() +"&data_folder=" + $("#data_folder").val() ;
$("#modal-label-tmpl").html("Purge");
$("#modal-body-tmpl").html('<div class="tx-nG6-wait">Purging, please wait...</div>');
$("#modal-foot-tmpl").html('<button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="glyphicon glyphicon-remove"></i> Close</button>' +
'<button id="modal-btn-delete" class="btn btn-primary disabled"><i class="glyphicon glyphicon-trash"></i> Delete</button>');
$("#ng6modal").modal();
$.ajax({
url: val_url,
success: function(val, status, xhr) {
// return code : 0 = ok, 1 = right error, 2 = authentication error, 3 = connexion error, 4 = error delete file , 5 error sql update
if (val == "0") {
// Uncheck all checkboxes
$(".tx-nG6-wait").hide();
$("#modal-body-tmpl").html('Done !');
$("#ng6modal").modal();
refresh_purge_demand(purge_table["demand"]);
} else {
$("#modal-label-tmpl").html("Error");
if (val == "1") {
$("#modal-body-tmpl").html("<div class='tx-nG6-pi6-error'>You don't have write permission on the specified directory.</div>");
} else if (val == "2") {
$("#modal-body-tmpl").html("<div class='tx-nG6-pi6-error'>An error occurred during login. Most likely you didn't enter the username or password correctly. Be certain that you enter them precisely as they are, including upper/lower case.</div>");
} else if (val == "3") {
$("#modal-body-tmpl").html("<div class='tx-nG6-pi6-error'>An error occurred during server connection. Please contact the site administrator.</div>");
} else if (val == "4") {
$("#modal-body-tmpl").html("<div class='tx-nG6-pi6-error'>An error occurred during file deletion check if user have unix rigth to remove files. Please contact the site administrator.</div>");
} else if (val == "5") {
$("#modal-body-tmpl").html("<div class='tx-nG6-pi6-error'>An error occurred while updating in db. Please contact the site administrator.</div>");
}
$("#modal-foot-tmpl").html('<button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="glyphicon glyphicon-remove"></i> Close</button>');
$("#ng6modal").modal();
}
},
error: function(val, status, xhr) {
$("#modal-body-tmpl").html("<div class='tx-nG6-pi6-error'>An error occurred during file deletion. Please contact the site administrator.</div>");
}
});
url: "index.php?eID=tx_nG6&type=delete_purge_data&purge_demand=" + demands.join(',') + "&user_id="+$('input[id=user_id]').val(),
success: function (data) {
console.log ("DONE");
console.log (data);
}
});
});
});
......@@ -505,7 +551,7 @@ function refresh_purge_demand(purge_demand_datatable){
if (values["delay_excedeed"]==1){
row.push("yes");
}else{
} else {
row.push("no");
}
oTable.row.add(row );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment