Skip to content

Commit

Permalink
Added new authentication method for downloading MaxMind geolocation d…
Browse files Browse the repository at this point in the history
…atabases

Fixes #64, fixes #65
  • Loading branch information
sjelfull committed Mar 13, 2021
1 parent 5ee07c4 commit 96109ec
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 99 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

# 2.0.0 - 2021-03-13

> {warning} You now have to register for a MaxMind account and obtain a license key in order to download the GeoLite2 databases. This is due to a change that was introduced December 30th 2019, [to comply with GDPR and CCPA](https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-geolite2-databases/). You need to add a MaxMind license key in the plugin settings after upgrading to 2.0.0 to continue downloading the databases. Check the readme for detailed instructions on this.
### Added
- Added new authentication method for downloading MaxMind geolocation databases

## 1.1.3 - 2020-08-27

### Fixed
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ return [

// Enable geolocation status
'enabledGeolocation' => true,
'maxmindLicenseKey' => '',

// Where to save Maxmind DB files
'dbPath' => '',
Expand All @@ -76,6 +77,12 @@ As long as the plugin is installed, it will log the following events automatical

More events like Commerce-specific event handling is planned.

### Geolocation

To enable geolocation lookup with the help of the MaxMind GeoLite2 databases, you first have to generate a license key.

Add your [MaxMind.com License Key](https://support.maxmind.com/account-faq/license-keys/can-generate-new-license-key/) obtained from the [MaxMind.com account area](https://www.maxmind.com/en/accounts/current/people/current).

## Clearing old records

You can prune records older than `n` days (configured by the `pruneDays` setting) either by using the console command `./craft audit/default/prune-logs` or by a button on the Audit index screen.
Expand Down
1 change: 1 addition & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

// Enable geolocation status
'enabledGeolocation' => true,
'maxmindLicenseKey' => '',

// Where to save Maxmind DB files
'dbPath' => '',
Expand Down
6 changes: 1 addition & 5 deletions src/console/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ class DefaultController extends Controller
public function actionUpdateDatabase()
{
ConsoleHelper::output('Updating Geolocation database');

ConsoleHelper::startProgress(1, 6);

$response = Audit::$plugin->geo->checkLicenseKey();
$response = Audit::$plugin->geo->downloadDatabase();

ConsoleHelper::startProgress(2, 6);

if (isset($response['error'])) {
ConsoleHelper::error($response['error']);

ConsoleHelper::endProgress();

return ExitCode::DATAERR;
Expand All @@ -61,16 +60,13 @@ public function actionUpdateDatabase()

if (isset($response['error'])) {
Console::error($response['error']);

ConsoleHelper::endProgress();

return ExitCode::DATAERR;
}

ConsoleHelper::startProgress(6, 6);

ConsoleHelper::endProgress();

ConsoleHelper::output('Finished updating database');

return ExitCode::OK;
Expand Down
90 changes: 85 additions & 5 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace superbig\audit\models;

use craft\helpers\FileHelper;
use superbig\audit\Audit;

use Craft;
Expand Down Expand Up @@ -53,13 +54,19 @@ class Settings extends Model
/**
* Where to save Maxmind DB files
*/
public $dbPath = '';
public $dbPath;
public $tempPath;

public $logPluginEvents = true;
public $logDraftEvents = false;
public $logPluginEvents = true;
public $logDraftEvents = false;
public $logElementEvents = true;
public $logUserEvents = true;
public $logRouteEvents = true;
public $logUserEvents = true;
public $logRouteEvents = true;

public $accountAreaUrl = 'https://www.maxmind.com/en/account';
public $cityDbFilename = 'GeoLite2-City.mmdb';
public $countryDbFilename = 'GeoLite2-Country.mmdb';
public $maxmindLicenseKey = '';

// Public Methods
// =========================================================================
Expand All @@ -84,4 +91,77 @@ public function rules()
['pruneDays', 'integer'],
]);
}

public function getCountryDownloadUrl()
{
return "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz&license_key={$this->maxmindLicenseKey}";
}

public function getCountryChecksumDownloadUrl()
{
return "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz.md5&license_key={$this->maxmindLicenseKey}";
}

public function getCityDownloadUrl()
{
return "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz&license_key={$this->maxmindLicenseKey}";
}

public function getCityChecksumDownloadUrl()
{
return "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz.md5&license_key={$this->maxmindLicenseKey}";
}

public function getCityDbPath($isTempPath = false)
{
if ($isTempPath) {
return $this->getTempPath($this->cityDbFilename);
}

return $this->getDbPath($this->cityDbFilename);
}

public function getCountryDbPath($isTempPath = false)
{
if ($isTempPath) {
return $this->getTempPath($this->countryDbFilename);
}

return $this->getDbPath($this->countryDbFilename);
}

public function getDbPath($filename = null, $createDirectory = false)
{
$dbPath = $this->dbPath;

if (empty($dbPath)) {
$dbPath = Craft::$app->getPath()->getStoragePath() . \DIRECTORY_SEPARATOR . 'audit';
}

if ($createDirectory) {
FileHelper::createDirectory($dbPath);
}

return FileHelper::normalizePath($dbPath . \DIRECTORY_SEPARATOR . $filename);
}

public function getTempPath($filename = null, $createDirectory = true)
{
$tempPath = $this->tempPath;

if (empty($tempPath)) {
$tempPath = Craft::$app->getPath()->getTempPath() . '/audit/';
}

if ($createDirectory) {
FileHelper::createDirectory($tempPath);
}

return FileHelper::normalizePath($tempPath . \DIRECTORY_SEPARATOR . $filename);
}

public function hasValidLicenseKey()
{
return !empty($this->licenseKey);
}
}
Loading

0 comments on commit 96109ec

Please sign in to comment.