using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using RSecurityBackend.Services; using RSecurityBackend.Controllers; using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Authorization; using System.Net; using RSecurityBackend.Models.Auth.ViewModels; using System.Threading.Tasks; using Audit.WebApi; using RSecurityBackend.Models.Generic; using System; using RMuseum.Models.Auth.ViewModel; namespace RMuseum.Controllers { /// /// User login/logout/register/... /// [Produces("application/json")] [Route("api/users")] public class AppUserController : AppUserControllerBase { /// /// login /// /// loginViewModel /// LoggedOnUserModel [HttpPost] [AllowAnonymous] [Route("login")] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(LoggedOnUserModelEx))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public override async Task Login( [AuditIgnore] [FromBody] LoginViewModel loginViewModel ) { string clientIPAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString(); RServiceResult res = await _appUserService.Login(loginViewModel, clientIPAddress); if (res.Result == null) { return BadRequest(res.ExceptionString); } var l = res.Result; bool keepHistory = false; var kRes = await _optionsService.GetValueAsync("KeepHistory", l.User.Id); if (!string.IsNullOrEmpty(kRes.Result)) bool.TryParse(kRes.Result, out keepHistory); LoggedOnUserModelEx loggedOnUserModelEx = new LoggedOnUserModelEx() { User = l.User, Token = l.Token, SessionId = l.SessionId, SecurableItem = l.SecurableItem, KeepHistory = keepHistory }; return Ok(loggedOnUserModelEx); } /// /// renew an expired session /// /// user session id /// LoggedOnUserModel [HttpPut] [AllowAnonymous] [Route("relogin/{sessionId}")] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(LoggedOnUserModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public override async Task ReLogin( Guid sessionId ) { string clientIPAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString(); RServiceResult res = await _appUserService.ReLogin(sessionId, clientIPAddress); if (res.Result == null) { return BadRequest(res.ExceptionString); } var l = res.Result; bool keepHistory = false; var kRes = await _optionsService.GetValueAsync("KeepHistory", l.User.Id); if (!string.IsNullOrEmpty(kRes.Result)) bool.TryParse(kRes.Result, out keepHistory); LoggedOnUserModelEx loggedOnUserModelEx = new LoggedOnUserModelEx() { User = l.User, Token = l.Token, SessionId = l.SessionId, SecurableItem = l.SecurableItem, KeepHistory = keepHistory }; return Ok(loggedOnUserModelEx); } /// /// options service /// protected readonly IRGenericOptionsService _optionsService; /// /// constructor /// /// /// /// /// /// /// /// /// public AppUserController( IRGenericOptionsService optionsService, IConfiguration configuration, IAppUserService appUserService, IHttpContextAccessor httpContextAccessor, IUserPermissionChecker userPermissionChecker, IEmailSender emailSender, IImageFileService imageFileService, ICaptchaService captchaService) : base(configuration, appUserService, httpContextAccessor, userPermissionChecker, emailSender, imageFileService, captchaService) { _optionsService = optionsService; } } }