59 lines
1.9 KiB
C#
59 lines
1.9 KiB
C#
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;
|
|
|
|
namespace WebAPI.Controllers
|
|
{
|
|
[Authorize]
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class AccountController : BaseController
|
|
{
|
|
private readonly PterodactylService _pterodactylService;
|
|
|
|
public AccountController(IServiceProvider serviceProvider, PterodactylService pterodactylService) : base(serviceProvider)
|
|
{
|
|
_pterodactylService = pterodactylService;
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("IsInitialized")]
|
|
public async Task<bool> IsUserInitialized()
|
|
{
|
|
return DbUser != default;
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("Initialize")]
|
|
public async Task<bool> InitializeUser(string email)
|
|
{
|
|
if (await IsUserInitialized())
|
|
{
|
|
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;
|
|
}
|
|
|
|
}
|
|
} |