-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from owncloud/feature/introduce-event-handler
Introduce class EventHandler
- Loading branch information
Showing
3 changed files
with
169 additions
and
26 deletions.
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,75 @@ | ||
<?php | ||
/** | ||
* ownCloud | ||
* | ||
* @author Thomas Müller <[email protected]> | ||
* @copyright (C) 2019 ownCloud GmbH | ||
* @license ownCloud Commercial License | ||
* | ||
* This code is covered by the ownCloud Commercial License. | ||
* | ||
* You should have received a copy of the ownCloud Commercial License | ||
* along with this program. If not, see | ||
* <https://owncloud.com/licenses/owncloud-commercial/>. | ||
* | ||
*/ | ||
namespace OCA\OpenIdConnect; | ||
|
||
use OCA\OpenIdConnect\Sabre\OpenIdSabreAuthPlugin; | ||
use OCP\IRequest; | ||
use OCP\ISession; | ||
use OCP\IUserSession; | ||
use OCP\SabrePluginEvent; | ||
use Sabre\DAV\Auth\Plugin; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
class EventHandler { | ||
|
||
/** @var EventDispatcherInterface */ | ||
private $dispatcher; | ||
/** @var IRequest */ | ||
private $request; | ||
/** @var IUserSession */ | ||
private $userSession; | ||
/** @var ISession */ | ||
private $session; | ||
|
||
public function __construct(EventDispatcherInterface $dispatcher, | ||
IRequest $request, | ||
IUserSession $userSession, | ||
ISession $session) { | ||
$this->dispatcher = $dispatcher; | ||
$this->request = $request; | ||
$this->userSession = $userSession; | ||
$this->session = $session; | ||
} | ||
|
||
public function registerEventHandler(): void { | ||
$this->dispatcher->addListener('OCA\DAV\Connector\Sabre::authInit', function ($event) { | ||
if (!$event instanceof SabrePluginEvent) { | ||
return; | ||
} | ||
if ($event->getServer() === null) { | ||
return; | ||
} | ||
$authPlugin = $event->getServer()->getPlugin('auth'); | ||
if ($authPlugin instanceof Plugin) { | ||
$authPlugin->addBackend($this->createPlugin()); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @return OpenIdSabreAuthPlugin | ||
* @throws \OCP\AppFramework\QueryException | ||
* @codeCoverageIgnore | ||
*/ | ||
protected function createPlugin(): OpenIdSabreAuthPlugin { | ||
$module = \OC::$server->query(OpenIdConnectAuthModule::class); | ||
return new OpenIdSabreAuthPlugin($this->session, | ||
$this->userSession, | ||
$this->request, | ||
$module, | ||
'principals/'); | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
namespace OCA\OpenIdConnect\Tests\Unit; | ||
|
||
use OCA\OpenIdConnect\EventHandler; | ||
use OCP\IRequest; | ||
use OCP\ISession; | ||
use OCP\IUserSession; | ||
use OCP\SabrePluginEvent; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Sabre\DAV\Auth\Plugin; | ||
use Sabre\DAV\Server; | ||
use Symfony\Component\EventDispatcher\Event; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Test\TestCase; | ||
|
||
class EventHandlerTest extends TestCase { | ||
|
||
/** | ||
* @var MockObject | ISession | ||
*/ | ||
private $session; | ||
/** | ||
* @var MockObject | EventHandler | ||
*/ | ||
private $eventHandler; | ||
/** | ||
* @var MockObject | IUserSession | ||
*/ | ||
private $userSession; | ||
/** | ||
* @var MockObject | EventDispatcherInterface | ||
*/ | ||
private $dispatcher; | ||
/** | ||
* @var MockObject | IRequest | ||
*/ | ||
private $request; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
$this->dispatcher = $this->createMock(EventDispatcherInterface::class); | ||
$this->request = $this->createMock(IRequest::class); | ||
$this->session = $this->createMock(ISession::class); | ||
$this->userSession = $this->createMock(IUserSession::class); | ||
|
||
$this->eventHandler = $this->getMockBuilder(EventHandler::class) | ||
->setConstructorArgs([$this->dispatcher, $this->request, $this->userSession, $this->session]) | ||
->setMethods(['createPlugin']) | ||
->getMock(); | ||
} | ||
|
||
public function testAddListener(): void { | ||
$this->dispatcher->expects(self::once())->method('addListener')->with('OCA\DAV\Connector\Sabre::authInit'); | ||
$this->eventHandler->registerEventHandler(); | ||
} | ||
|
||
public function testListenerDifferentEvent(): void { | ||
$event = $this->createMock(Event::class); | ||
$this->dispatcher->method('addListener')->willReturnCallback(static function ($name, $callback) use ($event) { | ||
$callback($event); | ||
}); | ||
$this->eventHandler->expects(self::never())->method('createPlugin'); | ||
$this->eventHandler->registerEventHandler(); | ||
} | ||
|
||
public function testListenerNullServer(): void { | ||
$event = $this->createMock(SabrePluginEvent::class); | ||
$event->expects(self::once())->method('getServer'); | ||
$this->dispatcher->method('addListener')->willReturnCallback(static function ($name, $callback) use ($event) { | ||
$callback($event); | ||
}); | ||
$this->eventHandler->expects(self::never())->method('createPlugin'); | ||
$this->eventHandler->registerEventHandler(); | ||
} | ||
|
||
public function testListener(): void { | ||
$plugin = $this->createMock(Plugin::class); | ||
$plugin->expects(self::once())->method('addBackend'); | ||
$server = $this->createMock(Server::class); | ||
$server->expects(self::once())->method('getPlugin')->willReturn($plugin); | ||
$event = new SabrePluginEvent($server); | ||
$this->dispatcher->method('addListener')->willReturnCallback(static function ($name, $callback) use ($event) { | ||
$callback($event); | ||
}); | ||
$this->eventHandler->expects(self::once())->method('createPlugin'); | ||
$this->eventHandler->registerEventHandler(); | ||
} | ||
} |