Skip to content

Commit

Permalink
ポリシーを示す文字列をWeb.Admin内にinternal constで定義するように修正 (#2248)
Browse files Browse the repository at this point in the history
  • Loading branch information
KentaHizume authored Jan 9, 2025
1 parent a4d0e88 commit 523cd65
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public static class Roles
/// <summary>
/// 管理者のロールを表す文字列です。
/// </summary>
public static readonly string Admin = "Admin";
public static readonly string Admin = "ROLE_ADMIN";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Dressca.Web.Admin.Authorization;

/// <summary>
/// ポリシーを管理するための静的クラスです。
/// </summary>
internal static class Policies
{
/// <summary>
/// 管理者ロールが必要であるというポリシーを示す文字列です。
/// </summary>
internal const string RequireAdminRole = "RequireAdminRole";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dressca.ApplicationCore.Catalog;
using Dressca.SystemCommon;
using Dressca.SystemCommon.Mapper;
using Dressca.Web.Admin.Authorization;
using Dressca.Web.Admin.Controllers.ApiModel;
using Dressca.Web.Admin.Dto.CatalogItems;
using Dressca.Web.Controllers;
Expand Down Expand Up @@ -65,7 +66,7 @@ public CatalogItemsController(
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ProblemDetails))]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ProblemDetails))]
[OpenApiOperation("getCatalogItem")]
[Authorize(Roles = nameof(Admin))]
[Authorize(Policy = Policies.RequireAdminRole)]
public async Task<IActionResult> GetCatalogItemAsync(long catalogItemId)
{
CatalogItem? catalogItem;
Expand Down Expand Up @@ -106,7 +107,7 @@ public async Task<IActionResult> GetCatalogItemAsync(long catalogItemId)
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ProblemDetails))]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ProblemDetails))]
[OpenApiOperation("getByQuery")]
[Authorize(Roles = nameof(Admin))]
[Authorize(Policy = Policies.RequireAdminRole)]
public async Task<IActionResult> GetByQueryAsync([FromQuery] FindCatalogItemsQuery query)
{
(IReadOnlyList<CatalogItem> CatalogItems, int TotalCount) itemsAndCount;
Expand Down Expand Up @@ -154,7 +155,7 @@ await this.service.GetCatalogItemsForAdminAsync(
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ProblemDetails))]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ProblemDetails))]
[OpenApiOperation("postCatalogItem")]
[Authorize(Roles = nameof(Admin))]
[Authorize(Policy = Policies.RequireAdminRole)]
public async Task<IActionResult> PostCatalogItemAsync(PostCatalogItemRequest postCatalogItemRequest)
{
CatalogItem catalogItem;
Expand Down Expand Up @@ -200,7 +201,7 @@ public async Task<IActionResult> PostCatalogItemAsync(PostCatalogItemRequest pos
[ProducesResponseType(StatusCodes.Status409Conflict, Type = typeof(ProblemDetails))]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ProblemDetails))]
[OpenApiOperation("deleteCatalogItem")]
[Authorize(Roles = nameof(Admin))]
[Authorize(Policy = Policies.RequireAdminRole)]
public async Task<IActionResult> DeleteCatalogItemAsync(long catalogItemId, [FromQuery] byte[] rowVersion)
{
try
Expand Down Expand Up @@ -241,7 +242,7 @@ public async Task<IActionResult> DeleteCatalogItemAsync(long catalogItemId, [Fro
[ProducesResponseType(StatusCodes.Status409Conflict, Type = typeof(ProblemDetails))]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ProblemDetails))]
[OpenApiOperation("putCatalogItem")]
[Authorize(Roles = nameof(Admin))]
[Authorize(Policy = Policies.RequireAdminRole)]
public async Task<IActionResult> PutCatalogItemAsync(long catalogItemId, PutCatalogItemRequest putCatalogItemRequest)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
// ローカル開発環境用の認証ハンドラーを登録します。
_ = builder.Services.AddAuthentication("DummyAuthentication")
.AddScheme<AuthenticationSchemeOptions, DummyAuthenticationHandler>("DummyAuthentication", null);
builder.Services.AddAuthorization();

builder.Services.AddHttpLogging(logging =>
{
Expand All @@ -104,6 +103,9 @@
// 本番環境用の認証ハンドラーを登録します。
}

builder.Services.AddAuthorizationBuilder()
.AddPolicy(Policies.RequireAdminRole, policy => policy.RequireRole(Roles.Admin));

builder.Services.AddSingleton<
IAuthorizationMiddlewareResultHandler, StatusCodeMapAuthorizationMiddlewareResultHandler>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override Task<AuthenticateResult> HandleAuthenticateAsync()
// ダミーのユーザー名とロール名を設定します。
Claim[] claims = [
new Claim(ClaimTypes.Name, "dummy_user"),
new Claim(ClaimTypes.Role, "Admin")
new Claim(ClaimTypes.Role, "ROLE_ADMIN")
];
var identity = new ClaimsIdentity(claims, this.Scheme.Name);
var principal = new ClaimsPrincipal(identity);
Expand Down

0 comments on commit 523cd65

Please sign in to comment.