Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Handle case of first CORS whitelist add-delete #29749

Merged
merged 1 commit into from
Dec 4, 2017
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
23 changes: 10 additions & 13 deletions settings/Controller/CorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,7 @@ private function getRedirectResponse() {
*/
public function getDomains() {
$userId = $this->userId;

if (empty($this->config->getUserValue($userId, 'core', 'domains'))) {
$domains = [];
} else {
$domains = json_decode($this->config->getUserValue($userId, 'core', 'domains'));
}

$domains = json_decode($this->config->getUserValue($userId, 'core', 'domains', '[]'), true);
return new JSONResponse($domains);
}

Expand All @@ -112,14 +106,14 @@ public function addDomain($domain) {
}

$userId = $this->userId;
$domains = json_decode($this->config->getUserValue($userId, 'core', 'domains'));
$domains = json_decode($this->config->getUserValue($userId, 'core', 'domains', '[]'), true);
$domains = array_filter($domains);
array_push($domains, $domain);

// In case same domain is added
$domains = array_unique($domains);

// Store as comma seperated string
// Store as comma separated string
$domainsString = json_encode($domains);

$this->config->setUserValue($userId, 'core', 'domains', $domainsString);
Expand All @@ -136,11 +130,14 @@ public function addDomain($domain) {
*/
public function removeDomain($id) {
$userId = $this->userId;
$domains = json_decode($this->config->getUserValue($userId, 'core', 'domains'));

if ($id >= 0 && $id < count($domains)) {
$domains = json_decode($this->config->getUserValue($userId, 'core', 'domains', '[]'), true);
if (isset($domains[$id])) {
unset($domains[$id]);
$this->config->setUserValue($userId, 'core', 'domains', json_encode($domains));
if (count($domains)) {
$this->config->setUserValue($userId, 'core', 'domains', json_encode($domains));
} else {
$this->config->deleteUserValue($userId, 'core', 'domains');
}
}

return $this->getRedirectResponse();
Expand Down
2 changes: 1 addition & 1 deletion settings/Panels/Personal/Cors.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getSectionID() {
*/
public function getPanel() {
$userId = $this->userSession->getUser()->getUID();
$domains = json_decode($this->config->getUserValue($userId, 'core', 'domains'));
$domains = json_decode($this->config->getUserValue($userId, 'core', 'domains', '[]'), true);

$t = new Template('settings', 'panels/personal/cors');
$t->assign('user_id', $userId);
Expand Down
3 changes: 2 additions & 1 deletion tests/Settings/Controller/CorsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function setUp() {
$this->config = $this->createMock(IConfig::class);
$this->config->method('getUserValue')->willReturn('["http:\/\/www.test.com"]');
$this->config->method('setUserValue')->willReturn(true);
$this->config->method('deleteUserValue')->willReturn(true);

$this->corsController = new CorsController(
'core',
Expand Down Expand Up @@ -155,7 +156,7 @@ public function testRemoveValidDomain() {
// the error message that invalid domain ID passed, would never be triggered
$this->config
->expects($this->once())
->method("setUserValue");
->method("deleteUserValue");

// The argument for removing domain is the ID of the white-listed domain
// and not the domain itself
Expand Down