Skip to content

Commit

Permalink
Adjustments to report formatting, update to treat last logged in like… (
Browse files Browse the repository at this point in the history
#2)

Adjustments to report formatting, update to treat last logged in like standard user report does

#---------

#Co-authored-by: Julian Thomson <[email protected]>
  • Loading branch information
jules0x authored Feb 21, 2024
1 parent 4fa66c9 commit f19de3e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# IdleLock
They say that idle hands are the devils playthings, and like so, idle accounts are the security vulnerability's gateway.
They say that idle hands are the devils playthings, so idle accounts are the security vulnerability's gateway.

## Introduction

Expand Down
53 changes: 41 additions & 12 deletions src/Extensions/MemberLockoutExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Security\LoginAttempt;
use SilverStripe\Security\Member;

class MemberLockoutExtension extends DataExtension
Expand All @@ -24,7 +25,7 @@ public function updateCMSFields(FieldList $fields)
/**
* Prevent login if the Member is locked out
*
* @param $result
* @param $result
* @return void
*/
public function canLogIn(&$result)
Expand Down Expand Up @@ -57,29 +58,52 @@ public function getLockIfInactiveAfter() : DBDateTime
$defaultLockoutThreshold = Config::inst()->get(Member::class, 'lockout_threshold_days');

$groups = $this->owner->Groups();
$lowestThreshold = $groups->filter([
$lowestThreshold = $groups->filter(
[
'LockoutThresholdDays:GreaterThan' => 0,
'LockoutThresholdDays:LessThan' => $defaultLockoutThreshold,
])->min('LockoutThresholdDays') ?: $defaultLockoutThreshold;
]
)->min('LockoutThresholdDays') ?: $defaultLockoutThreshold;

return DBDateTime::now()->modify("-{$lowestThreshold} days");
}

/**
* Return the date the user has last accessed the CMS
*
* @return DBDatetime|null
*/
public function getLastAccessed() : DBDatetime
public function getLastAccessed(): ?DBDateTime
{
// Default "Last"; i.e. accounts for new users who haven't yet logged in
$lastAccessed = $this->owner->dbObject('LastEdited');
if ($lastTime = $this->owner->getLastLogin()) {
return $lastTime;
}

// Check for LoginSessions, and overwrite the default "Last" value
if ($this->owner->LoginSessions()->exists()) {
$latestLoginSession = $this->owner->LoginSessions()->sort('LastAccessed', 'DESC')->first();
$lastAccessed = $latestLoginSession->dbObject('LastAccessed');
return null;
}

/**
* Get the last login attempt
*
* @return DBDatetime|null
*/
public function getLastLogin(): ?DBDateTime
{
$lastTime = LoginAttempt::get()
->filter(
[
'MemberID' => $this->owner->ID,
'Status' => 'Success',
]
)
->sort('Created', 'DESC')
->first();

if ($lastTime) {
return $lastTime->dbObject('Created');
}

return $lastAccessed;
return null;
}

/**
Expand All @@ -89,7 +113,12 @@ public function getLastAccessed() : DBDatetime
public function shouldBeLockedOut() : bool
{
$lockIfInactiveAfter = $this->owner->getLockIfInactiveAfter();
$lastAccessed = $this->owner->getLastAccessed();
$lastAccessed = $this->owner->getLastLogin();

// For accounts for new users who haven't logged in yet, use the created date
if (is_null($lastAccessed)) {
$lastAccessed = $this->owner->dbObject('Created');
}

return $lockIfInactiveAfter > $lastAccessed;
}
Expand Down
25 changes: 12 additions & 13 deletions src/Reports/LockedUsersReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace DNADesign\IdleLock\Reports;

use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldExportButton;
use SilverStripe\Forms\GridField\GridFieldPageCount;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Forms\GridField\GridFieldPrintButton;
use SilverStripe\ORM\FieldType\DBDate;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Reports\Report;
use SilverStripe\Security\Member;
Expand Down Expand Up @@ -55,7 +57,16 @@ public function columns()
'Email' => 'Email',
'Locked' => 'Locked',
'getGroupNames' => 'Groups',
'getLastAccessed' => 'Last Logged In',
'Date' => [
'title' => 'Last logged in',
'formatting' => function ($value, $item) {
$customDate = $item->getLastLogin();
if ($customDate instanceof DBDate || $customDate instanceof DBDatetime) {
return $customDate->format('dd/MM/y');
}
return $customDate ?: 'Never';
}
],
];
}

Expand Down Expand Up @@ -88,16 +99,4 @@ protected function formatGroupsList($record)
{
return implode(', ', $record->Groups()->column('Title'));
}

/**
* Format the last visited date and time for a user.
*
* @param DataObject $record
* @return string
*/
protected function formatLastVisited($record)
{
$lastVisited = $record->LastVisited ?: 'N/A';
return DBDatetime::create()->setValue($lastVisited)->Nice();
}
}

0 comments on commit f19de3e

Please sign in to comment.