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
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using Microsoft.AspNetCore.SignalR;
using SessionCompanion.Database.Tables;
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.Hubs;
using SessionCompanion.ViewModels.ApiResponses;
using SessionCompanion.ViewModels.CharacterViewModels;
using SessionCompanion.ViewModels.UserViewModels;
[Route("api/user")]
@ -20,13 +23,15 @@ namespace SessionCompanion.Controllers
public class UserController : Controller
{
private readonly IUserService _service;
private readonly ICharacterService _characterService;
private SessionHubData _sessionHubData;
public UserController(IUserService service)
public UserController(IUserService service, ICharacterService characterService)
{
this._service = service;
this._sessionHubData = new SessionHubData();
this._characterService = characterService;
}
/// <summary>
@ -41,13 +46,31 @@ namespace SessionCompanion.Controllers
UserViewModel user = await _service.SearchUserByUsername(userName);
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()
{
StatusCode = 403,
Message = "User name not found or incorrect password"
};
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()
{
StatusCode = 403,
Message = "User name not found or incorrect password"
};
}
}