From 70dc2afd1fe267cbb32373649559ed0b18cb0e04 Mon Sep 17 00:00:00 2001 From: code liturgy Date: Fri, 9 Dec 2022 00:27:00 +0000 Subject: [PATCH] Bump more methods --- .../Application/Users/Auth/AuthManager.cs | 2 +- CodeLiturgy.Views/Augeas/AugeasManager.cs | 55 ++++++++++++- .../01-git.codeliturgy.com.conf | 2 +- .../Controllers/ApacheController.cs | 81 +++++++++++++++++++ .../Controllers/AuthController.cs | 37 ++++++++- .../Controllers/SitesController.cs | 42 ++++++++-- .../Models/ModelBuilderExtensions.cs | 1 + include/Sharp.Augeas | 2 +- 8 files changed, 208 insertions(+), 14 deletions(-) diff --git a/CodeLiturgy.Views/Application/Users/Auth/AuthManager.cs b/CodeLiturgy.Views/Application/Users/Auth/AuthManager.cs index c60ee92..31fd2be 100644 --- a/CodeLiturgy.Views/Application/Users/Auth/AuthManager.cs +++ b/CodeLiturgy.Views/Application/Users/Auth/AuthManager.cs @@ -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)); diff --git a/CodeLiturgy.Views/Augeas/AugeasManager.cs b/CodeLiturgy.Views/Augeas/AugeasManager.cs index 5dd2e58..23d3779 100644 --- a/CodeLiturgy.Views/Augeas/AugeasManager.cs +++ b/CodeLiturgy.Views/Augeas/AugeasManager.cs @@ -2,26 +2,49 @@ using Sharp.Augeas; namespace CodeLiturgy.Views.Augeas { + /// + /// Manager class for Augeas. + /// public class AugeasManager { private readonly Sharp.Augeas.Augeas _augeas; private AugSettings _augSettings; + /// + /// Cached List with the current apache configurations. + /// public List CurrentApacheConfigurations => _currentApacheConfigurations; private List _currentApacheConfigurations; - private const string _apachePath = "/etc/apache2/sites-available"; + private const string ApachePath = "/etc/apache2/sites-available"; + /// + /// Augeas Manager constructor. + /// public AugeasManager() { _augSettings = AugeasExtensions.GetSettings(); _augeas = new Sharp.Augeas.Augeas(_augSettings); RefreshApacheConfigurations(); } + + /// + /// Write Changes to the disk + /// + /// Returns true if the save was successful. + public bool Save() + { + return _augeas.Save(); + } + /// + /// Gets a preview of the configuration. + /// + /// + /// 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; } + /// + /// Gets available Apache configurations. + /// + /// + /// + /// public List GetApacheAvailableConfigurations(int skip, int take) { var result = _currentApacheConfigurations @@ -47,6 +76,11 @@ namespace CodeLiturgy.Views.Augeas } + /// + /// Gets virtual host tree of a site. + /// + /// Name of the site. + /// 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; } + + /// + /// Gets a directive. + /// + /// + /// + /// + public Directive GetDirective(string siteName, string directiveName) + { + var tree = GetVirtualHostTree(siteName); + var directive = tree.GetDirective(directiveName); + return directive; + } } } diff --git a/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/01-git.codeliturgy.com.conf b/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/01-git.codeliturgy.com.conf index 192b260..29039b3 100644 --- a/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/01-git.codeliturgy.com.conf +++ b/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/01-git.codeliturgy.com.conf @@ -1,5 +1,5 @@ - ServerName git.example2.com + ServerName git.megacode3.com ServerAlias www.git.example2.com SSLEngine on SSLProxyEngine on diff --git a/CodeLiturgy.Views/Controllers/ApacheController.cs b/CodeLiturgy.Views/Controllers/ApacheController.cs index f8017d4..d791ce1 100644 --- a/CodeLiturgy.Views/Controllers/ApacheController.cs +++ b/CodeLiturgy.Views/Controllers/ApacheController.cs @@ -8,6 +8,9 @@ using System.Text.Json; namespace CodeLiturgy.Views.Controllers { + /// + /// Controller responsible to manage Apache configurations. + /// [ApiController] [Authorize] public class ApacheController : ControllerBase @@ -16,6 +19,13 @@ namespace CodeLiturgy.Views.Controllers private readonly SiteDbContext _siteDbContext; private readonly AugeasManager _augeasManager; + /// + /// Apache controller constructor. + /// + /// + /// + /// + /// public ApacheController( ApplicationUserManager userManager, ILogger logger, @@ -27,6 +37,13 @@ namespace CodeLiturgy.Views.Controllers _augeasManager = augeasManager; } + /// + /// Gets site in /etc/apache/sites-available + /// + /// + /// + /// + /// [HttpGet("/api/apache")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -42,6 +59,11 @@ namespace CodeLiturgy.Views.Controllers return Ok(sites); } + /// + /// Gets a preview of the configuration of a certain site. + /// + /// Site configuration filename. + /// [HttpGet("/api/apache/preview/{site}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -56,6 +78,11 @@ namespace CodeLiturgy.Views.Controllers return Ok(preview); } + /// + /// Gets tree object of a certain site. + /// + /// Site configuration file. + /// [HttpGet("/api/apache/tree/{site}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -70,6 +97,60 @@ namespace CodeLiturgy.Views.Controllers return Ok(tree); } + /// + /// Gets a directive. + /// + /// + /// + /// + [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); + } + /// + /// Sets a new value for the directive. The first word is the directive value, the following words correspond to the arguments. + /// + /// Site configuration filename. + /// + /// + /// + [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); + } + + /// + /// Persists changes to the configurations to disk. + /// + /// + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status406NotAcceptable)] + [HttpGet("save")] + public ActionResult Save() + { + var sucess = _augeasManager.Save(); + + return Ok(sucess); + } } } diff --git a/CodeLiturgy.Views/Controllers/AuthController.cs b/CodeLiturgy.Views/Controllers/AuthController.cs index 561c4b0..c6e9116 100644 --- a/CodeLiturgy.Views/Controllers/AuthController.cs +++ b/CodeLiturgy.Views/Controllers/AuthController.cs @@ -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 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 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 Account() diff --git a/CodeLiturgy.Views/Controllers/SitesController.cs b/CodeLiturgy.Views/Controllers/SitesController.cs index 47484b9..f9c10e6 100644 --- a/CodeLiturgy.Views/Controllers/SitesController.cs +++ b/CodeLiturgy.Views/Controllers/SitesController.cs @@ -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 { + /// + /// Controller responsible for managing websites. + /// [Route("api/sites")] [ApiController] [Authorize] @@ -16,14 +20,34 @@ namespace CodeLiturgy.Views.Controllers private ApplicationUserManager _userManager; private ILogger _logger; private readonly SiteDbContext _siteDbContext; + private readonly AugeasManager _augeasManager; - public SitesController(ApplicationUserManager userManager, ILogger logger, SiteDbContext siteDbContext) + /// + /// Sites controller constructor. + /// + /// + /// + /// + /// + public SitesController( + ApplicationUserManager userManager, + ILogger logger, + SiteDbContext siteDbContext, + AugeasManager augeasManager) { _logger = logger; _userManager = userManager; _siteDbContext = siteDbContext; + _augeasManager = augeasManager; } + /// + /// Gets the sites stored in the database. + /// + /// + /// + /// + /// [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -39,11 +63,11 @@ namespace CodeLiturgy.Views.Controllers return Ok(sites); } - + /// - /// Get Country by Id + /// Gets a site by Id. /// - /// ISO 3166-1 countryId numeric code + /// Site id. /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -62,7 +86,12 @@ namespace CodeLiturgy.Views.Controllers return new NotFoundResult(); } - /* + + /// + /// Adds a site to the database. + /// + /// + /// [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); - }*/ + } + } } diff --git a/CodeLiturgy.Views/Models/ModelBuilderExtensions.cs b/CodeLiturgy.Views/Models/ModelBuilderExtensions.cs index 32179b9..baf1cb9 100644 --- a/CodeLiturgy.Views/Models/ModelBuilderExtensions.cs +++ b/CodeLiturgy.Views/Models/ModelBuilderExtensions.cs @@ -29,6 +29,7 @@ namespace CodeLiturgy.Domain.Model //.ConfigureIdentityModel(); } + #endregion diff --git a/include/Sharp.Augeas b/include/Sharp.Augeas index eae24d1..607a30e 160000 --- a/include/Sharp.Augeas +++ b/include/Sharp.Augeas @@ -1 +1 @@ -Subproject commit eae24d1b142738f40597284a24d637ad51506c0c +Subproject commit 607a30e72876f829057dfa2d9f37733eb70879a3