From f165ad8864e5bdb213bdeb06d96b66612cee4fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Fri, 22 Jan 2016 11:34:49 +0100 Subject: [PATCH] Changing the avatar of the user emits the changeUser event which triggers update of the system addressbook --- lib/private/avatar.php | 27 +++++++++++++++++---------- lib/private/avatarmanager.php | 7 ++++--- lib/private/user/user.php | 14 ++++++++------ tests/lib/avatarmanagertest.php | 2 +- tests/lib/avatartest.php | 5 ++++- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/private/avatar.php b/lib/private/avatar.php index a276fb7ccf..3dcb683866 100644 --- a/lib/private/avatar.php +++ b/lib/private/avatar.php @@ -29,9 +29,12 @@ namespace OC; +use OC\User\User; use OCP\Files\Folder; use OCP\Files\File; use OCP\Files\NotFoundException; +use OCP\IAvatar; +use OCP\IImage; use OCP\IL10N; use OC_Image; @@ -39,22 +42,25 @@ use OC_Image; * This class gets and sets users avatars. */ -class Avatar implements \OCP\IAvatar { +class Avatar implements IAvatar { /** @var Folder */ private $folder; - /** @var IL10N */ private $l; + /** @var User */ + private $user; /** * constructor * * @param Folder $folder The folder where the avatars are * @param IL10N $l + * @param User $user */ - public function __construct (Folder $folder, IL10N $l) { + public function __construct (Folder $folder, IL10N $l, $user) { $this->folder = $folder; $this->l = $l; + $this->user = $user; } /** @@ -83,15 +89,15 @@ class Avatar implements \OCP\IAvatar { /** * sets the users avatar - * @param \OCP\IImage|resource|string $data An image object, imagedata or path to set a new avatar + * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar * @throws \Exception if the provided file is not a jpg or png image * @throws \Exception if the provided image is not valid - * @throws \OC\NotSquareException if the image is not square + * @throws NotSquareException if the image is not square * @return void */ public function set ($data) { - if($data instanceOf \OCP\IImage) { + if($data instanceOf IImage) { $img = $data; $data = $img->data(); } else { @@ -110,11 +116,12 @@ class Avatar implements \OCP\IAvatar { } if (!($img->height() === $img->width())) { - throw new \OC\NotSquareException(); + throw new NotSquareException(); } $this->remove(); $this->folder->newFile('avatar.'.$type)->putContent($data); + $this->user->triggerChange(); } /** @@ -136,7 +143,7 @@ class Avatar implements \OCP\IAvatar { * @inheritdoc */ public function getFile($size) { - $ext = $this->getExtention(); + $ext = $this->getExtension(); if ($size === -1) { $path = 'avatar.' . $ext; @@ -166,12 +173,12 @@ class Avatar implements \OCP\IAvatar { } /** - * Get the extention of the avatar. If there is no avatar throw Exception + * Get the extension of the avatar. If there is no avatar throw Exception * * @return string * @throws NotFoundException */ - private function getExtention() { + private function getExtension() { if ($this->folder->nodeExists('avatar.jpg')) { return 'jpg'; } elseif ($this->folder->nodeExists('avatar.png')) { diff --git a/lib/private/avatarmanager.php b/lib/private/avatarmanager.php index b14f8133e6..b39f549512 100644 --- a/lib/private/avatarmanager.php +++ b/lib/private/avatarmanager.php @@ -61,10 +61,11 @@ class AvatarManager implements IAvatarManager { * @return \OCP\IAvatar * @throws \Exception In case the username is potentially dangerous */ - public function getAvatar($user) { - if (!$this->userManager->userExists($user)) { + public function getAvatar($userId) { + $user = $this->userManager->get($userId); + if (is_null($user)) { throw new \Exception('user does not exist'); } - return new Avatar($this->rootFolder->getUserFolder($user)->getParent(), $this->l); + return new Avatar($this->rootFolder->getUserFolder($userId)->getParent(), $this->l, $user); } } diff --git a/lib/private/user/user.php b/lib/private/user/user.php index 5b8bb6947a..e8ce6b8cc8 100644 --- a/lib/private/user/user.php +++ b/lib/private/user/user.php @@ -140,9 +140,7 @@ class User implements IUser { $result = $this->backend->setDisplayName($this->uid, $displayName); if ($result) { $this->displayName = $displayName; - if ($this->emitter) { - $this->emitter->emit('\OC\User', 'changeUser', array($this)); - } + $this->triggerChange(); } return $result !== false; } else { @@ -163,9 +161,7 @@ class User implements IUser { } else { $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); } - if ($this->emitter) { - $this->emitter->emit('\OC\User', 'changeUser', array($this)); - } + $this->triggerChange(); } /** @@ -389,4 +385,10 @@ class User implements IUser { return $url; } + public function triggerChange() { + if ($this->emitter) { + $this->emitter->emit('\OC\User', 'changeUser', array($this)); + } + } + } diff --git a/tests/lib/avatarmanagertest.php b/tests/lib/avatarmanagertest.php index 40d07bb49a..cb9068c46a 100644 --- a/tests/lib/avatarmanagertest.php +++ b/tests/lib/avatarmanagertest.php @@ -55,7 +55,7 @@ class AvatarManagerTest extends \Test\TestCase { public function testGetAvatarValidUser() { $this->userManager->expects($this->once()) - ->method('userExists') + ->method('get') ->with('validUser') ->willReturn(true); diff --git a/tests/lib/avatartest.php b/tests/lib/avatartest.php index fe6d3e2fa3..2e7ec00428 100644 --- a/tests/lib/avatartest.php +++ b/tests/lib/avatartest.php @@ -20,9 +20,12 @@ class AvatarTest extends \Test\TestCase { parent::setUp(); $this->folder = $this->getMock('\OCP\Files\Folder'); + /** @var \OCP\IL10N | PHPUnit_Framework_MockObject_MockObject $l */ $l = $this->getMock('\OCP\IL10N'); $l->method('t')->will($this->returnArgument(0)); - $this->avatar = new \OC\Avatar($this->folder, $l); + /** @var \OC\User\User | PHPUnit_Framework_MockObject_MockObject $user */ + $user = $this->getMockBuilder('\OC\User\User')->disableOriginalConstructor()->getMock(); + $this->avatar = new \OC\Avatar($this->folder, $l, $user); } public function testGetNoAvatar() {