From a3683b76b8e8922562331d477441b0debceba9c5 Mon Sep 17 00:00:00 2001 From: yuri Date: Fri, 20 Nov 2015 15:02:13 +0200 Subject: [PATCH] authTokenControl job --- application/Espo/Core/Container.php | 15 ++-- application/Espo/Core/defaults/config.php | 31 ++++---- .../Espo/Core/defaults/systemConfig.php | 4 +- application/Espo/Jobs/AuthTokenControl.php | 70 +++++++++++++++++++ application/Espo/Jobs/Cleanup.php | 4 +- .../Resources/i18n/en_US/ScheduledJob.json | 3 +- .../Espo/Resources/i18n/en_US/Settings.json | 8 ++- .../layouts/Settings/authentication.json | 3 +- .../metadata/entityDefs/Settings.json | 12 ++++ install/core/afterInstall/records.php | 19 +++-- 10 files changed, 134 insertions(+), 35 deletions(-) create mode 100644 application/Espo/Jobs/AuthTokenControl.php diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index 5603f80d57..a08f6576d3 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -87,16 +87,19 @@ class Container protected function loadLog() { - $logConfig = $this->get('config')->get('logger'); + $config = $this->get('config'); + + $path = $config->get('logger.path', 'data/logs'); + $rotation = $config->get('logger.rotation', true); + $levelCode = $config->get('logger.level', 'WARNING'); $log = new \Espo\Core\Utils\Log('Espo'); - $levelCode = $log->getLevelCode($logConfig['level']); - - if ($logConfig['isRotate']) { - $handler = new \Espo\Core\Utils\Log\Monolog\Handler\RotatingFileHandler($logConfig['path'], $logConfig['maxRotateFiles'], $levelCode); + if ($rotation) { + $maxFileNumber = $config->get('logger.maxFileNumber', 30); + $handler = new \Espo\Core\Utils\Log\Monolog\Handler\RotatingFileHandler($path, $maxFileNumber, $levelCode); } else { - $handler = new \Espo\Core\Utils\Log\Monolog\Handler\StreamHandler($logConfig['path'], $levelCode); + $handler = new \Espo\Core\Utils\Log\Monolog\Handler\StreamHandler($path, $levelCode); } $log->pushHandler($handler); diff --git a/application/Espo/Core/defaults/config.php b/application/Espo/Core/defaults/config.php index c6448c586c..0498a6160d 100644 --- a/application/Espo/Core/defaults/config.php +++ b/application/Espo/Core/defaults/config.php @@ -27,8 +27,8 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -return array ( 'database' => - array ( +return array ( + 'database' => array ( 'driver' => 'pdo_mysql', 'host' => 'localhost', 'port' => '', @@ -48,13 +48,10 @@ return array ( 'database' => 'thousandSeparator' => ',', 'decimalMark' => '.', 'exportDelimiter' => ';', - 'currencyList' => - array ( - ), + 'currencyList' => ['USD'], 'defaultCurrency' => 'USD', 'baseCurrency' => 'USD', - 'currencyRates' => array( - ), + 'currencyRates' => [], 'outboundEmailIsShared' => true, 'outboundEmailFromName' => 'EspoCRM', 'outboundEmailFromAddress' => '', @@ -64,7 +61,7 @@ return array ( 'database' => 'smtpSecurity' => '', 'smtpUsername' => '', 'smtpPassword' => '', - 'languageList' => array( + 'languageList' => [ 'en_US', 'de_DE', 'es_ES', @@ -77,14 +74,14 @@ return array ( 'database' => 'pt_BR', 'uk_UA', 'vi_VN' - ), + ], 'language' => 'en_US', 'logger' => array ( 'path' => 'data/logs/espo.log', 'level' => 'WARNING', /** DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY */ - 'isRotate' => true, /** rotate log files every day */ - 'maxRotateFiles' => 30, /** max number of rotate files */ + 'rotation' => true, /** create log */ + 'maxFileNumber' => 30, /** max number of log files */ ), 'authenticationMethod' => 'Espo', 'globalSearchEntityList' => @@ -94,16 +91,16 @@ return array ( 'database' => 'Lead', 'Opportunity', ), - "tabList" => array("Account", "Contact", "Lead", "Opportunity", "Calendar", "Meeting", "Call", "Task", "Case", "Email", "Document", "Campaign"), - "quickCreateList" => array("Account", "Contact", "Lead", "Opportunity", "Meeting", "Call", "Task", "Case"), + "tabList" => ["Account", "Contact", "Lead", "Opportunity", "Calendar", "Meeting", "Call", "Task", "Case", "Email", "Document", "Campaign"], + "quickCreateList" => ["Account", "Contact", "Lead", "Opportunity", "Meeting", "Call", "Task", "Case"], 'calendarDefaultEntity' => 'Meeting', 'exportDisabled' => false, 'assignmentEmailNotifications' => false, - 'assignmentEmailNotificationsEntityList' => array('Lead', 'Opportunity', 'Task', 'Case'), - 'assignmentNotificationsEntityList' => array('Meeting', 'Call', 'Task', 'Email'), + 'assignmentEmailNotificationsEntityList' => ['Lead', 'Opportunity', 'Task', 'Case'], + 'assignmentNotificationsEntityList' => ['Meeting', 'Call', 'Task', 'Email'], 'emailMessageMaxSize' => 10, 'notificationsCheckInterval' => 10, - 'disabledCountQueryEntityList' => array('Email'), + 'disabledCountQueryEntityList' => ['Email'], 'maxEmailAccountCount' => 2, 'followCreatedEntities' => false, 'b2cMode' => false, @@ -112,6 +109,8 @@ return array ( 'database' => 'massEmailMaxPerHourCount' => 100, 'personalEmailMaxPortionSize' => 10, 'inboundEmailMaxPortionSize' => 20, + 'authTokenLifetime' => 0, + 'authTokenMaxIdleTime' => 120 'isInstalled' => false, ); diff --git a/application/Espo/Core/defaults/systemConfig.php b/application/Espo/Core/defaults/systemConfig.php index 9ee7b37f3b..e929371ba6 100644 --- a/application/Espo/Core/defaults/systemConfig.php +++ b/application/Espo/Core/defaults/systemConfig.php @@ -125,7 +125,9 @@ return array ( 'defaultPermissions' => 'maxEmailAccountCount', 'massEmailMaxPerHourCount', 'personalEmailMaxPortionSize', - 'inboundEmailMaxPortionSize' + 'inboundEmailMaxPortionSize', + 'authTokenLifetime', + 'authTokenMaxIdleTime' ), 'isInstalled' => false, ); diff --git a/application/Espo/Jobs/AuthTokenControl.php b/application/Espo/Jobs/AuthTokenControl.php new file mode 100644 index 0000000000..8f6c9ca200 --- /dev/null +++ b/application/Espo/Jobs/AuthTokenControl.php @@ -0,0 +1,70 @@ +getConfig()->get('authTokenLifetime'); + $authTokenMaxIdleTime = $this->getConfig()->get('authTokenMaxIdleTime'); + + if (!$authTokenLifetime && !$authTokenMaxIdleTime) { + return; + } + + $whereClause = array(); + + if ($authTokenLifetime) { + $dt = new \DateTime(); + $dt->modify('-' . $authTokenLifetime . ' hours'); + $authTokenLifetimeThreshold = $dt->format('Y-m-d H:i:s'); + + $whereClause['createdAt<'] = $authTokenLifetimeThreshold; + } + + if ($authTokenMaxIdleTime) { + $dt = new \DateTime(); + $dt->modify('-' . $authTokenMaxIdleTime . ' hours'); + $authTokenMaxIdleTimeThreshold = $dt->format('Y-m-d H:i:s'); + + $whereClause['lastAccess<'] = $authTokenMaxIdleTimeThreshold; + } + + $tokenList = $this->getEntityManager()->getRepository('AuthToken')->where($whereClause)->limit(0, 100)->find(); + + foreach ($tokenList as $token) { + $this->getEntityManager()->removeEntity($token); + } + } +} + diff --git a/application/Espo/Jobs/Cleanup.php b/application/Espo/Jobs/Cleanup.php index a485d60b44..053fa7c6d1 100644 --- a/application/Espo/Jobs/Cleanup.php +++ b/application/Espo/Jobs/Cleanup.php @@ -78,8 +78,10 @@ class Cleanup extends \Espo\Core\Jobs\Base } } - protected function getCleanupFromDate($format = 'Y-m-d') + protected function getCleanupFromDate() { + $format = 'Y-m-d'; + $datetime = new \DateTime(); $datetime->modify($this->period); return $datetime->format($format); diff --git a/application/Espo/Resources/i18n/en_US/ScheduledJob.json b/application/Espo/Resources/i18n/en_US/ScheduledJob.json index 95f4eebe34..f3a89df3e4 100644 --- a/application/Espo/Resources/i18n/en_US/ScheduledJob.json +++ b/application/Espo/Resources/i18n/en_US/ScheduledJob.json @@ -16,7 +16,8 @@ "Cleanup": "Clean-up", "CheckInboundEmails": "Check Group Email Accounts", "CheckEmailAccounts": "Check Personal Email Accounts", - "SendEmailReminders": "Send Email Reminders" + "SendEmailReminders": "Send Email Reminders", + "AuthTokenControl": "Auth Token Control" }, "cronSetup": { "linux": "Note: Add this line to the crontab file to run Espo Scheduled Jobs:", diff --git a/application/Espo/Resources/i18n/en_US/Settings.json b/application/Espo/Resources/i18n/en_US/Settings.json index 26c140da44..97b011fb81 100644 --- a/application/Espo/Resources/i18n/en_US/Settings.json +++ b/application/Espo/Resources/i18n/en_US/Settings.json @@ -59,7 +59,9 @@ "massEmailMaxPerHourCount": "Max count of emails sent per hour", "personalEmailMaxPortionSize": "Max email portion size for a personal account fetching", "inboundEmailMaxPortionSize": "Max email portion size for a group account fetching", - "maxEmailAccountCount": "Max count of personal email accounts per user" + "maxEmailAccountCount": "Max count of personal email accounts per user", + "authTokenLifetime": "Auth Token Lifetime (hours)", + "authTokenMaxIdleTime": "Auth Token Max Idle Time (hours)" }, "options": { "weekStart": { @@ -71,7 +73,9 @@ "recordsPerPageSmall": "Count of records in relatinship panels.", "outboundEmailIsShared": "Allow users to sent emails via this SMTP.", "followCreatedEntities": "Users will automatically follow records they created.", - "emailMessageMaxSize": "All inbound emails exceeding a specified size will be skipped." + "emailMessageMaxSize": "All inbound emails exceeding a specified size will be skipped.", + "authTokenLifetime": "Defines how long tokens can exist.\n0 - means no expiration.", + "authTokenMaxIdleTime": "Defines how long since a last access tokens can exist.\n0 - means no expiration." }, "labels": { "System": "System", diff --git a/application/Espo/Resources/layouts/Settings/authentication.json b/application/Espo/Resources/layouts/Settings/authentication.json index 3e5e8d295f..6309377214 100644 --- a/application/Espo/Resources/layouts/Settings/authentication.json +++ b/application/Espo/Resources/layouts/Settings/authentication.json @@ -2,7 +2,8 @@ { "label": "Configuration", "rows": [ - [{"name": "authenticationMethod"}] + [{"name": "authenticationMethod"}, {"name": "authTokenLifetime"}], + [false, {"name": "authTokenMaxIdleTime"}] ] }, { diff --git a/application/Espo/Resources/metadata/entityDefs/Settings.json b/application/Espo/Resources/metadata/entityDefs/Settings.json index 940f2ced59..4567e0e082 100644 --- a/application/Espo/Resources/metadata/entityDefs/Settings.json +++ b/application/Espo/Resources/metadata/entityDefs/Settings.json @@ -265,6 +265,18 @@ "massEmailMaxPerHourCount": { "type": "int", "min": 0 + }, + "authTokenLifetime": { + "type": "float", + "min": 0, + "default": 0, + "tooltip": true + }, + "authTokenMaxIdleTime": { + "type": "float", + "min": 0, + "default": 0, + "tooltip": true } } } diff --git a/install/core/afterInstall/records.php b/install/core/afterInstall/records.php index 722c7357fa..2a2b93962a 100644 --- a/install/core/afterInstall/records.php +++ b/install/core/afterInstall/records.php @@ -29,7 +29,7 @@ return array( 'EmailTemplate' => array( - 0 => array( + array( 'name' => 'Case-to-Email auto-reply', 'subject' => 'Case has been created', 'body' => '

{Person.name},

Case \'{Case.name}\' has been created with number {Case.number} and assigned to {User.name}.

', @@ -37,36 +37,41 @@ return array( ), ), 'ScheduledJob' => array( - 0 => array( + array( 'name' => 'Check Group Email Accounts', 'job' => 'CheckInboundEmails', 'status' => 'Active', 'scheduling' => '*/4 * * * *', ), - 1 => array( + array( 'name' => 'Check Personal Email Accounts', 'job' => 'CheckEmailAccounts', 'status' => 'Active', 'scheduling' => '*/5 * * * *', ), - 2 => array( + array( 'name' => 'Send Email Reminders', 'job' => 'SendEmailReminders', 'status' => 'Active', 'scheduling' => '*/2 * * * *', ), - 3 => array( + array( 'name' => 'Clean-up', 'job' => 'Cleanup', 'status' => 'Active', 'scheduling' => '1 1 * * 0', ), - 4 => array( + array( 'name' => 'Send Mass Emails', 'job' => 'ProcessMassEmail', 'status' => 'Active', 'scheduling' => '15 * * * *', ), - + array( + 'name' => 'Auth Token Control', + 'job' => 'AuthTokenControl', + 'status' => 'Active', + 'scheduling' => '*/6 * * * *', + ) ), ); \ No newline at end of file