Verified Commit 161ce2bf authored by Côme Chilliet's avatar Côme Chilliet
Browse files

🚑 fix(plugins) Use new error objects in plugins

issue #6061
parent 5a8d69b2
......@@ -127,15 +127,15 @@ class argonautFuseConfig extends simpleService
function check (): array
{
$message = parent::check();
$errors = parent::check();
foreach ($this->plugin as $plugin) {
if ($plugin->is_account) {
$message = array_merge($message, $plugin->check());
$errors = array_merge($errors, $plugin->check());
}
}
return $message;
return $errors;
}
function set_acl_category (string $cat)
......
......@@ -254,7 +254,10 @@ class DhcpSectionsAttribute extends DialogOrderedArrayAttribute
return $error;
}
if (empty($this->value) || empty($this->dhcpObjectCache[$this->value[0][0]]['cn']) || ($this->dhcpObjectCache[$this->value[0][0]]['cn'][0] == '')) {
return msgPool::required(_('Name'));
return new SimplePluginCheckError(
$this,
msgPool::required(_('Name'))
);
}
}
......
......@@ -78,7 +78,7 @@ class dhcpHost extends dhcpPlugin
function check (): array
{
$messages = parent::check();
$errors = parent::check();
$dn = $this->dn;
if (($dn == '') || ($dn == 'new')) {
$dn = $this->compute_dn();
......@@ -87,16 +87,22 @@ class dhcpHost extends dhcpPlugin
foreach ($this->dhcpStatements as $statement) {
if (preg_match('/^fixed-address (.+)$/', $statement, $m)) {
if (!tests::is_ip($m[1])) {
$messages[] = sprintf(_('"%s" is not a valid IP address.'), $m[1]);
$errors[] = new SimplePluginCheckError(
$this,
htmlescape(sprintf(_('"%s" is not a valid IP address.'), $m[1]))
);
} else {
$error = $this->parentPlugin->attributesAccess['dhcpSections']->isIpInParentSubnet($dn, $m[1]);
if ($error !== TRUE) {
$messages[] = $error;
$errors[] = new SimplePluginCheckError(
$this,
htmlescape($error)
);
}
}
}
}
return $messages;
return $errors;
}
}
......@@ -255,12 +255,18 @@ class DhcpHostsAttribute extends OrderedArrayAttribute
return $error;
} else {
if (!is_array($this->value)) {
return sprintf(_('The value for multivaluated field "%s" is not an array'), $this->getLabel());
return new SimplePluginCheckError(
$this,
SimplePluginCheckError::invalidValue(_('Value is not an array'))
);
}
foreach ($this->value as $value) {
$error = dhcpSystem::dhcpIsIpInSubnet($this->nodesCache[$value[0]], $value[2]);
if ($error !== TRUE) {
return $error;
return new SimplePluginCheckError(
$this,
htmlescape($error)
);
}
}
}
......
......@@ -416,7 +416,10 @@ class DnsRecordAttribute extends CompositeAttribute
$reverse = $this->attributes[2]->getValue();
if (!static::matchReverseZone($type, $ip, $reverse)) {
return _('The entered IP does not match the selected reverse zone');
return new SimplePluginCheckError(
$this,
htmlescape(_('The entered IP does not match the selected reverse zone'))
);
}
}
}
......
......@@ -141,7 +141,10 @@ class FaiPartitionSizeAttribute extends CompositeAttribute
$res1 = $this->attributes[1]->getValue() * $mp[$this->attributes[2]->getValue()];
$res2 = $this->attributes[3]->getValue() * $mp[$this->attributes[4]->getValue()];
if ($res1 > $res2) {
return msgPool::toobig(_('Minimum partition size'), '\''._('Maximum partition size').'\'');
return new SimplePluginCheckError(
$this,
msgPool::toobig(_('Minimum partition size'), '\''._('Maximum partition size').'\'')
);
}
}
}
......@@ -499,14 +502,20 @@ class faiPartition extends simplePlugin
function check (): array
{
$messages = parent::check();
$errors = parent::check();
if ($this->FAIdiskType == 'raid') {
$raidDevices = $this->attributesAccess['raidDevices']->attributes[0]->getValue();
if (count($raidDevices) < 2) {
$messages[] = _('Raid arrays must contain at least two partitions!');
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['raidDevices'],
htmlescape(_('Raid arrays must contain at least two partitions!'))
);
} elseif (($this->FAIpartitionType == 'raid0') && (count($this->raidDevices) != 2)) {
$messages[] = _('Raid 0 arrays can only be realized with a combination of two partitions!');
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['raidDevices'],
htmlescape(_('Raid 0 arrays can only be realized with a combination of two partitions!'))
);
}
}
......
......@@ -39,8 +39,7 @@ class faiSimplePluginClass extends simplePlugin
function check (): array
{
global $config;
/* Call common method to give check the hook */
$message = parent::check();
$errors = parent::check();
/* Ensure that we do not overwrite an allready existing entry */
$new_dn = $this->compute_dn();
......@@ -48,11 +47,14 @@ class faiSimplePluginClass extends simplePlugin
$ldap = $config->get_ldap_link();
$ldap->cat($new_dn);
if ($ldap->count() != 0) {
$message[] = msgPool::duplicated(_("Name"));
$errors[] = new SimplePluginCheckError(
$this,
msgPool::duplicated(_('Name'))
);
}
}
return $message;
return $errors;
}
function compute_dn (): string
......
......@@ -128,7 +128,10 @@ class FAITemplateFilesAttribute extends DialogOrderedArrayAttribute
$used_cn = [];
foreach ($this->value as $value) {
if (isset($used_cn[$value['cn']])) {
return sprintf(_('There are several files with the same path: %s'), $value['cn']);
return new SimplePluginCheckError(
$this,
htmlescape(sprintf(_('There are several files with the same path: %s'), $value['cn']))
);
}
$used_cn[$value['cn']] = TRUE;
}
......
......@@ -111,16 +111,19 @@ class invitation extends simplePlugin
function check (): array
{
$messages = parent::check();
$errors = parent::check();
$emails = explode("\n", $this->emails);
foreach ($emails as $email) {
if (!tests::is_email($email)) {
$messages[] = sprintf(_('"%s" is not a valid email address'), $email);
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['emails'],
htmlescape(sprintf(_('"%s" is not a valid email address'), $email))
);
}
}
return $messages;
return $errors;
}
protected function shouldSave (): bool
......
......@@ -254,13 +254,13 @@ class mailGroup extends simplePlugin
$this->attributesAccess['specialACLs']->attribute->attributes[0]->setChoices($this->getMemberEmailList());
}
$messages = parent::check();
$errors = parent::check();
if (!$this->is_template && is_object($this->mailMethod)) {
$messages = array_merge($messages, $this->mailMethod->check());
$errors = array_merge($errors, $this->mailMethod->check());
}
return $messages;
return $errors;
}
protected function shouldSave (): bool
......
......@@ -337,13 +337,13 @@ class mailAccount extends simplePlugin
function check (): array
{
$messages = parent::check();
$errors = parent::check();
if (!$this->is_template && is_object($this->mailMethod)) {
$messages = array_merge($messages, $this->mailMethod->check());
$errors = array_merge($errors, $this->mailMethod->check());
}
return $messages;
return $errors;
}
protected function shouldSave (): bool
......
......@@ -111,16 +111,19 @@ class mixedGroup extends simplePlugin
function check (): array
{
global $config;
$message = parent::check();
$errors = parent::check();
/* Check ID's if they are forced by user */
if ($this->force_id) {
if ($this->gidNumber < $config->get_cfg_value("minId")) {
$message[] = msgPool::toosmall(_("GID"), $config->get_cfg_value("minId"));
if ($this->gidNumber < $config->get_cfg_value('minId')) {
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['gidNumber'],
msgPool::toosmall(_('GID'), $config->get_cfg_value('minId'))
);
}
}
return $message;
return $errors;
}
function resetCopyInfos ()
......
......@@ -62,7 +62,10 @@ class SocialAccountAttribute extends CompositeAttribute
}
$handlerClass = 'socialHandler_'.$this->attributes[0]->getValue();
if (!class_available($handlerClass)) {
return 'Social handler "'.$handlerClass.'" does not exists';
return new SimplePluginCheckError(
$this,
htmlescape(sprintf(_('Social handler "%s" does not exists'), $handlerClass))
);
}
$handler = new $handlerClass();
$value = $this->attributes[1]->getValue();
......@@ -70,7 +73,12 @@ class SocialAccountAttribute extends CompositeAttribute
$value = $handler->validate($this->attributes[1]->getValue());
$this->attributes[1]->setValue($value);
} catch (socialHandlerInvalidValueException $e) {
return $e->getMessage();
new SimplePluginCheckError(
$this,
htmlescape($e->getMessage()),
0,
$e
);
}
}
}
......
......@@ -368,30 +368,45 @@ class posixAccount extends simplePlugin
{
global $config;
$message = parent::check();
$errors = parent::check();
/* Check shadow settings */
if ($this->shadowWarning !== '') {
if ($this->shadowMax === '') {
$message[] = msgPool::depends('shadowWarning', 'shadowMax');
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['shadowWarning'],
msgPool::depends('shadowWarning', 'shadowMax')
);
}
if ($this->shadowWarning > $this->shadowMax) {
$message[] = msgPool::toobig('shadowWarning', 'shadowMax');
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['shadowWarning'],
msgPool::toobig('shadowWarning', 'shadowMax')
);
}
if (($this->shadowMin !== '') && ($this->shadowWarning < $this->shadowMin)) {
$message[] = msgPool::toosmall('shadowWarning', 'shadowMin');
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['shadowWarning'],
msgPool::toosmall('shadowWarning', 'shadowMin')
);
}
}
if (($this->shadowInactive !== '') && ($this->shadowMax === '')) {
$message[] = msgPool::depends('shadowInactive', 'shadowMax');
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['shadowInactive'],
msgPool::depends('shadowInactive', 'shadowMax')
);
}
if (($this->shadowMin !== '') && ($this->shadowMax !== '') &&
($this->shadowMin > $this->shadowMax)) {
$message[] = msgPool::toobig('shadowMin', 'shadowMax');
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['shadowMin'],
msgPool::toobig('shadowMin', 'shadowMax')
);
}
return $message;
return $errors;
}
protected function prepare_save (): array
......
......@@ -141,13 +141,13 @@ class sympaAliasPartage extends simplePlugin
function check (): array
{
$messages = parent::check();
$errors = parent::check();
if (!$this->is_template && is_object($this->mailMethod)) {
$messages = array_merge($messages, $this->mailMethod->check());
$errors = array_merge($errors, $this->mailMethod->check());
}
return $messages;
return $errors;
}
protected function shouldSave (): bool
......
......@@ -161,18 +161,21 @@ class mailMethodRenaterPartage extends mailMethod
*/
public function check ()
{
$messages = parent::check();
$errors = parent::check();
$servers = static::getMailServers();
$infos = $servers[$this->parent->gosaMailServer];
$alternates = $this->parent->gosaMailAlternateAddress;
foreach ($alternates as $alternate) {
if (!preg_match('/@'.preg_quote($infos['domain'], '/').'$/', $alternate)) {
$messages[] = sprintf(_('Email address "%s" does not use the correct partage domain "%s"'), $alternate, $infos['domain']);
$errors[] = new SimplePluginCheckError(
$this->parent,
htmlescape(sprintf(_('Email address "%s" does not use the correct partage domain "%s"'), $alternate, $infos['domain']))
);
}
}
return $messages;
return $errors;
}
/*! \brief Returns the connection status of this method.
......
......@@ -472,30 +472,39 @@ class sambaAccount extends simplePlugin implements UserTabLockingAction
/* Check for input problems */
function check (): array
{
$messages = parent::check();
$errors = parent::check();
/* sambaHomePath requires sambaHomeDrive and vice versa */
if (!empty($this->sambaHomePath) && empty($this->sambaHomeDrive)) {
$messages[] = sprintf(
_('Field "%s" is required when "%s" is filled! Fill both field or empty them.'),
$this->attributesAccess['sambaHomeDrive']->getLabel(),
$this->attributesAccess['sambaHomePath']->getLabel()
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['sambaHomeDrive'],
htmlescape(sprintf(
_('Field "%s" is required when "%s" is filled! Fill both field or empty them.'),
$this->attributesAccess['sambaHomeDrive']->getLabel(),
$this->attributesAccess['sambaHomePath']->getLabel()
))
);
}
if (!empty($this->sambaHomeDrive) && empty($this->sambaHomePath)) {
$messages[] = sprintf(
_('Field "%s" is required when "%s" is filled! Fill both field or empty them.'),
$this->attributesAccess['sambaHomePath']->getLabel(),
$this->attributesAccess['sambaHomeDrive']->getLabel()
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['sambaHomePath'],
htmlescape(sprintf(
_('Field "%s" is required when "%s" is filled! Fill both field or empty them.'),
$this->attributesAccess['sambaHomePath']->getLabel(),
$this->attributesAccess['sambaHomeDrive']->getLabel()
))
);
}
/* Too many workstations? Windows usrmgr only supports eight */
if (count($this->attributesAccess['sambaUserWorkstations']->attributes[0]->getValue()) >= 8) {
$messages[] = _('The windows user manager allows eight clients at maximum!');
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['sambaUserWorkstations'],
htmlescape(_('The windows user manager allows eight clients at maximum!'))
);
}
return $messages;
return $errors;
}
function prepare_save (): array
......
......@@ -119,7 +119,7 @@ class sshAccount extends simplePlugin implements UserTabLockingAction
function check (): array
{
$message = parent::check();
$errors = parent::check();
$fingerprints = [];
$values = $this->attributesAccess['sshPublicKey']->getValue();
......@@ -127,13 +127,16 @@ class sshAccount extends simplePlugin implements UserTabLockingAction
$this->attributesAccess['sshPublicKey']->attribute->setValue($value);
$arrayValue = $this->attributesAccess['sshPublicKey']->attribute->getArrayValue();
if (in_array($arrayValue[1], $fingerprints)) {
$message[] = sprintf(_('Error : there are several keys with fingerprint %s'), $arrayValue[1]);
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['sshPublicKey'],
htmlescape(sprintf(_('Error : there are several keys with fingerprint %s'), $arrayValue[1]))
);
} else {
$fingerprints[] = $arrayValue[1];
}
}
return $message;
return $errors;
}
public function fillLockingLDAPAttrs (string $mode, array &$modify)
......
......@@ -234,9 +234,12 @@ class SudoOptionAttribute extends Attribute
$error = parent::check();
if (!empty($error)) {
return $error;
} elseif ($this->type == "INTEGER") {
} elseif ($this->type == 'INTEGER') {
if (!is_numeric($this->value) && !empty($this->value)) {
return msgPool::invalid($this->getLabel(), $this->value);
return new SimplePluginCheckError(
$this,
SimplePluginCheckError::invalidValue('Value is not an integer')
);
}
}
}
......
......@@ -129,7 +129,10 @@ class supannStructureExt extends simplePlugin
($this->attributesAccess['fdSupannEndDate']->getValue() != '') &&
($this->attributesAccess['fdSupannStartDate']->getDateValue() > $this->attributesAccess['fdSupannEndDate']->getDateValue())
) {
$errors[] = _('Start date must be smaller than end date');
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['fdSupannEndDate'],
htmlescape(sprintf(_('End date "%s" is older than start date "%s"'), $this->attributesAccess['fdSupannEndDate']->getDateValue()->format('Y-m-d'), $this->attributesAccess['fdSupannStartDate']->getDateValue()->format('Y-m-d')))
);
}
return $errors;
......
......@@ -445,14 +445,17 @@ class supannAccount extends simplePlugin
function check (): array
{
$message = parent::check();
$errors = parent::check();
$affiliations = $this->eduPersonAffiliation;
if (in_array('member', $affiliations) && in_array('affiliate', $affiliations)) {
$message[] = _('"member" and "affiliate" values are incompatible for eduPersonAffiliation. Please remove one of them.');
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['eduPersonAffiliation'],
htmlescape(_('"member" and "affiliate" values are incompatible for eduPersonAffiliation. Please remove one of them.'))
);
}
return $message;
return $errors;
}
function save_object ()
......
......@@ -184,7 +184,10 @@ class supannAccountStatus extends simplePlugin implements UserTabLockingAction
foreach ($this->supannRessourceEtatDate as $line) {
$resource = preg_replace('/^\{([^}]+)\}.*$/', '\1', $line);
if (in_array($resource, $resources)) {
$errors[] = sprintf(_('Resource "%s" is present more than once in supannRessourceEtatDate'), $resource);
$errors[] = new SimplePluginCheckError(
$this->attributesAccess['supannRessourceEtatDate'],
htmlescape(sprintf(_('Resource "%s" is present more than once in supannRessourceEtatDate'), $resource))
);
} else {
$resources[] = $resource;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment