Bump more methods

This commit is contained in:
code liturgy 2022-12-09 00:27:00 +00:00
parent 78d8c8d0bd
commit 70dc2afd1f
8 changed files with 208 additions and 14 deletions

View File

@ -73,7 +73,7 @@ namespace CodeLiturgy.Data.Auth.Context.Users
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
identity.AddClaim(new Claim(ClaimTypes.MobilePhone, user.PhoneNumber));
//identity.AddClaim(new Claim(ClaimTypes.MobilePhone, user.PhoneNumber));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));

View File

@ -2,26 +2,49 @@ using Sharp.Augeas;
namespace CodeLiturgy.Views.Augeas
{
/// <summary>
/// Manager class for Augeas.
/// </summary>
public class AugeasManager
{
private readonly Sharp.Augeas.Augeas _augeas;
private AugSettings _augSettings;
/// <summary>
/// Cached List with the current apache configurations.
/// </summary>
public List<string> CurrentApacheConfigurations => _currentApacheConfigurations;
private List<string> _currentApacheConfigurations;
private const string _apachePath = "/etc/apache2/sites-available";
private const string ApachePath = "/etc/apache2/sites-available";
/// <summary>
/// Augeas Manager constructor.
/// </summary>
public AugeasManager()
{
_augSettings = AugeasExtensions.GetSettings();
_augeas = new Sharp.Augeas.Augeas(_augSettings);
RefreshApacheConfigurations();
}
/// <summary>
/// Write Changes to the disk
/// </summary>
/// <returns>Returns true if the save was successful.</returns>
public bool Save()
{
return _augeas.Save();
}
/// <summary>
/// Gets a preview of the configuration.
/// </summary>
/// <param name="site"></param>
/// <returns></returns>
public string GetPreview(string site)
{
@ -30,12 +53,18 @@ namespace CodeLiturgy.Views.Augeas
return string.Empty;
}
var siteFullPath = $"{_apachePath}/{site}";
var siteFullPath = $"{ApachePath}/{site}";
_augeas.LoadFile(siteFullPath);
var preview = _augeas.GetPreview($"/files{siteFullPath}");
return preview;
}
/// <summary>
/// Gets available Apache configurations.
/// </summary>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <returns></returns>
public List<string> GetApacheAvailableConfigurations(int skip, int take)
{
var result = _currentApacheConfigurations
@ -47,6 +76,11 @@ namespace CodeLiturgy.Views.Augeas
}
/// <summary>
/// Gets virtual host tree of a site.
/// </summary>
/// <param name="site">Name of the site.</param>
/// <returns></returns>
public SuperNode GetVirtualHostTree(string site)
{
if (!_currentApacheConfigurations.Contains(site))
@ -54,7 +88,7 @@ namespace CodeLiturgy.Views.Augeas
return null;
}
var siteFullPath = $"{_apachePath}/{site}";
var siteFullPath = $"{ApachePath}/{site}";
_augeas.LoadFile(siteFullPath);
var tree = _augeas.GetTree("VirtualHost",$"/files{siteFullPath}/VirtualHost/*");
return tree;
@ -63,7 +97,7 @@ namespace CodeLiturgy.Views.Augeas
private void RefreshApacheConfigurations()
{
var apacheDir = _augSettings.root + _apachePath;
var apacheDir = _augSettings.root + ApachePath;
DirectoryInfo d = new DirectoryInfo(apacheDir);
FileInfo[] files = d.GetFiles("*.conf");
@ -74,6 +108,19 @@ namespace CodeLiturgy.Views.Augeas
_currentApacheConfigurations = result;
}
/// <summary>
/// Gets a directive.
/// </summary>
/// <param name="siteName"></param>
/// <param name="directiveName"></param>
/// <returns></returns>
public Directive GetDirective(string siteName, string directiveName)
{
var tree = GetVirtualHostTree(siteName);
var directive = tree.GetDirective(directiveName);
return directive;
}
}
}

View File

@ -1,5 +1,5 @@
<VirtualHost 10.10.10.10:443>
ServerName git.example2.com
ServerName git.megacode3.com
ServerAlias www.git.example2.com
SSLEngine on
SSLProxyEngine on

View File

