mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 14:34:23 +01:00
Switching to UserManager for fetching user id
This commit is contained in:
@@ -12,6 +12,7 @@ using BTCPayServer.Models.NotificationViewModels;
|
||||
using BTCPayServer.Security;
|
||||
using Google;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BTCPayServer.Controllers
|
||||
@@ -22,20 +23,21 @@ namespace BTCPayServer.Controllers
|
||||
{
|
||||
private readonly ApplicationDbContext _db;
|
||||
private readonly EventAggregator _eventAggregator;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public NotificationsController(ApplicationDbContext db, EventAggregator eventAggregator)
|
||||
public NotificationsController(ApplicationDbContext db, EventAggregator eventAggregator, UserManager<ApplicationUser> userManager)
|
||||
{
|
||||
_db = db;
|
||||
_eventAggregator = eventAggregator;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index(int skip = 0, int count = 50, int timezoneOffset = 0)
|
||||
{
|
||||
if (!validUserClaim(out var claimWithId))
|
||||
if (!validUserClaim(out var userId))
|
||||
return RedirectToAction("Index", "Home");
|
||||
|
||||
var userId = claimWithId.Value;
|
||||
var model = new IndexViewModel()
|
||||
{
|
||||
Items = _db.Notifications
|
||||
@@ -61,9 +63,9 @@ namespace BTCPayServer.Controllers
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> FlipRead(string id)
|
||||
{
|
||||
if (validUserClaim(out var claimWithId))
|
||||
if (validUserClaim(out var userId))
|
||||
{
|
||||
var notif = _db.Notifications.Single(a => a.Id == id && a.ApplicationUserId == claimWithId.Value);
|
||||
var notif = _db.Notifications.Single(a => a.Id == id && a.ApplicationUserId == userId);
|
||||
notif.Seen = !notif.Seen;
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
@@ -83,9 +85,9 @@ namespace BTCPayServer.Controllers
|
||||
|
||||
if (parsedGuids != null)
|
||||
{
|
||||
if (command == "delete" && validUserClaim(out var claimWithId))
|
||||
if (command == "delete" && validUserClaim(out var userId))
|
||||
{
|
||||
var toRemove = _db.Notifications.Where(a => a.ApplicationUserId == claimWithId.Value && parsedGuids.Contains(a.Id));
|
||||
var toRemove = _db.Notifications.Where(a => a.ApplicationUserId == userId && parsedGuids.Contains(a.Id));
|
||||
_db.Notifications.RemoveRange(toRemove);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
@@ -96,10 +98,10 @@ namespace BTCPayServer.Controllers
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
private bool validUserClaim(out Claim claimWithId)
|
||||
private bool validUserClaim(out string userId)
|
||||
{
|
||||
claimWithId = User.Claims.SingleOrDefault(a => a.Type == ClaimTypes.NameIdentifier);
|
||||
return claimWithId != null;
|
||||
userId = _userManager.GetUserId(User);
|
||||
return userId != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,26 +74,28 @@ namespace BTCPayServer.HostedServices
|
||||
public class NotificationManager
|
||||
{
|
||||
private readonly ApplicationDbContext _db;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public NotificationManager(ApplicationDbContext db)
|
||||
public NotificationManager(ApplicationDbContext db, UserManager<ApplicationUser> userManager)
|
||||
{
|
||||
_db = db;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
public NotificationSummaryViewModel GetSummaryNotifications(ClaimsPrincipal user)
|
||||
{
|
||||
var resp = new NotificationSummaryViewModel();
|
||||
var claimWithId = user.Claims.SingleOrDefault(a => a.Type == ClaimTypes.NameIdentifier);
|
||||
var userId = _userManager.GetUserId(user);
|
||||
|
||||
// TODO: Soft caching in order not to pound database too much
|
||||
resp.UnseenCount = _db.Notifications
|
||||
.Where(a => a.ApplicationUserId == claimWithId.Value && !a.Seen)
|
||||
.Where(a => a.ApplicationUserId == userId && !a.Seen)
|
||||
.Count();
|
||||
|
||||
if (resp.UnseenCount > 0)
|
||||
{
|
||||
resp.Last5 = _db.Notifications
|
||||
.Where(a => a.ApplicationUserId == claimWithId.Value && !a.Seen)
|
||||
.Where(a => a.ApplicationUserId == userId && !a.Seen)
|
||||
.OrderByDescending(a => a.Created)
|
||||
.Take(5)
|
||||
.Select(a => a.ViewModel())
|
||||
|
||||
Reference in New Issue
Block a user