Skip to content

Commit 059865d

Browse files
committed
fix(announcements): grant acess to all announcements for Admins and AnnouncementManagers
1 parent c591c07 commit 059865d

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

src/Eurofurence.App.Backoffice/Services/AnnouncementService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public async Task UpdateAnnouncementAsync(Guid id, AnnouncementRequest request)
2323
response.EnsureSuccessStatusCode();
2424
}
2525

26+
/// <inheritdoc />
2627
public async Task DeleteAnnouncementAsync(Guid id)
2728
{
2829
using var response = await http.DeleteAsync($"Announcements/{id}");
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
3+
using System.Threading;
4+
using System.Threading.Tasks;
25
using Eurofurence.App.Domain.Model.Announcements;
36

47
namespace Eurofurence.App.Server.Services.Abstractions.Announcements
@@ -7,6 +10,18 @@ public interface IAnnouncementService :
710
IEntityServiceOperations<AnnouncementRecord, AnnouncementResponse>,
811
IPatchOperationProcessor<AnnouncementRecord>
912
{
13+
/// <summary>
14+
/// Fetches all announcements regardless of whether the user is a member of the group or not.
15+
/// </summary>
16+
/// <returns>Collection of all found records.</returns>
1017
public IQueryable<AnnouncementRecord> FetchAll();
18+
19+
/// <summary>
20+
/// Finds an announcement by its id regardless of whether the user is a member of the group or not.
21+
/// </summary>
22+
/// <param name="id">The id to look up.</param>
23+
/// <param name="cancellationToken"></param>
24+
/// <returns>A task with the record. Can be null.</returns>
25+
public Task<AnnouncementRecord> FindOneInAllAsync(Guid id, CancellationToken cancellationToken = default);
1126
}
1227
}

src/Eurofurence.App.Server.Services/Announcements/AnnouncementService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,19 @@ IIdentityService identityService
3737
_identityService = identityService;
3838
}
3939

40+
/// <inheritdoc />
4041
public IQueryable<AnnouncementRecord> FetchAll()
4142
{
4243
return _appDbContext.Announcements.AsNoTracking();
4344
}
4445

46+
/// <inheritdoc />
47+
public async Task<AnnouncementRecord> FindOneInAllAsync(Guid id, CancellationToken cancellationToken = default)
48+
{
49+
return await _appDbContext.Announcements.AsNoTracking().FirstOrDefaultAsync(entity => entity.Id == id, cancellationToken);
50+
}
51+
52+
/// <inheritdoc />
4553
public override async Task<AnnouncementRecord> FindOneAsync(Guid id, CancellationToken cancellationToken = default)
4654
{
4755
return await FindAll()

src/Eurofurence.App.Server.Web/Controllers/AnnouncementsController.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public async Task<AnnouncementResponse> GetAnnouncementAsync([FromRoute] Guid id
8787
[ProducesResponseType(typeof(string), 404)]
8888
public async Task<ActionResult> DeleteAnnouncementAsync([FromRoute] Guid id)
8989
{
90-
if (await _announcementService.FindOneAsync(id) == null) return NotFound();
90+
if (await QueryRecordForIdAsync(id) == null) return NotFound();
9191

9292
await _announcementService.DeleteOneAsync(id);
9393
await _pushNotificationChannelManager.PushSyncRequestAsync();
@@ -146,7 +146,9 @@ public async Task<ActionResult> PutAnnouncementAsync([FromRoute] Guid id,
146146
return BadRequest("Error parsing request");
147147
}
148148

149-
if (await _announcementService.FindOneAsync(id) is not { } announcementRecord)
149+
AnnouncementRecord record = await QueryRecordForIdAsync(id);
150+
151+
if (record is not { } announcementRecord)
150152
{
151153
return NotFound();
152154
}
@@ -160,6 +162,29 @@ public async Task<ActionResult> PutAnnouncementAsync([FromRoute] Guid id,
160162
return NoContent();
161163
}
162164

165+
/// <summary>
166+
/// Returns the announcement record for the given id.
167+
/// If the user is an admin or announcement manager, the record is returned regardless of whether
168+
/// they are a member of the groups or not.
169+
/// </summary>
170+
/// <param name="id">The id to look up.</param>
171+
/// <returns>Task with the record. Can be null.</returns>
172+
private async Task<AnnouncementRecord> QueryRecordForIdAsync(Guid id)
173+
{
174+
AnnouncementRecord record;
175+
176+
if (User.IsInRole(IdentityRoles.Admin) ||
177+
User.IsInRole(IdentityRoles.AnnouncementManager))
178+
{
179+
record = await _announcementService.FindOneInAllAsync(id);
180+
}
181+
else
182+
{
183+
record = await _announcementService.FindOneAsync(id);
184+
}
185+
return record;
186+
}
187+
163188
/// <summary>
164189
/// !DANGER! – Deletes all announcements from the database!
165190
/// </summary>

0 commit comments

Comments
 (0)