mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-19 16:06:32 +02:00
6888901468
* First cut * Temporarily disable composer.json for netlify build * POC * New recursive directory query, various refinements * Fix flexbox * new styled components plugin * Apply frontmatter delimiters * Mobile styles, animation * Search * Redesign, clean up * Nuke the cache, try again * fix file casing * Remove production env file * ID headers * Move app to new repo * Add frontmatter universally * Hide children changelogs * Add how to title * New callout tags * Revert inline code block change * Replace note callouts * Fix icons * Repalce images * Fix icon * Fix image links * Use proper SQL icon
1.7 KiB
1.7 KiB
title | summary | icon |
---|---|---|
Track member logins | Keep a log in the database of who logs in and when | user-friends |
Howto: Track Member Logins
Sometimes its good to know how active your users are,
and when they last visited the site (and logged on).
A simple LastVisited
property on the Member
record
with some hooks into the login process can achieve this.
In addition, a NumVisit
property will tell us how
often the member has visited. Or more specifically,
how often he has started a browser session, either through
explicitly logging in or by invoking the "remember me" functionality.
use SilverStripe\Forms\ReadonlyField;
use SilverStripe\Security\Security;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataExtension;
class MyMemberExtension extends DataExtension
{
private static $db = [
'LastVisited' => 'Datetime',
'NumVisit' => 'Int',
];
public function afterMemberLoggedIn()
{
$this->logVisit();
}
public function memberAutoLoggedIn()
{
$this->logVisit();
}
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldsToTab('Root.Main', [
ReadonlyField::create('LastVisited', 'Last visited'),
ReadonlyField::create('NumVisit', 'Number of visits')
]);
}
protected function logVisit()
{
if(!Security::database_is_ready()) return;
DB::query(sprintf(
'UPDATE "Member" SET "LastVisited" = %s, "NumVisit" = "NumVisit" + 1 WHERE "ID" = %d',
DB::get_conn()->now(),
$this->owner->ID
));
}
}
Now you just need to apply this extension through your config:
SilverStripe\Security\Member:
extensions:
- MyMemberExtension