From 29fef3160f2a46aa5d14d2734dd12fbf21cb1605 Mon Sep 17 00:00:00 2001
From: Dominik Frantisek Bucik <bucik@ics.muni.cz>
Date: Tue, 28 Nov 2023 09:00:22 +0100
Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Cron=20enable/disable=20?=
 =?UTF-8?q?flag?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Configuration option 'cronEnabled' to enable/disable cron.
---
 config-templates/module_proxystatistics.php | 16 ++++++++++++----
 hooks/hook_cron.php                         |  8 +++++++-
 lib/Config.php                              | 10 ++++++++++
 3 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/config-templates/module_proxystatistics.php b/config-templates/module_proxystatistics.php
index c0dfbe2..ad4b0de 100644
--- a/config-templates/module_proxystatistics.php
+++ b/config-templates/module_proxystatistics.php
@@ -104,9 +104,17 @@ $config = [
      */
     //'apiWritePasswordHash' => password_hash('ap1Wr1T3rP@S$'),
 
-    'ignoredIds' => [
-        'IDP' => ['spentityid1', 'spentityid2', 'spentityid3',],
-        'SP' => ['idpentityid1', 'idpentityid2', 'idpentityid3',],
-    ],
+    /*
+     * List of IDP and/or SP EntityIDs for which the login statistic will be ignored even
+     * when requested to be instered into the storage. By default lists are empty
+     */
+    //'ignoredIds' => [
+    //    'IDP' => ['spentityid1', 'spentityid2', 'spentityid3',],
+    //    'SP' => ['idpentityid1', 'idpentityid2', 'idpentityid3',],
+    //],
 
+    /*
+     * Enabled or disables CRON hook (aggregating the stats). By default is enabled.
+     */
+    //'cronEnabled' => false,
 ];
diff --git a/hooks/hook_cron.php b/hooks/hook_cron.php
index 5dcb39b..3e63477 100644
--- a/hooks/hook_cron.php
+++ b/hooks/hook_cron.php
@@ -3,6 +3,7 @@
 declare(strict_types=1);
 
 use SimpleSAML\Logger;
+use SimpleSAML\Module\proxystatistics\Config;
 use SimpleSAML\Module\proxystatistics\DatabaseCommand;
 
 /**
@@ -18,8 +19,13 @@ function proxystatistics_hook_cron(&$croninfo)
         return;
     }
 
-    Logger::info('cron [proxystatistics]: Running cron in cron tag [' . $croninfo['tag'] . '] ');
+    $config = Config::getInstance();
+    if (!$config->isCronEnabled()) {
+        Logger::debug('cron [proxystatistics]: Skipping cron - disabled by confiuration');
+        return;
+    }
 
+    Logger::info('cron [proxystatistics]: Running cron in cron tag [' . $croninfo['tag'] . '] ');
     try {
         $dbCmd = new DatabaseCommand();
         $dbCmd->aggregate();
diff --git a/lib/Config.php b/lib/Config.php
index fd443dd..6f8b9a3 100644
--- a/lib/Config.php
+++ b/lib/Config.php
@@ -43,6 +43,8 @@ class Config
 
     private const IGNORED_IDS = 'ignoredIds';
 
+    private const CRON_ENABLED = 'cronEnabled';
+
     private $config;
 
     private $store;
@@ -67,6 +69,8 @@ class Config
 
     private $ignoredIds;
 
+    private $cronEnabled;
+
     private static $instance;
 
     private function __construct()
@@ -79,6 +83,7 @@ class Config
         $this->keepPerUser = $this->config->getIntegerRange(self::KEEP_PER_USER, 31, 1827, 31);
         $this->requiredAuthSource = $this->config->getString(self::REQUIRE_AUTH_SOURCE, '');
         $this->idAttribute = $this->config->getString(self::USER_ID_ATTRIBUTE, 'uid');
+        $this->cronEnabled = $this->config->getBoolean(self::CRON_ENABLED, true);
         $this->apiWriteEnabled = $this->config->getBoolean(self::API_WRITE_ENABLED, false);
         $this->ignoredIds = $this->config->getArray(self::IGNORED_IDS, []);
         if ($this->apiWriteEnabled) {
@@ -172,4 +177,9 @@ class Config
     {
         return $this->ignoredIds;
     }
+
+    public function isCronEnabled()
+    {
+        return $this->cronEnabled;
+    }
 }
-- 
GitLab