// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Orleans; using Squidex.Areas.Api.Controllers.UI.Models; using Squidex.Domain.Apps.Entities.Apps; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Orleans; using Squidex.Infrastructure.Security; using Squidex.Shared; using Squidex.Web; namespace Squidex.Areas.Api.Controllers.UI { public sealed class UIController : ApiController { private static readonly Permission CreateAppPermission = new Permission(Permissions.AdminAppCreate); private readonly MyUIOptions uiOptions; private readonly IGrainFactory grainFactory; public UIController(ICommandBus commandBus, IOptions uiOptions, IGrainFactory grainFactory) : base(commandBus) { this.uiOptions = uiOptions.Value; this.grainFactory = grainFactory; } /// /// Get ui settings. /// /// /// 200 => UI settings returned. /// [HttpGet] [Route("ui/settings/")] [ProducesResponseType(typeof(UISettingsDto), 200)] [ApiPermission] public IActionResult GetSettings() { var result = new UISettingsDto { MapType = uiOptions.Map?.Type ?? "OSM", MapKey = uiOptions.Map?.GoogleMaps?.Key }; var canCreateApps = !uiOptions.OnlyAdminsCanCreateApps || Context.Permissions.Includes(CreateAppPermission); result.CanCreateApps = canCreateApps; return Ok(result); } /// /// Get ui settings. /// /// The name of the app. /// /// 200 => UI settings returned. /// 404 => App not found. /// [HttpGet] [Route("apps/{app}/ui/settings/")] [ProducesResponseType(typeof(Dictionary), 200)] [ApiPermission] public async Task GetSettings(string app) { var result = await grainFactory.GetGrain(AppId).GetAsync(); return Ok(result.Value); } /// /// Set ui settings. /// /// The name of the app. /// The name of the setting. /// The request with the value to update. /// /// 200 => UI setting set. /// 404 => App not found. /// [HttpPut] [Route("apps/{app}/ui/settings/{key}")] [ApiPermission] public async Task PutSetting(string app, string key, [FromBody] UpdateSettingDto request) { await grainFactory.GetGrain(AppId).SetAsync(key, request.Value.AsJ()); return NoContent(); } /// /// Remove ui settings. /// /// The name of the app. /// The name of the setting. /// /// 200 => UI setting removed. /// 404 => App not found. /// [HttpDelete] [Route("apps/{app}/ui/settings/{key}")] [ApiPermission] public async Task DeleteSetting(string app, string key) { await grainFactory.GetGrain(AppId).RemoveAsync(key); return NoContent(); } } }