Skip to content

Commit

Permalink
NEW Add onBeforeRemoveLoginSession extension hook
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Mar 8, 2021
1 parent 243999c commit 079a423
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"squizlabs/php_codesniffer": "^3.0"
},
"suggest": {
"silverstripe/crontask": "^2.0"
"silverstripe/crontask": "^2.0",
"silverstripe/auditor": "^2.3"
},
"autoload": {
"psr-4": {
Expand Down
88 changes: 88 additions & 0 deletions src/Control/LoginSessionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace SilverStripe\SessionManager\Control;

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
use SilverStripe\Security\SecurityToken;
use SilverStripe\SessionManager\Model\LoginSession;

// TODO: extend Controller instead of LeftAndMain?
class LoginSessionController extends LeftAndMain
{
private static $url_segment = 'loginsession';

// TODO: ignore_menuitem is legacy and not used anywhere else, work out what intention behind
// this was an reimplement
private static $ignore_menuitem = true;

private static $url_handlers = [
// TODO: 'DELETE remove/$ID' => 'remove',
'DELETE remove/$ID' => 'removeLoginSession'
];

private static $allowed_actions = [
// TODO: 'remove'
'removeLoginSession',
];

/**
* Remove the specified login session
*
* @param HTTPRequest $request
* @return HTTPResponse
*/
// TODO: rename to 'remove'
public function removeLoginSession(HTTPRequest $request): HTTPResponse
{
// Ensure CSRF protection
if (!SecurityToken::inst()->checkRequest($request)) {
return $this->jsonResponse(
['errors' => 'Request timed out, please try again'],
400
);
}

$id = $request->param('ID');
$loginSession = LoginSession::get()->byID($id);
if (!$loginSession) {
return $this->jsonResponse(
['errors' => 'Something went wrong.'],
400
);
}

if (!$loginSession->canDelete()) {
return $this->jsonResponse(
['errors' => 'You do not have permission to delete this record.'],
400
);
}

$this->extend('onBeforeRemoveLoginSession', $loginSession);

$loginSession->delete();

return $this->jsonResponse([
'success' => true,
]);
}

/**
* Respond with the given array as a JSON response
*
* @param array $response
* @param int $code The HTTP response code to set on the response
* @return HTTPResponse
*/
// TODO: change visibility to private
protected function jsonResponse(array $response, int $code = 200): HTTPResponse
{
return HTTPResponse::create(json_encode($response))
->addHeader('Content-Type', 'application/json')
->setStatusCode($code);
}
}

0 comments on commit 079a423

Please sign in to comment.