Skip to content

Commit e0a9943

Browse files
committed
Add/Update: AdminPasswordAttribute and Login Logic
1 parent f172194 commit e0a9943

38 files changed

Lines changed: 225 additions & 41 deletions

TcgPlatformApi/Controllers/BoosterController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using TcgPlatformApi.Services;
44
using Microsoft.AspNetCore.Authorization;
55
using System.Security.Claims;
6+
using TcgPlatformApi.FiltersAndAttributes;
67

78
namespace TcgPlatformApi.Controllers
89
{
@@ -19,6 +20,7 @@ public BoosterController(IBoosterService playerBoosterService)
1920
}
2021

2122
[HttpPost("addboosters")]
23+
[AdminPassword]
2224
public async Task<IActionResult> AddBoosterAsync([FromBody] List<BoosterRequest> requests)
2325
{
2426
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
@@ -55,6 +57,7 @@ public async Task<IActionResult> GetPlayerBoostersAsync()
5557

5658

5759
[HttpPost("removeboosters")]
60+
[AdminPassword]
5861
public async Task<IActionResult> RemoveBoosterAsync([FromBody] List<BoosterRequest> requests)
5962
{
6063
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

TcgPlatformApi/Controllers/CardController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Authorization;
22
using Microsoft.AspNetCore.Mvc;
33
using System.Security.Claims;
4+
using TcgPlatformApi.FiltersAndAttributes;
45
using TcgPlatformApi.Models;
56
using TcgPlatformApi.Services;
67

@@ -19,6 +20,7 @@ public CardController(ICardService playerCardService)
1920
}
2021

2122
[HttpPost("addcards")]
23+
[AdminPassword]
2224
public async Task<IActionResult> AddCardsAsync([FromBody] List<CardRequest> requests)
2325
{
2426
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
@@ -54,6 +56,7 @@ public async Task<IActionResult> GetPlayerCardsAsync()
5456
}
5557

5658
[HttpPost("removecards")]
59+
[AdminPassword]
5760
public async Task<IActionResult> RemoveCardsAsync([FromBody] List<CardRequest> requests)
5861
{
5962
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

TcgPlatformApi/Controllers/DeckCardController.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Authorization;
22
using Microsoft.AspNetCore.Mvc;
33
using System.Security.Claims;
4+
using TcgPlatformApi.FiltersAndAttributes;
45
using TcgPlatformApi.Models;
56
using TcgPlatformApi.Services;
67

@@ -19,6 +20,7 @@ public DeckCardController(IDeckCardService deckCardService)
1920
}
2021

2122
[HttpPost("adddeckcard")]
23+
[AdminPassword]
2224
public async Task<IActionResult> AddCardInDeckAsync([FromBody] DeckCardRequest request)
2325
{
2426
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
@@ -34,6 +36,7 @@ public async Task<IActionResult> AddCardInDeckAsync([FromBody] DeckCardRequest r
3436
}
3537

3638
[HttpPost("removedeckcard")]
39+
[AdminPassword]
3740
public async Task<IActionResult> RemoveCardsAsync([FromBody] DeckCardRemoveRequest request)
3841
{
3942
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
@@ -49,6 +52,7 @@ public async Task<IActionResult> RemoveCardsAsync([FromBody] DeckCardRemoveReque
4952
}
5053

5154
[HttpPost("updatedeckcardsorder")]
55+
[AdminPassword]
5256
public async Task<IActionResult> UpdateDeckCardOrderAsync([FromBody] List<DeckCardOrderRequest> requests)
5357
{
5458
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

TcgPlatformApi/Controllers/DeckController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Authorization;
22
using Microsoft.AspNetCore.Mvc;
33
using System.Security.Claims;
4+
using TcgPlatformApi.FiltersAndAttributes;
45
using TcgPlatformApi.Models;
56
using TcgPlatformApi.Services;
67

@@ -19,6 +20,7 @@ public DeckController(IDeckService playerDeckService)
1920
}
2021

2122
[HttpPost("addorupdatedecks")]
23+
[AdminPassword]
2224
public async Task<IActionResult> AddorUpdateDecksAsync([FromBody] List<DeckRequest> requests)
2325
{
2426
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
@@ -34,7 +36,7 @@ public async Task<IActionResult> AddorUpdateDecksAsync([FromBody] List<DeckReque
3436
DeckName = r.DeckName,
3537
PlayerId = parsedPlayerId,
3638
Order = r.Order,
37-
Cards = r.Cards.Select(c => new CardInDeck
39+
Cards = r.Cards.Select(c => new DeckCardClient
3840
{
3941
CardId = c.CardId,
4042
Quantity = c.Quantity
@@ -46,6 +48,7 @@ public async Task<IActionResult> AddorUpdateDecksAsync([FromBody] List<DeckReque
4648
}
4749

4850
[HttpPost("removedecks")]
51+
[AdminPassword]
4952
public async Task<IActionResult> RemoveDeckAsync([FromBody] List<DeckRemoveRequest> requests)
5053
{
5154
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
@@ -87,6 +90,7 @@ public async Task<IActionResult> GetAllDecksByPlayerIdAsync()
8790
}
8891

8992
[HttpPost("updatedecksorder")]
93+
[AdminPassword]
9094
public async Task<IActionResult> UpdateDeckCardOrderAsync([FromBody] List<DeckOrderRequest> requests)
9195
{
9296
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

TcgPlatformApi/Controllers/ProfileController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Authorization;
22
using Microsoft.AspNetCore.Mvc;
33
using System.Security.Claims;
4+
using TcgPlatformApi.FiltersAndAttributes;
45
using TcgPlatformApi.Models;
56
using TcgPlatformApi.Services;
67

@@ -25,6 +26,7 @@ public async Task<IActionResult> GetProfile(int id)
2526
}
2627

2728
[HttpPost("createprofile")]
29+
[AdminPassword]
2830
public async Task<IActionResult> CreateProfile([FromBody] PlayerProfile newProfile)
2931
{
3032
var createProfile = await _profileService.CreateProfile(newProfile);
@@ -33,6 +35,7 @@ public async Task<IActionResult> CreateProfile([FromBody] PlayerProfile newProfi
3335

3436
[Authorize]
3537
[HttpPost("updateprofile")]
38+
[AdminPassword]
3639
public async Task<IActionResult> UpdateProfile([FromBody] PlayerProfileDTO updatedProfile)
3740
{
3841
var profileId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

TcgPlatformApi/Controllers/RegVerLogController.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Mvc;
2+
using System.Security.Claims;
23
using TcgPlatformApi.Models;
34
using TcgPlatformApi.Services;
45

@@ -36,6 +37,20 @@ public async Task<IActionResult> Login([FromBody] PlayerProfileLogRequest reques
3637
return Ok(result);
3738
}
3839

40+
[HttpGet("getplayerprofile")]
41+
public async Task<IActionResult> GetPlayerProfile()
42+
{
43+
var playerId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
44+
45+
if (!int.TryParse(playerId, out int parsedPlayerId))
46+
{
47+
return BadRequest("Invalid playerId!");
48+
}
49+
50+
var result = await _regVerLogService.GetPlayerProfile(parsedPlayerId);
51+
return Ok(result);
52+
}
53+
3954
[HttpPost("resetpassword")]
4055
public async Task<IActionResult> ResetPassword([FromBody] RessPassRequest request)
4156
{

TcgPlatformApi/Data/AppDbContext.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.EntityFrameworkCore;
1+
using Microsoft.AspNetCore.Http.HttpResults;
2+
using Microsoft.EntityFrameworkCore;
23
using TcgPlatformApi.Models;
34

45
namespace TcgPlatformApi.Data
@@ -24,4 +25,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2425

2526
//dotnet tool install --global dotnet-ef //first
2627
//dotnet ef migrations add AddRegistrationFields
27-
//dotnet ef database update
28+
//dotnet ef database update
29+
30+
//CREATE INDEX idx_player_cards_playerid ON "PlayerCards" ("PlayerId");
31+
//CREATE INDEX idx_player_boosters_playerid ON "PlayerBoosters" ("PlayerId");
32+
//CREATE INDEX idx_player_decks_playerid ON "PlayerDecks" ("PlayerId");
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Mvc.Filters;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.Options;
4+
using TcgPlatformApi.Settings;
5+
6+
namespace TcgPlatformApi.FiltersAndAttributes
7+
{
8+
public class AdminPasswordAttribute : ActionFilterAttribute
9+
{
10+
public override void OnActionExecuting(ActionExecutingContext context)
11+
{
12+
var providedPassword = context.HttpContext.Request.Headers["Admin-Password"].FirstOrDefault();
13+
14+
var config = context.HttpContext.RequestServices.GetRequiredService<IOptions<AdminSettings>>();
15+
var correctPassword = config.Value.Password;
16+
17+
if (string.IsNullOrEmpty(providedPassword) || providedPassword != correctPassword)
18+
{
19+
context.Result = new UnauthorizedObjectResult("Invalid admin password");
20+
return;
21+
}
22+
23+
base.OnActionExecuting(context);
24+
}
25+
}
26+
}

TcgPlatformApi/Filters/FileUploadOperationFilter.cs renamed to TcgPlatformApi/FiltersAndAttributes/FileUploadOperationFilter.cs

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)