Updating initial Location crud endpoints to be global & restricted to SUPER role

This commit is contained in:
2026-01-20 17:13:12 -06:00
parent eb34e6692f
commit cdeea60e52
5 changed files with 7 additions and 35 deletions
@@ -2,7 +2,6 @@ using LocationsApi.Models;
using LocationsApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
namespace LocationsApi.Controllers;
@@ -24,13 +23,8 @@ public class LocationsController : ControllerBase
if (string.IsNullOrWhiteSpace(req.Name))
return BadRequest("Name required");
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrWhiteSpace(userId))
return Unauthorized();
var location = new Location
{
OwnerUserId = userId,
Name = req.Name.Trim(),
CreatedUtc = DateTime.UtcNow
};
@@ -40,14 +34,10 @@ public class LocationsController : ControllerBase
}
[HttpGet]
[Authorize(Roles = "USER,SUPER")]
[Authorize(Roles = "SUPER")]
public async Task<IActionResult> ListMine()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrWhiteSpace(userId))
return Unauthorized();
var locations = await _locations.GetForOwnerAsync(userId);
var locations = await _locations.GetAllAsync();
return Ok(locations);
}
@@ -55,12 +45,7 @@ public class LocationsController : ControllerBase
[Authorize(Roles = "SUPER")]
public async Task<IActionResult> Delete(string id)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrWhiteSpace(userId))
return Unauthorized();
var allowAnyOwner = User.IsInRole("SUPER");
var deleted = await _locations.DeleteForOwnerAsync(id, userId, allowAnyOwner);
var deleted = await _locations.DeleteAsync(id);
if (!deleted)
return NotFound();