Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions core/Controller/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace OC\Core\Controller;

use OC\AppFramework\Utility\TimeFactory;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDisplayResponse;
Expand Down Expand Up @@ -76,19 +77,9 @@ class AvatarController extends Controller {

/** @var TimeFactory */
protected $timeFactory;
/** @var IAccountManager */
private $accountManager;

/**
* @param string $appName
* @param IRequest $request
* @param IAvatarManager $avatarManager
* @param ICache $cache
* @param IL10N $l10n
* @param IUserManager $userManager
* @param IRootFolder $rootFolder
* @param ILogger $logger
* @param string $userId
* @param TimeFactory $timeFactory
*/
public function __construct($appName,
IRequest $request,
IAvatarManager $avatarManager,
Expand All @@ -98,7 +89,8 @@ public function __construct($appName,
IRootFolder $rootFolder,
ILogger $logger,
$userId,
TimeFactory $timeFactory) {
TimeFactory $timeFactory,
IAccountManager $accountManager) {
parent::__construct($appName, $request);

$this->avatarManager = $avatarManager;
Expand All @@ -109,6 +101,7 @@ public function __construct($appName,
$this->logger = $logger;
$this->userId = $userId;
$this->timeFactory = $timeFactory;
$this->accountManager = $accountManager;
}


Expand All @@ -130,6 +123,19 @@ public function getAvatar($userId, $size) {
$size = 64;
}

$user = $this->userManager->get($userId);
if ($user === null) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}

$account = $this->accountManager->getAccount($user);
$scope = $account->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope();

if ($scope !== IAccountManager::VISIBILITY_PUBLIC && $this->userId === null) {
// Public avatar access is not allowed
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}

try {
$avatar = $this->avatarManager->getAvatar($userId);
$avatarFile = $avatar->getFile($size);
Expand All @@ -139,9 +145,7 @@ public function getAvatar($userId, $size) {
['Content-Type' => $avatarFile->getMimeType()]
);
} catch (\Exception $e) {
$resp = new Http\Response();
$resp->setStatus(Http::STATUS_NOT_FOUND);
return $resp;
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}

// Cache for 30 minutes
Expand Down
43 changes: 42 additions & 1 deletion tests/Core/Controller/AvatarControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ function is_uploaded_file($filename) {

use OC\AppFramework\Utility\TimeFactory;
use OC\Core\Controller\AvatarController;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
use OCP\AppFramework\Http;
use OCP\ICache;
use OCP\Files\File;
Expand All @@ -46,6 +49,7 @@ function is_uploaded_file($filename) {
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Class AvatarControllerTest
Expand Down Expand Up @@ -78,6 +82,8 @@ class AvatarControllerTest extends \Test\TestCase {
private $request;
/** @var TimeFactory|\PHPUnit_Framework_MockObject_MockObject */
private $timeFactory;
/** @var IAccountManager|MockObject */
private $accountManager;

protected function setUp() {
parent::setUp();
Expand All @@ -92,6 +98,7 @@ protected function setUp() {
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
$this->logger = $this->getMockBuilder(ILogger::class)->getMock();
$this->timeFactory = $this->getMockBuilder('OC\AppFramework\Utility\TimeFactory')->getMock();
$this->accountManager = $this->createMock(IAccountManager::class);

$this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock();
$this->userMock = $this->getMockBuilder(IUser::class)->getMock();
Expand All @@ -106,7 +113,8 @@ protected function setUp() {
$this->rootFolder,
$this->logger,
'userid',
$this->timeFactory
$this->timeFactory,
$this->accountManager
);

// Configure userMock
Expand Down Expand Up @@ -137,6 +145,39 @@ public function testGetAvatarNoAvatar() {
$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
}

public function testAvatarNotPublic() {
$account = $this->createMock(IAccount::class);
$this->accountManager->method('getAccount')
->with($this->userMock)
->willReturn($account);

$property = $this->createMock(IAccountProperty::class);
$account->method('getProperty')
->with(IAccountManager::PROPERTY_AVATAR)
->willReturn($property);

$property->method('getScope')
->willReturn(IAccountManager::VISIBILITY_PRIVATE);

$controller = new AvatarController(
'core',
$this->request,
$this->avatarManager,
$this->cache,
$this->l,
$this->userManager,
$this->rootFolder,
$this->logger,
null,
$this->timeFactory,
$this->accountManager
);

$result = $controller->getAvatar('userId', 128);

$this->assertEquals(Http::STATUS_NOT_FOUND, $result->getStatus());
}

/**
* Fetch the user's avatar
*/
Expand Down