Compare commits
6 commits
dad144a80f
...
8058add053
Author | SHA1 | Date | |
---|---|---|---|
![]() |
8058add053 | ||
![]() |
a246764e44 | ||
![]() |
3230a5ed0f | ||
b59fef222f | |||
b2b8d1e076 | |||
c0860b05d1 |
6 changed files with 126 additions and 18 deletions
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DrinkRateAPI.ApiModels.UserProfile;
|
||||||
|
|
||||||
|
public class UserProfileAdminStatusPut
|
||||||
|
{
|
||||||
|
public bool ChangeStatusTo { get; set; }
|
||||||
|
}
|
49
DrinkRateAPI/AuthorizationPolicies/AdminOnlyRequirement.cs
Normal file
49
DrinkRateAPI/AuthorizationPolicies/AdminOnlyRequirement.cs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
using DrinkRateAPI.DbEntities;
|
||||||
|
using DrinkRateAPI.Services;
|
||||||
|
|
||||||
|
namespace DrinkRateAPI.AuthorizationPolicies;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
|
public class AdminOnlyRequirement : IAuthorizationRequirement
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AdminOnlyHandler : AuthorizationHandler<AdminOnlyRequirement>
|
||||||
|
{
|
||||||
|
private readonly ApplicationUserService _applicationUserService;
|
||||||
|
private readonly UserProfileService _userProfileService;
|
||||||
|
|
||||||
|
public AdminOnlyHandler(
|
||||||
|
ApplicationUserService applicationUserService,
|
||||||
|
UserProfileService userProfileService)
|
||||||
|
{
|
||||||
|
_applicationUserService = applicationUserService;
|
||||||
|
_userProfileService = userProfileService;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task HandleRequirementAsync(
|
||||||
|
AuthorizationHandlerContext context,
|
||||||
|
AdminOnlyRequirement requirement)
|
||||||
|
{
|
||||||
|
DbUserProfile userProfile;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
userProfile = await _applicationUserService.UserProfileByApplicationUserAsync(context.User);
|
||||||
|
}
|
||||||
|
catch (Exception _)
|
||||||
|
{
|
||||||
|
context.Fail();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_userProfileService.IsUserProfileAdmin(userProfile))
|
||||||
|
{
|
||||||
|
context.Succeed(requirement);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
context.Fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,18 +1,38 @@
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using DrinkRateAPI.ApiModels.UserProfile;
|
using DrinkRateAPI.ApiModels.UserProfile;
|
||||||
|
using DrinkRateAPI.Services;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace DrinkRateAPI.Controllers;
|
namespace DrinkRateAPI.Controllers;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("user_profile")]
|
[Route("userProfile")]
|
||||||
public class UserProfileController : ControllerBase
|
public class UserProfileController : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpPut(Name = "user_profile")]
|
private readonly ILogger<UserProfileController> _logger;
|
||||||
public UserProfileGet PutUserProfile(UserProfilePut userProfile)
|
private readonly UserProfileService _userProfileService;
|
||||||
|
|
||||||
|
public UserProfileController(ILogger<UserProfileController> logger, UserProfileService userProfileService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_userProfileService = userProfileService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserProfileGet PutUserProfile([FromBody] UserProfilePut userProfile)
|
||||||
{
|
{
|
||||||
throw new ApplicationException();
|
throw new ApplicationException();
|
||||||
var x = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //HttpContext.User.Identities.First();
|
var x = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //HttpContext.User.Identities.First();
|
||||||
return new();
|
return new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPut("{userId}/adminStatus")]
|
||||||
|
[Authorize(Policy = "AdminOnly")]
|
||||||
|
[Produces("application/json")]
|
||||||
|
public async Task<IActionResult> PutUserAdminStatus(string userId, [FromBody] UserProfileAdminStatusPut body)
|
||||||
|
{
|
||||||
|
var changedProfile = await _userProfileService.PutUserProfileAdminStatusAsync(userId, body.ChangeStatusTo);
|
||||||
|
|
||||||
|
return Ok(changedProfile);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -20,6 +20,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Folder Include="ApiModels\" />
|
||||||
<Folder Include="Migrations\" />
|
<Folder Include="Migrations\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
|
using DrinkRateAPI.AuthorizationPolicies;
|
||||||
using DrinkRateAPI.Contexts;
|
using DrinkRateAPI.Contexts;
|
||||||
using DrinkRateAPI.DbEntities;
|
using DrinkRateAPI.DbEntities;
|
||||||
|
using DrinkRateAPI.Services;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
|
||||||
|
@ -10,10 +13,13 @@ var builder = WebApplication.CreateBuilder(args);
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddAuthorization();
|
builder.Services.AddAuthorizationBuilder()
|
||||||
|
.AddPolicy("AdminOnly", policy =>
|
||||||
|
policy.Requirements.Add(new AdminOnlyRequirement()));
|
||||||
builder.Services.AddIdentityApiEndpoints<DbApplicationUser>()
|
builder.Services.AddIdentityApiEndpoints<DbApplicationUser>()
|
||||||
.AddEntityFrameworkStores<ApplicationDbContext>();
|
.AddEntityFrameworkStores<ApplicationDbContext>();
|
||||||
builder.Services.AddScoped<UserManager<DbApplicationUser>, UserWithProfileManager>();
|
builder.Services.AddScoped<UserManager<DbApplicationUser>, UserWithProfileManager>();
|
||||||
|
builder.Services.AddScoped<IAuthorizationHandler, AdminOnlyHandler>();
|
||||||
|
|
||||||
builder.Services.AddSwaggerGen(c =>
|
builder.Services.AddSwaggerGen(c =>
|
||||||
{
|
{
|
||||||
|
@ -42,7 +48,6 @@ builder.Services.AddSwaggerGen(c =>
|
||||||
Scheme = "oauth2",
|
Scheme = "oauth2",
|
||||||
Name = "Bearer",
|
Name = "Bearer",
|
||||||
In = ParameterLocation.Header,
|
In = ParameterLocation.Header,
|
||||||
|
|
||||||
},
|
},
|
||||||
new List<string>()
|
new List<string>()
|
||||||
}
|
}
|
||||||
|
@ -50,6 +55,8 @@ builder.Services.AddSwaggerGen(c =>
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Services.AddDbContext<ApplicationDbContext>();
|
builder.Services.AddDbContext<ApplicationDbContext>();
|
||||||
|
builder.Services.AddScoped<ApplicationUserService>();
|
||||||
|
builder.Services.AddScoped<UserProfileService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,36 @@
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using DrinkRateAPI.ApiModels.UserProfile;
|
using DrinkRateAPI.ApiModels.UserProfile;
|
||||||
using DrinkRateAPI.Contexts;
|
using DrinkRateAPI.Contexts;
|
||||||
|
using DrinkRateAPI.DbEntities;
|
||||||
|
using DrinkRateAPI.DbEntities;
|
||||||
|
using DrinkRateAPI.Exceptions;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
|
||||||
namespace DrinkRateAPI.Services;
|
namespace DrinkRateAPI.Services;
|
||||||
|
|
||||||
public class UserProfileService(ApplicationDbContext context,
|
public class UserProfileService(ApplicationDbContext context, ApplicationUserService applicationUserService)
|
||||||
ApplicationUserService applicationUserService)
|
|
||||||
{
|
{
|
||||||
private ApplicationDbContext _context = context;
|
private ApplicationDbContext _context = context;
|
||||||
private ApplicationUserService _applicationUserService = applicationUserService;
|
private ApplicationUserService _applicationUserService = applicationUserService;
|
||||||
|
|
||||||
|
public bool IsUserProfileAdmin(DbUserProfile userProfile)
|
||||||
|
{
|
||||||
|
return userProfile.IsAdmin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<DbUserProfile> PutUserProfileAdminStatusAsync(string userId, bool changeStatusTo)
|
||||||
|
{
|
||||||
|
var userProfile = GetUserProfileById(userId);
|
||||||
|
userProfile.IsAdmin = changeStatusTo;
|
||||||
|
_context.UserProfiles.Update(userProfile);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return userProfile;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<UserProfileGet> PutUserProfileAsync(UserProfilePut userProfile, ClaimsPrincipal identity)
|
public async Task<UserProfileGet> PutUserProfileAsync(UserProfilePut userProfile, ClaimsPrincipal identity)
|
||||||
{
|
{
|
||||||
var profile = _applicationUserService.UserProfileByApplicationUserAsync(identity);
|
var profile = _applicationUserService.UserProfileByApplicationUserAsync(identity);
|
||||||
|
@ -21,4 +39,11 @@ public class UserProfileService(ApplicationDbContext context,
|
||||||
|
|
||||||
return new();
|
return new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DbUserProfile GetUserProfileById(string userId)
|
||||||
|
{
|
||||||
|
var userProfile = _context.UserProfiles.FirstOrDefault(x => x.Id.ToString() == userId);
|
||||||
|
|
||||||
|
return userProfile ?? throw new NotFoundException();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue