Skip to content

Commit

Permalink
v1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
antcooper committed Nov 22, 2022
1 parent f962b38 commit d9f1f31
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 23 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
# Nag Changelog

## 1.0.4 - 2022-11-22

### Added

- Send alert on username/email change in User Profile
- Test button on Settings page to check format and delivery of emails

### Fixed

- Improved error catching and logging around failed request to ip2location.io
- Tidied Settings template

## 1.0.3 - 2022-11-21

### Added

- New icon, readme improvments

## 1.0.2 - 2022-11-04

### Fixed

- Fixed craft requirement to 4.x

## 1.0.1 - 2022-11-03

### Changed

- Added `{% requireAdmin %}` to Settings

## 1.0.0 - 2022-11-03

### Added

- Initial release
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "simplygoodwork/craft-nag",
"description": "Email alerts in case of suspicious user logins",
"type": "craft-plugin",
"version": "1.0.3",
"version": "1.0.4",
"license": "MIT",
"keywords": [
"craft",
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class DefaultController extends Controller

public function actionTest()
{
Nag::$plugin->nagService->handleLogin(Craft::$app->user->getIdentity());
Nag::$plugin->nagService->handleLogin(Craft::$app->user->getIdentity());

return true;
Craft::$app->session->setNotice('Test message queued.');
return $this->redirect(Craft::$app->request->referrer);
}

}
47 changes: 33 additions & 14 deletions src/services/NagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function onAfterUserSaveHandler($event): void
) {
$sendAlert = true;

$subject = sprintf('Username changed on %s website',
$subject = sprintf('Username changed on %s website',
Craft::$app->getSystemName()
);

Expand All @@ -63,7 +63,7 @@ public function onAfterUserSaveHandler($event): void
elseif ($event->sender->email !== $oldProfile->email) {
$sendAlert = true;

$subject = sprintf('Email address changed on %s website',
$subject = sprintf('Email address changed on %s website',
Craft::$app->getSystemName()
);

Expand All @@ -82,7 +82,7 @@ public function onAfterUserSaveHandler($event): void

public function handleLogin($user): void
{
$subject = sprintf('New sign in on %s website',
$subject = sprintf('New sign in on %s website',
Craft::$app->getSystemName()
);

Expand All @@ -97,7 +97,7 @@ private function _sendAlert($user, $subject, $message)
{
// Kicks off after a user logs in
$sendAlert = false;

// Are we restricting alerts?
if (!$this->_settings->alertRestrictedGroups) {
$sendAlert = true;
Expand Down Expand Up @@ -170,22 +170,41 @@ private function _ip2Location($ip4): string
if ($apiKey) {
$endpoint = 'https://api.ip2location.io/';

$uri = sprintf('%s?key=%s&ip=%s',
$endpoint,
$uri = sprintf('%s?key=%s&ip=%s',
$endpoint,
$apiKey,
$ip4
);

$client = Craft::createGuzzleClient();
$response= $client->request('GET', $uri);
$data = $response->getBody()->getContents() ?? null;
try {
$client = Craft::createGuzzleClient();
$response = $client->request('GET', $uri);
$data = $response->getBody()->getContents() ?? null;

$locationData = json_decode($data, false);
if ($locationData) {
return $locationData->city_name . ', ' . $locationData->country_name;
}
$locationData = json_decode($data, false);
if ($locationData) {
return $locationData->city_name . ', ' . $locationData->country_name;
}
}
catch (\GuzzleHttp\Exception\GuzzleException $error) {
$response = $error->getResponse();
$body = $response->getBody()->getContents();
$message = json_decode($body, false);

// Log that the user has logged in
LogToFile::info(sprintf('ip2location.io error. Response %s - %s',
$response->getStatusCode(),
$message->error->error_message,
), 'nag');
}
catch (\Exception $error) {
// Log that the user has logged in
LogToFile::info(sprintf('Error connecting to ip2location.io service. %s',
$error->getMessage()
), 'nag');
}
}

return '';
}
}
19 changes: 13 additions & 6 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
}) }}

<div id="restrictAlerts" class="nested-fields{% if not settings.alertRestrictedGroups %} hidden{% endif %}">
{% set allGroups = craft.app.userGroups.allGroups()|map(g => {
value: g.id,
label: g.name
{% set allGroups = craft.app.userGroups.allGroups()|map(g => {
value: g.id,
label: g.name
}) %}

{{ forms.checkboxGroupField({
Expand All @@ -30,14 +30,18 @@
on: settings.alertAdmins,
}) }}
</div>

</div>
<hr>
<div>
{{ forms.lightswitchField({
label: "Alert Users on Profile Change"|t('nag'),
instructions: "Sends an email if the username/email is changed"|t('nag'),
name: 'alertOnProfileChange',
on: settings.alertOnProfileChange,
}) }}

</div>
<hr>
<div>
{{ forms.textareaField({
label: "Message Footer"|t('nag'),
instructions: "Add contact details to the end of the alert message."|t('nag'),
Expand All @@ -55,5 +59,8 @@
required: false,
placeholder: "$IP2LOCATION_API_KEY",
}) }}
</div>

</div>
<hr>
<a href="{{ actionUrl('nag/default/test') }}" class="btn formsubmit">Test notification</a><br>
<small>Save settings first</small>

0 comments on commit d9f1f31

Please sign in to comment.