diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index badbd26de754..a1986d8261a3 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -158,7 +158,7 @@ protected function getUserObject($uid, $backend, $cacheUser = true) { } } - $user = new User($uid, $backend, $this, $this->config); + $user = new User($uid, $backend, $this, $this->config, null, \OC::$server->getEventDispatcher() ); if ($cacheUser) { $this->cachedUsers[$uid] = $user; } diff --git a/lib/private/User/User.php b/lib/private/User/User.php index c161f64b2977..f75c5be17f43 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -39,6 +39,8 @@ use OCP\IConfig; use OCP\UserInterface; use \OCP\IUserBackend; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\GenericEvent; class User implements IUser { /** @var string $uid */ @@ -71,17 +73,24 @@ class User implements IUser { /** @var IURLGenerator */ private $urlGenerator; + /** @var EventDispatcher */ + private $eventDispatcher; + /** * @param string $uid * @param UserInterface $backend * @param \OC\Hooks\Emitter $emitter * @param IConfig|null $config * @param IURLGenerator $urlGenerator + * @param EventDispatcher $eventDispatcher */ - public function __construct($uid, $backend, $emitter = null, IConfig $config = null, $urlGenerator = null) { + public function __construct($uid, $backend, $emitter = null, IConfig $config = null, + $urlGenerator = null, EventDispatcher $eventDispatcher = null + ) { $this->uid = $uid; $this->backend = $backend; $this->emitter = $emitter; + $this->eventDispatcher = $eventDispatcher; if(is_null($config)) { $config = \OC::$server->getConfig(); } @@ -337,6 +346,10 @@ public function setEnabled($enabled) { $this->enabled = $enabled; $enabled = ($enabled) ? 'true' : 'false'; $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled); + + if ($this->eventDispatcher){ + $this->eventDispatcher->dispatch(self::class . '::postSetEnabled', new GenericEvent($this)); + } } /** diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php index b25f390df136..341bb56c9515 100644 --- a/tests/lib/User/UserTest.php +++ b/tests/lib/User/UserTest.php @@ -14,6 +14,8 @@ use OC\User\Database; use OC\User\User; use OCP\IConfig; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\GenericEvent; use Test\TestCase; use Test\Util\User\Dummy; @@ -502,6 +504,34 @@ public function testDeleteHooks() { $this->assertEquals(2, $hooksCalled); } + public function testSetEnabledHook(){ + /** + * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->createMock(Dummy::class); + $eventDispatcherMock = $this->createMock(EventDispatcher::class); + + $expectations = [true, false]; + $eventDispatcherMock->expects($this->exactly(2)) + ->method('dispatch') + ->with( + $this->callback( + function($eventName){ + if ($eventName === User::class . '::postSetEnabled' ){ + return true; + } + return false; + } + ), + $this->anything() + ) + ; + + $user = new User('foo', $backend, null, null, null, $eventDispatcherMock); + $user->setEnabled(false); + $user->setEnabled(true); + } + public function testGetCloudId() { /** * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend