#272 overriding login method to add the configuration for tracking user browsing history by her or his will

This commit is contained in:
Hamid Reza Mohammadi 2022-01-08 19:10:48 +03:30
parent 25eb416899
commit 904d9152ee
4 changed files with 153 additions and 4 deletions

View File

@ -4,7 +4,14 @@ 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
{
@ -15,9 +22,103 @@ namespace RMuseum.Controllers
[Route("api/users")]
public class AppUserController : AppUserControllerBase
{
/// <summary>
/// login
/// </summary>
/// <param name="loginViewModel">loginViewModel</param>
/// <returns>LoggedOnUserModel</returns>
[HttpPost]
[AllowAnonymous]
[Route("login")]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(LoggedOnUserModelEx))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public override async Task<IActionResult> Login(
[AuditIgnore]
[FromBody]
LoginViewModel loginViewModel
)
{
string clientIPAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
RServiceResult<LoggedOnUserModel> 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);
}
/// <summary>
/// renew an expired session
/// </summary>
/// <param name="sessionId">user session id</param>
/// <returns>LoggedOnUserModel</returns>
[HttpPut]
[AllowAnonymous]
[Route("relogin/{sessionId}")]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(LoggedOnUserModel))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public override async Task<IActionResult> ReLogin(
Guid sessionId
)
{
string clientIPAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
RServiceResult<LoggedOnUserModel> 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);
}
/// <summary>
/// options service
/// </summary>
private IRGenericOptionsService _optionsService;
/// <summary>
/// constructor
/// </summary>
/// <param name="optionsService"></param>
/// <param name="configuration"></param>
/// <param name="appUserService"></param>
/// <param name="httpContextAccessor"></param>
@ -25,10 +126,12 @@ namespace RMuseum.Controllers
/// <param name="emailSender"></param>
/// <param name="imageFileService"></param>
/// <param name="captchaService"></param>
public AppUserController(IConfiguration configuration, IAppUserService appUserService, IHttpContextAccessor httpContextAccessor, IUserPermissionChecker userPermissionChecker, IEmailSender emailSender, IImageFileService imageFileService, ICaptchaService captchaService)
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;
}
}
}

View File

@ -0,0 +1,15 @@
using RSecurityBackend.Models.Auth.ViewModels;
namespace RMuseum.Models.Auth.ViewModel
{
/// <summary>
/// LoginViewModel with app specfic properties
/// </summary>
public class LoggedOnUserModelEx : LoggedOnUserModel
{
/// <summary>
/// keep user browsing history if he or she wants so
/// </summary>
public bool KeepHistory { get; set; }
}
}

View File

@ -9,10 +9,30 @@
User login/logout/register/...
</summary>
</member>
<member name="M:RMuseum.Controllers.AppUserController.#ctor(Microsoft.Extensions.Configuration.IConfiguration,RSecurityBackend.Services.IAppUserService,Microsoft.AspNetCore.Http.IHttpContextAccessor,RSecurityBackend.Services.IUserPermissionChecker,Microsoft.AspNetCore.Identity.UI.Services.IEmailSender,RSecurityBackend.Services.IImageFileService,RSecurityBackend.Services.ICaptchaService)">
<member name="M:RMuseum.Controllers.AppUserController.Login(RSecurityBackend.Models.Auth.ViewModels.LoginViewModel)">
<summary>
login
</summary>
<param name="loginViewModel">loginViewModel</param>
<returns>LoggedOnUserModel</returns>
</member>
<member name="M:RMuseum.Controllers.AppUserController.ReLogin(System.Guid)">
<summary>
renew an expired session
</summary>
<param name="sessionId">user session id</param>
<returns>LoggedOnUserModel</returns>
</member>
<member name="F:RMuseum.Controllers.AppUserController._optionsService">
<summary>
options service
</summary>
</member>
<member name="M:RMuseum.Controllers.AppUserController.#ctor(RSecurityBackend.Services.IRGenericOptionsService,Microsoft.Extensions.Configuration.IConfiguration,RSecurityBackend.Services.IAppUserService,Microsoft.AspNetCore.Http.IHttpContextAccessor,RSecurityBackend.Services.IUserPermissionChecker,Microsoft.AspNetCore.Identity.UI.Services.IEmailSender,RSecurityBackend.Services.IImageFileService,RSecurityBackend.Services.ICaptchaService)">
<summary>
constructor
</summary>
<param name="optionsService"></param>
<param name="configuration"></param>
<param name="appUserService"></param>
<param name="httpContextAccessor"></param>
@ -3393,6 +3413,16 @@
web site
</summary>
</member>
<member name="T:RMuseum.Models.Auth.ViewModel.LoggedOnUserModelEx">
<summary>
LoginViewModel with app specfic properties
</summary>
</member>
<member name="P:RMuseum.Models.Auth.ViewModel.LoggedOnUserModelEx.KeepHistory">
<summary>
keep user browsing history if he or she wants so
</summary>
</member>
<member name="T:RMuseum.Models.Bookmark.RBookmarkType">
<summary>
Bookmark Type

View File

@ -87,5 +87,6 @@
},
"WebServiceUrl": "http://localhost:3439",
"ReadOnlyMode": "False",
"AuditNetEnabled": "False",
"AllowedHosts": "*"
}