Skip to content

Commit

Permalink
obey to config in share mail notifications APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
karakayasemi committed Sep 5, 2019
1 parent ce786ac commit 1fbf953
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
15 changes: 14 additions & 1 deletion apps/files_sharing/lib/Controller/NotificationController.php
Expand Up @@ -25,6 +25,7 @@
use OC\Share\MailNotifications;
use OCP\AppFramework\OCSController;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
Expand All @@ -46,6 +47,8 @@ class NotificationController extends OCSController {
private $rootFolder;
/** @var IL10N */
private $l;
/** @var IConfig */
private $config;
public function __construct(
string $appName,
IRequest $request,
Expand All @@ -54,7 +57,8 @@ public function __construct(
IUserManager $userManager,
IGroupManager $groupManager,
IRootFolder $rootFolder,
IL10N $l
IL10N $l,
IConfig $config
) {
parent::__construct($appName, $request);
$this->mailNotifications = $mailNotifications;
Expand All @@ -63,6 +67,7 @@ public function __construct(
$this->groupManager = $groupManager;
$this->rootFolder = $rootFolder;
$this->l = $l;
$this->config = $config;
}

/**
Expand All @@ -75,6 +80,10 @@ public function __construct(
* @return Result
*/
public function notifyPublicLinkRecipientsByEmail($link, $recipients, $personalNote) {
if ($this->config->getAppValue('core', 'shareapi_allow_public_notification', 'no') !== 'yes') {
$message = $this->l->t("Public link mail notification is not allowed");
return new Result(null, 403, $message);
}
$message = null;
$code = 100;
$sender = $this->userSession->getUser();
Expand Down Expand Up @@ -110,6 +119,10 @@ public function notifyPublicLinkRecipientsByEmail($link, $recipients, $personalN
* @return Result
*/
public function notifyRecipients($itemSource, $itemType, $shareType, $recipient) {
if ($this->config->getAppValue('core', 'shareapi_allow_mail_notification', 'no') !== 'yes') {
$message = $this->l->t("Internal mail notification for shared files is not allowed");
return new Result(null, 403, $message);
}
$recipientList = [];
$sender = $this->userSession->getUser();
if ($shareType === Share::SHARE_TYPE_USER) {
Expand Down
Expand Up @@ -22,8 +22,10 @@
namespace OCA\Files_Sharings\Tests\Controller;

use OC\Share\MailNotifications;
use OC\Share\Share;
use OCA\Files_Sharing\Controller\NotificationController;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
Expand All @@ -48,6 +50,8 @@ class NotificationControllerTest extends TestCase {
private $rootFolder;
/** @var IL10N | MockObject */
private $l;
/** @var IConfig | MockObject */
private $config;
/** @var NotificationController $notificationController */
private $notificationController;

Expand All @@ -60,6 +64,7 @@ protected function setUp() {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->l = $this->createMock(IL10N::class);
$this->config = $this->createMock(IConfig::class);

$this->notificationController = new NotificationController(
'files_sharing',
Expand All @@ -69,7 +74,8 @@ protected function setUp() {
$this->userManager,
$this->groupManager,
$this->rootFolder,
$this->l
$this->l,
$this->config
);
}

Expand All @@ -89,6 +95,10 @@ public function dataNotifyPublicLinkByEmail() {
* @param int $statusCode
*/
public function testNotifyPublicLinkRecipientsByEmail($sender, $failedRecipients, $statusCode) {
$this->config
->method('getAppValue')
->with('core', 'shareapi_allow_public_notification', 'no')
->willReturn('yes');
$this->mailNotifications->method('sendLinkShareMail')->willReturn($failedRecipients);
$this->userSession->method('getUser')->willReturn($sender);
$this->assertEquals($statusCode,
Expand All @@ -99,4 +109,57 @@ public function testNotifyPublicLinkRecipientsByEmail($sender, $failedRecipients
)->getStatusCode()
);
}

public function testNotifyPublicLinkRecipientsByEmailIfObeysConfig() {
$this->config
->method('getAppValue')
->with('core', 'shareapi_allow_public_notification', 'no')
->willReturn('no');

$notificationController = new NotificationController(
'files_sharing',
$this->request,
$this->mailNotifications,
$this->userSession,
$this->userManager,
$this->groupManager,
$this->rootFolder,
$this->l,
$this->config
);
$this->assertEquals(403,
$notificationController->notifyPublicLinkRecipientsByEmail(
'localhost/token',
['test@user'],
''
)->getStatusCode()
);
}

public function testNotifyRecipientsIfObeysConfig() {
$this->config
->method('getAppValue')
->with('core', 'shareapi_allow_mail_notification', 'no')
->willReturn('no');

$notificationController = new NotificationController(
'files_sharing',
$this->request,
$this->mailNotifications,
$this->userSession,
$this->userManager,
$this->groupManager,
$this->rootFolder,
$this->l,
$this->config
);
$this->assertEquals(403,
$notificationController->notifyRecipients(
1,
'file',
Share::SHARE_TYPE_USER,
'recipient_user'
)->getStatusCode()
);
}
}

0 comments on commit 1fbf953

Please sign in to comment.