Skip to content

Commit

Permalink
Merge pull request #26984 from owncloud/dispatch-endisable
Browse files Browse the repository at this point in the history
Emit postSetEnabled for user
  • Loading branch information
Vincent Petry authored Feb 2, 2017
2 parents 72a6aca + 50c134e commit 660abad
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 14 additions & 1 deletion lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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));
}
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 660abad

Please sign in to comment.