Skip to content

Commit

Permalink
fix(target): deduplicate actors
Browse files Browse the repository at this point in the history
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 <tbugier@teclib.com>
  • Loading branch information
btry committed Sep 10, 2018
1 parent c8bc82a commit d5d4e0c
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions inc/targetbase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,31 @@ abstract public function export($remove_uuid = false);
abstract public function save(PluginFormcreatorForm_Answer $formanswer);

/**
* Gets an instance object for the relation between the target itemtype
* Gets an instance object for the relation between the target itemtype
* and an user
*
* @return CommonDBTM
*/
abstract protected function getItem_User();

/**
* Gets an instance object for the relation between the target itemtype
* Gets an instance object for the relation between the target itemtype
* and a group
*
* @return CommonDBTM
*/
abstract protected function getItem_Group();

/**
* Gets an instance object for the relation between the target itemtype
* Gets an instance object for the relation between the target itemtype
* and supplier
*
* @return CommonDBTM
*/
abstract protected function getItem_Supplier();

/**
* Gets an instance object for the relation between the target itemtype
* Gets an instance object for the relation between the target itemtype
* and an object of any itemtype
*
* @return CommonDBTM
Expand Down Expand Up @@ -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) {
Expand All @@ -320,25 +320,40 @@ protected function addActor($role, $user, $notify) {
}

$actorType = null;
$actorTypeNotif = null;
switch ($role) {
case 'requester':
$actorType = &$this->requesters['_users_id_requester'];
$actorTypeNotif = &$this->requesters['_users_id_requester_notif'];
break;
case 'observer':
$actorType = &$this->observers['_users_id_observer'];
$actorTypeNotif = &$this->observers['_users_id_observer_notif'];
break;
case 'assigned' :
$actorType = &$this->assigned['_users_id_assign'];
$actorTypeNotif = &$this->assigned['_users_id_assign_notif'];
break;
case 'supplier' :
$actorType = &$this->assignedSuppliers['_suppliers_id_assign'];
$actorTypeNotif = &$this->assignedSuppliers['_suppliers_id_assign_notif'];
break;
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;
$actorTypeNotif['use_notification'][] = ($notify == true);
$actorTypeNotif['alternative_email'][] = $alternativeEmail;
} else {
// New actor settings takes precedence
$actorType[$actorKey] = $userId;
$actorTypeNotif['use_notification'][$actorKey] = ($notify == true);
$actorTypeNotif['alternative_email'][$actorKey] = $alternativeEmail;
}

return true;
}
Expand All @@ -347,20 +362,34 @@ protected function addActor($role, $user, $notify) {
* Adds a group to the given actor role
*
* @param string $role Role of the group
* @param string $group Group ID
* @param string $group Group ID
* @return boolean true on sucess, false on error
*/
protected function addGroupActor($role, $group) {
protected function addGroupActor($role, $group) {
$actorType = null;
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;
}

/**
Expand Down

0 comments on commit d5d4e0c

Please sign in to comment.