From 95e37e55eb7700c9375d714420da8cc19c509d53 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 14 May 2014 18:47:50 +0200 Subject: [PATCH] Restored support for anonymous setting changes --- src/Api/Jobs/UserJobs/EditUserSettingsJob.php | 6 +++++- src/Api/Jobs/UserJobs/GetUserSettingsJob.php | 2 +- src/Auth.php | 5 ++++- src/core.php | 3 ++- tests/Tests/ApiTests/ApiAuthTest.php | 4 ++-- tests/Tests/JobTests/EditUserSettingsJobTest.php | 4 ---- tests/Tests/JobTests/GetUserSettingsJobTest.php | 3 --- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Api/Jobs/UserJobs/EditUserSettingsJob.php b/src/Api/Jobs/UserJobs/EditUserSettingsJob.php index 293e187c..f78de30d 100644 --- a/src/Api/Jobs/UserJobs/EditUserSettingsJob.php +++ b/src/Api/Jobs/UserJobs/EditUserSettingsJob.php @@ -20,6 +20,10 @@ class EditUserSettingsJob extends AbstractJob { $user->getSettings()->set($key, $value); } + + if ($user->getAccessRank()->toInteger() == AccessRank::Anonymous) + return $user; + return UserModel::save($user); } @@ -39,6 +43,6 @@ class EditUserSettingsJob extends AbstractJob public function isAuthenticationRequired() { - return true; + return false; } } diff --git a/src/Api/Jobs/UserJobs/GetUserSettingsJob.php b/src/Api/Jobs/UserJobs/GetUserSettingsJob.php index be2cb1a5..d548bc7e 100644 --- a/src/Api/Jobs/UserJobs/GetUserSettingsJob.php +++ b/src/Api/Jobs/UserJobs/GetUserSettingsJob.php @@ -28,6 +28,6 @@ class GetUserSettingsJob extends AbstractJob public function isAuthenticationRequired() { - return true; + return false; } } diff --git a/src/Auth.php b/src/Auth.php index 49943517..b503d950 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -41,6 +41,9 @@ class Auth public static function tryAutoLogin() { + if (self::isLoggedIn()) + return; + if (!isset($_COOKIE['auth'])) return; @@ -77,7 +80,7 @@ class Auth public static function getCurrentUser() { - return self::isLoggedIn() + return isset($_SESSION['user']) ? unserialize($_SESSION['user']) : self::getAnonymousUser(); } diff --git a/src/core.php b/src/core.php index 85c0e972..3033e0c2 100644 --- a/src/core.php +++ b/src/core.php @@ -80,7 +80,8 @@ function prepareEnvironment($testEnvironment) if (\Chibi\Database::connected()) \Chibi\Database::disconnect(); - Auth::setCurrentUser(null); + if ($testEnvironment) + Auth::setCurrentUser(null); Access::init(); Logger::init(); Mailer::init(); diff --git a/tests/Tests/ApiTests/ApiAuthTest.php b/tests/Tests/ApiTests/ApiAuthTest.php index f319cf44..bb8eb928 100644 --- a/tests/Tests/ApiTests/ApiAuthTest.php +++ b/tests/Tests/ApiTests/ApiAuthTest.php @@ -24,7 +24,7 @@ class ApiAuthTest extends AbstractFullApiTest $this->testAuth(new EditUserJob(), false); $this->testAuth(new EditUserNameJob(), false); $this->testAuth(new EditUserPasswordJob(), false); - $this->testAuth(new EditUserSettingsJob(), true); + $this->testAuth(new EditUserSettingsJob(), false); $this->testAuth(new FeaturePostJob(), true); $this->testAuth(new FlagPostJob(), false); $this->testAuth(new FlagUserJob(), false); @@ -33,7 +33,7 @@ class ApiAuthTest extends AbstractFullApiTest $this->testAuth(new GetPostJob(), false); $this->testAuth(new GetPostThumbJob(), false); $this->testAuth(new GetUserJob(), false); - $this->testAuth(new GetUserSettingsJob(), true); + $this->testAuth(new GetUserSettingsJob(), false); $this->testAuth(new ListCommentsJob(), false); $this->testAuth(new ListLogsJob(), false); $this->testAuth(new ListPostsJob(), false); diff --git a/tests/Tests/JobTests/EditUserSettingsJobTest.php b/tests/Tests/JobTests/EditUserSettingsJobTest.php index 2083c203..55a78d19 100644 --- a/tests/Tests/JobTests/EditUserSettingsJobTest.php +++ b/tests/Tests/JobTests/EditUserSettingsJobTest.php @@ -5,7 +5,6 @@ class EditUserSettingsJobTest extends AbstractTest { $this->grantAccess('changeUserSettings'); $user = $this->userMocker->mockSingle(); - $this->login($user); $expectedSafety = (new PostSafety(PostSafety::Sketchy))->toFlag(); $user = $this->assert->doesNotThrow(function() use ($user, $expectedSafety) @@ -36,7 +35,6 @@ class EditUserSettingsJobTest extends AbstractTest { $this->grantAccess('changeUserSettings'); $user = $this->userMocker->mockSingle(); - $this->login($user); $user = $this->assert->doesNotThrow(function() use ($user) { @@ -65,7 +63,6 @@ class EditUserSettingsJobTest extends AbstractTest { $this->grantAccess('changeUserSettings'); $user = $this->userMocker->mockSingle(); - $this->login($user); $user = $this->assert->doesNotThrow(function() use ($user) { @@ -95,7 +92,6 @@ class EditUserSettingsJobTest extends AbstractTest { $this->grantAccess('changeUserSettings'); $user = $this->userMocker->mockSingle(); - $this->login($user); $this->assert->throws(function() use ($user) { diff --git a/tests/Tests/JobTests/GetUserSettingsJobTest.php b/tests/Tests/JobTests/GetUserSettingsJobTest.php index aa6959ae..b4cbac6d 100644 --- a/tests/Tests/JobTests/GetUserSettingsJobTest.php +++ b/tests/Tests/JobTests/GetUserSettingsJobTest.php @@ -5,7 +5,6 @@ class GetUserSettingsJobTest extends AbstractTest { $this->grantAccess('changeUserSettings'); $user = $this->userMocker->mockSingle(); - $this->login($user); $settings = $this->assert->doesNotThrow(function() use ($user) { @@ -28,7 +27,6 @@ class GetUserSettingsJobTest extends AbstractTest { $this->grantAccess('changeUserSettings'); $user = $this->userMocker->mockSingle(); - $this->login($user); $user->getSettings()->enableSafety(new PostSafety(PostSafety::Sketchy), true); UserModel::save($user); @@ -57,7 +55,6 @@ class GetUserSettingsJobTest extends AbstractTest { $this->grantAccess('changeUserSettings'); $user = $this->userMocker->mockSingle(); - $this->login($user); $user->getSettings()->enableSafety(new PostSafety(PostSafety::Sketchy), true); $user->getSettings()->enableSafety(new PostSafety(PostSafety::Safe), false);