using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Oqtane.Repository; using Oqtane.Models; using Oqtane.Shared; using System.Linq; using System.IO; using Microsoft.AspNetCore.Hosting; using Oqtane.Infrastructure; namespace Oqtane.Controllers { [Route("{site}/api/[controller]")] public class SiteController : Controller { private readonly ISiteRepository _sites; private readonly ITenantResolver _tenants; private readonly IWebHostEnvironment _environment; private readonly ILogManager _logger; public SiteController(ISiteRepository Sites, ITenantResolver Tenants, IWebHostEnvironment environment, ILogManager logger) { this._sites = Sites; this._tenants = Tenants; this._environment = environment; this._logger = logger; } // GET: api/ [HttpGet] [Authorize(Roles = Constants.HostRole)] public IEnumerable Get() { return _sites.GetSites(); } // GET api//5 [HttpGet("{id}")] public Site Get(int id) { return _sites.GetSite(id); } // POST api/ [HttpPost] public Site Post([FromBody] Site Site) { if (ModelState.IsValid) { bool authorized; if (!_sites.GetSites().Any()) { // provision initial site during installation authorized = true; Tenant tenant = _tenants.GetTenant(); Site.TenantId = tenant.TenantId; } else { authorized = User.IsInRole(Constants.HostRole); } if (authorized) { Site = _sites.AddSite(Site); _logger.Log(Site.SiteId, LogLevel.Information, this, LogFunction.Create, "Site Added {Site}", Site); } } return Site; } // PUT api//5 [HttpPut("{id}")] [Authorize(Roles = Constants.HostRole)] public Site Put(int id, [FromBody] Site Site) { if (ModelState.IsValid) { Site = _sites.UpdateSite(Site); _logger.Log(Site.SiteId, LogLevel.Information, this, LogFunction.Update, "Site Updated {Site}", Site); } return Site; } // DELETE api//5 [HttpDelete("{id}")] [Authorize(Roles = Constants.HostRole)] public void Delete(int id) { _sites.DeleteSite(id); _logger.Log(id, LogLevel.Information, this, LogFunction.Delete, "Site Deleted {SiteId}", id); } } }