Skip to content

Commit 04ee626

Browse files
committed
Added role checks to base kits and hazard info delete
1 parent 78a6906 commit 04ee626

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

api/TwoWeeksReady/EmergencyKits/BaseKitsApi.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Newtonsoft.Json;
1616
using AzureFunctions.OidcAuthentication;
1717
using TwoWeeksReady.Common.EmergencyKits;
18+
using TwoWeeksReady.Authorization;
1819

1920
namespace TwoWeeksReady.EmergencyKits
2021
{
@@ -74,7 +75,13 @@ public async Task<IActionResult> CreateBaseKit(
7475
return new UnauthorizedResult();
7576
}
7677

77-
var content = await new StreamReader(req.Body).ReadToEndAsync();
78+
if (!authorizationResult.IsInRole(Roles.Admin))
79+
{
80+
log.LogWarning($"User is not in the {Roles.Admin} role");
81+
return new UnauthorizedResult();
82+
}
83+
84+
var content = await new StreamReader(req.Body).ReadToEndAsync();
7885
var newBaseKit = JsonConvert.DeserializeObject<BaseKit>(content);
7986
newBaseKit.Id = Guid.NewGuid().ToString();
8087
if(newBaseKit.Items.Count > 0)
@@ -105,6 +112,12 @@ public async Task<IActionResult> UpdateBaseKit(
105112
return new UnauthorizedResult();
106113
}
107114

115+
if (!authorizationResult.IsInRole(Roles.Admin))
116+
{
117+
log.LogWarning($"User is not in the {Roles.Admin} role");
118+
return new UnauthorizedResult();
119+
}
120+
108121
var content = await new StreamReader(req.Body).ReadToEndAsync();
109122
var kit = JsonConvert.DeserializeObject<BaseKit>(content);
110123

@@ -154,8 +167,13 @@ public async Task<IActionResult> DeleteBaseKit(
154167
log.LogWarning(authorizationResult.FailureReason);
155168
return new UnauthorizedResult();
156169
}
170+
if (!authorizationResult.IsInRole(Roles.Admin))
171+
{
172+
log.LogWarning($"User is not in the {Roles.Admin} role");
173+
return new UnauthorizedResult();
174+
}
157175

158-
if(String.IsNullOrWhiteSpace(id))
176+
if (String.IsNullOrWhiteSpace(id))
159177
{
160178
return new BadRequestObjectResult("Base Kit id was not specified.");
161179
}

api/TwoWeeksReady/Hazards/HazardApiBase.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ protected async Task<IActionResult> UpdateDocument(
149149
}
150150

151151
protected async Task<IActionResult> DeleteDocument(
152-
HttpRequest req, string id, DocumentClient client, ILogger log, string collectionName)
152+
HttpRequest req, string id, DocumentClient client, ILogger log, string collectionName, string requiredRole = null)
153153
{
154154
log.LogInformation($"Deleting {collectionName} document: id = {id}");
155155
var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers);
@@ -158,6 +158,12 @@ protected async Task<IActionResult> DeleteDocument(
158158
log.LogWarning(authorizationResult.FailureReason);
159159
return new UnauthorizedResult();
160160
}
161+
162+
if (!string.IsNullOrEmpty(requiredRole) && !authorizationResult.IsInRole(requiredRole))
163+
{
164+
log.LogWarning($"User is not in the {requiredRole} role");
165+
return new UnauthorizedResult();
166+
}
161167

162168
if (String.IsNullOrWhiteSpace(id))
163169
{

api/TwoWeeksReady/Hazards/HazardInfoApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public async Task<IActionResult> DeleteDocument(
7878
DocumentClient client,
7979
ILogger log)
8080
{
81-
return await DeleteDocument(req, id, client, log, CollectionName);
81+
return await DeleteDocument(req, id, client, log, CollectionName, Roles.Admin);
8282
}
8383
}
8484
}

0 commit comments

Comments
 (0)