@@ -19,81 +19,81 @@ |
||
| 19 | 19 | class LoginAttemptNotifications extends Extension |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * |
|
| 24 | - * @return mixed null |
|
| 25 | - */ |
|
| 26 | - public function init() |
|
| 27 | - { |
|
| 22 | + /** |
|
| 23 | + * |
|
| 24 | + * @return mixed null |
|
| 25 | + */ |
|
| 26 | + public function init() |
|
| 27 | + { |
|
| 28 | 28 | |
| 29 | - // Exclude default admin. |
|
| 30 | - $member = Security::getCurrentUser(); |
|
| 31 | - if (!$member || !$member->ID) { |
|
| 32 | - return; |
|
| 33 | - } |
|
| 29 | + // Exclude default admin. |
|
| 30 | + $member = Security::getCurrentUser(); |
|
| 31 | + if (!$member || !$member->ID) { |
|
| 32 | + return; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - $message = null; |
|
| 36 | - $session = $this->owner->getRequest()->getSession(); |
|
| 35 | + $message = null; |
|
| 36 | + $session = $this->owner->getRequest()->getSession(); |
|
| 37 | 37 | |
| 38 | - Requirements::javascript('cwp/cwp-core:javascript/LoginAttemptNotifications.js'); |
|
| 39 | - $sessionLastVisited = $session->get('LoginAttemptNotifications.SessionLastVisited'); |
|
| 40 | - if ($sessionLastVisited) { |
|
| 41 | - // Session already in progress. Show all attempts since the session was last visited. |
|
| 38 | + Requirements::javascript('cwp/cwp-core:javascript/LoginAttemptNotifications.js'); |
|
| 39 | + $sessionLastVisited = $session->get('LoginAttemptNotifications.SessionLastVisited'); |
|
| 40 | + if ($sessionLastVisited) { |
|
| 41 | + // Session already in progress. Show all attempts since the session was last visited. |
|
| 42 | 42 | |
| 43 | - $meantimeLoginAttempts = LoginAttempt::get()->filter([ |
|
| 44 | - 'MemberID' => $member->ID, |
|
| 45 | - 'Created:GreaterThan' => $sessionLastVisited |
|
| 46 | - ]); |
|
| 43 | + $meantimeLoginAttempts = LoginAttempt::get()->filter([ |
|
| 44 | + 'MemberID' => $member->ID, |
|
| 45 | + 'Created:GreaterThan' => $sessionLastVisited |
|
| 46 | + ]); |
|
| 47 | 47 | |
| 48 | - $attempts = $meantimeLoginAttempts->count(); |
|
| 49 | - if ($attempts) { |
|
| 50 | - $lastVisitedObj = DBDatetime::create(); |
|
| 51 | - $lastVisitedObj->setValue($sessionLastVisited); |
|
| 52 | - $elapsed = $lastVisitedObj->TimeDiff(); |
|
| 53 | - $failures = $meantimeLoginAttempts->filter(['Status' => 'Failure'])->count(); |
|
| 54 | - $IPs = array_unique($meantimeLoginAttempts->column('IP')); |
|
| 48 | + $attempts = $meantimeLoginAttempts->count(); |
|
| 49 | + if ($attempts) { |
|
| 50 | + $lastVisitedObj = DBDatetime::create(); |
|
| 51 | + $lastVisitedObj->setValue($sessionLastVisited); |
|
| 52 | + $elapsed = $lastVisitedObj->TimeDiff(); |
|
| 53 | + $failures = $meantimeLoginAttempts->filter(['Status' => 'Failure'])->count(); |
|
| 54 | + $IPs = array_unique($meantimeLoginAttempts->column('IP')); |
|
| 55 | 55 | |
| 56 | - if ($attempts == 1) { |
|
| 57 | - $statusString = $failures ? "a failed" : "a successful"; |
|
| 58 | - $message = "In the last $elapsed $statusString login attempt to your account was " |
|
| 59 | - . "registered. The attempt was made from ${IPs[0]}. "; |
|
| 60 | - } else { |
|
| 61 | - if ($failures == $attempts) { |
|
| 62 | - $statusString = $failures ? "failed" : "successful"; |
|
| 63 | - $message = "In the last $elapsed $attempts $statusString login " |
|
| 64 | - . "attempts to your account were registered. "; |
|
| 65 | - } else { |
|
| 66 | - $message = "In the last $elapsed $attempts login attempts to your " |
|
| 67 | - . "account were registered, of which $failures failed. "; |
|
| 68 | - } |
|
| 56 | + if ($attempts == 1) { |
|
| 57 | + $statusString = $failures ? "a failed" : "a successful"; |
|
| 58 | + $message = "In the last $elapsed $statusString login attempt to your account was " |
|
| 59 | + . "registered. The attempt was made from ${IPs[0]}. "; |
|
| 60 | + } else { |
|
| 61 | + if ($failures == $attempts) { |
|
| 62 | + $statusString = $failures ? "failed" : "successful"; |
|
| 63 | + $message = "In the last $elapsed $attempts $statusString login " |
|
| 64 | + . "attempts to your account were registered. "; |
|
| 65 | + } else { |
|
| 66 | + $message = "In the last $elapsed $attempts login attempts to your " |
|
| 67 | + . "account were registered, of which $failures failed. "; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - $message .= "The attempts were from " . implode(', ', $IPs) . '. '; |
|
| 70 | + $message .= "The attempts were from " . implode(', ', $IPs) . '. '; |
|
| 71 | 71 | |
| 72 | - // TODO: add this call to action in a way that doesn't break out of the availabel space. Fix CSS? |
|
| 73 | - // $message .= "If you suspect somebody else might be trying to access |
|
| 74 | - // . "your account, please contact support."; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - } else { |
|
| 78 | - // New session - show last login attempt. |
|
| 79 | - // TODO: this currently does NOT surface to the frontend in any way. |
|
| 80 | - $lastLoginAttempt = LoginAttempt::get()->filter([ |
|
| 81 | - 'MemberID' => $member->ID |
|
| 82 | - ])->sort('Created DESC')->First(); |
|
| 72 | + // TODO: add this call to action in a way that doesn't break out of the availabel space. Fix CSS? |
|
| 73 | + // $message .= "If you suspect somebody else might be trying to access |
|
| 74 | + // . "your account, please contact support."; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + } else { |
|
| 78 | + // New session - show last login attempt. |
|
| 79 | + // TODO: this currently does NOT surface to the frontend in any way. |
|
| 80 | + $lastLoginAttempt = LoginAttempt::get()->filter([ |
|
| 81 | + 'MemberID' => $member->ID |
|
| 82 | + ])->sort('Created DESC')->First(); |
|
| 83 | 83 | |
| 84 | - if ($lastLoginAttempt) { |
|
| 85 | - $date = $lastLoginAttempt->Created; |
|
| 86 | - $message = "Last login attempt to your account was on $lastLoginAttempt->Created " |
|
| 87 | - . "from $lastLoginAttempt->IP"; |
|
| 88 | - $message .= $lastLoginAttempt->Status == 'Failure' ? " and was successful." : "and has failed."; |
|
| 89 | - } |
|
| 90 | - } |
|
| 84 | + if ($lastLoginAttempt) { |
|
| 85 | + $date = $lastLoginAttempt->Created; |
|
| 86 | + $message = "Last login attempt to your account was on $lastLoginAttempt->Created " |
|
| 87 | + . "from $lastLoginAttempt->IP"; |
|
| 88 | + $message .= $lastLoginAttempt->Status == 'Failure' ? " and was successful." : "and has failed."; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - $session->set( |
|
| 93 | - 'LoginAttemptNotifications.SessionLastVisited', |
|
| 94 | - DBDatetime::now()->Format(DBDatetime::ISO_DATETIME) |
|
| 95 | - ); |
|
| 92 | + $session->set( |
|
| 93 | + 'LoginAttemptNotifications.SessionLastVisited', |
|
| 94 | + DBDatetime::now()->Format(DBDatetime::ISO_DATETIME) |
|
| 95 | + ); |
|
| 96 | 96 | |
| 97 | - $this->owner->getResponse()->addHeader('X-LoginAttemptNotifications', $message); |
|
| 98 | - } |
|
| 97 | + $this->owner->getResponse()->addHeader('X-LoginAttemptNotifications', $message); |
|
| 98 | + } |
|
| 99 | 99 | } |