started moving api layer to shared abstract class
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebAPI.Auth;
|
||||
using WebAPI.Data;
|
||||
using WebAPI.Data.Models;
|
||||
|
||||
@@ -13,32 +15,45 @@ namespace WebAPI.Controllers
|
||||
[Authorize]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class AccountController : ControllerBase
|
||||
public class AccountController : BaseController
|
||||
{
|
||||
private readonly PterodactylService _pterodactylService;
|
||||
private readonly AppDbContext _appDbContext;
|
||||
|
||||
public AccountController(PterodactylService pterodactylService, AppDbContext appDbContext)
|
||||
public AccountController(IServiceProvider serviceProvider, PterodactylService pterodactylService) : base(serviceProvider)
|
||||
{
|
||||
_pterodactylService = pterodactylService;
|
||||
_appDbContext = appDbContext;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("IsInitialized")]
|
||||
public async Task<bool> IsUserInitialized()
|
||||
{
|
||||
return true;
|
||||
return DbUser != default;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("Initialize")]
|
||||
public async Task InitializeUser(string email)
|
||||
public async Task<bool> InitializeUser(string email)
|
||||
{
|
||||
if (await IsUserInitialized())
|
||||
{
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
var name = User.FindFirst(ClaimTypes.Name)?.Value.Split(' ') ?? "No Name".Split(' ');
|
||||
var firstName = name.FirstOrDefault();
|
||||
var lastName = name.ElementAt(1);
|
||||
var username = User.FindFirst(OIDCClaimTypes.Username)?.Value ?? "nouser";
|
||||
var externalId = User.FindFirst(OIDCClaimTypes.Subject)?.Value ?? "nosubject";
|
||||
var pterodactylResult = await _pterodactylService.SendPterodactylUserCreate(username, email, firstName, lastName, externalId);
|
||||
int clientId = pterodactylResult.Attributes.Id;
|
||||
AppDbContext.Users.Add(new User
|
||||
{
|
||||
ExternalAuthIdentifier = externalId,
|
||||
PterodactylUserId = clientId
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,29 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using WebAPI.Auth;
|
||||
using WebAPI.Data;
|
||||
using WebAPI.Data.Models;
|
||||
|
||||
namespace WebAPI.Controllers
|
||||
{
|
||||
public class BaseController : ControllerBase
|
||||
{
|
||||
[Inject]
|
||||
protected AppDbContext AppDbContext { get; private set; }
|
||||
|
||||
public BaseController(IServiceProvider serviceProvider)
|
||||
{
|
||||
AppDbContext = serviceProvider.GetRequiredService<AppDbContext>();
|
||||
}
|
||||
|
||||
protected User DbUser => AppDbContext.Users.FirstOrDefault(user =>
|
||||
user.ExternalAuthIdentifier == User.FindFirstValue(OIDCClaimTypes.Subject));
|
||||
}
|
||||
}
|
||||
@@ -16,17 +16,12 @@ namespace WebAPI.Controllers
|
||||
[ApiController]
|
||||
public class HelloWorldController : BaseController
|
||||
{
|
||||
private readonly PterodactylService _pterodactylService;
|
||||
|
||||
public HelloWorldController(PterodactylService pterodactylService)
|
||||
{
|
||||
_pterodactylService = pterodactylService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<string> HelloWorld()
|
||||
{
|
||||
return JsonConvert.SerializeObject(User.Claims.Select(claim => new {claim.Type, claim.Value}));
|
||||
return AppDbContext.ToString();
|
||||
//return JsonConvert.SerializeObject(User.Claims.Select(claim => new {claim.Type, claim.Value}));
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -35,6 +30,9 @@ namespace WebAPI.Controllers
|
||||
{
|
||||
return "success";
|
||||
}
|
||||
|
||||
|
||||
public HelloWorldController(IServiceProvider serviceProvider) : base(serviceProvider)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user