LSldapObject: fix/improve validateAttrData()

This commit is contained in:
Benjamin Renard 2020-12-14 19:36:21 +01:00
parent 8b292f538d
commit a49cf4ba0c

View file

@ -462,102 +462,123 @@ class LSldapObject extends LSlog_staticLoggerClass {
public function validateAttrData(&$LSform,&$attr) { public function validateAttrData(&$LSform,&$attr) {
$retval = true; $retval = true;
$vconfig=$attr -> getValidateConfig(); $vconfig = $attr -> getValidateConfig();
$data=$attr -> getUpdateData(); // Validate attribute values
if(!is_array($data)) { foreach($attr -> getConfig('validation', array(), 'array') as $test) {
$data=array($data); self :: log_trace("validateAttrData(".$LSform->idForm.", ".$attr->name."): run validation with following config:\n".varDump($test));
}
// Validation des valeurs de l'attribut // Generate error message
if(is_array($vconfig)) { $msg_error = LSconfig :: get('msg', null, 'string', $test);
foreach($vconfig as $test) { if ($msg_error) {
// Définition du basedn par défaut $msg_error = $this -> getFData(__($msg_error));
if (!isset($test['basedn'])) { }
$test['basedn']=LSsession :: getTopDn(); else {
} $msg_error = getFData(_("The attribute %{attr} is not valid."), $attr -> getLabel());
}
// Définition du message d'erreur // Iter on attribute values to check all of it
if (!empty($test['msg'])) { foreach(ensureIsArray($attr -> getUpdateData()) as $val) {
$msg_error=getFData(__($test['msg']),$this,'getValue'); // Validation using LDAP search
} if((isset($test['filter'])||isset($test['basedn']))&&(isset($test['result']))) {
else { $this -> other_values['val'] = $val;
$msg_error=getFData(_("The attribute %{attr} is not valid."),$attr -> getLabel()); $sparams = array(
} 'scope' => LSconfig :: get('scope', 'sub', 'string', $test),
foreach($data as $val) { 'onlyAccessible' => False,
// validation par check LDAP );
if((isset($test['filter'])||isset($test['basedn']))&&(isset($test['result']))) {
$sparams=array('onlyAccessible' => False);
if (isset($test['scope']))
$sparams['scope'] = $test['scope'];
$this -> other_values['val']=$val;
// Filter from test configuration
if (isset($test['filter']) && !empty($test['filter'])) {
$sfilter_user=getFData($test['filter'],$this,'getValue');
if ($sfilter_user[0]!='(') $sfilter_user="(".$sfilter_user.")";
$sfilter_user=Net_LDAP2_Filter::parse($sfilter_user);
}
else {
$sfilter_user=NULL;
}
if(isset($test['object_type']) && LSsession :: loadLSobject($test['object_type']) ) {
$sfilter=self :: _getObjectFilter($test['object_type']);
if ($sfilter_user) { // Handle filter parameter
$sfilter=LSldap::combineFilters('and',array($sfilter_user,$sfilter)); $sfilter_user = LSconfig :: get('filter', null, 'string', $test);
} if ($sfilter_user) {
} $sfilter_user = $this -> getFData($test['filter']);
else { // Check if filter format is surronded by '()'
$sfilter=$sfilter_user; if ($sfilter_user[0]!='(')
} $sfilter_user = "($sfilter_user)";
$sbasedn=(isset($test['basedn']))?getFData($test['basedn'],$this,'getValue'):NULL; $sfilter_user = Net_LDAP2_Filter::parse($sfilter_user);
if (isset($test['except_current_object']) && (bool)$test['except_current_object'] && !$LSform -> idForm!='create') {
$sret=LSldap :: search ($sfilter,$sbasedn,$sparams);
$dn=$this->getDn();
$ret=0;
foreach($sret as $obj) {
if ($obj['dn']!=$dn)
$ret++;
}
}
else {
$ret=LSldap :: getNumberResult ($sfilter,$sbasedn,$sparams);
}
if($test['result']==0) {
if($ret!=0) {
if ($LSform) $LSform -> setElementError($attr,$msg_error);
$retval = false;
}
}
else {
if($ret<0) {
if ($LSform) $LSform -> setElementError($attr,$msg_error);
$retval = false;
}
}
} }
// Validation par fonction externe
else if(isset($test['function'])) { // Handle object_type parameter & compose final LDAP filter string
if (function_exists($test['function'])) { $object_type = LSconfig :: get('object_type', null, 'string', $test);
if(!call_user_func_array($test['function'],array(&$this))) { if($object_type && LSsession :: loadLSobject($object_type) ) {
if ($LSform) $LSform -> setElementError($attr,$msg_error); $sfilter = self :: _getObjectFilter($object_type);
$retval = false; if ($sfilter_user)
} $sfilter = LSldap::combineFilters('and', array($sfilter_user, $sfilter));
} }
else { else {
LSerror :: addErrorCode('LSldapObject_04',array('attr' => $attr->name,'obj' => $this->getType(),'func' => $test['function'])); $sfilter = $sfilter_user;
}
// Handle base_dn parameter
$sbasedn = LSconfig :: get('basedn', null, 'string', $test);
if ($sbasedn)
$sbasedn = $this -> getFData($sbasedn);
// If except_current_object (and not create form), list matching objets to exclude current one
if (LSconfig :: get('except_current_object', false, 'bool', $test) && $LSform -> idForm != 'create') {
$sret = LSldap :: search($sfilter, $sbasedn, $sparams);
$dn = $this->getDn();
$ret = 0;
foreach($sret as $obj)
if ($obj['dn'] != $dn)
$ret++;
}
else {
// Otherwise, just retreive number of matching objets
$ret=LSldap :: getNumberResult($sfilter, $sbasedn, $sparams);
}
// Check result
if(LSconfig :: get('result', null, 'int', $test) == 0) {
if($ret != 0) {
if ($LSform)
$LSform -> setElementError($attr, $msg_error);
$retval = false; $retval = false;
} }
} }
else { else {
LSerror :: addErrorCode('LSldapObject_05',array('attr' => $attr->name,'obj' => $this->getType())); if($ret < 0) {
if ($LSform)
$LSform -> setElementError($attr, $msg_error);
$retval = false;
}
}
}
// Validation using external function
else if(isset($test['function'])) {
if (function_exists($test['function'])) {
if(!call_user_func_array($test['function'], array(&$this))) {
if ($LSform)
$LSform -> setElementError($attr,$msg_error);
$retval = false;
}
}
else {
LSerror :: addErrorCode(
'LSldapObject_04',
array(
'attr' => $attr->name,
'obj' => $this->getType(),
'func' => $test['function']
)
);
$retval = false; $retval = false;
} }
} }
else {
LSerror :: addErrorCode(
'LSldapObject_05',
array(
'attr' => $attr->name,
'obj' => $this->getType()
)
);
$retval = false;
}
} }
} }
// Génération des valeurs des attributs dépendants
$dependsAttrs=$attr->getDependsAttrs(); // Generate values of dependent attributes
$dependsAttrs = $attr->getDependsAttrs();
if (!empty($dependsAttrs)) { if (!empty($dependsAttrs)) {
foreach($dependsAttrs as $dependAttr) { foreach($dependsAttrs as $dependAttr) {
if(!isset($this -> attrs[$dependAttr])){ if(!isset($this -> attrs[$dependAttr])){
@ -581,6 +602,9 @@ class LSldapObject extends LSlog_staticLoggerClass {
} }
} }
} }
else {
self :: log_trace("validateAttrData(".$LSform->idForm.", ".$attr->name."): no dependent attribute");
}
$attr -> validate(); $attr -> validate();
unset($this -> other_values['val']); unset($this -> other_values['val']);