@ -8,6 +8,9 @@ using System.Text.Json;
namespace CodeLiturgy.Views.Controllers
{
/// <summary>
/// Controller responsible to manage Apache configurations.
/// </summary>
[ApiController]
[Authorize]
public class ApacheController : ControllerBase
@ -16,6 +19,13 @@ namespace CodeLiturgy.Views.Controllers
private readonly SiteDbContext _siteDbContext;
private readonly AugeasManager _augeasManager;
/// <summary>
/// Apache controller constructor.
/// </summary>
/// <param name="userManager"></param>
/// <param name="logger"></param>
/// <param name="siteDbContext"></param>
/// <param name="augeasManager"></param>
public ApacheController(
ApplicationUserManager userManager,
ILogger<SitesController> logger,
@ -27,6 +37,13 @@ namespace CodeLiturgy.Views.Controllers
_augeasManager = augeasManager;
}
/// <summary>
/// Gets site in /etc/apache/sites-available
/// </summary>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <param name="orderDir"></param>
/// <returns></returns>
[HttpGet("/api/apache")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
@ -42,6 +59,11 @@ namespace CodeLiturgy.Views.Controllers
return Ok(sites);
}
/// <summary>
/// Gets a preview of the configuration of a certain site.
/// </summary>
/// <param name="site">Site configuration filename.</param>
/// <returns></returns>
[HttpGet("/api/apache/preview/{site}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
@ -56,6 +78,11 @@ namespace CodeLiturgy.Views.Controllers
return Ok(preview);
}
/// <summary>
/// Gets tree object of a certain site.
/// </summary>
/// <param name="site">Site configuration file.</param>
/// <returns></returns>
[HttpGet("/api/apache/tree/{site}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
@ -70,6 +97,60 @@ namespace CodeLiturgy.Views.Controllers
return Ok(tree);
}
/// <summary>
/// Gets a directive.
/// </summary>
/// <param name="site"></param>
/// <param name="directive"></param>
/// <returns></returns>
[HttpGet("/api/apache/tree/{site}/directive/{directive}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public ActionResult GetDirective(
string site,
string directive)
{
var rDirective = _augeasManager.GetDirective(site, directive);
return Ok(rDirective);
}
/// <summary>
/// Sets a new value for the directive. The first word is the directive value, the following words correspond to the arguments.
/// </summary>
/// <param name="site">Site configuration filename.</param>
/// <param name="directive"></param>
/// <param name="newValue"></param>
/// <returns></returns>
[HttpPost("/api/apache/tree/{site}/directive/{directive}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public ActionResult SetDirective(
string site,
string directive,
string newValue)
{
var rDirective = _augeasManager.GetDirective(site, directive);
rDirective?.Set(newValue);
return Ok(rDirective);
}
/// <summary>
/// Persists changes to the configurations to disk.
/// </summary>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status406NotAcceptable)]
[HttpGet("save")]
public ActionResult Save()
{
var sucess = _augeasManager.Save();
return Ok(sucess);
}
}
}

View File

@ -1,10 +1,12 @@
using System.Security.Claims;
using CodeLiturgy.Data.Application.Users;
using CodeLiturgy.Data.Auth;
using CodeLiturgy.Data.Auth.Context.Users;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Controller = Microsoft.AspNetCore.Mvc.Controller;
namespace CodeLiturgy.Views.Controllers
@ -34,6 +36,7 @@ namespace CodeLiturgy.Views.Controllers
[ActionName("LoginAction")]
public async Task<IActionResult> LoginAction(LoginRequest loginRequest)
{
var (identity,success) =
await _authManager.DoLogin(loginRequest);
@ -56,13 +59,45 @@ namespace CodeLiturgy.Views.Controllers
return Redirect(RootLocation);
}
return Redirect(RootLocation);
}
public IActionResult Login()
public async Task<IActionResult> Login()
{
#if DEBUG
// Check if the debug user exists, if not create it
if (!await _userManager.Users.AnyAsync(x => x.Email == "debuguser@admin.com"))
{
var registerRequest = new RegisterRequest();
registerRequest.Email = "debuguser@admin.com";
registerRequest.Username = "debuguser";
registerRequest.Password = "debuguser";
await _authManager.CreateUserAsync(registerRequest);
}
var (identity,success) = await _authManager.DoLogin(new LoginRequest{Email = "debuguser@admin.com", Password = "debuguser"});
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity),
new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTime.UtcNow.Add(SessionConstants.DefaultSessionMaxAge)
});
HttpContext.Session.SetString("hello", "world");
return Redirect(RootLocation);
#else
this.HandleGlobalization();
return View();
#endif
}
public async Task<IActionResult> Account()

View File

@ -1,12 +1,16 @@
using CodeLiturgy.Data.Auth.Context.Users;
using CodeLiturgy.Data.Application;
using CodeLiturgy.Domain;
using CodeLiturgy.Views.Augeas;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace CodeLiturgy.Views.Controllers
{
/// <summary>
/// Controller responsible for managing websites.
/// </summary>
[Route("api/sites")]
[ApiController]
[Authorize]
@ -16,14 +20,34 @@ namespace CodeLiturgy.Views.Controllers
private ApplicationUserManager _userManager;
private ILogger<SitesController> _logger;
private readonly SiteDbContext _siteDbContext;
private readonly AugeasManager _augeasManager;
public SitesController(ApplicationUserManager userManager, ILogger<SitesController> logger, SiteDbContext siteDbContext)
/// <summary>
/// Sites controller constructor.
/// </summary>
/// <param name="userManager"></param>
/// <param name="logger"></param>
/// <param name="siteDbContext"></param>
/// <param name="augeasManager"></param>
public SitesController(
ApplicationUserManager userManager,
ILogger<SitesController> logger,
SiteDbContext siteDbContext,
AugeasManager augeasManager)
{
_logger = logger;
_userManager = userManager;
_siteDbContext = siteDbContext;
_augeasManager = augeasManager;
}
/// <summary>
/// Gets the sites stored in the database.
/// </summary>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <param name="orderDir"></param>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
@ -39,11 +63,11 @@ namespace CodeLiturgy.Views.Controllers
return Ok(sites);
}
/// <summary>
/// Get Country by Id
/// Gets a site by Id.
/// </summary>
/// <param name="countryId">ISO 3166-1 countryId numeric code</param>
/// <param name="siteId">Site id.</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
@ -62,7 +86,12 @@ namespace CodeLiturgy.Views.Controllers
return new NotFoundResult();
}
/*
/// <summary>
/// Adds a site to the database.
/// </summary>
/// <param name="siteToCreate"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status406NotAcceptable)]
[HttpPost]
@ -71,7 +100,8 @@ namespace CodeLiturgy.Views.Controllers
var (success, site) = _siteDbContext.AddSite(siteToCreate);
if (!success) return new BadRequestResult();
return CreatedAtRoute(nameof(GetSiteById), new {countryId = site.Id}, site);
}*/
}
}
}

View File

@ -29,6 +29,7 @@ namespace CodeLiturgy.Domain.Model
//.ConfigureIdentityModel();
}
#endregion

@ -1 +1 @@
Subproject commit eae24d1b142738f40597284a24d637ad51506c0c
Subproject commit 607a30e72876f829057dfa2d9f37733eb70879a3