NEW Add a button to re-encrypt data of a dolcrypt extrafield password

This commit is contained in:
Laurent Destailleur 2023-09-19 16:07:45 +02:00
parent c735e6e75f
commit 4fa60709d5
4 changed files with 118 additions and 6 deletions

View file

@ -414,3 +414,63 @@ if ($action == 'delete') {
$mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
}
}
// Recrypt data password
if ($action == 'encrypt') {
// Load $extrafields->attributes
$extrafields->fetch_name_optionals_label($elementtype);
$attributekey = GETPOST('attrname', 'aZ09');
if (!empty($extrafields->attributes[$elementtype]['type'][$attributekey]) && $extrafields->attributes[$elementtype]['type'][$attributekey] == 'password') {
if (!empty($extrafields->attributes[$elementtype]['param'][$attributekey]['options'])) {
if (array_key_exists('dolcrypt', $extrafields->attributes[$elementtype]['param'][$attributekey]['options'])) {
// We can encrypt data with dolCrypt()
$arrayofelement = getElementProperties($elementtype);
if (!empty($arrayofelement['table_element'])) {
if ($extrafields->attributes[$elementtype]['entityid'][$attributekey] == $conf->entity || empty($extrafields->attributes[$elementtype]['entityid'][$attributekey])) {
dol_syslog("Loop on each extafields of table ".$arrayofelement['table_element']);
$sql .= "SELECT te.rowid, te.".$attributekey;
$sql .= " FROM ".MAIN_DB_PREFIX.$arrayofelement['table_element']." as t, ".MAIN_DB_PREFIX.$arrayofelement['table_element'].'_extrafields as te';
$sql .= " WHERE te.fk_object = t.rowid";
$sql .= " AND te.".$attributekey." NOT LIKE 'dolcrypt:%'";
$sql .= " AND te.".$attributekey." IS NOT NULL";
$sql .= " AND te.".$attributekey." <> ''";
if ($extrafields->attributes[$elementtype]['entityid'][$attributekey] == $conf->entity) {
$sql .= " AND t.entity = ".getEntity($arrayofelement['table_element'], 0);
}
//print $sql;
$resql = $db->query($sql);
if ($resql) {
$num_rows = $db->num_rows($resql);
$i=0;
while ($i < $num_rows) {
$objtmp = $db->fetch_object($resql);
$id = $objtmp->rowid;
$pass = $objtmp->password;
if ($pass) {
$newpassword = dolEncrypt($pass);
$sqlupdate = "UPDATE ".MAIN_DB_PREFIX.$arrayofelement['table_element'].'_extrafields';
$sqlupdate .= " SET ".$attributekey." = '".$db->escape($newpassword)."'";
$sqlupdate .= " WHERE rowid = ".((int) $id);
$db->query($sqlupdate);
}
$i++;
}
}
if ($num_rows > 0) {
setEventMessages($langs->trans("PasswordFieldEncrypted", $num_rows), null, 'mesgs');
} else {
setEventMessages($langs->trans("PasswordFieldEncrypted", $num_rows), null, 'warnings');
}
}
}
}
}
}
}

View file

