2008-08-11 02:14:48 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
namespace SilverStripe\Security;
|
|
|
|
|
2017-11-30 04:56:18 +01:00
|
|
|
use SilverStripe\ORM\DataList;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2008-08-11 02:14:48 +02:00
|
|
|
/**
|
|
|
|
* Record all login attempts through the {@link LoginForm} object.
|
|
|
|
* This behaviour is disabled by default.
|
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* Enable through {@link Security::$login_recording}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-11 02:21:44 +02:00
|
|
|
* Caution: Please make sure that enabling logging
|
|
|
|
* complies with your privacy standards. We're logging
|
|
|
|
* username and IP.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2018-09-28 10:46:36 +02:00
|
|
|
* @property string $Email Email address used for login attempt. @deprecated 3.0.0:5.0.0
|
2017-11-30 04:56:18 +01:00
|
|
|
* @property string $EmailHashed sha1 hashed Email address used for login attempt
|
|
|
|
* @property string $Status Status of the login attempt, either 'Success' or 'Failure'
|
|
|
|
* @property string $IP IP address of user attempting to login
|
|
|
|
* @property int $MemberID ID of the Member, only if Member with Email exists
|
2014-01-26 04:17:17 +01:00
|
|
|
*
|
|
|
|
* @method Member Member() Member object of the user trying to log in, only if Member with Email exists
|
2008-08-11 02:14:48 +02:00
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class LoginAttempt extends DataObject
|
|
|
|
{
|
2017-05-09 22:24:15 +02:00
|
|
|
/**
|
|
|
|
* Success status
|
|
|
|
*/
|
|
|
|
const SUCCESS = 'Success';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Failure status
|
|
|
|
*/
|
|
|
|
const FAILURE = 'Failure';
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
private static $db = [
|
2017-11-30 04:56:18 +01:00
|
|
|
'Email' => 'Varchar(255)', // Remove in 5.0
|
|
|
|
'EmailHashed' => 'Varchar(255)',
|
2016-11-29 00:31:16 +01:00
|
|
|
'Status' => "Enum('Success,Failure')",
|
|
|
|
'IP' => 'Varchar(255)',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
private static $has_one = [
|
2017-05-09 22:24:15 +02:00
|
|
|
'Member' => Member::class, // only linked if the member actually exists
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
private static $indexes = [
|
2018-09-19 04:53:12 +02:00
|
|
|
"EmailHashed" => true
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2018-09-19 04:53:12 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
private static $table_name = "LoginAttempt";
|
|
|
|
|
|
|
|
/**
|
2017-07-03 02:21:27 +02:00
|
|
|
* @skipUpgrade
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param bool $includerelations Indicate if the labels returned include relation fields
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fieldLabels($includerelations = true)
|
|
|
|
{
|
|
|
|
$labels = parent::fieldLabels($includerelations);
|
2018-01-16 19:39:30 +01:00
|
|
|
$labels['Email'] = _t(__CLASS__ . '.Email', 'Email Address');
|
|
|
|
$labels['EmailHashed'] = _t(__CLASS__ . '.EmailHashed', 'Email Address (hashed)');
|
|
|
|
$labels['Status'] = _t(__CLASS__ . '.Status', 'Status');
|
|
|
|
$labels['IP'] = _t(__CLASS__ . '.IP', 'IP Address');
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
return $labels;
|
|
|
|
}
|
2017-11-30 04:56:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set email used for this attempt
|
|
|
|
*
|
|
|
|
* @param string $email
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setEmail($email)
|
|
|
|
{
|
|
|
|
// Store hashed email only
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->EmailHashed = sha1($email ?? '');
|
2017-11-30 04:56:18 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all login attempts for the given email address
|
|
|
|
*
|
|
|
|
* @param string $email
|
|
|
|
* @return DataList|LoginAttempt[]
|
|
|
|
*/
|
|
|
|
public static function getByEmail($email)
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
return static::get()->filterAny([
|
2022-04-14 03:12:59 +02:00
|
|
|
'EmailHashed' => sha1($email ?? ''),
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2017-11-30 04:56:18 +01:00
|
|
|
}
|
2008-08-11 02:14:48 +02:00
|
|
|
}
|