SES-150 fix bug that you can log as same user multiple times #66

Merged
s426135 merged 2 commits from SES-150 into dev 2021-01-13 09:30:33 +01:00

View File

@ -6,13 +6,16 @@ using System.Threading.Tasks;
namespace SessionCompanion.Controllers namespace SessionCompanion.Controllers
{ {
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy; using System.Security.Policy;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using SessionCompanion.Database.Tables;
using SessionCompanion.Extensions.EitherType; using SessionCompanion.Extensions.EitherType;
using SessionCompanion.Hubs; using SessionCompanion.Hubs;
using SessionCompanion.ViewModels.ApiResponses; using SessionCompanion.ViewModels.ApiResponses;
using SessionCompanion.ViewModels.CharacterViewModels;
using SessionCompanion.ViewModels.UserViewModels; using SessionCompanion.ViewModels.UserViewModels;
[Route("api/user")] [Route("api/user")]
@ -20,13 +23,15 @@ namespace SessionCompanion.Controllers
public class UserController : Controller public class UserController : Controller
{ {
private readonly IUserService _service; private readonly IUserService _service;
private readonly ICharacterService _characterService;
private SessionHubData _sessionHubData; private SessionHubData _sessionHubData;
public UserController(IUserService service) public UserController(IUserService service, ICharacterService characterService)
{ {
this._service = service; this._service = service;
this._sessionHubData = new SessionHubData(); this._sessionHubData = new SessionHubData();
this._characterService = characterService;
} }
/// <summary> /// <summary>
@ -41,13 +46,31 @@ namespace SessionCompanion.Controllers
UserViewModel user = await _service.SearchUserByUsername(userName); UserViewModel user = await _service.SearchUserByUsername(userName);
if (user != null && user.Password.Equals(password)) if (user != null && user.Password.Equals(password))
return user.Id; {
List<int> userCharactersIds = _characterService.Get(c => c.UserId.Equals(user.Id)).Result.Select(c => c.Id).ToList();
return new ErrorResponse() foreach(int characterId in userCharactersIds)
{ {
StatusCode = 403, if (SessionHubData.ConnectedCharacters.ContainsKey(characterId))
Message = "User name not found or incorrect password" {
}; return new ErrorResponse()
{
StatusCode = 403,
Message = "User is already logged in"
};
}
}
return user.Id;
}
else
{
return new ErrorResponse()
{
StatusCode = 403,
Message = "User name not found or incorrect password"
};
}
} }