Skip to content

Commit bca37dd

Browse files
committed
NFS-616: refactor to allow deletion of own session
1 parent 202c23d commit bca37dd

2 files changed

Lines changed: 69 additions & 19 deletions

File tree

src/PluginSession.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,32 @@ public function isUserView() {
284284
return $this->userView;
285285
}
286286

287-
/**
288-
* Destroy the session with the given id
289-
*
290-
* @param String $sessionId
291-
* @return bool true on success or false on failure.
292-
*/
293-
public function destroySession(String $sessionId) {
294-
$currentId = session_id();
295-
session_write_close();
296-
297-
session_id($this->createCompatibleSessionId($sessionId));
298-
session_start();
299-
$result = session_destroy();
300-
301-
session_id($currentId);
302-
session_start();
303-
304-
return $result;
305-
}
287+
/**
288+
* Destroy the session with the given id
289+
*
290+
* @param String $sessionId
291+
* @return bool true on success or false on failure.
292+
*/
293+
public function destroySession(String $sessionId = null) {
294+
295+
$sessionId = $sessionId ?: $this->sso->getSessionId();
296+
297+
// save the current session
298+
$currentId = session_id();
299+
session_write_close();
300+
301+
// switch to the target session and removes it
302+
session_id($this->createCompatibleSessionId($sessionId));
303+
session_start();
304+
$result = session_destroy();
305+
306+
// switches back to the original session
307+
if ($currentId !== $sessionId) {
308+
session_id($currentId);
309+
session_start();
310+
}
311+
312+
return $result;
313+
}
306314

307315
}

test/PluginSessionTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,49 @@ public function testDestroyOtherSession() {
501501
->method('destroy')
502502
->with($sessionId);
503503

504+
$handler->expects($this->exactly(2))
505+
->method('write')
506+
->with($this->logicalOr(
507+
$this->equalTo($sessionId),
508+
$this->equalTo($this->tokenData[PluginSession::CLAIM_SESSION_ID])
509+
));
510+
511+
$handler->expects($this->exactly(2))
512+
->method('open');
513+
504514
$session->destroySession($sessionHash);
505515
}
506516

517+
public function testDestroyOwnSession() {
518+
519+
$sessionId = $this->tokenData[PluginSession::CLAIM_SESSION_ID];
520+
$this->setupEnvironment(null, $this->token, false);
521+
522+
// successfull remote call handler mock
523+
$handler = $this->getMockBuilder(SessionHandlerInterface::class)
524+
->setMethodsExcept()
525+
->getMock();
526+
527+
$handler->method('close')->willReturn(true);
528+
$handler->method('destroy')->willReturn(true);
529+
$handler->method('open')->willReturn(true);
530+
$handler->method('write')->willReturn(true);
531+
$handler->method('read')->willReturn($sessionId);
532+
533+
/** @var PluginSession $session */
534+
$session = new PluginSession($this->pluginId, $this->publicKey, $handler);
535+
536+
$handler->expects($this->once())
537+
->method('destroy')
538+
->with($sessionId);
539+
540+
$handler->expects($this->once())
541+
->method('write')
542+
->with($sessionId);
543+
544+
$handler->expects($this->once())
545+
->method('open');
546+
547+
$session->destroySession($sessionId);
548+
}
507549
}

0 commit comments

Comments
 (0)