180 lines
7.9 KiB
C#
180 lines
7.9 KiB
C#
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Core.Services;
|
|
using gehGassi.Domain.Users;
|
|
using gehGassi.Web.Helper;
|
|
using gehGassi.Web.Models;
|
|
using gehGassi.Web.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Localization;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Web.Hubs;
|
|
|
|
namespace gehGassi.Web.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller für Funktionen die via App nicht verfügbar sind, aber für App-Benutzer wichtig sind
|
|
/// </summary>
|
|
[Authorize]
|
|
public class AppController : BaseController
|
|
{
|
|
private readonly ILogger<AppController> _logger;
|
|
private readonly IStringLocalizer<AppController> _localizer;
|
|
private readonly IEmailSender _emailSender;
|
|
private readonly IUserService _userService;
|
|
private readonly IOptions<EmailSenderOptions> _emailSenderOptions;
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
private readonly DataProtectorTokenProvider<ApplicationUser> _tokenProvider;
|
|
private readonly IRefreshTokenService _refreshTokenService;
|
|
private readonly IPushNotificationService _pushNotificationService;
|
|
private readonly IAppUserService _appUserService;
|
|
private readonly IAppHubSender _appHubSender;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="logger">Instanz eines ILogger</param>
|
|
/// <param name="localizer">Instanz eines IStringLocalizer</param>
|
|
/// <param name="emailSender">Instanz eines IEmailSender</param>
|
|
/// <param name="userService">Instanz eines IUserService</param>
|
|
/// <param name="emailSenderOptions">Instanz von EmailSenderOptions</param>
|
|
/// <param name="userManager">Instanz eines UserManager</param>
|
|
/// <param name="tokenProvider">Instanz eines DataProtectorTokenProvider</param>
|
|
/// <param name="refreshTokenService">Instanz eines IRefreshTokenService</param>
|
|
/// <param name="pushNotificationService">Instanz eines IPushNotificationService</param>
|
|
/// <param name="appUserService">Instanz eines IAppUserService</param>
|
|
/// <param name="appHubSender">Instanz eines IAppHubSender</param>
|
|
public AppController(ILogger<AppController> logger, IStringLocalizer<AppController> localizer, IEmailSender emailSender, IUserService userService, IOptions<EmailSenderOptions> emailSenderOptions,
|
|
UserManager<ApplicationUser> userManager, DataProtectorTokenProvider<ApplicationUser> tokenProvider, IRefreshTokenService refreshTokenService, IPushNotificationService pushNotificationService, IAppUserService appUserService,
|
|
IAppHubSender appHubSender)
|
|
{
|
|
_logger = logger;
|
|
_localizer = localizer;
|
|
_emailSender = emailSender;
|
|
_userService = userService;
|
|
_emailSenderOptions = emailSenderOptions;
|
|
_userManager = userManager;
|
|
_tokenProvider = tokenProvider;
|
|
_refreshTokenService = refreshTokenService;
|
|
_pushNotificationService = pushNotificationService;
|
|
_appUserService = appUserService;
|
|
_appHubSender = appHubSender;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Zeigt eine View für das Löschen eines Accounts an
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
public IActionResult DeleteAccount()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handling für Account löschen
|
|
/// </summary>
|
|
/// <param name="model">Benutzereingaben</param>
|
|
/// <returns>Redirect oder View</returns>
|
|
[HttpPost]
|
|
[AllowAnonymous]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> DeleteAccount(DeleteAccountVm model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var user = await _userManager.FindByNameAsync(model.Email);
|
|
if (user != null && !user.LockoutEnd.HasValue)
|
|
{
|
|
var token = await _tokenProvider.GenerateAsync("deleteAccount", _userManager, user);
|
|
var callbackUrl = Url.DeleteAccountCallbackLinkApp(user.Id, token, Request.Scheme);
|
|
await _emailSender.SendDeleteAccountAsync(model.Email, callbackUrl, _localizer);
|
|
}
|
|
}
|
|
catch { }
|
|
|
|
return RedirectToAction(nameof(DeleteAccountConfirmation));
|
|
}
|
|
|
|
// If we got this far, something failed, redisplay form
|
|
return View(model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Zeigt eine Bestätungun an, dass eine Email zum Löschen des Accounts versendet wurde
|
|
/// Wird in jedem Fall angezeigt, auch wenn die Email nicht versendet werden konnte
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
public IActionResult DeleteAccountConfirmation()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handler wenn ein Account Löschen Link angeklickt wurde
|
|
/// </summary>
|
|
/// <param name="userId">Id des Benutzers</param>
|
|
/// <param name="code">Token-Code</param>
|
|
/// <returns>View</returns>
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> DoDeleteAccount(string userId, string code)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(userId))
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(code))
|
|
{
|
|
var user = await _userManager.FindByIdAsync(userId);
|
|
if (user != null)
|
|
{
|
|
var isCodeValid = await _tokenProvider.ValidateAsync("deleteAccount", code, _userManager, user);
|
|
if (isCodeValid)
|
|
{
|
|
//Sperren des Benutzers
|
|
user.LockoutEnd = DateTimeOffset.UtcNow.AddYears(5);
|
|
await _userManager.UpdateAsync(user);
|
|
|
|
//Löschen aller Refresh-tokens des Benutzers da eine Abmeldung erfolgt ist
|
|
await _refreshTokenService.RemoveAllAsync(user.Id, user.UserName);
|
|
await _refreshTokenService.CommitAsync(user.UserName);
|
|
|
|
var appUser = await _appUserService.GetAsync(user.AppUserId);
|
|
if (appUser != null)
|
|
{
|
|
appUser.Locked = true;
|
|
appUser.LockedReason = "Delete account requested";
|
|
await _appUserService.CommitAsync(user.UserName);
|
|
}
|
|
|
|
//Nun Email an gehgassi senden, dass der Benutzer gelöscht werden soll
|
|
await _emailSender.SendDeleteAccountEmailAsync(user.Email, user.FirstName, user.LastName, _localizer, _emailSenderOptions);
|
|
|
|
//Senden einer Nachricht an die App dass ein Logout durchgeführt werden soll?
|
|
if (!string.IsNullOrWhiteSpace(user.AppUserId))
|
|
{
|
|
//Logout-Aufforderung via SignalR
|
|
await _appHubSender.LogoutAsync(user.AppUserId);
|
|
//Logout-Aufforderung via Push-Notification
|
|
await _pushNotificationService.SendLogoutAsync(user.AppUserId);
|
|
}
|
|
|
|
return View();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return View("DoDeleteAccountFailed");
|
|
}
|
|
}
|
|
}
|