Last active
August 2, 2019 10:46
-
-
Save kinglozzer/dbc1c43df203a0145c1b9e08c91f9224 to your computer and use it in GitHub Desktop.
SilverStripe 4 - re-add member “LastVisited” date/time tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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