Skip to content

Instantly share code, notes, and snippets.

@kinglozzer
Last active August 2, 2019 10:46
Show Gist options
  • Save kinglozzer/dbc1c43df203a0145c1b9e08c91f9224 to your computer and use it in GitHub Desktop.
Save kinglozzer/dbc1c43df203a0145c1b9e08c91f9224 to your computer and use it in GitHub Desktop.
SilverStripe 4 - re-add member “LastVisited” date/time tracking
---
Name: app
---
SilverStripe\Security\Member:
extensions:
- App\Extensions\MemberExtension
---
Name: app-middleware
After: '#coresecurity'
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Control\Director:
properties:
Middlewares:
LastVisitedMiddleware: '%$App\Control\Middleware\LastVisitedMiddleware'
<?php
namespace App\Control\Middleware;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\ORM\DB;
use SilverStripe\Security\Security;
class LastVisitedMiddleware implements HTTPMiddleware
{
public function process(HTTPRequest $request, callable $delegate)
{
$member = Security::getCurrentUser();
if ($member) {
DB::prepared_query(
sprintf('UPDATE "Member" SET "LastVisited" = %s WHERE "ID" = ?', DB::get_conn()->now()),
[$member->ID]
);
}
return $delegate($request);
}
}
<?php
namespace App\Extensions;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\ReadonlyField;
class MemberExtension extends Extension
{
private static $db = [
'LastVisited' => 'Datetime'
];
/**
* @param FieldList $fields
*/
public function updateCMSFields(FieldList $fields)
{
$fields->insertBefore(
'Locale',
ReadonlyField::create('LastVisited', 'Last Visited Date')
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment