From 9f51f12ccac35862ccf5a979faa67f3763cd2b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20G=C3=B3reczny?= Date: Tue, 12 Jan 2021 10:33:59 +0100 Subject: [PATCH] SES-150 fix bug that you can log as same user multiple times --- .../Controllers/UserController.cs | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/SessionCompanion/SessionCompanion/Controllers/UserController.cs b/SessionCompanion/SessionCompanion/Controllers/UserController.cs index 0f8f9d7..7074092 100644 --- a/SessionCompanion/SessionCompanion/Controllers/UserController.cs +++ b/SessionCompanion/SessionCompanion/Controllers/UserController.cs @@ -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; } /// @@ -41,13 +46,31 @@ namespace SessionCompanion.Controllers UserViewModel user = await _service.SearchUserByUsername(userName); if (user != null && user.Password.Equals(password)) - return user.Id; + { + List 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" + }; + } } -- 2.20.1