We should check for exceptions when trying to get the avatar
Fixes #22550 * Updated phpdoc of avatatmanager * Add unit test
This commit is contained in:
parent
8a8209796d
commit
fe08b5e9d9
|
@ -27,6 +27,7 @@
|
||||||
namespace OC;
|
namespace OC;
|
||||||
|
|
||||||
use OCP\Files\Folder;
|
use OCP\Files\Folder;
|
||||||
|
use OCP\Files\NotFoundException;
|
||||||
use OCP\IAvatarManager;
|
use OCP\IAvatarManager;
|
||||||
use OCP\IUserManager;
|
use OCP\IUserManager;
|
||||||
use OCP\Files\IRootFolder;
|
use OCP\Files\IRootFolder;
|
||||||
|
@ -68,6 +69,7 @@ class AvatarManager implements IAvatarManager {
|
||||||
* @param string $userId the ownCloud user id
|
* @param string $userId the ownCloud user id
|
||||||
* @return \OCP\IAvatar
|
* @return \OCP\IAvatar
|
||||||
* @throws \Exception In case the username is potentially dangerous
|
* @throws \Exception In case the username is potentially dangerous
|
||||||
|
* @throws NotFoundException In case there is no user folder yet
|
||||||
*/
|
*/
|
||||||
public function getAvatar($userId) {
|
public function getAvatar($userId) {
|
||||||
$user = $this->userManager->get($userId);
|
$user = $this->userManager->get($userId);
|
||||||
|
|
|
@ -36,6 +36,8 @@ interface IAvatarManager {
|
||||||
* @see \OCP\IAvatar
|
* @see \OCP\IAvatar
|
||||||
* @param string $user the ownCloud user id
|
* @param string $user the ownCloud user id
|
||||||
* @return \OCP\IAvatar
|
* @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
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public function getAvatar($user);
|
public function getAvatar($user);
|
||||||
|
|
|
@ -176,7 +176,11 @@ class UsersController extends Controller {
|
||||||
|
|
||||||
$avatarAvailable = false;
|
$avatarAvailable = false;
|
||||||
if ($this->config->getSystemValue('enable_avatars', true) === true) {
|
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 [
|
return [
|
||||||
|
|
|
@ -1696,6 +1696,32 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
$this->assertEquals($expectedResult, $result);
|
$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
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue