Unverified Commit 16eb07bf authored by Alexandre Delaunay's avatar Alexandre Delaunay Committed by GitHub
Browse files

make notification email/name from entity inheritables (#8559)

* make notification email/name from entity inheritables

* rework inheritance hints

* extends new inherit display on other tabs

* make signature also inheritable

* add tests

* remove added entity in bootstrap

* bad replacement

* fix review

* clean

* add missing test

* add comment
parent 5d1f5c61
......@@ -172,6 +172,28 @@ sup {
background-color: #cf9b9b;
}
.inherited {
color: rgb(34, 77, 194);
padding: 5px;
margin: 3px 0;
border: 1px solid transparent;
border-radius: 2px;
background-color: rgba(34, 77, 194, .1);
white-space: nowrap;
font-style: italic;
display: table;
&.inline {
display: inline-block;
}
i.fas {
margin-right: 2px;
font-size: 0.7em;
}
}
.separ {
clear: both;
visibility: hidden;
......
This diff is collapsed.
......@@ -584,13 +584,13 @@ class Notification extends CommonDBTM {
* @param $entity
**/
static function getMailingSignature($entity) {
global $DB, $CFG_GLPI;
global $CFG_GLPI;
foreach ($DB->request('glpi_entities', ['id' => $entity]) as $data) {
if (!empty($data['mailing_signature'])) {
return $data['mailing_signature'];
}
$signature = trim(Entity::getUsedConfig('mailing_signature', $entity, '', ''));
if (strlen($signature) > 0) {
return $signature;
}
return $CFG_GLPI['mailing_signature'];
}
......
......@@ -1037,13 +1037,13 @@ class NotificationTarget extends CommonDBChild {
$sender['email'] = $CFG_GLPI['from_email'];
$sender['name'] = $CFG_GLPI['from_email_name'];
} else {
$entity = new \Entity();
$entity->getFromDB($this->getEntity());
$admin_email = trim(Entity::getUsedConfig('admin_email', $this->getEntity(), '', ''));
$admin_email_name = trim(Entity::getUsedConfig('admin_email_name', $this->getEntity(), '', ''));
if (NotificationMailing::isUserAddressValid($entity->fields['admin_email'])) {
if (NotificationMailing::isUserAddressValid($admin_email)) {
//If the entity administrator's address is defined, return it
$sender['email'] = $entity->fields['admin_email'];
$sender['name'] = $entity->fields['admin_email_name'];
$sender['email'] = $admin_email;
$sender['name'] = $admin_email_name;
} else {
//Entity admin is not defined, return the global admin's address
$sender['email'] = $CFG_GLPI['admin_email'];
......@@ -1078,20 +1078,24 @@ class NotificationTarget extends CommonDBChild {
* @return the reply to address
**/
public function getReplyTo($options = []) {
global $DB, $CFG_GLPI;
global $CFG_GLPI;
//If the entity administrator's address is defined, return it
foreach ($DB->request('glpi_entities',
['id' => $this->getEntity()]) as $data) {
$admin_reply = trim(Entity::getUsedConfig('admin_reply', $this->getEntity(), '', ''));
$admin_reply_name = trim(Entity::getUsedConfig('admin_reply_name', $this->getEntity(), '', ''));
if (NotificationMailing::isUserAddressValid($data['admin_reply'])) {
return ['email' => $data['admin_reply'],
'name' => $data['admin_reply_name']];
}
if (NotificationMailing::isUserAddressValid($admin_reply)) {
return [
'email' => $admin_reply,
'name' => $admin_reply_name,
];
}
//Entity admin is not defined, return the global admin's address
return ['email' => $CFG_GLPI['admin_reply'],
'name' => $CFG_GLPI['admin_reply_name']];
return [
'email' => $CFG_GLPI['admin_reply'],
'name' => $CFG_GLPI['admin_reply_name']
];
}
......
......@@ -664,7 +664,7 @@ function loadDataset() {
* @param string $type
* @param string $name
* @param boolean $onlyid
* @return CommonGLPI|false the item, or its id
* @return CommonDBTM|false the item, or its id
*/
function getItemByTypeName($type, $name, $onlyid = false) {
......
......@@ -286,4 +286,63 @@ class Entity extends DbTestCase {
// Profile_User has been deleted when entity has been deleted
$this->boolean($profile_user->getFromDB($profile_user_id))->isFalse();
}
protected function inheritanceProvider() {
return [
['admin_email', "username+admin@domain.tld"],
['admin_email_name', "Username admin"],
['admin_reply', "username+admin+reply@domain.tld"],
['admin_reply_name', "Username admin reply"],
];
}
/**
* @dataProvider inheritanceProvider
*/
public function testGetUsedConfig(string $field, $value) {
$this->login();
$root = getItemByTypeName('Entity', 'Root entity', true);
$parent = getItemByTypeName('Entity', '_test_root_entity', true);
$child_1 = getItemByTypeName('Entity', '_test_child_1', true);
$child_2 = getItemByTypeName('Entity', '_test_child_2', true);
$entity = new \Entity;
$this->boolean($entity->update([
'id' => $root,
$field => $value."_root",
]));
$this->string(\Entity::getUsedConfig($field, $parent))->isEqualTo($value."_root");
$this->string(\Entity::getUsedConfig($field, $child_1))->isEqualTo($value."_root");
$this->string(\Entity::getUsedConfig($field, $child_2))->isEqualTo($value."_root");
$this->boolean($entity->update([
'id' => $parent,
$field => $value."_parent",
]));
$this->string(\Entity::getUsedConfig($field, $parent))->isEqualTo($value."_parent");
$this->string(\Entity::getUsedConfig($field, $child_1))->isEqualTo($value."_parent");
$this->string(\Entity::getUsedConfig($field, $child_2))->isEqualTo($value."_parent");
$this->boolean($entity->update([
'id' => $child_1,
$field => $value."_child_1",
]));
$this->string(\Entity::getUsedConfig($field, $parent))->isEqualTo($value."_parent");
$this->string(\Entity::getUsedConfig($field, $child_1))->isEqualTo($value."_child_1");
$this->string(\Entity::getUsedConfig($field, $child_2))->isEqualTo($value."_parent");
$this->boolean($entity->update([
'id' => $child_2,
$field => $value."_child_2",
]));
$this->string(\Entity::getUsedConfig($field, $parent))->isEqualTo($value."_parent");
$this->string(\Entity::getUsedConfig($field, $child_1))->isEqualTo($value."_child_1");
$this->string(\Entity::getUsedConfig($field, $child_2))->isEqualTo($value."_child_2");
}
}
<?php
/**
* ---------------------------------------------------------------------
* GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2015-2021 Teclib' and contributors.
*
* http://glpi-project.org
*
* based on GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2003-2014 by the INDEPNET Development Team.
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* GLPI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GLPI 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GLPI. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
*/
namespace tests\units;
use \DbTestCase;
/* Test for inc/notification.class.php */
class Notification extends DbTestCase {
public function testGetMailingSignature() {
global $CFG_GLPI;
$this->login();
$root = getItemByTypeName('Entity', 'Root entity', true);
$parent = getItemByTypeName('Entity', '_test_root_entity', true);
$child_1 = getItemByTypeName('Entity', '_test_child_1', true);
$child_2 = getItemByTypeName('Entity', '_test_child_2', true);
$CFG_GLPI['mailing_signature'] = 'global_signature';
$this->string(\Notification::getMailingSignature($parent))->isEqualTo("global_signature");
$this->string(\Notification::getMailingSignature($child_1))->isEqualTo("global_signature");
$this->string(\Notification::getMailingSignature($child_2))->isEqualTo("global_signature");
$entity = new \Entity;
$this->boolean($entity->update([
'id' => $root,
'mailing_signature' => "signature_root",
]))->isTrue();
$this->string(\Notification::getMailingSignature($parent))->isEqualTo("signature_root");
$this->string(\Notification::getMailingSignature($child_1))->isEqualTo("signature_root");
$this->string(\Notification::getMailingSignature($child_2))->isEqualTo("signature_root");
$this->boolean($entity->update([
'id' => $parent,
'mailing_signature' => "signature_parent",
]))->isTrue();
$this->string(\Notification::getMailingSignature($parent))->isEqualTo("signature_parent");
$this->string(\Notification::getMailingSignature($child_1))->isEqualTo("signature_parent");
$this->string(\Notification::getMailingSignature($child_2))->isEqualTo("signature_parent");
$this->boolean($entity->update([
'id' => $child_1,
'mailing_signature' => "signature_child_1",
]))->isTrue();
$this->string(\Notification::getMailingSignature($parent))->isEqualTo("signature_parent");
$this->string(\Notification::getMailingSignature($child_1))->isEqualTo("signature_child_1");
$this->string(\Notification::getMailingSignature($child_2))->isEqualTo("signature_parent");
$this->boolean($entity->update([
'id' => $child_2,
'mailing_signature' => "signature_child_2",
]))->isTrue();
$this->string(\Notification::getMailingSignature($parent))->isEqualTo("signature_parent");
$this->string(\Notification::getMailingSignature($child_1))->isEqualTo("signature_child_1");
$this->string(\Notification::getMailingSignature($child_2))->isEqualTo("signature_child_2");
}
}
<?php
/**
* ---------------------------------------------------------------------
* GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2015-2021 Teclib' and contributors.
*
* http://glpi-project.org
*
* based on GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2003-2014 by the INDEPNET Development Team.
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* GLPI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GLPI 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GLPI. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
*/
namespace tests\units;
use \DbTestCase;
/* Test for inc/notificationtarget.class.php */
class NotificationTarget extends DbTestCase {
public function testGetSubjectPrefix() {
$this->login();
$root = getItemByTypeName('Entity', 'Root entity', true);
$parent = getItemByTypeName('Entity', '_test_root_entity', true);
$child_1 = getItemByTypeName('Entity', '_test_child_1', true);
$child_2 = getItemByTypeName('Entity', '_test_child_2', true);
$ntarget_parent = new \NotificationTarget($parent);
$ntarget_child_1 = new \NotificationTarget($child_1);
$ntarget_child_2 = new \NotificationTarget($child_2);
$this->string($ntarget_parent->getSubjectPrefix())->isEqualTo("[GLPI] ");
$this->string($ntarget_child_1->getSubjectPrefix())->isEqualTo("[GLPI] ");
$this->string($ntarget_child_2->getSubjectPrefix())->isEqualTo("[GLPI] ");
$entity = new \Entity;
$this->boolean($entity->update([
'id' => $root,
'notification_subject_tag' => "prefix_root",
]))->isTrue();
$this->string($ntarget_parent->getSubjectPrefix())->isEqualTo("[prefix_root] ");
$this->string($ntarget_child_1->getSubjectPrefix())->isEqualTo("[prefix_root] ");
$this->string($ntarget_child_2->getSubjectPrefix())->isEqualTo("[prefix_root] ");
$this->boolean($entity->update([
'id' => $parent,
'notification_subject_tag' => "prefix_parent",
]))->isTrue();
$this->string($ntarget_parent->getSubjectPrefix())->isEqualTo("[prefix_parent] ");
$this->string($ntarget_child_1->getSubjectPrefix())->isEqualTo("[prefix_parent] ");
$this->string($ntarget_child_2->getSubjectPrefix())->isEqualTo("[prefix_parent] ");
$this->boolean($entity->update([
'id' => $child_1,
'notification_subject_tag' => "prefix_child_1",
]))->isTrue();
$this->string($ntarget_parent->getSubjectPrefix())->isEqualTo("[prefix_parent] ");
$this->string($ntarget_child_1->getSubjectPrefix())->isEqualTo("[prefix_child_1] ");
$this->string($ntarget_child_2->getSubjectPrefix())->isEqualTo("[prefix_parent] ");
$this->boolean($entity->update([
'id' => $child_2,
'notification_subject_tag' => "prefix_child_2",
]))->isTrue();
$this->string($ntarget_parent->getSubjectPrefix())->isEqualTo("[prefix_parent] ");
$this->string($ntarget_child_1->getSubjectPrefix())->isEqualTo("[prefix_child_1] ");
$this->string($ntarget_child_2->getSubjectPrefix())->isEqualTo("[prefix_child_2] ");
}
public function testGetReplyTo() {
global $CFG_GLPI;
$this->login();
$root = getItemByTypeName('Entity', 'Root entity', true);
$parent = getItemByTypeName('Entity', '_test_root_entity', true);
$child_1 = getItemByTypeName('Entity', '_test_child_1', true);
$child_2 = getItemByTypeName('Entity', '_test_child_2', true);
$ntarget_parent = new \NotificationTarget($parent);
$ntarget_child_1 = new \NotificationTarget($child_1);
$ntarget_child_2 = new \NotificationTarget($child_2);
// test global settings
$CFG_GLPI['admin_reply'] = 'test@global.tld';
$CFG_GLPI['admin_reply_name'] = 'test global';
$CFG_GLPI['from_email'] = '';
$this->array($ntarget_parent->getReplyTo())->isEqualTo([
'email' => 'test@global.tld',
'name' => 'test global'
]);
$this->array($ntarget_child_1->getReplyTo())->isEqualTo([
'email' => 'test@global.tld',
'name' => 'test global'
]);
$this->array($ntarget_child_2->getReplyTo())->isEqualTo([
'email' => 'test@global.tld',
'name' => 'test global'
]);
// test root entity settings
$entity = new \Entity;
$this->boolean($entity->update([
'id' => $root,
'admin_reply' => "test@root.tld",
'admin_reply_name' => "test root",
]))->isTrue();
$this->array($ntarget_parent->getReplyTo())->isEqualTo([
'email' => 'test@root.tld',
'name' => 'test root'
]);
$this->array($ntarget_child_1->getReplyTo())->isEqualTo([
'email' => 'test@root.tld',
'name' => 'test root'
]);
$this->array($ntarget_child_2->getReplyTo())->isEqualTo([
'email' => 'test@root.tld',
'name' => 'test root'
]);
// test parent entity settings
$this->boolean($entity->update([
'id' => $parent,
'admin_reply' => "test@parent.tld",
'admin_reply_name' => "test parent",
]))->isTrue();
$this->array($ntarget_parent->getReplyTo())->isEqualTo([
'email' => 'test@parent.tld',
'name' => 'test parent'
]);
$this->array($ntarget_child_1->getReplyTo())->isEqualTo([
'email' => 'test@parent.tld',
'name' => 'test parent'
]);
$this->array($ntarget_child_2->getReplyTo())->isEqualTo([
'email' => 'test@parent.tld',
'name' => 'test parent'
]);
// test child_1 entity settings
$this->boolean($entity->update([
'id' => $child_1,
'admin_reply' => "test@child1.tld",
'admin_reply_name' => "test child1",
]))->isTrue();
$this->array($ntarget_parent->getReplyTo())->isEqualTo([
'email' => 'test@parent.tld',
'name' => 'test parent'
]);
$this->array($ntarget_child_1->getReplyTo())->isEqualTo([
'email' => 'test@child1.tld',
'name' => 'test child1'
]);
$this->array($ntarget_child_2->getReplyTo())->isEqualTo([
'email' => 'test@parent.tld',
'name' => 'test parent'
]);
// test child_2 entity settings
$this->boolean($entity->update([
'id' => $child_2,
'admin_reply' => "test@child2.tld",
'admin_reply_name' => "test child2",
]))->isTrue();
$this->array($ntarget_parent->getReplyTo())->isEqualTo([
'email' => 'test@parent.tld',
'name' => 'test parent'
]);
$this->array($ntarget_child_1->getReplyTo())->isEqualTo([
'email' => 'test@child1.tld',
'name' => 'test child1'
]);
$this->array($ntarget_child_2->getReplyTo())->isEqualTo([
'email' => 'test@child2.tld',
'name' => 'test child2'
]);
}
}
Supports Markdown
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