Skip to content

Commit

Permalink
Update classes
Browse files Browse the repository at this point in the history
  • Loading branch information
charleneauger committed May 5, 2023
1 parent 556cbb5 commit 79e665e
Show file tree
Hide file tree
Showing 4 changed files with 457 additions and 336 deletions.
228 changes: 149 additions & 79 deletions inc/action.class.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
<?php
/**
* @package gdprcompliance
* @author Rudy Laurent
* @copyright Copyright (c) 2015-2019 FactorFX
* @license AGPL License 3.0 or (at your option) any later version
* http://www.gnu.org/licenses/agpl-3.0-standalone.html
* @link https://www.factorfx.com
* @since 2019
* ---------------------------------------------------------------------
* ITSM-NG
* Copyright (C) 2022 ITSM-NG and contributors.
*
* --------------------------------------------------------------------------
* https://www.itsm-ng.org
*
* based on GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2003-2014 by the INDEPNET Development Team.
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of ITSM-NG.
*
* ITSM-NG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* ITSM-NG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ITSM-NG. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
*/

class PluginGdprcomplianceAction extends CommonDBTM
{
/**
/**
* @param int $nb
*
* @return translated
Expand Down Expand Up @@ -43,78 +62,84 @@ static function install($migration) {
* @return arrray of information
**/
static function cronInfo($name) {

switch ($name) {
case 'CleanUsers' :
return array('description' => __('Clean inactive Users', 'gdprcompliance'));
switch ($name) {
case 'CleanUsers' :
return array('description' => __('Clean inactive Users', 'gdprcompliance'));
}
return [];
}
return [];
}


/**
* cronCleanUsers
*
* @param mixed $task
* @return void
*/
static function cronCleanUsers($task){
global $DB;

$cronTask = new CronTask();

if ($cronTask->getFromDBbyName("PluginGdprcomplianceAction", "CleanUsers")) {
if ($cronTask->fields["state"] == CronTask::STATE_DISABLE) {
return 0;
}
if ($cronTask->fields["state"] == CronTask::STATE_DISABLE) {
return 0;
}
} else {
return 0;
return 0;
}

$cron_status = 0;

$query = "SELECT * FROM glpi_plugin_gdprcompliance_configs";
$result_glpi = $DB->query($query);
$config = [];

if ($DB->numrows($result_glpi) > 0) {
$i = 0;
while ($data = $DB->fetchArray($result_glpi)) {
$config['active'] = $data['active'];
$config['mode'] = $data['mode'];
if (!is_null($data['change'])) {
$changes = explode(',', $data['change']);
}
foreach ($changes as $key => $change) {
if ($change == "") {
continue;
}
$test = explode(';', $change);
if (count($test) > 1) {
$config['changes'][$test[0]]['value'] = 2;
$config['changes'][$test[0]]['change'] = $test[1];
} else {
$config['changes'][$change]['value'] = 1;
$config['changes'][$change]['change'] = "";
}
}
$i++;
}
while ($data = $DB->fetchArray($result_glpi)) {
$config['active'] = $data['active'];
$config['mode'] = $data['mode'];

$changes = [];

if (!is_null($data['change'])) $changes = json_decode($data['change']);

foreach ($changes as $key => $change) {
if($change == 999) {
$config['changes'][$key]['value'] = 999;
} elseif($change == 1) {
$config['changes'][$key]['value'] = 1;
} else {
$config['changes'][$key]['value'] = 2;
$config['changes'][$key]['change'] = $change;
}
}
}
}

$listUsers = [];

if ($config['active']) {
$User = new User();

$query = "SELECT * FROM glpi_users";
$users_result = $DB->query($query);
$users = $User->find(["is_active" => 0, "is_deleted" => 0]);

while ($user = $DB->fetchArray($users_result)) {
if (!$user['is_active'] && !$user['is_deleted']) {
$listUsers[] = self::changeUserData($user, $config['changes']);
}
foreach($users as $id => $values) {
$listUsers[] = self::changeUserData($values, $config['changes']);
}
}

$sucess = 0;
$errors = 0;

foreach ($listUsers as $key => $user) {
try {
if (!$config['mode']) {
self::cleanUsers($user);
self::cleanUsers($user["user"]);
self::cleanUserEmail($user["mail"]);
} else {
self::removeUsers($user);
self::removeUsers($user["user"]);
}

$sucess += 1;
} catch(Exception $e){
$errors += 1;
Expand All @@ -126,51 +151,96 @@ static function cronCleanUsers($task){

return $cron_status;
}

/**
* changeUserData
*
* @param mixed $user
* @param mixed $changes
* @return void
*/
static private function changeUserData($user, $changes = []) {
$usersToUpdate['user'] = $user;
$usersToUpdate['mail'] = [];

static private function changeUserData($user, $changes = []){
$exclude = ['id', 'name' => 1, 'locations_id' => 1,'use_mode' => 1,'is_active' => 1,'auths_id' => 1,'authtype' => 1,'is_deleted' => 1,'profiles_id' => 1,'entities_id' => 1,'usertitles_id' => 1,'usercategories_id' => 1,'is_deleted_ldap' => 1,'groups_id' => 1,'users_id_supervisor' => 1,'password' => 1];
foreach ($user as $key => $value) {

if (is_int($key)) {
unset($user[$key]);
continue;
}

if (array_key_exists($key, $changes)) {
if ($changes[$key]['value'] == 1) {
# code...
} elseif ($changes[$key]['value'] == 2) {
$user[$key] = $changes[$key]['change'];
}
} else {
if (in_array($key, $exclude)) {
if ($key != 'id') {
unset($user[$key]);
}
} else {
$user[$key] = "";
if ($changes[$key]['value'] == 2) {
$usersToUpdate['user'][$key] = $changes[$key]['change'];
} elseif($changes[$key]['value'] == 999) {
$usersToUpdate['user'][$key] = null;
}
}
}

return $user;
// User email process
if($changes["email"]["value"] == 2) {
$usersToUpdate["mail"]["update"] = [
"users_id" => $user["id"],
"email" => $changes["email"]["change"]
];
} elseif($changes["email"]["value"] == 999) {
$usersToUpdate["mail"]["delete"] = [
"users_id" => $user["id"]
];
}

return $usersToUpdate;
}


/**
* cleanUsers
*
* @param mixed $user
* @return void
*/
static private function cleanUsers($user){
global $DB;

$nUser = new User();
$user['is_deleted'] = true;

$nUser->update($user);
}


/**
* cleanUserEmail
*
* @param mixed $userEmail
* @return void
*/
static function cleanUserEmail($userEmail) {
$UserEmail = new UserEmail();

if(isset($userEmail["update"])) {
$userEmails = $UserEmail->find(["users_id" => $userEmail["update"]["users_id"]]);
// increment and concat to email for field unicity
$index = 0;

foreach($userEmails as $id => $mail) {
$mail["email"] = $userEmail["update"]["email"].$index;
$UserEmail->update($mail);
$index++;
}
} elseif($userEmail["delete"]) {
$userEmails = $UserEmail->find(["users_id" => $userEmail["delete"]["users_id"]]);
foreach($userEmails as $id => $mail) {
$mail["email"] = $userEmail["update"]["email"];
$UserEmail->delete($mail);
}
}
}

/**
* removeUsers
*
* @param mixed $user
* @return void
*/
static private function removeUsers($user){
global $DB;

$query = "DELETE FROM glpi_users WHERE id = " . $user['id'];
$nUser = new User();
$userToPurge = [
"id" => $user["id"]
];

$DB->query($query);
$nUser->delete($userToPurge, 1);
}
}

0 comments on commit 79e665e

Please sign in to comment.