Fixed frontend behavior after edited user name

This commit is contained in:
Marcin Kurczewski 2014-09-10 19:19:30 +02:00
parent cb08f68469
commit 6ce7beffd2
4 changed files with 32 additions and 5 deletions

View file

@ -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,

View file

@ -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) {

View file

@ -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))
{

View file

@ -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));
}