From 81edb54e6ad4056d9035d3cd7ac583e5fca0283b Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Wed, 16 Aug 2017 03:18:28 +0000
Subject: [PATCH] Check Thread Referral

Move referral checking to thread class
---
 include/class.thread.php | 45 ++++++++++++++++++++++++++++++++++++++--
 include/class.ticket.php |  3 +--
 2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/include/class.thread.php b/include/class.thread.php
index f6dffd7a8..532a100f5 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -278,12 +278,53 @@ implements Searchable {
     function getReferral($id, $type) {
 
         return $this->referrals->findFirst(array(
-                    'object_id' => $id,
-                    'object_type' => $type));
+                    'object_id'     => $id,
+                    'object_type'   => $type));
+    }
+
+    function isReferred($to=null, $strict=false) {
+
+        if (is_null($to))
+            return ($this->referrals);
+
+        switch (true) {
+        case $to instanceof Staff:
+            // Referred to the staff
+            if ($this->getReferral($to->getId(),
+                        ObjectModel::OBJECT_TYPE_STAFF))
+                return true;
+
+            // Strict check only checks the Agent
+            if ($strict)
+                return false;
+
+            // Referred to staff's department
+            if ($this->referrals->findFirst(array(
+                            'object_id__in' => $to->getDepts(),
+                            'object_type'   => ObjectModel::OBJECT_TYPE_DEPT)))
+                return true;
+            // Referred to staff's  team
+            if ($this->referrals->findFirst(array(
+                            'object_id__in' => $to->getTeams(),
+                            'object_type'   => ObjectModel::OBJECT_TYPE_TEAM)))
+                return true;
+            break;
+        case $to instanceof Dept:
+            // Refered to the dept
+            if ($this->getReferral($to->getId,
+                        ObjectModel::OBJECT_TYPE_DEPT))
+                return true;
+            break;
+        }
+
+        return false;
     }
 
     function refer($to) {
 
+        if ($this->isReferred($to, true))
+            return true;
+
         $vars = array('thread_id' => $this->getId());
         switch (true) {
         case $to instanceof Staff:
diff --git a/include/class.ticket.php b/include/class.ticket.php
index d98d504a3..c73b897fa 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -286,8 +286,7 @@ implements RestrictedAccess, Threadable, Searchable {
             && $this->isOpen()
             && $staff->getId() != $this->getStaffId()
             && !$staff->isTeamMember($this->getTeamId())
-            && !$this->thread->getReferral($staff->getId(),
-                ObjectModel::OBJECT_TYPE_STAFF)
+            && !$this->thread->isReferred($staff)
         ) {
             return false;
         }
-- 
GitLab