started moving api layer to shared abstract class
This commit is contained in:
@@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAPI", "WebAPI\WebAPI.csp
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web", "Web\Web.csproj", "{94D0887A-5A98-4738-A15B-E191EF3DDB7E}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web", "Web\Web.csproj", "{94D0887A-5A98-4738-A15B-E191EF3DDB7E}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{E23FD66C-9126-45A3-8615-9963CB74652B}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -18,5 +20,9 @@ Global
|
|||||||
{94D0887A-5A98-4738-A15B-E191EF3DDB7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{94D0887A-5A98-4738-A15B-E191EF3DDB7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{94D0887A-5A98-4738-A15B-E191EF3DDB7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{94D0887A-5A98-4738-A15B-E191EF3DDB7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{94D0887A-5A98-4738-A15B-E191EF3DDB7E}.Release|Any CPU.Build.0 = Release|Any CPU
|
{94D0887A-5A98-4738-A15B-E191EF3DDB7E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{E23FD66C-9126-45A3-8615-9963CB74652B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{E23FD66C-9126-45A3-8615-9963CB74652B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{E23FD66C-9126-45A3-8615-9963CB74652B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{E23FD66C-9126-45A3-8615-9963CB74652B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|||||||
18
Shared/APIService.cs
Normal file
18
Shared/APIService.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
namespace Shared
|
||||||
|
{
|
||||||
|
public abstract class APIService
|
||||||
|
{
|
||||||
|
protected ILogger Logger { get; set; }
|
||||||
|
protected HttpClient Client { get; set; }
|
||||||
|
|
||||||
|
protected APIService(ILogger logger, HttpClient httpClient)
|
||||||
|
{
|
||||||
|
Logger = logger;
|
||||||
|
Client = httpClient ?? new HttpClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Shared/Shared.csproj
Normal file
11
Shared/Shared.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
@using Web.Shared.Models
|
|
||||||
<EditForm Model="@_serverModel" OnValidSubmit="@HandleValidSubmit">
|
<EditForm Model="@_serverModel" OnValidSubmit="@HandleValidSubmit">
|
||||||
<DataAnnotationsValidator/>
|
<DataAnnotationsValidator/>
|
||||||
<ValidationSummary/>
|
<ValidationSummary/>
|
||||||
|
|||||||
@@ -10,4 +10,12 @@
|
|||||||
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.13.1" />
|
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.13.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Shared\Models" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\WebAPI\WebAPI.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -9,3 +9,4 @@
|
|||||||
@using Web
|
@using Web
|
||||||
@using Web.Shared
|
@using Web.Shared
|
||||||
@using Web.Shared.Forms
|
@using Web.Shared.Forms
|
||||||
|
@using WebAPI.Data.Dto
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Security.Claims;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using WebAPI.Auth;
|
||||||
using WebAPI.Data;
|
using WebAPI.Data;
|
||||||
using WebAPI.Data.Models;
|
using WebAPI.Data.Models;
|
||||||
|
|
||||||
@@ -13,32 +15,45 @@ namespace WebAPI.Controllers
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class AccountController : ControllerBase
|
public class AccountController : BaseController
|
||||||
{
|
{
|
||||||
private readonly PterodactylService _pterodactylService;
|
private readonly PterodactylService _pterodactylService;
|
||||||
private readonly AppDbContext _appDbContext;
|
|
||||||
|
|
||||||
public AccountController(PterodactylService pterodactylService, AppDbContext appDbContext)
|
public AccountController(IServiceProvider serviceProvider, PterodactylService pterodactylService) : base(serviceProvider)
|
||||||
{
|
{
|
||||||
_pterodactylService = pterodactylService;
|
_pterodactylService = pterodactylService;
|
||||||
_appDbContext = appDbContext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("IsInitialized")]
|
[Route("IsInitialized")]
|
||||||
public async Task<bool> IsUserInitialized()
|
public async Task<bool> IsUserInitialized()
|
||||||
{
|
{
|
||||||
return true;
|
return DbUser != default;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("Initialize")]
|
[Route("Initialize")]
|
||||||
public async Task InitializeUser(string email)
|
public async Task<bool> InitializeUser(string email)
|
||||||
{
|
{
|
||||||
if (await IsUserInitialized())
|
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;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
|
using WebAPI.Auth;
|
||||||
|
using WebAPI.Data;
|
||||||
|
using WebAPI.Data.Models;
|
||||||
|
|
||||||
namespace WebAPI.Controllers
|
namespace WebAPI.Controllers
|
||||||
{
|
{
|
||||||
public class BaseController : ControllerBase
|
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]
|
[ApiController]
|
||||||
public class HelloWorldController : BaseController
|
public class HelloWorldController : BaseController
|
||||||
{
|
{
|
||||||
private readonly PterodactylService _pterodactylService;
|
|
||||||
|
|
||||||
public HelloWorldController(PterodactylService pterodactylService)
|
|
||||||
{
|
|
||||||
_pterodactylService = pterodactylService;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<string> HelloWorld()
|
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]
|
[HttpGet]
|
||||||
@@ -36,5 +31,8 @@ namespace WebAPI.Controllers
|
|||||||
return "success";
|
return "success";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HelloWorldController(IServiceProvider serviceProvider) : base(serviceProvider)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace Web.Shared.Models
|
namespace WebAPI.Data.Dto
|
||||||
{
|
{
|
||||||
public class ServerLimitsModel
|
public class ServerLimitsModel
|
||||||
{
|
{
|
||||||
Reference in New Issue
Block a user