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
{
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,14 +46,32 @@ 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();
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"
};
}
}
/// <summary>