From 75e048f4cdd92c1be46a37e20f899d77b2b26c82 Mon Sep 17 00:00:00 2001 From: Patrick Barroca <pbarroca@afi-sa.fr> Date: Thu, 7 Jun 2018 15:16:49 +0200 Subject: [PATCH] hotline #75603 : extract specific adminvar, ensure default and valid fields --- library/Class/AdminVar.php | 4 +- library/Class/AdminVar/UserDoubleDetectOn.php | 79 +++++++++++++++ library/Class/User/DbDoubleFinder.php | 4 +- .../ZendAfi/Validate/UserDoubleDetectOn.php | 55 +++++++++++ library/ZendAfi/Validate/UserDoubleFields.php | 96 ------------------- .../admin/controllers/UsersControllerTest.php | 19 ++-- 6 files changed, 150 insertions(+), 107 deletions(-) create mode 100644 library/Class/AdminVar/UserDoubleDetectOn.php create mode 100644 library/ZendAfi/Validate/UserDoubleDetectOn.php delete mode 100644 library/ZendAfi/Validate/UserDoubleFields.php diff --git a/library/Class/AdminVar.php b/library/Class/AdminVar.php index 848357de675..ac20d50d988 100644 --- a/library/Class/AdminVar.php +++ b/library/Class/AdminVar.php @@ -237,8 +237,8 @@ class Class_AdminVarLoader extends Storm_Model_Loader { 'ENABLE_BOOKMARKABLE_LIBRARIES' => Class_AdminVar_Meta::newOnOff($this->_('Les utilisateurs peuvent sélectionner des bibliothèques favorites pour leurs recherche'), ['value' => 1]), 'ENABLE_BOOKMARKABLE_SEARCHES' => Class_AdminVar_Meta::newOnOff($this->_('Les utilisateurs peuvent enregister des recherches dans leurs favoris'), ['value' => 0]), 'ENABLE_BOOKMARKABLE_SEARCHES_NOTIFY' => Class_AdminVar_Meta::newOnOff($this->_('Les utilisateurs peuvent recevoir les nouveautés de leurs recherches favorites par email'), ['value' => 0]), - 'DOUBLE_DETECT_ON' => Class_AdminVar_Meta::newMultiInput($this->_('Champs servant à la détection des doublons. Champs possibles : %s', (new ZendAfi_Validate_UserDoubleFields)->possiblesList()), - ['validate' => 'ZendAfi_Validate_UserDoubleFields'])->bePrivate(), + Class_AdminVar_UserDoubleDetectOn::KEY => Class_AdminVar_Meta::newMultiInput($this->_('Champs servant à la détection des doublons. Champs possibles : %s', (new Class_AdminVar_UserDoubleDetectOn)->knownAsList()), + ['validate' => 'ZendAfi_Validate_UserDoubleDetectOn'])->bePrivate(), ]; } diff --git a/library/Class/AdminVar/UserDoubleDetectOn.php b/library/Class/AdminVar/UserDoubleDetectOn.php new file mode 100644 index 00000000000..4261b9b7bdd --- /dev/null +++ b/library/Class/AdminVar/UserDoubleDetectOn.php @@ -0,0 +1,79 @@ +<?php +/** + * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class Class_AdminVar_UserDoubleDetectOn { + use Trait_Translator; + + const KEY = 'USER_DOUBLE_DETECT_ON'; + + protected static $_known = []; + + protected $_defaults = ['login', 'idabon', 'ordreabon', 'password']; + + public function isKnown($value) { + return in_array($value, array_keys($this->_getKnown())); + } + + + public function knownAsList() { + $map = $this->_getKnown(); + + $value = []; + foreach($map as $k => $v) + $value[] = $v . ' : ' . $k; + + return '<ul><li>' . implode('</li><li>', $value) . '</li></ul>'; + } + + + public function getCurrent() { + $values = array_filter(explode(';', Class_AdminVar::get(static::KEY))); + if (!$values) + return $this->_defaults; + + $values = array_filter($values, [$this, 'isKnown']); + return $values ? $values : $this->_defaults; + } + + + protected function _getKnown() { + if (static::$_known) + return static::$_known; + + return static::$_known = ['nom' => $this->_('Nom'), + 'prenom' => $this->_('Prénom'), + 'naissance' => $this->_('Date de naissance'), + 'id_site' => $this->_('Site de rattachement'), + 'login' => $this->_('Login'), + 'password' => $this->_('Mot de passe'), + 'idabon' => $this->_('Numéro de carte'), + 'ordreabon' => $this->_('Numéro d\'ordre sur la carte'), + 'mail' => $this->_('Mail'), + 'date_debut' => $this->_('Date de début d\'abonnement'), + 'date_fin' => $this->_('Date de fin d\'abonnement'), + 'id_sigb' => $this->_('Identifiant dans le SIGB'), + 'telephone' => $this->_('Téléphone'), + 'adresse' => $this->_('Adresse'), + 'code_postal' => $this->_('Code postal'), + 'ville' => $this->_('Ville'),]; + } +} diff --git a/library/Class/User/DbDoubleFinder.php b/library/Class/User/DbDoubleFinder.php index 36dac1e520a..d852bdbd451 100644 --- a/library/Class/User/DbDoubleFinder.php +++ b/library/Class/User/DbDoubleFinder.php @@ -24,8 +24,6 @@ class Class_User_DbDoubleFinder { const LIMIT_DEDUPE = 10; - protected $_default_fields = ['login', 'idabon', 'ordreabon', 'password']; - public function getRequest($cursor = 0) { $fields = implode(', ', $this->_getFields()); return sprintf('select min(id_user) as id_user, %s, count(*) as doublon from bib_admin_users where role_level = 2 and id_user > %s group by %s having doublon > 1 order by id_user asc', @@ -34,7 +32,7 @@ class Class_User_DbDoubleFinder { protected function _getFields() { - return $this->_default_fields; + return (new Class_AdminVar_UserDoubleDetectOn)->getCurrent(); } diff --git a/library/ZendAfi/Validate/UserDoubleDetectOn.php b/library/ZendAfi/Validate/UserDoubleDetectOn.php new file mode 100644 index 00000000000..cdf3354b9a2 --- /dev/null +++ b/library/ZendAfi/Validate/UserDoubleDetectOn.php @@ -0,0 +1,55 @@ +<?php +/** + * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class ZendAfi_Validate_UserDoubleDetectOn extends Zend_Validate_Abstract { + + const NOT_KNOWN = 'notKnown'; + + protected $_messageTemplates = + [self::NOT_KNOWN => 'Le champ "%field%" n\'est pas parmi les champs possibles']; + + protected $_messageVariables = ['field' => '_field']; + + protected $_field; + + public function isValid($value, $context = null) { + $parts = explode(';', $value); + foreach($parts as $part) + if (!$this->_validOne($part)) { + $this->_error(static::NOT_KNOWN); + return false; + } + + return true; + } + + + protected function _validOne($value) { + if ('' == $value) + return true; + + $this->_field = $value; + + return (new Class_AdminVar_UserDoubleDetectOn()) + ->isKnown($value); + } +} diff --git a/library/ZendAfi/Validate/UserDoubleFields.php b/library/ZendAfi/Validate/UserDoubleFields.php deleted file mode 100644 index 73984e22ad6..00000000000 --- a/library/ZendAfi/Validate/UserDoubleFields.php +++ /dev/null @@ -1,96 +0,0 @@ -<?php -/** - * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved. - * - * BOKEH is free software; you can redistribute it and/or modify - * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by - * the Free Software Foundation. - * - * There are special exceptions to the terms and conditions of the AGPL as it - * is applied to this software (see README file). - * - * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details. - * - * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE - * along with BOKEH; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - - -class ZendAfi_Validate_UserDoubleFields extends Zend_Validate_Abstract { - use Trait_Translator; - - const NOT_IN_POSSIBLES = 'notInPossibles'; - - protected static $_possibles = []; - - protected $_messageTemplates = - [self::NOT_IN_POSSIBLES => 'Le champ "%field%" n\'est pas parmi les champs possibles']; - - protected $_messageVariables = ['field' => '_field']; - - protected $_field; - - public function possiblesList() { - $map = $this->_getPossibles(); - - $value = []; - foreach($map as $k => $v) - $value[] = $v . ' : ' . $k; - - return '<ul><li>' . implode('</li><li>', $value) . '</li></ul>'; - } - - - public function isValid($value, $context = null) { - $parts = explode(';', $value); - $possibles = array_keys($this->_getPossibles()); - - foreach($parts as $part) - if (!$this->_validOne($part, $possibles)) { - $this->_error(static::NOT_IN_POSSIBLES); - return false; - } - - return true; - } - - - protected function _validOne($value, $possibles) { - if ('' == $value) - return true; - - if (in_array($value, $possibles)) - return true; - - $this->_field = $value; - $this->_error(static::NOT_IN_POSSIBLES); - return false; - } - - - protected function _getPossibles() { - if (static::$_possibles) - return static::$_possibles; - - return static::$_possibles = ['nom' => $this->_('Nom'), - 'prenom' => $this->_('Prénom'), - 'naissance' => $this->_('Date de naissance'), - 'id_site' => $this->_('Site de rattachement'), - 'login' => $this->_('Login'), - 'password' => $this->_('Mot de passe'), - 'idabon' => $this->_('Numéro de carte'), - 'ordreabon' => $this->_('Numéro d\'ordre sur la carte'), - 'mail' => $this->_('Mail'), - 'date_debut' => $this->_('Date de début d\'abonnement'), - 'date_fin' => $this->_('Date de fin d\'abonnement'), - 'id_sigb' => $this->_('Identifiant dans le SIGB'), - 'telephone' => $this->_('Téléphone'), - 'adresse' => $this->_('Adresse'), - 'code_postal' => $this->_('Code postal'), - 'ville' => $this->_('Ville'),]; - } -} diff --git a/tests/application/modules/admin/controllers/UsersControllerTest.php b/tests/application/modules/admin/controllers/UsersControllerTest.php index 5106bb06c31..c2dc5a0360f 100644 --- a/tests/application/modules/admin/controllers/UsersControllerTest.php +++ b/tests/application/modules/admin/controllers/UsersControllerTest.php @@ -99,7 +99,7 @@ class UsersControllerIndexTest extends UsersControllerWithMarcusTestCase { Zend_Registry::set('sql', $this->mock() ->whenCalled('fetchAll') - ->with('select min(id_user) as id_user, password, login, idabon, ordreabon, count(*) as doublon from bib_admin_users where role_level = 2 and id_user > 0 group by idabon, ordreabon, login, password having doublon > 1 order by id_user asc') + ->with('select min(id_user) as id_user, login, idabon, ordreabon, password, count(*) as doublon from bib_admin_users where role_level = 2 and id_user > 0 group by login, idabon, ordreabon, password having doublon > 1 order by id_user asc') ->answers([]) ->whenCalled('fetchAll') @@ -1394,16 +1394,14 @@ abstract class UsersControllerDoubleTestCase extends Admin_AbstractControllerTes 'login' => 'Ret', 'password' => 'urn', 'idabon' => '89', - 'ordreabon' => 18, - 'id_sigb' => 6767]); + 'ordreabon' => 18]); $this->fixture('Class_Users', ['id' => 655, 'login' => 'Ret', 'password' => 'urn', 'idabon' => '89', - 'ordreabon' => 18, - 'id_sigb' => 6767]); + 'ordreabon' => 18]); $this->createUsersWithStatusDifferentAndSameNames(89, [100,101,102], [0,1,1]); $this->createUsersWithStatusDifferentAndSameNames(90, [103,104,105], [1,1,1]); @@ -1536,7 +1534,16 @@ class UsersControllerManageDoubleWithCustomCriteriaTest extends UsersControllerD public function setUp() { parent::setUp(); - Class_AdminVar::set('DOUBLE_DETECT_ON', 'id_sigb'); + Class_AdminVar::set('USER_DOUBLE_DETECT_ON', 'id_sigb'); + Zend_Registry::get('sql') + ->whenCalled('fetchAll') + ->with('select min(id_user) as id_user, id_sigb, count(*) as doublon from bib_admin_users where role_level = 2 and id_user > 0 group by id_sigb having doublon > 1 order by id_user asc') + ->answers([ + ['id_user' => '25', + 'id_sigb' => '5', + 'doublon' => '2'], + ]); + $this->dispatch('/admin/users/manage-double', true); } -- GitLab