]> source.dussan.org Git - nextcloud-server.git/commitdiff
We should check for exceptions when trying to get the avatar
authorRoeland Jago Douma <rullzer@owncloud.com>
Mon, 22 Feb 2016 08:55:29 +0000 (09:55 +0100)
committerRoeland Jago Douma <rullzer@owncloud.com>
Mon, 22 Feb 2016 09:14:14 +0000 (10:14 +0100)
Fixes #22550

* Updated phpdoc of avatatmanager
* Add unit test

lib/private/avatarmanager.php
lib/public/iavatarmanager.php
settings/controller/userscontroller.php
tests/settings/controller/userscontrollertest.php

index 21f88b1fd3fe943c6264cad75bab61689abbaefe..b2d3e6eb3dd0b848e988f830a52ed6c678d689fb 100644 (file)
@@ -27,6 +27,7 @@
 namespace OC;
 
 use OCP\Files\Folder;
+use OCP\Files\NotFoundException;
 use OCP\IAvatarManager;
 use OCP\IUserManager;
 use OCP\Files\IRootFolder;
@@ -68,6 +69,7 @@ class AvatarManager implements IAvatarManager {
         * @param string $userId the ownCloud user id
         * @return \OCP\IAvatar
         * @throws \Exception In case the username is potentially dangerous
+        * @throws NotFoundException In case there is no user folder yet
         */
        public function getAvatar($userId) {
                $user = $this->userManager->get($userId);
index 264c4fcf05116404bb7e968ef55da529370db89e..cb63ccaf6fd1f2e8e7c48429552dd816ce198829 100644 (file)
@@ -36,6 +36,8 @@ interface IAvatarManager {
         * @see \OCP\IAvatar
         * @param string $user the ownCloud user id
         * @return \OCP\IAvatar
+        * @throws \Exception In case the username is potentially dangerous
+        * @throws \OCP\Files\NotFoundException In case there is no user folder yet
         * @since 6.0.0
         */
        public function getAvatar($user);
index 3e5455751abf8a86e02680c71abf0e67a8f26477..0abcabed11c9385f782b8c112ad389bc629bed67 100644 (file)
@@ -176,7 +176,11 @@ class UsersController extends Controller {
 
                $avatarAvailable = false;
                if ($this->config->getSystemValue('enable_avatars', true) === true) {
-                       $avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
+                       try {
+                               $avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
+                       } catch (\Exception $e) {
+                               //No avatar yet
+                       }
                }
 
                return [
index 947540fa67b3591d6ec24044ea7caa257279b28f..6f07f34ba8da64b8b5969a69e36c1028cf047d76 100644 (file)
@@ -1696,6 +1696,32 @@ class UsersControllerTest extends \Test\TestCase {
                $this->assertEquals($expectedResult, $result);
        }
 
+       public function testNoAvatar() {
+               $this->container['IsAdmin'] = true;
+
+               list($user, $expectedResult) = $this->mockUser();
+
+               $subadmin = $this->getMockBuilder('\OC\SubAdmin')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $subadmin->expects($this->once())
+                       ->method('getSubAdminsGroups')
+                       ->with($user)
+                       ->will($this->returnValue([]));
+               $this->container['GroupManager']
+                       ->expects($this->any())
+                       ->method('getSubAdmin')
+                       ->will($this->returnValue($subadmin));
+
+               $this->container['OCP\\IAvatarManager']
+                       ->method('getAvatar')
+                       ->will($this->throwException(new \OCP\Files\NotFoundException()));
+               $expectedResult['isAvatarAvailable'] = false;
+
+               $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
+               $this->assertEquals($expectedResult, $result);
+       }
+
        /**
         * @return array
         */