From 4ba80a363c45553eddeca4adc16def5445042ce9 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 17 Mar 2015 11:38:26 -0500
Subject: [PATCH] kb: Add setting to enable only for authenticated clients

---
 include/class.config.php                      | 16 ++++++++++++++--
 include/i18n/en_US/help/tips/settings.kb.yaml |  9 +++++++++
 include/staff/settings-kb.inc.php             |  8 ++++++--
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/include/class.config.php b/include/class.config.php
index e61c06814..73261e24c 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -210,6 +210,13 @@ class OsticketConfig extends Config {
     }
 
     function isKnowledgebaseEnabled() {
+        global $thisclient;
+
+        if ($this->get('restrict_kb', false)
+            && (!$thisclient || $thisclient->isGuest())
+        ) {
+            return false;
+        }
         require_once(INCLUDE_DIR.'class.faq.php');
         return ($this->get('enable_kb') && FAQ::countPublishedFAQs());
     }
@@ -1089,11 +1096,16 @@ class OsticketConfig extends Config {
 
     function updateKBSettings($vars, &$errors) {
 
-        if($errors) return false;
+        if ($vars['restrict_kb'] && !$this->isClientRegistrationEnabled())
+            $errors['restrict_kb'] =
+                __('The knowledge base cannot be restricted unless client registration is enabled');
+
+        if ($errors) return false;
 
         return $this->updateAll(array(
             'enable_kb'=>isset($vars['enable_kb'])?1:0,
-               'enable_premade'=>isset($vars['enable_premade'])?1:0,
+            'restrict_kb'=>isset($vars['restrict_kb'])?1:0,
+            'enable_premade'=>isset($vars['enable_premade'])?1:0,
         ));
     }
 
diff --git a/include/i18n/en_US/help/tips/settings.kb.yaml b/include/i18n/en_US/help/tips/settings.kb.yaml
index 12f495472..702dac9d5 100644
--- a/include/i18n/en_US/help/tips/settings.kb.yaml
+++ b/include/i18n/en_US/help/tips/settings.kb.yaml
@@ -28,6 +28,15 @@ knowledge_base_status:
       - title: Manage Knowledge Base
         href: /scp/kb.php
 
+restrict_kb:
+    title: Resctrict Access to the Knowledge Base
+    content: >
+        Enable this setting to prevent unregistered users from accessing
+        your knowledge base articles on the client interface.
+    links:
+      - title: Access Control Settings
+        href: /scp/settings.php?t=access
+
 canned_responses:
     title: Canned Responses
     content: >
diff --git a/include/staff/settings-kb.inc.php b/include/staff/settings-kb.inc.php
index cfac97418..941b08e30 100644
--- a/include/staff/settings-kb.inc.php
+++ b/include/staff/settings-kb.inc.php
@@ -16,12 +16,16 @@ if(!defined('OSTADMININC') || !$thisstaff || !$thisstaff->isAdmin() || !$config)
     </thead>
     <tbody>
         <tr>
-            <td width="180"><?php echo __('Knowledge Base Status'); ?>:</td>
+            <td width="180" valign="top"><?php echo __('Knowledge Base Status'); ?>:</td>
             <td>
                 <input type="checkbox" name="enable_kb" value="1" <?php echo $config['enable_kb']?'checked="checked"':''; ?>>
                 <?php echo __('Enable Knowledge Base'); ?>
-                &nbsp;<font class="error">&nbsp;<?php echo $errors['enable_kb']; ?></font>
                 <i class="help-tip icon-question-sign" href="#knowledge_base_status"></i>
+                <div class="error"><?php echo $errors['enable_kb']; ?></div>
+                <input type="checkbox" name="restrict_kb" value="1" <?php echo $config['restrict_kb']?'checked="checked"':''; ?> >
+                <?php echo __('Require Client Login'); ?>
+                <i class="help-tip icon-question-sign" href="#restrict_kb"></i>
+                <div class="error"><?php echo $errors['restrict_kb']; ?></div>
             </td>
         </tr>
         <tr>
-- 
GitLab