A lot of Misc Fixes (#2656)
This commit is contained in:
parent
088af37960
commit
b1e9d8cbba
15 changed files with 116 additions and 92 deletions
|
|
@ -395,7 +395,7 @@ public class AccountController : BaseApiController
|
|||
// Send a confirmation email
|
||||
try
|
||||
{
|
||||
var emailLink = await _accountService.GenerateEmailLink(Request, user.ConfirmationToken, "confirm-email-update", dto.Email);
|
||||
var emailLink = await _emailService.GenerateEmailLink(Request, user.ConfirmationToken, "confirm-email-update", dto.Email);
|
||||
_logger.LogCritical("[Update Email]: Email Link for {UserName}: {Link}", user.UserName, emailLink);
|
||||
|
||||
if (!_emailService.IsValidEmail(user.Email))
|
||||
|
|
@ -585,7 +585,7 @@ public class AccountController : BaseApiController
|
|||
if (string.IsNullOrEmpty(user.ConfirmationToken))
|
||||
return BadRequest(await _localizationService.Translate(User.GetUserId(), "manual-setup-fail"));
|
||||
|
||||
return await _accountService.GenerateEmailLink(Request, user.ConfirmationToken, "confirm-email", user.Email!, withBaseUrl);
|
||||
return await _emailService.GenerateEmailLink(Request, user.ConfirmationToken, "confirm-email", user.Email!, withBaseUrl);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -691,7 +691,7 @@ public class AccountController : BaseApiController
|
|||
|
||||
try
|
||||
{
|
||||
var emailLink = await _accountService.GenerateEmailLink(Request, user.ConfirmationToken, "confirm-email", dto.Email);
|
||||
var emailLink = await _emailService.GenerateEmailLink(Request, user.ConfirmationToken, "confirm-email", dto.Email);
|
||||
_logger.LogCritical("[Invite User]: Email Link for {UserName}: {Link}", user.UserName, emailLink);
|
||||
|
||||
var settings = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();
|
||||
|
|
@ -911,7 +911,7 @@ public class AccountController : BaseApiController
|
|||
}
|
||||
|
||||
var token = await _userManager.GeneratePasswordResetTokenAsync(user);
|
||||
var emailLink = await _accountService.GenerateEmailLink(Request, token, "confirm-reset-password", user.Email);
|
||||
var emailLink = await _emailService.GenerateEmailLink(Request, token, "confirm-reset-password", user.Email);
|
||||
user.ConfirmationToken = token;
|
||||
_unitOfWork.UserRepository.Update(user);
|
||||
await _unitOfWork.CommitAsync();
|
||||
|
|
@ -989,7 +989,7 @@ public class AccountController : BaseApiController
|
|||
user.ConfirmationToken = token;
|
||||
_unitOfWork.UserRepository.Update(user);
|
||||
await _unitOfWork.CommitAsync();
|
||||
var emailLink = await _accountService.GenerateEmailLink(Request, token, "confirm-email-update", user.Email);
|
||||
var emailLink = await _emailService.GenerateEmailLink(Request, token, "confirm-email-update", user.Email);
|
||||
_logger.LogCritical("[Email Migration]: Email Link for {UserName}: {Link}", user.UserName, emailLink);
|
||||
|
||||
if (!_emailService.IsValidEmail(user.Email))
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ public class DeviceController : BaseApiController
|
|||
}
|
||||
finally
|
||||
{
|
||||
await _eventHub.SendMessageToAsync(MessageFactory.SendingToDevice,
|
||||
await _eventHub.SendMessageToAsync(MessageFactory.NotificationProgress,
|
||||
MessageFactory.SendingToDeviceEvent(await _localizationService.Translate(userId, "send-to-device-status"),
|
||||
"ended"), userId);
|
||||
}
|
||||
|
|
@ -167,7 +167,7 @@ public class DeviceController : BaseApiController
|
|||
}
|
||||
finally
|
||||
{
|
||||
await _eventHub.SendMessageToAsync(MessageFactory.SendingToDevice,
|
||||
await _eventHub.SendMessageToAsync(MessageFactory.NotificationProgress,
|
||||
MessageFactory.SendingToDeviceEvent(await _localizationService.Translate(User.GetUserId(), "send-to-device-status"),
|
||||
"ended"), userId);
|
||||
}
|
||||
|
|
@ -175,8 +175,6 @@ public class DeviceController : BaseApiController
|
|||
return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-send-to"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -112,6 +112,13 @@ public class LibraryController : BaseApiController
|
|||
if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-library"));
|
||||
_logger.LogInformation("Created a new library: {LibraryName}", library.Name);
|
||||
|
||||
// Restart Folder watching if on
|
||||
var settings = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();
|
||||
if (settings.EnableFolderWatching)
|
||||
{
|
||||
await _libraryWatcher.RestartWatching();
|
||||
}
|
||||
|
||||
// Assign all the necessary users with this library side nav
|
||||
var userIds = admins.Select(u => u.Id).Append(User.GetUserId()).ToList();
|
||||
var userNeedingNewLibrary = (await _unitOfWork.UserRepository.GetAllUsersAsync(AppUserIncludes.SideNavStreams))
|
||||
|
|
|
|||
|
|
@ -209,18 +209,6 @@ public class ServerController : BaseApiController
|
|||
return Ok(await _versionUpdaterService.GetAllReleases());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Is this server accessible to the outside net
|
||||
/// </summary>
|
||||
/// <remarks>If the instance has the HostName set, this will return true whether or not it is accessible externally</remarks>
|
||||
/// <returns></returns>
|
||||
[HttpGet("accessible")]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<bool>> IsServerAccessible()
|
||||
{
|
||||
return Ok(await _accountService.CheckIfAccessible(Request));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of reoccurring jobs. Scheduled ad-hoc jobs will not be returned.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -500,4 +500,16 @@ public class SettingsController : BaseApiController
|
|||
// NOTE: This must match Hangfire's underlying cron system. Hangfire is unique
|
||||
return Ok(CronHelper.IsValidCron(cronExpression));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a test email to see if email settings are hooked up correctly
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[HttpPost("test-email-url")]
|
||||
public async Task<ActionResult<EmailTestResultDto>> TestEmailServiceUrl()
|
||||
{
|
||||
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId());
|
||||
return Ok(await _emailService.SendTestEmail(user!.Email));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,9 +27,6 @@ public interface IAccountService
|
|||
Task<bool> HasBookmarkPermission(AppUser? user);
|
||||
Task<bool> HasDownloadPermission(AppUser? user);
|
||||
Task<bool> HasChangeRestrictionRole(AppUser? user);
|
||||
Task<bool> CheckIfAccessible(HttpRequest request);
|
||||
Task<string> GenerateEmailLink(HttpRequest request, string token, string routePart, string email, bool withHost = true);
|
||||
|
||||
}
|
||||
|
||||
public class AccountService : IAccountService
|
||||
|
|
@ -37,50 +34,13 @@ public class AccountService : IAccountService
|
|||
private readonly UserManager<AppUser> _userManager;
|
||||
private readonly ILogger<AccountService> _logger;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IHostEnvironment _environment;
|
||||
private readonly IEmailService _emailService;
|
||||
public const string DefaultPassword = "[k.2@RZ!mxCQkJzE";
|
||||
private const string LocalHost = "localhost:4200";
|
||||
|
||||
public AccountService(UserManager<AppUser> userManager, ILogger<AccountService> logger, IUnitOfWork unitOfWork,
|
||||
IHostEnvironment environment, IEmailService emailService)
|
||||
public AccountService(UserManager<AppUser> userManager, ILogger<AccountService> logger, IUnitOfWork unitOfWork)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_logger = logger;
|
||||
_unitOfWork = unitOfWork;
|
||||
_environment = environment;
|
||||
_emailService = emailService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the instance is accessible. If the host name is filled out, then it will assume it is accessible as email generation will use host name.
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> CheckIfAccessible(HttpRequest request)
|
||||
{
|
||||
var host = _environment.IsDevelopment() ? LocalHost : request.Host.ToString();
|
||||
return !string.IsNullOrEmpty((await _unitOfWork.SettingsRepository.GetSettingsDtoAsync()).HostName) || await _emailService.CheckIfAccessible(host);
|
||||
}
|
||||
|
||||
public async Task<string> GenerateEmailLink(HttpRequest request, string token, string routePart, string email, bool withHost = true)
|
||||
{
|
||||
var serverSettings = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();
|
||||
var host = _environment.IsDevelopment() ? LocalHost : request.Host.ToString();
|
||||
var basePart = $"{request.Scheme}://{host}{request.PathBase}/";
|
||||
if (!string.IsNullOrEmpty(serverSettings.HostName))
|
||||
{
|
||||
basePart = serverSettings.HostName;
|
||||
if (!serverSettings.BaseUrl.Equals(Configuration.DefaultBaseUrl))
|
||||
{
|
||||
var removeCount = serverSettings.BaseUrl.EndsWith('/') ? 1 : 0;
|
||||
basePart += serverSettings.BaseUrl.Substring(0, serverSettings.BaseUrl.Length - removeCount);
|
||||
}
|
||||
}
|
||||
|
||||
if (withHost) return $"{basePart}/registration/{routePart}?token={HttpUtility.UrlEncode(token)}&email={HttpUtility.UrlEncode(email)}";
|
||||
return $"registration/{routePart}?token={HttpUtility.UrlEncode(token)}&email={HttpUtility.UrlEncode(email)}"
|
||||
.Replace("//", "/");
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ApiException>> ChangeUserPassword(AppUser user, string newPassword)
|
||||
|
|
|
|||
|
|
@ -5,10 +5,13 @@ using System.IO;
|
|||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using API.Data;
|
||||
using API.DTOs.Email;
|
||||
using Kavita.Common;
|
||||
using MailKit.Security;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MimeKit;
|
||||
|
||||
|
|
@ -36,6 +39,9 @@ public interface IEmailService
|
|||
Task<EmailTestResultDto> SendTestEmail(string adminEmail);
|
||||
Task SendEmailChangeEmail(ConfirmationEmailDto data);
|
||||
bool IsValidEmail(string email);
|
||||
|
||||
Task<string> GenerateEmailLink(HttpRequest request, string token, string routePart, string email,
|
||||
bool withHost = true);
|
||||
}
|
||||
|
||||
public class EmailService : IEmailService
|
||||
|
|
@ -43,19 +49,17 @@ public class EmailService : IEmailService
|
|||
private readonly ILogger<EmailService> _logger;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IDirectoryService _directoryService;
|
||||
private readonly IHostEnvironment _environment;
|
||||
|
||||
private const string TemplatePath = @"{0}.html";
|
||||
/// <summary>
|
||||
/// This is used to initially set or reset the ServerSettingKey. Do not access from the code, access via UnitOfWork
|
||||
/// </summary>
|
||||
public const string DefaultApiUrl = "https://email.kavitareader.com";
|
||||
private const string LocalHost = "localhost:4200";
|
||||
|
||||
|
||||
public EmailService(ILogger<EmailService> logger, IUnitOfWork unitOfWork, IDirectoryService directoryService)
|
||||
public EmailService(ILogger<EmailService> logger, IUnitOfWork unitOfWork, IDirectoryService directoryService, IHostEnvironment environment)
|
||||
{
|
||||
_logger = logger;
|
||||
_unitOfWork = unitOfWork;
|
||||
_directoryService = directoryService;
|
||||
_environment = environment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -142,6 +146,26 @@ public class EmailService : IEmailService
|
|||
return new EmailAddressAttribute().IsValid(email);
|
||||
}
|
||||
|
||||
public async Task<string> GenerateEmailLink(HttpRequest request, string token, string routePart, string email, bool withHost = true)
|
||||
{
|
||||
var serverSettings = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();
|
||||
var host = _environment.IsDevelopment() ? LocalHost : request.Host.ToString();
|
||||
var basePart = $"{request.Scheme}://{host}{request.PathBase}";
|
||||
if (!string.IsNullOrEmpty(serverSettings.HostName))
|
||||
{
|
||||
basePart = serverSettings.HostName;
|
||||
if (!serverSettings.BaseUrl.Equals(Configuration.DefaultBaseUrl))
|
||||
{
|
||||
var removeCount = serverSettings.BaseUrl.EndsWith('/') ? 1 : 0;
|
||||
basePart += serverSettings.BaseUrl[..^removeCount];
|
||||
}
|
||||
}
|
||||
|
||||
if (withHost) return $"{basePart}/registration/{routePart}?token={HttpUtility.UrlEncode(token)}&email={HttpUtility.UrlEncode(email)}";
|
||||
return $"registration/{routePart}?token={HttpUtility.UrlEncode(token)}&email={HttpUtility.UrlEncode(email)}"
|
||||
.Replace("//", "/");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends an invite email to a user to setup their account
|
||||
/// </summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue