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

This commit is contained in:
Łukasz Góreczny 2021-01-12 10:33:59 +01:00
parent 766b323042
commit 9f51f12cca

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,14 +46,32 @@ 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();
foreach(int characterId in userCharactersIds)
{
if (SessionHubData.ConnectedCharacters.ContainsKey(characterId))
{
return new ErrorResponse()
{
StatusCode = 403,
Message = "User is already logged in"
};
}
}
return user.Id;
}
else
{
return new ErrorResponse() return new ErrorResponse()
{ {
StatusCode = 403, StatusCode = 403,
Message = "User name not found or incorrect password" Message = "User name not found or incorrect password"
}; };
} }
}
/// <summary> /// <summary>