From 72cfb440bca3ac65d23546930ee8423f835f32f2 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Sun, 9 Sep 2018 22:18:53 +0200 Subject: [PATCH] fix(target): deduplicate actors if an actor is added several times (from ticket template and form settings) then unicity in SQL fails and breaks target creation @see #1089 Signed-off-by: Thierry Bugier --- inc/targetbase.class.php | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/inc/targetbase.class.php b/inc/targetbase.class.php index 0005db8cd7..e0dbad205f 100644 --- a/inc/targetbase.class.php +++ b/inc/targetbase.class.php @@ -304,7 +304,7 @@ protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorF * @param string $role role of the user * @param string $user user ID or email address for anonymous users * @param boolean $notify true to enable notification for the actor - * @return void + * @return boolean true on success, false on error */ protected function addActor($role, $user, $notify) { if (filter_var($user, FILTER_VALIDATE_EMAIL) !== false) { @@ -336,9 +336,19 @@ protected function addActor($role, $user, $notify) { default: return false; } - $actorType[] = $userId; - $actorType['use_notification'][] = ($notify == true); - $actorType['alternative_email'][] = $alternativeEmail; + + $actorKey = array_search($userId, $actorType); + if ($actorKey === false) { + // Add the actor + $actorType[] = $userId; + $actorType['use_notification'][] = ($notify == true); + $actorType['alternative_email'][] = $alternativeEmail; + } else { + // New actor settings takes precedence + $actorType[$actorKey] = $userId; + $actorType['use_notification'][$actorKey] = ($notify == true); + $actorType['alternative_email'][$actorKey] = $alternativeEmail; + } return true; } @@ -348,19 +358,32 @@ protected function addActor($role, $user, $notify) { * * @param string $role Role of the group * @param string $group Group ID + * @return boolean true on sucess, false on error */ protected function addGroupActor($role, $group) { switch ($role) { case 'requester': - $this->requesterGroups['_groups_id_requester'][] = $group; + $actorType = &$this->requesterGroups['_groups_id_requester']; break; case 'observer' : - $this->observerGroups['_groups_id_observer'][] = $group; + $actorType = &$this->observerGroups['_groups_id_observer']; break; case 'assigned' : - $this->assignedGroups['_groups_id_assign'][] = $group; + $actorType = &$this->assignedGroups['_groups_id_assign']; break; + default: + return false; } + + $actorKey = array_search($userId, $actorType); + if ($actorKey !== false) { + return false; + } + + // Add the group actor + $actorType[] = $group; + + return true; } /**