Merge pull request #12956 from vingle/patch-3
[civicrm-core.git] / Civi / Core / SettingsManager.php
index e56d723b2fbaf501a18acd9953530bd9462b872b..cca82f79dec907c1138cda0889073f9b77d467a7 100644 (file)
@@ -171,13 +171,24 @@ class SettingsManager {
 
   /**
    * @param int|NULL $domainId
+   *   For the default domain, leave $domainID as NULL.
    * @param int|NULL $contactId
+   *   For the default/active user's contact, leave $domainID as NULL.
    * @return SettingsBag
+   * @throws \CRM_Core_Exception
+   *   If there is no contact, then there's no SettingsBag, and we'll throw
+   *   an exception.
    */
   public function getBagByContact($domainId, $contactId) {
     if ($domainId === NULL) {
       $domainId = \CRM_Core_Config::domainID();
     }
+    if ($contactId === NULL) {
+      $contactId = \CRM_Core_Session::getLoggedInContactID();
+      if (!$contactId) {
+        throw new \CRM_Core_Exception("Cannot access settings subsystem - user or domain is unavailable");
+      }
+    }
 
     $key = "$domainId:$contactId";
     if (!isset($this->bagsByContact[$key])) {