-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW Add onBeforeRemoveLoginSession extension hook
- Loading branch information
1 parent
243999c
commit 079a423
Showing
2 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |