Admin (failed) login notification
Previous article Next articleYou want to have more control over your Admin panel? Who is entering it and when are people failing to login... With this tutorial you will get a mail notification of these two events.
How to use
In the Admin panel go to "Extentions" >> "User Defined Tags" and create a new UDT named "AdminLoginNotification".
Note: Change your mail address in the UDT.
To avoid that notifications mails are sent when you login from your home address enter your IP address (3.149.214.223) too.
Both parameters can have multiple values separated by a comma.
$email_address = 'you@website.com';
$whitelabel_ip = array ("127.0.0.1","0,0,0,0");
// ++++++++ Update these lines ++++++++
// Source: https://cmscanbesimple.org/blog/admin-failed-login-notification
$current_ip = cms_utils::get_real_ip();
if (!in_array($current_ip, $whitelabel_ip))
{
$user = $params['user'];
if ($params['_eventname'] == 'LoginPost')
{
$subject = 'Successful login: ' . $user->username . ' - ' . $current_ip;
$message_template = <<<'EOD'
There has been a successful login in your Admin panel.
User Name: %s
IP Address: %s
First Name: %s
Last Name: %s
E-mail: %s
EOD;
$message = sprintf($message_template,
$user->username,
$current_ip,
$user->firstname,
$user->lastname,
$user->email
);
}
elseif ($params['_eventname'] == 'LoginFailed')
{
$subject = 'Failed login: ' . $user . ' - ' . $current_ip;
$message_template = <<<'EOD'
There has been a failed login in your Admin panel.
User Name: %s
IP Address: %s
EOD;
$message = sprintf($message_template,
$user,
$current_ip
);
}
$mailer = new \cms_mailer;
$mailer->AddAddress($email_address);
$mailer->SetSubject($subject);
$mailer->SetBody($message);
$mailer->Send();
}
Depending on your wishes you should attach the UDT to the LoginPost and/or LoginFailed events in the CMSMS Event Manager. You will find it in the "Extensions" menu.
Comment Form
ReviewManager
ReviewManager
2 Comments
This is gold. Thanks Rolf. It works great.
Wow, great article!!! Thank you!