Unverified Commit 04365b2c authored by btry's avatar btry Committed by GitHub
Browse files

auto assign tech and group for change and problem (#7789)



* auto assign for change

Signed-off-by: default avatarThierry Bugier <tbugier@teclib.com>

* auto assign for problem

Signed-off-by: default avatarThierry Bugier <tbugier@teclib.com>

* factorize auto auto assign of ITIL objects

Signed-off-by: default avatarThierry Bugier <tbugier@teclib.com>

* add tests

Signed-off-by: default avatarThierry Bugier <tbugier@teclib.com>
parent a1cc9ac8
......@@ -140,6 +140,33 @@ class Change extends CommonITILObject {
}
function prepareInputForAdd($input) {
$input = parent::prepareInputForAdd($input);
if ($input === false) {
return false;
}
// Manage auto assign
$auto_assign_mode = Entity::getUsedConfig('auto_assign_mode', $input['entities_id']);
switch ($auto_assign_mode) {
case Entity::CONFIG_NEVER :
break;
case Entity::AUTO_ASSIGN_HARDWARE_CATEGORY :
case Entity::AUTO_ASSIGN_CATEGORY_HARDWARE :
// Auto assign tech/group from Category
// Changes are not associated to a hardware then both settings behave the same way
$input = $this->setTechAndGroupFromItilCategory($input);
break;
}
$input = $this->assign($input);
return $input;
}
function pre_deleteItem() {
global $CFG_GLPI;
......
......@@ -8279,4 +8279,78 @@ abstract class CommonITILObject extends CommonDBTM {
}
}
/**
* Fill the tech and the group from the category
* @param array $input
* @return array
*/
protected function setTechAndGroupFromItilCategory($input) {
if (($input['itilcategories_id'] > 0)
&& ((!isset($input['_users_id_assign']) || !$input['_users_id_assign'])
|| (!isset($input['_groups_id_assign']) || !$input['_groups_id_assign']))) {
$cat = new ITILCategory();
$cat->getFromDB($input['itilcategories_id']);
if ((!isset($input['_users_id_assign']) || !$input['_users_id_assign'])
&& $cat->isField('users_id')) {
$input['_users_id_assign'] = $cat->getField('users_id');
}
if ((!isset($input['_groups_id_assign']) || !$input['_groups_id_assign'])
&& $cat->isField('groups_id')) {
$input['_groups_id_assign'] = $cat->getField('groups_id');
}
}
return $input;
}
/**
* Fill the tech and the group from the hardware
* @param array $input
* @return array
*/
protected function setTechAndGroupFromHardware($input, $item) {
if ($item != null) {
// Auto assign tech from item
if ((!isset($input['_users_id_assign']) || ($input['_users_id_assign'] == 0))
&& $item->isField('users_id_tech')) {
$input['_users_id_assign'] = $item->getField('users_id_tech');
}
// Auto assign group from item
if ((!isset($input['_groups_id_assign']) || ($input['_groups_id_assign'] == 0))
&& $item->isField('groups_id_tech')) {
$input['_groups_id_assign'] = $item->getField('groups_id_tech');
}
}
return $input;
}
/**
* Replay setting auto assign if set in rules engine or by auto_assign_mode
* Do not force status if status has been set by rules
*
* @param array $input
*
* @return array
*/
protected function assign(array $input) {
if (((isset($input["_users_id_assign"])
&& ((!is_array($input['_users_id_assign']) && $input["_users_id_assign"] > 0)
|| is_array($input['_users_id_assign']) && count($input['_users_id_assign']) > 0))
|| (isset($input["_groups_id_assign"])
&& ((!is_array($input['_groups_id_assign']) && $input["_groups_id_assign"] > 0)
|| is_array($input['_groups_id_assign']) && count($input['_groups_id_assign']) > 0))
|| (isset($input["_suppliers_id_assign"])
&& ((!is_array($input['_suppliers_id_assign']) && $input["_suppliers_id_assign"] > 0)
|| is_array($input['_suppliers_id_assign']) && count($input['_suppliers_id_assign']) > 0)))
&& (in_array($input['status'], $this->getNewStatusArray()))
&& !$this->isStatusComputationBlocked($input)) {
$input["status"] = self::ASSIGNED;
}
return $input;
}
}
......@@ -2546,7 +2546,8 @@ class Entity extends CommonTreeDropdown {
}
echo "</td></tr>";
echo "<tr class='tab_bg_1'><td colspan='2'>".__('Automatic assignment of tickets')."</td>";
// Auto assign mode
echo "<tr class='tab_bg_1'><td colspan='2'>".__('Automatic assignment of tickets, changes and problems')."</td>";
echo "<td colspan='2'>";
$autoassign = self::getAutoAssignMode();
......@@ -2982,10 +2983,12 @@ class Entity extends CommonTreeDropdown {
**/
static function getAutoAssignMode($val = null) {
$tab = [self::CONFIG_PARENT => __('Inheritance of the parent entity'),
self::CONFIG_NEVER => __('No'),
self::AUTO_ASSIGN_HARDWARE_CATEGORY => __('Based on the item then the category'),
self::AUTO_ASSIGN_CATEGORY_HARDWARE => __('Based on the category then the item')];
$tab = [
self::CONFIG_PARENT => __('Inheritance of the parent entity'),
self::CONFIG_NEVER => __('No'),
self::AUTO_ASSIGN_HARDWARE_CATEGORY => __('Based on the item then the category'),
self::AUTO_ASSIGN_CATEGORY_HARDWARE => __('Based on the category then the item'),
];
if (is_null($val)) {
return $tab;
......
......@@ -284,17 +284,28 @@ class Problem extends CommonITILObject {
function prepareInputForAdd($input) {
$input = parent::prepareInputForAdd($input);
if ($input === false) {
return false;
}
if (((isset($input["_users_id_assign"]) && ($input["_users_id_assign"] > 0))
|| (isset($input["_groups_id_assign"]) && ($input["_groups_id_assign"] > 0))
|| (isset($input["_suppliers_id_assign"]) && ($input["_suppliers_id_assign"] > 0)))
&& (in_array($input['status'], $this->getNewStatusArray()))) {
// Manage auto assign
$auto_assign_mode = Entity::getUsedConfig('auto_assign_mode', $input['entities_id']);
$input["status"] = self::ASSIGNED;
switch ($auto_assign_mode) {
case Entity::CONFIG_NEVER :
break;
case Entity::AUTO_ASSIGN_HARDWARE_CATEGORY :
case Entity::AUTO_ASSIGN_CATEGORY_HARDWARE :
// Auto assign tech/group from Category
// Problems are not associated to a hardware then both settings behave the same way
$input = $this->setTechAndGroupFromItilCategory($input);
break;
}
$input = $this->assign($input);
return $input;
}
......
......@@ -1687,83 +1687,21 @@ class Ticket extends CommonITILObject {
break;
case Entity::AUTO_ASSIGN_HARDWARE_CATEGORY :
if ($item != null) {
// Auto assign tech from item
if ((!isset($input['_users_id_assign']) || ($input['_users_id_assign'] == 0))
&& $item->isField('users_id_tech')) {
$input['_users_id_assign'] = $item->getField('users_id_tech');
}
// Auto assign group from item
if ((!isset($input['_groups_id_assign']) || ($input['_groups_id_assign'] == 0))
&& $item->isField('groups_id_tech')) {
$input['_groups_id_assign'] = $item->getField('groups_id_tech');
}
}
// Auto assign tech/group from hardware
$input = $this->setTechAndGroupFromHardware($input, $item);
// Auto assign tech/group from Category
if (($input['itilcategories_id'] > 0)
&& ((!isset($input['_users_id_assign']) || !$input['_users_id_assign'])
|| (!isset($input['_groups_id_assign']) || !$input['_groups_id_assign']))) {
$cat = new ITILCategory();
$cat->getFromDB($input['itilcategories_id']);
if ((!isset($input['_users_id_assign']) || !$input['_users_id_assign'])
&& $cat->isField('users_id')) {
$input['_users_id_assign'] = $cat->getField('users_id');
}
if ((!isset($input['_groups_id_assign']) || !$input['_groups_id_assign'])
&& $cat->isField('groups_id')) {
$input['_groups_id_assign'] = $cat->getField('groups_id');
}
}
$input = $this->setTechAndGroupFromItilCategory($input);
break;
case Entity::AUTO_ASSIGN_CATEGORY_HARDWARE :
// Auto assign tech/group from Category
if (($input['itilcategories_id'] > 0)
&& ((!isset($input['_users_id_assign']) || !$input['_users_id_assign'])
|| (!isset($input['_groups_id_assign']) || !$input['_groups_id_assign']))) {
$cat = new ITILCategory();
$cat->getFromDB($input['itilcategories_id']);
if ((!isset($input['_users_id_assign']) || !$input['_users_id_assign'])
&& $cat->isField('users_id')) {
$input['_users_id_assign'] = $cat->getField('users_id');
}
if ((!isset($input['_groups_id_assign']) || !$input['_groups_id_assign'])
&& $cat->isField('groups_id')) {
$input['_groups_id_assign'] = $cat->getField('groups_id');
}
}
if ($item != null) {
// Auto assign tech from item
if ((!isset($input['_users_id_assign']) || ($input['_users_id_assign'] == 0))
&& $item->isField('users_id_tech')) {
$input['_users_id_assign'] = $item->getField('users_id_tech');
}
// Auto assign group from item
if ((!isset($input['_groups_id_assign']) || ($input['_groups_id_assign'] == 0))
&& $item->isField('groups_id_tech')) {
$input['_groups_id_assign'] = $item->getField('groups_id_tech');
}
}
$input = $this->setTechAndGroupFromItilCategory($input);
// Auto assign tech/group from hardware
$input = $this->setTechAndGroupFromHardware($input, $item);
break;
}
// Replay setting auto assign if set in rules engine or by auto_assign_mode
// Do not force status if status has been set by rules
if (((isset($input["_users_id_assign"])
&& ((!is_array($input['_users_id_assign']) && $input["_users_id_assign"] > 0)
|| is_array($input['_users_id_assign']) && count($input['_users_id_assign']) > 0))
|| (isset($input["_groups_id_assign"])
&& ((!is_array($input['_groups_id_assign']) && $input["_groups_id_assign"] > 0)
|| is_array($input['_groups_id_assign']) && count($input['_groups_id_assign']) > 0))
|| (isset($input["_suppliers_id_assign"])
&& ((!is_array($input['_suppliers_id_assign']) && $input["_suppliers_id_assign"] > 0)
|| is_array($input['_suppliers_id_assign']) && count($input['_suppliers_id_assign']) > 0)))
&& (in_array($input['status'], $this->getNewStatusArray()))
&& !$this->isStatusComputationBlocked($input)) {
$input["status"] = self::ASSIGNED;
}
$input = $this->assign($input);
// Manage SLA / OLA asignment
// Manual SLA / OLA defined : reset due date
......
......@@ -56,4 +56,118 @@ class Change extends DbTestCase {
$change_item = new \Change_Item;
$this->boolean($change_item->getFromDBForItems($change, $computer))->isTrue();
}
public function testAssignFromCategory() {
$this->login('glpi', 'glpi');
$entity = new \Entity();
$entityId = $entity->import([
'name' => 'an entity configured to check change auto assignation of user ad group',
'entities_id' => 0,
'level' => 1,
'auto_assign_mode' => \Entity::CONFIG_NEVER,
]);
$this->boolean($entity->isNewID($entityId))->isFalse();
$entity->getFromDB($entityId);
$this->integer((int) $entity->fields['auto_assign_mode'])->isEqualTo(\Entity::CONFIG_NEVER);
// Login again to acess the new entity
$this->login('glpi', 'glpi');
$success = \Session::changeActiveEntities($entity->getID(), true);
$this->boolean($success)->isTrue();
$group = new \Group();
$group->add([
'name' => 'A group to check automatic tech and group assignation',
'entities_id' => 0,
'is_recursive' => '1',
'level' => 0,
]);
$this->boolean($group->isNewItem())->isFalse();
$itilCategory = new \ITILCategory();
$itilCategory->add([
'name' => 'A category to check automatic tech and group assignation',
'itilcategories_id' => 0,
'users_id' => 4, // Tech
'groups_id' => $group->getID(),
]);
$this->boolean($itilCategory->isNewItem())->isFalse();
$change = new \Change();
$change->add([
'name' => 'A change to check if it is not automatically assigned user and group',
'content' => 'foo',
'itilcategories_id' => $itilCategory->getID(),
]);
$this->boolean($change->isNewItem())->isFalse();
$change->getFromDB($change->getID());
$changeUser = new \Change_User();
$changeGroup = new \Change_Group();
$rows = $changeUser->find([
'changes_id' => $change->getID(),
'type' => \CommonITILActor::ASSIGN,
]);
$this->integer(count($rows))->isEqualTo(0);
$rows = $changeGroup->find([
'changes_id' => $change->getID(),
'type' => \CommonITILActor::ASSIGN,
]);
// check Entity::AUTO_ASSIGN_HARDWARE_CATEGORY assignment
$entity->update([
'id' => $entity->getID(),
'auto_assign_mode' => \Entity::AUTO_ASSIGN_HARDWARE_CATEGORY,
]);
$change = new \Change();
$change->add([
'name' => 'A change to check if it is automatically assigned user and group (1)',
'content' => 'foo',
'itilcategories_id' => $itilCategory->getID(),
]);
$this->boolean($change->isNewItem())->isFalse();
$change->getFromDB($change->getID());
$changeUser = new \Change_User();
$changeGroup = new \Change_Group();
$rows = $changeUser->find([
'changes_id' => $change->getID(),
'users_id' => 4, // Tech
'type' => \CommonITILActor::ASSIGN,
]);
$this->integer(count($rows))->isEqualTo(0);
$rows = $changeGroup->find([
'changes_id' => $change->getID(),
'groups_id' => $group->getID(),
'type' => \CommonITILActor::ASSIGN,
]);
// check Entity::AUTO_ASSIGN_CATEGORY_HARDWARE assignment
$entity->update([
'id' => $entity->getID(),
'auto_assign_mode' => \Entity::AUTO_ASSIGN_CATEGORY_HARDWARE,
]);
$change = new \Change();
$change->add([
'name' => 'A change to check if it is automatically assigned user and group (2)',
'content' => 'foo',
'itilcategories_id' => $itilCategory->getID(),
]);
$this->boolean($change->isNewItem())->isFalse();
$change->getFromDB($change->getID());
$changeUser = new \Change_User();
$changeGroup = new \Change_Group();
$rows = $changeUser->find([
'changes_id' => $change->getID(),
'users_id' => 4, // Tech
'type' => \CommonITILActor::ASSIGN,
]);
$this->integer(count($rows))->isEqualTo(0);
$rows = $changeGroup->find([
'changes_id' => $change->getID(),
'groups_id' => $group->getID(),
'type' => \CommonITILActor::ASSIGN,
]);
}
}
......@@ -187,7 +187,7 @@ class ITILSolution extends DbTestCase {
]))->isGreaterThan(0);
$this->boolean($change->isNewItem())->isFalse();
$this->variable($change->getField('status'))->isIdenticalTo($change::INCOMING);
$this->variable($change->getField('status'))->isIdenticalTo($change::ASSIGNED);
$solution = new \ITILSolution();
$this->integer(
......
......@@ -56,4 +56,119 @@ class Problem extends DbTestCase {
$problem_item = new \Item_Problem;
$this->boolean($problem_item->getFromDBForItems($problem, $computer))->isTrue();
}
public function testAssignFromCategory() {
$this->login('glpi', 'glpi');
$entity = new \Entity();
$entityId = $entity->import([
'name' => 'an entity configured to check problem auto assignation of user ad group',
'entities_id' => 0,
'level' => 0,
'auto_assign_mode' => \Entity::CONFIG_NEVER,
]);
$this->boolean($entity->isNewID($entityId))->isFalse();
$entity->getFromDB($entityId);
$this->integer((int) $entity->fields['auto_assign_mode'])->isEqualTo(\Entity::CONFIG_NEVER);
// Login again to acess the new entity
$this->login('glpi', 'glpi');
$success = \Session::changeActiveEntities($entity->getID(), true);
$this->boolean($success)->isTrue();
$group = new \Group();
$group->add([
'name' => 'A group to check automatic tech and group assignation',
'entities_id' => 0,
'is_recursive' => '1',
'level' => 0,
]);
$this->boolean($group->isNewItem())->isFalse();
$itilCategory = new \ITILCategory();
$itilCategory->add([
'name' => 'A category to check automatic tech and group assignation',
'itilcategories_id' => 0,
'users_id' => 4, // Tech
'groups_id' => $group->getID(),
]);
$this->boolean($itilCategory->isNewItem())->isFalse();
$problem = new \Problem();
$problem->add([
'name' => 'A problem to check if it is not automatically assigned user and group',
'content' => 'foo',
'itilcategories_id' => $itilCategory->getID(),
]);
$this->boolean($problem->isNewItem())->isFalse();
$problem->getFromDB($problem->getID());
$problemUser = new \Problem_User();
$groupProblem = new \group_Problem();
$rows = $problemUser->find([
'problems_id' => $problem->getID(),
'type' => \CommonITILActor::ASSIGN,
]);
$this->integer(count($rows))->isEqualTo(0);
$rows = $groupProblem->find([
'problems_id' => $problem->getID(),
'type' => \CommonITILActor::ASSIGN,
]);
$this->integer(count($rows))->isEqualTo(0);
// check Entity::AUTO_ASSIGN_HARDWARE_CATEGORY assignment
$entity->update([
'id' => $entity->getID(),
'auto_assign_mode' => \Entity::AUTO_ASSIGN_HARDWARE_CATEGORY,
]);
$problem = new \Problem();
$problem->add([
'name' => 'A problem to check if it is automatically assigned user and group (1)',
'content' => 'foo',
'itilcategories_id' => $itilCategory->getID(),
]);
$this->boolean($problem->isNewItem())->isFalse();
$problem->getFromDB($problem->getID());
$problemUser = new \Problem_User();
$groupProblem = new \group_Problem();
$rows = $problemUser->find([
'problems_id' => $problem->getID(),
'users_id' => 4, // tech
'type' => \CommonITILActor::ASSIGN,
]);
$this->integer(count($rows))->isEqualTo(0);
$rows = $groupProblem->find([
'problems_id' => $problem->getID(),
'groups_id' => $group->getID(),
'type' => \CommonITILActor::ASSIGN,
]);
// check Entity::AUTO_ASSIGN_CATEGORY_HARDWARE assignment
$entity->update([
'id' => $entity->getID(),
'auto_assign_mode' => \Entity::AUTO_ASSIGN_CATEGORY_HARDWARE,
]);
$problem = new \Problem();
$problem->add([
'name' => 'A problem to check if it is automatically assigned user and group (2)',
'content' => 'foo',
'itilcategories_id' => $itilCategory->getID(),
]);
$this->boolean($problem->isNewItem())->isFalse();
$problem->getFromDB($problem->getID());
$problemUser = new \Problem_User();
$groupProblem = new \group_Problem();
$rows = $problemUser->find([
'problems_id' => $problem->getID(),
'users_id' => 4, // tech
'type' => \CommonITILActor::ASSIGN,
]);
$this->integer(count($rows))->isEqualTo(0);
$rows = $groupProblem->find([
'problems_id' => $problem->getID(),
'groups_id' => $group->getID(),
'type' => \CommonITILActor::ASSIGN,
]);
}
}
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