Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AdminのControllerのAuthorizeに渡す値が誤っている問題を修正 #2248

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -871,7 +871,7 @@
},
"price": {
"type": "integer",
"description": "単価を取得または設定します。",
"description": "単価を取得または設定します。\n ",
"format": "int64",
"pattern": "^[1-9]\\d{0,15}$"
},
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
Loading