Base.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Threading.Tasks;
  3. using BO.AppServer.Business.Services;
  4. using BO.AppServer.Metadata.Dto;
  5. using BO.AppServer.Metadata.Enums;
  6. using Microsoft.AspNetCore.Authorization;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Logging;
  9. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  10. using Quadarax.Foundation.Core.Exceptions;
  11. namespace BO.AppServer.Web.Services
  12. {
  13. public abstract class Base<TController> : ControllerBase
  14. where TController : ControllerBase
  15. {
  16. protected abstract RoleEnum LoginRoleAllowed { get; }
  17. protected AccessService _srvAccess;
  18. private ILogger<TController> _logger;
  19. protected Base(ILoggerFactory logger, AccessService srvAccess)
  20. {
  21. _logger = logger.CreateLogger<TController>() ?? throw new ArgumentNullException(nameof(logger));
  22. _srvAccess = srvAccess ?? throw new ArgumentNullException(nameof(srvAccess));
  23. }
  24. [AllowAnonymous]
  25. [HttpGet("{magicKey}/auth")]
  26. public async Task<ResultValueDto<AuthDto>> Authorize(string magicKey)
  27. {
  28. _srvAccess.OpenMagic(magicKey);
  29. return new ResultValueDto<AuthDto>(new AuthDto() { Ticket = _srvAccess.GetToken() });
  30. }
  31. [AllowAnonymous]
  32. [HttpGet("{ticket}/ping")]
  33. public async Task<ResultValueDto<PingDto>> Ping(string ticket)
  34. {
  35. CheckAccess(ticket);
  36. return new ResultValueDto<PingDto>(new PingDto(){Status = "OK"});
  37. }
  38. [AllowAnonymous]
  39. [HttpPost("{ticket}/login")]
  40. public async Task<ResultValueDto<UserRDto>> Login(string ticket, [FromHeader] string userName, [FromHeader] string userPassword)
  41. {
  42. CheckAccess(ticket);
  43. return await _srvAccess.Login(userName, userPassword, LoginRoleAllowed);
  44. }
  45. public async Task<TResult> Call<TResult>(Func<Task<TResult>> block) where TResult : ResultDto, new()
  46. {
  47. var result = new TResult();
  48. try
  49. {
  50. return await block();
  51. }
  52. catch (Exception e)
  53. {
  54. ProcessException(result, e);
  55. }
  56. return result;
  57. }
  58. protected void ProcessException(ResultDto result, Exception exception)
  59. {
  60. if (exception is CodeException codeException)
  61. result.Errors = new[] { new ErrorMessageDto(codeException) };
  62. else
  63. result.Errors = new[] { new ErrorMessageDto(exception, 0001) };
  64. }
  65. protected void LogInfo(string message)
  66. {
  67. _logger.Log(LogLevel.Information, message);
  68. }
  69. protected void LogDebug(string message)
  70. {
  71. _logger.Log(LogLevel.Debug, message);
  72. }
  73. protected void LogTrace(string message)
  74. {
  75. _logger.Log(LogLevel.Trace, message);
  76. }
  77. protected void LogWarn(string message)
  78. {
  79. _logger.Log(LogLevel.Warning, message);
  80. }
  81. protected void LogWarn(string message, Exception exception)
  82. {
  83. _logger.Log(LogLevel.Warning, message);
  84. }
  85. protected void LogWarn(Exception exception)
  86. {
  87. _logger.Log(LogLevel.Warning, exception.ToString());
  88. }
  89. protected void LogError(string message)
  90. {
  91. _logger.Log(LogLevel.Error, message);
  92. }
  93. protected void LogError(string message, Exception exception)
  94. {
  95. _logger.Log(LogLevel.Error, message);
  96. }
  97. protected void LogError(Exception exception)
  98. {
  99. _logger.Log(LogLevel.Error, exception.ToString());
  100. }
  101. protected void CheckAccess(string token)
  102. {
  103. _srvAccess.CheckToken(token);
  104. }
  105. }
  106. }