From ce7687208010cc8524903b7cf949a438e8ed6787 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 20 Jun 2019 20:14:54 +0200 Subject: [PATCH] Add LSformRule :: differentPassword --- doc/conf/LSattribute/check-data.docbook | 1 + .../LSattribute-check_data.entities.xml | 1 + .../check_data/differentPassword.docbook | 24 ++++ .../class.LSformRule_differentPassword.php | 106 ++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 doc/conf/LSattribute/check_data/differentPassword.docbook create mode 100644 public_html/includes/class/class.LSformRule_differentPassword.php diff --git a/doc/conf/LSattribute/check-data.docbook b/doc/conf/LSattribute/check-data.docbook index 041906b3..c6c2c5d8 100644 --- a/doc/conf/LSattribute/check-data.docbook +++ b/doc/conf/LSattribute/check-data.docbook @@ -47,6 +47,7 @@ règles. &conf-LSattribute-check-data-alphanumeric; &conf-LSattribute-check-data-callable; &conf-LSattribute-check-data-date; +&conf-LSattribute-check-data-differentPassword; &conf-LSattribute-check-data-email; &conf-LSattribute-check-data-filesize; &conf-LSattribute-check-data-imagefile; diff --git a/doc/conf/LSattribute/check_data/LSattribute-check_data.entities.xml b/doc/conf/LSattribute/check_data/LSattribute-check_data.entities.xml index e13378e8..0010bfa5 100644 --- a/doc/conf/LSattribute/check_data/LSattribute-check_data.entities.xml +++ b/doc/conf/LSattribute/check_data/LSattribute-check_data.entities.xml @@ -2,6 +2,7 @@ + diff --git a/doc/conf/LSattribute/check_data/differentPassword.docbook b/doc/conf/LSattribute/check_data/differentPassword.docbook new file mode 100644 index 00000000..5edc1c18 --- /dev/null +++ b/doc/conf/LSattribute/check_data/differentPassword.docbook @@ -0,0 +1,24 @@ + + differentPassword + Cette règle vérifie que la valeur saisie ne correspond pas à + un des mots de passe stockés dans d'autres attributs du même objet. + + + Les autres attributs doivent utiliser le type + d'attribut LDAP + LSattr_ldap_password + . + + +Paramètres de configuration + + + otherPasswordAttributes + + La liste des autres attributs dont les mots de passe doivent être différent. + + + + + + diff --git a/public_html/includes/class/class.LSformRule_differentPassword.php b/public_html/includes/class/class.LSformRule_differentPassword.php new file mode 100644 index 00000000..b5f65e23 --- /dev/null +++ b/public_html/includes/class/class.LSformRule_differentPassword.php @@ -0,0 +1,106 @@ + + */ +class LSformRule_differentPassword extends LSformRule { + + /** + * Check the value + * + * @param string $values Value to check + * @param array $options Validation options : + * - Other attribute : $options['params']['otherAttributes'] + * @param object $formElement The linked LSformElement object + * + * @return boolean true si la valeur est valide, false sinon + */ + public static function validate($value, $options, $formElement) { + if (is_array($options) && isset($options['params']['otherPasswordAttributes'])) { + // Make sure otherPasswordAttributes is an array + if (!is_array($options['params']['otherPasswordAttributes'])) + $options['params']['otherPasswordAttributes'] = array($options['params']['otherPasswordAttributes']); + + // Load LSattr_ldap_password + if (!LSsession :: loadLSclass("LSattr_ldap_password")) { + LSerror :: addErrorCode('LSformRule_differentPassword_02'); + return false; + } + + // Iter on otherPasswordAttributes to check password does not match + foreach($options['params']['otherPasswordAttributes'] as $attr) { + // Check attribute exist + if (!isset($formElement -> attr_html -> attribute -> ldapObject -> attrs[$attr])) { + LSerror :: addErrorCode('LSformRule_differentPassword_03', $attr); + return false; + } + + // Check is not the same attribute of the current one + if ($formElement -> attr_html -> attribute -> name == $attr) { + LSerror :: addErrorCode('LSformRule_differentPassword_04'); + return false; + } + + // Check attribute use LSldap_attr :: password type + if (!$formElement -> attr_html -> attribute -> ldapObject -> attrs[$attr] -> ldap instanceof LSattr_ldap_password) { + LSerror :: addErrorCode('LSformRule_differentPassword_05', $attr); + return false; + } + + if ($formElement -> attr_html -> attribute -> ldapObject -> attrs[$attr] -> ldap -> verify($value, $formElement -> form -> getValue($attr))) { + LSdebug($formElement -> name . " : Password matched with attribute $attr"); + return false; + } + else + LSdebug($formElement -> name . " : Password does not match with $attr"); + } + } + else { + LSerror :: addErrorCode('LSformRule_differentPassword_01'); + return false; + } + return true; + } + +} + +/* + * Error Codes + */ +LSerror :: defineError('LSformRule_differentPassword_01', +_("LSformRule_differentPassword : Other password attribute is not configured.") +); +LSerror :: defineError('LSformRule_differentPassword_02', +_("LSformRule_differentPassword : Fail to load LSattr_ldap :: password class.") +); +LSerror :: defineError('LSformRule_differentPassword_03', +_("LSformRule_differentPassword : The other password attribute %{attr} does not exist.") +); +LSerror :: defineError('LSformRule_differentPassword_04', +_("LSformRule_differentPassword : The other password attribute could not be the same of the current one.") +); +LSerror :: defineError('LSformRule_differentPassword_05', +_("LSformRule_differentPassword : The other password attributes does not used LSattr_ldap :: password. It's not the case of the attribure %{attr}.") +);