Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto-assign from category/item #12130

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 118 additions & 50 deletions src/CommonITILObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -8219,24 +8219,19 @@ protected function getActorObjectForItem(string $itemtype = ""): CommonITILActor
*/
protected function setTechAndGroupFromItilCategory($input)
{
$cat = new ITILCategory();
$has_user_assigned = $this->hasValidActorInInput($input, User::class, CommonITILActor::ASSIGN);
$has_group_assigned = $this->hasValidActorInInput($input, Group::class, CommonITILActor::ASSIGN);
if (
($input['itilcategories_id'] > 0)
&& ((!isset($input['_users_id_assign']) || !$input['_users_id_assign'])
|| (!isset($input['_groups_id_assign']) || !$input['_groups_id_assign']))
$input['itilcategories_id'] > 0
&& (!$has_user_assigned || !$has_group_assigned)
&& $cat->getFromDB($input['itilcategories_id'])
) {
$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 (!$has_user_assigned && $cat->fields['users_id'] > 0) {
$input['_users_id_assign'] = $cat->fields['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 (!$has_group_assigned && $cat->fields['groups_id'] > 0) {
$input['_groups_id_assign'] = $cat->fields['groups_id'];
}
}

Expand All @@ -8252,19 +8247,16 @@ protected function setTechAndGroupFromItilCategory($input)
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 tech from item
$has_user_assigned = $this->hasValidActorInInput($input, User::class, CommonITILActor::ASSIGN);
if (!$has_user_assigned && $item->isField('users_id_tech') && $item->fields['users_id_tech'] > 0) {
$input['_users_id_assign'] = $item->fields['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 group from item
$has_group_assigned = $this->hasValidActorInInput($input, Group::class, CommonITILActor::ASSIGN);
if (!$has_group_assigned && $item->isField('groups_id_tech') && $item->fields['groups_id_tech'] > 0) {
$input['_groups_id_assign'] = $item->fields['groups_id_tech'];
}
}

Expand All @@ -8287,29 +8279,9 @@ protected function assign(array $input)

if (
(
(
isset($input['_actors']['assign'])
&& is_array($input['_actors']['assign'])
&& count($input['_actors']['assign']) > 0
) || (
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
)
)
$this->hasValidActorInInput($input, User::class, CommonITILActor::ASSIGN)
|| $this->hasValidActorInInput($input, Group::class, CommonITILActor::ASSIGN)
|| $this->hasValidActorInInput($input, Supplier::class, CommonITILActor::ASSIGN)
)
&& (in_array($input['status'], $this->getNewStatusArray()))
&& !$this->isStatusComputationBlocked($input)
Expand All @@ -8320,6 +8292,102 @@ protected function assign(array $input)
return $input;
}

/**
* Check if input contains a valid actor for given itemtype / actortype.
*
* @param array $input
* @param string $itemtype
* @param string $actortype
*
* @return bool
*/
private function hasValidActorInInput(array $input, string $itemtype, string $actortype): bool
{
$input_id_key = sprintf(
'_%s_%s',
getForeignKeyFieldForItemType($itemtype),
self::getActorFieldNameType($actortype)
);
$input_notif_key = sprintf(
'%s_notif',
$input_id_key
);

$has_valid_actor = false;
if (array_key_exists($input_id_key, $input)) {
if (is_array($input[$input_id_key]) && !empty($input[$input_id_key])) {
foreach ($input[$input_id_key] as $key => $actor_id) {
if (
// actor with valid ID
(int)$actor_id > 0
// or "email" actor
|| (
$itemtype === User::class
&& (int)$actor_id === 0
&& array_key_exists($input_notif_key, $input)
&& (bool)($input[$input_notif_key]['use_notification'][$key] ?? false) === true
&& !empty($input[$input_notif_key]['alternative_email'][$key])
)
) {
$has_valid_actor = true;
break;
}
}
} elseif (is_numeric($input[$input_id_key])) {
$actor_id = (int)$input[$input_id_key];
if (
// actor with valid ID
$actor_id > 0
// or "email" actor
|| (
$itemtype === User::class
&& $actor_id === 0
// Expected formats
//
// Value provided by Change::getDefaultValues()
// '_users_id_requester_notif' => [
// 'use_notification' => 1,
// 'alternative_email' => 'user1@example.com',
// ]
//
// OR
//
// Value provided by Ticket::getDefaultValues()
// '_users_id_requester_notif' => [
// 'use_notification' => [1, 0],
// 'alternative_email' => ['user1@example.com', 'user2@example.com'],
// ]
&& array_key_exists($input_notif_key, $input)
&& (
array_key_exists('use_notification', $input[$input_notif_key])
&& (
(
is_array($input[$input_notif_key]['use_notification'])
&& (bool)($input[$input_notif_key]['use_notification'][0] ?? false) === true
)
|| (bool)($input[$input_notif_key]['use_notification'] ?? false) === true
)
)
&& (
array_key_exists('alternative_email', $input[$input_notif_key])
&& (
(
is_array($input[$input_notif_key]['alternative_email'])
&& !empty($input[$input_notif_key]['alternative_email'][0])
)
|| !empty($input[$input_notif_key]['alternative_email'])
)
)
)
) {
$has_valid_actor = true;
}
}
}

return $has_valid_actor;
}

/**
* Parameter class to be use for this item (user templates)
* @return string class name
Expand Down
Loading