Added some GDPR things (data export, deletion, edit)

This commit is contained in:
Matěj Kubíček
2026-04-18 19:04:22 +02:00
parent 982093ff7a
commit ee7aa57383
6 changed files with 222 additions and 15 deletions
@@ -44,9 +44,43 @@ public class AuthController : Controller
{
return View();
}
[HttpGet]
[Authorize]
public async Task<IActionResult> Profile()
{
var userResult = await _authService.FindUserByEmailOrUsernameAsync(User.Identity?.Name ?? "");
if (!userResult.Success || userResult.Data == null) return RedirectToAction("Login");
var model = new ProfileViewModel
{
UserName = userResult.Data.UserName,
Email = userResult.Data.Email
};
return View(model);
}
#endregion
#region httpPost
[HttpPost] [Authorize] [ValidateAntiForgeryToken]
public async Task<IActionResult> Profile(ProfileViewModel model)
{
if (!ModelState.IsValid) return View(model);
var userIdClaim = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);
if (userIdClaim == null || !int.TryParse(userIdClaim.Value, out int userId)) return RedirectToAction("Login");
var result = await _authService.UpdateProfileAsync(userId, model);
if (!result.Success)
{
ModelState.AddModelError(string.Empty, result.Message);
return View(model);
}
TempData["SuccessMessage"] = "Profile updated successfully. Please log in again if you changed your username.";
return RedirectToAction("Profile");
}
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken]
public async Task<IActionResult> Login(AuthLoginViewModel model)
{
@@ -136,5 +170,21 @@ public class AuthController : Controller
return RedirectToAction("Register");
}
[HttpGet]
[Authorize]
public async Task<IActionResult> DownloadData()
{
var userIdClaim = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);
if (userIdClaim == null || !int.TryParse(userIdClaim.Value, out int userId)) return RedirectToAction("Login");
var result = await _authService.GetUserDataAsync(userId);
if (!result.Success || result.Data == null) return BadRequest(result.Message);
var json = System.Text.Json.JsonSerializer.Serialize(result.Data, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
var bytes = System.Text.Encoding.UTF8.GetBytes(json);
return File(bytes, "application/json", $"user_data_{User.Identity?.Name}.json");
}
#endregion
}