@ -9427,6 +9427,7 @@ function dol_eval($s, $returnvalue = 0, $hideerrors = 1, $onlysimplestring = '1'
*
* @param string $element Variable to check
* @return boolean Return true of variable is not empty
* @see getElementProperties()
*/
function dol_validElement($element)
{
@ -11532,6 +11533,7 @@ function getElementProperties($element_type)
$module = $element_type;
$element = $element_type;
$subelement = $element_type;
$table_element = $element_type;
// If we ask a resource form external module (instead of default path)
if (preg_match('/^([^@]+)@([^@]+)$/i', $element_type, $regs)) { // 'myobject@mymodule'
@ -11551,15 +11553,18 @@ function getElementProperties($element_type)
$classpath = 'comm/action/class';
$subelement = 'Actioncomm';
$module = 'agenda';
$table_element = 'actioncomm';
} elseif ($element_type == 'cronjob') {
$classpath = 'cron/class';
$module = 'cron';
$table_element = 'cron';
} elseif ($element_type == 'adherent_type') {
$classpath = 'adherents/class';
$classfile = 'adherent_type';
$module = 'adherent';
$subelement = 'adherent_type';
$classname = 'AdherentType';
$table_element = 'adherent_type';
} elseif ($element_type == 'bank_account') {
$classpath = 'compta/bank/class';
$module = 'bank'; // We need $conf->bank->dir_output and not $conf->banque->dir_output
@ -11569,37 +11574,46 @@ function getElementProperties($element_type)
$classpath = 'categories/class';
$module = 'categorie';
$subelement = 'categorie';
$table_element = 'categorie';
} elseif ($element_type == 'contact') {
$classpath = 'contact/class';
$classfile = 'contact';
$module = 'societe';
$subelement = 'contact';
$table_element = 'socpeople';
} elseif ($element_type == 'stock') {
$classpath = 'product/stock/class';
$classfile = 'entrepot';
$classname = 'Entrepot';
$table_element = 'entrepot';
} elseif ($element_type == 'project') {
$classpath = 'projet/class';
$module = 'projet';
$table_element = 'projet';
} elseif ($element_type == 'project_task') {
$classpath = 'projet/class';
$module = 'projet';
$subelement = 'task';
$table_element = 'projet_task';
} elseif ($element_type == 'facture' || $element_type == 'invoice') {
$classpath = 'compta/facture/class';
$module = 'facture';
$subelement = 'facture';
$table_element = 'facture';
} elseif ($element_type == 'commande' || $element_type == 'order') {
$classpath = 'commande/class';
$module = 'commande';
$subelement = 'commande';
$table_element = 'commande';
} elseif ($element_type == 'propal') {
$classpath = 'comm/propal/class';
$table_element = 'propal';
} elseif ($element_type == 'shipping') {
$classpath = 'expedition/class';
$classfile = 'expedition';
$classname = 'Expedition';
$module = 'expedition';
$table_element = 'expedition';
} elseif ($element_type == 'supplier_proposal') {
$classpath = 'supplier_proposal/class';
$module = 'supplier_proposal';
@ -11618,6 +11632,7 @@ function getElementProperties($element_type)
$classpath = 'contrat/class';
$module = 'contrat';
$subelement = 'contrat';
$table_element = 'contract';
} elseif ($element_type == 'mailing') {
$classpath = 'comm/mailing/class';
$module = 'mailing';
@ -11628,6 +11643,7 @@ function getElementProperties($element_type)
$classpath = 'adherents/class';
$module = 'adherent';
$subelement = 'adherent';
$table_element = 'adherent';
} elseif ($element_type == 'usergroup') {
$classpath = 'user/class';
$module = 'user';
@ -11637,18 +11653,22 @@ function getElementProperties($element_type)
$classname = 'Mo';
$module = 'mrp';
$subelement = '';
$table_element = 'mrp_mo';
} elseif ($element_type == 'cabinetmed_cons') {
$classpath = 'cabinetmed/class';
$module = 'cabinetmed';
$subelement = 'cabinetmedcons';
$table_element = 'cabinetmedcons';
} elseif ($element_type == 'fichinter') {
$classpath = 'fichinter/class';
$module = 'ficheinter';
$subelement = 'fichinter';
$table_element = 'fichinter';
} elseif ($element_type == 'dolresource' || $element_type == 'resource') {
$classpath = 'resource/class';
$module = 'resource';
$subelement = 'dolresource';
$table_element = 'resource';
} elseif ($element_type == 'propaldet') {
$classpath = 'comm/propal/class';
$module = 'propal';
@ -11664,6 +11684,7 @@ function getElementProperties($element_type)
$element = 'order_supplier';
$subelement = '';
$classname = 'CommandeFournisseur';
$table_element = 'commande_fournisseur';
} elseif ($element_type == 'invoice_supplier') {
$classpath = 'fourn/class';
$module = 'fournisseur';
@ -11671,9 +11692,11 @@ function getElementProperties($element_type)
$element = 'invoice_supplier';
$subelement = '';
$classname = 'FactureFournisseur';
$table_element = 'facture_fourn';
} elseif ($element_type == "service") {
$classpath = 'product/class';
$subelement = 'product';
$table_element = 'product';
} elseif ($element_type == 'salary') {
$classpath = 'salaries/class';
$module = 'salaries';
@ -11684,12 +11707,14 @@ function getElementProperties($element_type)
$classname = 'Productlot';
$element = 'productlot';
$subelement = '';
$table_element = 'product_lot';
} elseif ($element_type == 'websitepage') {
$classpath = 'website/class';
$classfile = 'websitepage';
$classname = 'Websitepage';
$module = 'website';
$subelement = 'websitepage';
$table_element = 'website_page';
} elseif ($element_type == 'fiscalyear') {
$classpath = 'core/class';
$module = 'accounting';
@ -11697,10 +11722,12 @@ function getElementProperties($element_type)
} elseif ($element_type == 'chargesociales') {
$classpath = 'compta/sociales/class';
$module = 'tax';
$table_element = 'chargesociales';
} elseif ($element_type == 'tva') {
$classpath = 'compta/tva/class';
$module = 'tax';
$subdir = '/vat';
$table_element = 'tva';
}
if (empty($classfile)) {
@ -11737,6 +11764,7 @@ function getElementProperties($element_type)
$element_properties = array(
'module' => $module,
'element' => $element,
'table_element' => $table_element,
'subelement' => $subelement,
'classpath' => $classpath,
'classfile' => $classfile,
@ -11754,6 +11782,7 @@ function getElementProperties($element_type)
* @param string $element_type Element type ('module' or 'myobject@mymodule' or 'mymodule_myobject')
* @param string $element_ref Element ref (Use this or element_id but not both)
* @return int|object object || 0 || <0 if error
* @see getElementProperties()
*/
function fetchObjectByElement($element_id, $element_type, $element_ref = '')
{

View file

@ -59,6 +59,9 @@ print '<div class="div-table-responsive">';
print '<table summary="listofattributes" class="noborder centpercent small">';
print '<tr class="liste_titre">';
if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
print '<td width="80">&nbsp;</td>';
}
print '<td class="left">'.$langs->trans("Position");
print '<span class="nowrap">';
print img_picto('A-Z', '1downarrow.png');
@ -82,7 +85,10 @@ print '<td class="center">'.$form->textwithpicto($langs->trans("CssOnList"), $la
if (isModEnabled('multicompany')) {
print '<td class="center">'.$langs->trans("Entity").'</td>';
}
print '<td width="80">&nbsp;</td>';
// Action column
if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
print '<td width="80">&nbsp;</td>';
}
print "</tr>\n";
if (isset($extrafields->attributes[$elementtype]['type']) && is_array($extrafields->attributes[$elementtype]['type']) && count($extrafields->attributes[$elementtype]['type'])) {
@ -98,6 +104,16 @@ if (isset($extrafields->attributes[$elementtype]['type']) && is_array($extrafiel
}
print '<tr class="oddeven">';
// Actions
if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
print '<td class="center nowraponall">';
print '<a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().'&attrname='.urlencode($key).'#formeditextrafield">'.img_edit().'</a>';
print '&nbsp; <a class="paddingleft" href="'.$_SERVER["PHP_SELF"].'?action=delete&token='.newToken().'&attrname='.urlencode($key).'">'.img_delete().'</a>';
if ($extrafields->attributes[$elementtype]['type'][$key] == 'password' && !empty($extrafields->attributes[$elementtype]['param'][$key]['options']) && array_key_exists('dolcrypt', $extrafields->attributes[$elementtype]['param'][$key]['options'])) {
print '&nbsp; <a class="aaa" href="'.$_SERVER["PHP_SELF"].'?action=encrypt&token='.newToken().'&attrname='.urlencode($key).'" title="'.dol_escape_htmltag($langs->trans("ReEncryptDesc")).'">'.img_picto('', 'refresh').'</a>';
}
print '</td>'."\n";
}
// Position
print "<td>".dol_escape_htmltag($extrafields->attributes[$elementtype]['pos'][$key])."</td>\n";
// Label
@ -156,10 +172,15 @@ if (isset($extrafields->attributes[$elementtype]['type']) && is_array($extrafiel
print '</td>';
}
// Actions
print '<td class="right nowraponall">';
print '<a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().'&attrname='.urlencode($key).'#formeditextrafield">'.img_edit().'</a>';
print '&nbsp; <a class="paddingleft" href="'.$_SERVER["PHP_SELF"].'?action=delete&token='.newToken().'&attrname='.urlencode($key).'">'.img_delete().'</a>';
print '</td>'."\n";
if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
print '<td class="right nowraponall">';
print '<a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().'&attrname='.urlencode($key).'#formeditextrafield">'.img_edit().'</a>';
print '&nbsp; <a class="paddingleft" href="'.$_SERVER["PHP_SELF"].'?action=delete&token='.newToken().'&attrname='.urlencode($key).'">'.img_delete().'</a>';
if ($extrafields->attributes[$elementtype]['type'][$key] == 'password' && !empty($extrafields->attributes[$elementtype]['param'][$key]['options']) && array_key_exists('dolcrypt', $extrafields->attributes[$elementtype]['param'][$key]['options'])) {
print '&nbsp; <a class="aaa" href="'.$_SERVER["PHP_SELF"].'?action=encrypt&token='.newToken().'&attrname='.urlencode($key).'" title="'.dol_escape_htmltag($langs->trans("ReEncryptDesc")).'">'.img_picto('', 'refresh').'</a>';
}
print '</td>'."\n";
}
print "</tr>";
}
} else {

View file

@ -2414,4 +2414,6 @@ MaxNumberOfFailedAuth=Max number of failed authentication in 24h to deny login.
AllowPasswordResetBySendingANewPassByEmail=If a user A has this permission, and even if the user A is not an "admin" user, A is allowed to reset the password of any other user B, the new password will be send to the email of the other user B but it won't be visible to A. If the user A has the "admin" flag, he will also be able to know what is the new generated password of B so he will be able to take control of the B user account.
AllowAnyPrivileges=If a user A has this permission, he can create a user B with all privileges then use this user B, or grant himself any other group with any permission. So it means user A owns all business privileges (only system access to setup pages will be forbidden)
ThisValueCanBeReadBecauseInstanceIsNotInProductionMode=This value can be read because your instance is not set in production mode
SeeConfFile=See inside conf.php file on the server
SeeConfFile=See inside conf.php file on the server
ReEncryptDesc=Reencrypt data if not yet encrypted
PasswordFieldEncrypted=%s new record have this field been encrypted