Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions modules/account/daos/account_idp.dao.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<property name="last_used" fieldname="last_used" datatype="datetime"/>
<property name="usage_count" fieldname="usage_count" datatype="integer" required="yes" default="0"/>
<property name="idp_data" fieldname="idp_data" datatype="text"/>
<property name="inactivity_notification_sended" fieldname="inactivity_notification_sended" datatype="integer" />
</record>
<factory>
<method name="findByIdpAndUser" type="selectfirst">
Expand All @@ -40,5 +41,18 @@
<eq property="account_id" expr="$account_id" />
</conditions>
</method>
<method name="findInactiveNotNotified" type='select'>
<parameter name="inactive_date" />
<conditions logic="AND">
<lt property="last_used" expr="$inactive_date" />
<neq property="inactivity_notification_sended" value="1" />
</conditions>
</method>
<method name="findInactiveToDelete" type='select'>
<parameter name="deletion_date" />
<conditions logic="AND">
<lt property="last_used" expr="$deletion_date" />
</conditions>
</method>
</factory>
</dao>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE account_idp ADD COLUMN inactivity_notification_sended integer;
19 changes: 19 additions & 0 deletions modules/account/install/upgrade_042.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @author Laurent Jouanneau
* @copyright 2024 Laurent Jouanneau
*
* @see https://jelix.org
* @licence MIT
*/

class accountModuleUpgrader_042 extends \Jelix\Installer\Module\Installer
{
protected $targetVersions = array('0.4.2');
protected $date = '2025-09-01';

public function install(Jelix\Installer\Module\API\InstallHelpers $helpers)
{
$helpers->database()->execSQLScript('sql/add_inactivity_notification.sql');
}
}
81 changes: 80 additions & 1 deletion modules/account/lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
* @copyright 2021-2024 Laurent Jouanneau and other contributors
* @license MIT
*/

namespace Jelix\Authentication\Account;

use DateInterval;
use DateTimeImmutable;
use jAuthentication;
use Jelix\Authentication\Core\AuthSession\AuthUser;
use Jelix\Authentication\Core\IdentityProviderInterface;
use Jelix\Core\Infos\AppInfos;

class Manager
{
Expand Down Expand Up @@ -172,6 +176,7 @@ public static function searchAccountByIdp($idpId, $authUserId, $markUsage = fals
}
$accIdp->last_used = date('Y-m-d H:i:s');
$accIdp->usage_count++;
$accIdp->inactivity_notification_sended = 0;
$daoIdp->update($accIdp);
}

Expand Down Expand Up @@ -250,4 +255,78 @@ public static function detachAccountFromIdp($accountId, $idpId, $authUserId)
$daoIdp->delete([$idpId, $authUserId, $accountId]);
}

}
/**
* return delay value form app config param named $paramName, ensure is integer > 0
*
* @param string $paramName the param name
*
* @return bool|integer
*/
protected static function getInactiveDelayValue(string $paramName)
{
$config = \jApp::config()->authentication;
if (isset($config[$paramName]) && $config[$paramName]) {
$inactiveDelay = $config[$paramName];
if ($inactiveDelay == 'none') {

return false;
}
if (filter_var($inactiveDelay, FILTER_VALIDATE_INT, ['min_range' => 1])) {

return $inactiveDelay;
}
trigger_error('Invalid value for '.$inactiveDelay.' must be > 0', E_USER_NOTICE);

return false;
}

return false;
}

public static function notifyOrDropInactive()
{
$inactiveNotificationDelay = self::getInactiveDelayValue('inactiveNotificationDelay');
$inactiveDeletionDelay = self::getInactiveDelayValue('inactiveDeletionDelay');

$now = new DateTimeImmutable();
if (is_int($inactiveDeletionDelay)) {
$inactiveNotificationDelay = $now->sub(new DateInterval('P'.$inactiveNotificationDelay.'D'));

$daoIdp = \jDao::get(self::$daoIdp, self::$daoProfile);

$inactiveUsersToNotif = ($daoIdp->findInactiveNotNotified($inactiveNotificationDelay->format('Y-m-d')));
$appInfos = AppInfos::load();
$appName = $appInfos->getLabel();

foreach($inactiveUsersToNotif as $user) {
// send email
$email = $user->idp_user_email;
$mailer = new \jMailer();
$tpl = $mailer->Tpl('account~mailBodyInactivity', true);
$mailer->addAddress($email);
$mailer->Subject = \jLocale::get('account~account.email.inactivity.subject', [$appName]);
$tpl->assign('lastUsedDate', $user->last_used);
$tpl->assign('appName', $appName);
$tpl->assign('deletionDate', $inactiveNotificationDelay->format('Y-m-d'));
try {
$mailer->send();
} catch (\Exception $e) {
//
continue;
}
// mail sended : update user field
$user->inactivity_notification_sended = 1;
$daoIdp->update($user);
}
}
if (is_int($inactiveDeletionDelay)) {
$notifDeletionDate = $now->sub(new DateInterval('P'.$inactiveDeletionDelay.'D'));

$inactiveUsersToDelete = ($daoIdp->findInactiveToDelete($notifDeletionDate->format('Y-m-d')));
foreach($inactiveUsersToDelete as $user) {
$user->enabled = 0;
$daoIdp->update($user);
}
}
}
}
1 change: 1 addition & 0 deletions modules/account/locales/en_US/account.UTF-8.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ cancel.and.back.to.profile = Cancel and back to your profile
back.to.profile = Back to your profile

error.no.account=Sorry, there is no account with the login %s into this application.
email.inactivity.subject=Inactive account on application %s
1 change: 1 addition & 0 deletions modules/account/locales/fr_FR/account.UTF-8.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ cancel.and.back.to.profile = Annuler et retourner à votre profil
back.to.profile = Retourner à votre profil

error.no.account=Désolé, il n'y a pas de compte avec l'identifiant %s dans cette application.
email.inactivity.subject=Compte inactif sur l'application %s
2 changes: 1 addition & 1 deletion modules/account/module.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="http://jelix.org/ns/module/1.0">
<info id="account@jelix.org" name="account" createdate="2021-01-12">
<version date="2025-06-16">0.4.0</version>
<version date="2025-09-01">0.4.2</version>
<label lang="en_US">account</label>
<description lang="en_US" />
<license URL="">All rights reserved</license>
Expand Down
5 changes: 5 additions & 0 deletions modules/account/templates/en_US/mailBodyInactivity.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Hello</p>
<p>
Your account on {$appName} application is inactive since {$lastUsedDate}, unless you log in in the meantime
it will be deactivated on {$deletionDate}
</p>
4 changes: 4 additions & 0 deletions modules/account/templates/fr_FR/mailBodyInactivity.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<p>Bonjour</p>
<p>Votre compte sur l'application {$appName} est inactif depuis le {$lastUsedDate},
à moins que vous vous connectiez entre temps, il sera désactivé la date {$deletionDate}.
</p>