From 6ce7beffd2834d421fae01a7cd32d8a983e6d44e Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 10 Sep 2014 19:19:30 +0200 Subject: [PATCH] Fixed frontend behavior after edited user name --- public_html/js/Auth.js | 12 ++++++++++-- .../Presenters/UserAccountSettingsPresenter.js | 6 ++++++ src/Services/PrivilegeService.php | 2 +- tests/Services/PrivilegeServiceTest.php | 17 +++++++++++++++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/public_html/js/Auth.js b/public_html/js/Auth.js index aa514004..631447b0 100644 --- a/public_html/js/Auth.js +++ b/public_html/js/Auth.js @@ -100,8 +100,8 @@ App.Auth = function(_, jQuery, util, api, appState, promise) { function updateAppState(response) { appState.set('privileges', response.json.privileges || []); appState.set('loginToken', response.json.token && response.json.token.name); - appState.set('loggedInUser', response.json.user); appState.set('loggedIn', response.json.user && !!response.json.user.id); + appState.set('loggedInUser', response.json.user); } function isLoggedIn(userName) { @@ -124,12 +124,19 @@ App.Auth = function(_, jQuery, util, api, appState, promise) { return appState.get('privileges'); } + function updateCurrentUser(user) { + if (user.id !== getCurrentUser().id) { + throw new Error('Cannot set current user to other user this way.'); + } + appState.set('loggedInUser', user); + } + function hasPrivilege(privilege) { return _.contains(getCurrentPrivileges(), privilege); } function startObservingLoginChanges(listenerName, callback) { - appState.startObserving('loggedIn', listenerName, callback); + appState.startObserving('loggedInUser', listenerName, callback); } return { @@ -142,6 +149,7 @@ App.Auth = function(_, jQuery, util, api, appState, promise) { startObservingLoginChanges: startObservingLoginChanges, isLoggedIn: isLoggedIn, getCurrentUser: getCurrentUser, + updateCurrentUser: updateCurrentUser, getCurrentPrivileges: getCurrentPrivileges, hasPrivilege: hasPrivilege, diff --git a/public_html/js/Presenters/UserAccountSettingsPresenter.js b/public_html/js/Presenters/UserAccountSettingsPresenter.js index f32de0c8..8f670c39 100644 --- a/public_html/js/Presenters/UserAccountSettingsPresenter.js +++ b/public_html/js/Presenters/UserAccountSettingsPresenter.js @@ -127,8 +127,14 @@ App.Presenters.UserAccountSettingsPresenter = function( } function editSuccess(apiResponse) { + var wasLoggedIn = auth.isLoggedIn(user.name); user = apiResponse.json; + if (wasLoggedIn) { + auth.updateCurrentUser(user); + } + render(); + var $messages = jQuery(target).find('.messages'); var message = 'Account settings updated!'; if (!apiResponse.json.confirmed) { diff --git a/src/Services/PrivilegeService.php b/src/Services/PrivilegeService.php index 3a27d44d..6f6111ea 100644 --- a/src/Services/PrivilegeService.php +++ b/src/Services/PrivilegeService.php @@ -58,7 +58,7 @@ class PrivilegeService $loggedInUser = $this->authService->getLoggedInUser(); if ($userIdentifier instanceof \Szurubooru\Entities\User) { - return $loggedInUser->name === $userIdentifier->name; + return $loggedInUser->id and ($loggedInUser->id === $userIdentifier->id); } elseif (is_string($userIdentifier)) { diff --git a/tests/Services/PrivilegeServiceTest.php b/tests/Services/PrivilegeServiceTest.php index 36fa95c3..5f6d3fd5 100644 --- a/tests/Services/PrivilegeServiceTest.php +++ b/tests/Services/PrivilegeServiceTest.php @@ -56,7 +56,20 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase $this->assertFalse($privilegeService->isLoggedIn($testUser2->email)); } - public function testIsLoggedInByUser() + public function testIsLoggedInByUserId() + { + $testUser1 = new \Szurubooru\Entities\User(); + $testUser1->id = 'dummy'; + $testUser2 = new \Szurubooru\Entities\User(); + $testUser2->id = 'godzilla'; + $this->authServiceMock->method('getLoggedInUser')->willReturn($testUser1); + + $privilegeService = $this->getPrivilegeService(); + $this->assertTrue($privilegeService->isLoggedIn($testUser1)); + $this->assertFalse($privilegeService->isLoggedIn($testUser2)); + } + + public function testIsLoggedInByUserName() { $testUser1 = new \Szurubooru\Entities\User(); $testUser1->name = 'dummy'; @@ -65,7 +78,7 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase $this->authServiceMock->method('getLoggedInUser')->willReturn($testUser1); $privilegeService = $this->getPrivilegeService(); - $this->assertTrue($privilegeService->isLoggedIn($testUser1)); + $this->assertFalse($privilegeService->isLoggedIn($testUser1)); $this->assertFalse($privilegeService->isLoggedIn($testUser2)); }