// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Net.Http.Headers; using Squidex.Areas.Api.Controllers.Apps.Models; using Squidex.Domain.Apps.Entities.Apps; using Squidex.Domain.Apps.Entities.Apps.Commands; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Shared; using Squidex.Web; namespace Squidex.Areas.Api.Controllers.Apps { /// /// Manages and configures apps. /// [ApiExplorerSettings(GroupName = nameof(Apps))] public sealed class AppRolesController : ApiController { private readonly RolePermissionsProvider permissionsProvider; public AppRolesController(ICommandBus commandBus, RolePermissionsProvider permissionsProvider) : base(commandBus) { this.permissionsProvider = permissionsProvider; } /// /// Get app roles. /// /// The name of the app. /// /// 200 => App roles returned. /// 404 => App not found. /// [HttpGet] [Route("apps/{app}/roles/")] [ProducesResponseType(typeof(RolesDto), 200)] [ApiPermission(Permissions.AppRolesRead)] [ApiCosts(0)] public IActionResult GetRoles(string app) { var response = Deferred.Response(() => { return GetResponse(App); }); Response.Headers[HeaderNames.ETag] = App.ToEtag(); return Ok(response); } /// /// Get app permissions. /// /// The name of the app. /// /// 200 => App permissions returned. /// 404 => App not found. /// [HttpGet] [Route("apps/{app}/roles/permissions")] [ProducesResponseType(typeof(string[]), 200)] [ApiPermission(Permissions.AppRolesRead)] [ApiCosts(0)] public IActionResult GetPermissions(string app) { var response = Deferred.AsyncResponse(() => { return permissionsProvider.GetPermissionsAsync(App); }); Response.Headers[HeaderNames.ETag] = string.Concat(response).Sha256Base64(); return Ok(response); } /// /// Add role to app. /// /// The name of the app. /// Role object that needs to be added to the app. /// /// 201 => User assigned to app. /// 400 => Role name already in use. /// 404 => App not found. /// [HttpPost] [Route("apps/{app}/roles/")] [ProducesResponseType(typeof(RolesDto), 201)] [ApiPermission(Permissions.AppRolesCreate)] [ApiCosts(1)] public async Task PostRole(string app, [FromBody] AddRoleDto request) { var command = request.ToCommand(); var response = await InvokeCommandAsync(command); return CreatedAtAction(nameof(GetRoles), new { app }, response); } /// /// Update an existing app role. /// /// The name of the app. /// The name of the role to be updated. /// Role to be updated for the app. /// /// 200 => Role updated. /// 400 => Role request not valid. /// 404 => Role or app not found. /// [HttpPut] [Route("apps/{app}/roles/{name}/")] [ProducesResponseType(typeof(RolesDto), 200)] [ApiPermission(Permissions.AppRolesUpdate)] [ApiCosts(1)] public async Task PutRole(string app, string name, [FromBody] UpdateRoleDto request) { var command = request.ToCommand(name); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Remove role from app. /// /// The name of the app. /// The name of the role. /// /// 200 => Role deleted. /// 400 => Role is in use by contributor or client or default role. /// 404 => Role or app not found. /// [HttpDelete] [Route("apps/{app}/roles/{name}/")] [ProducesResponseType(typeof(RolesDto), 200)] [ApiPermission(Permissions.AppRolesDelete)] [ApiCosts(1)] public async Task DeleteRole(string app, string name) { var command = new DeleteRole { Name = name }; var response = await InvokeCommandAsync(command); return Ok(response); } private async Task InvokeCommandAsync(ICommand command) { var context = await CommandBus.PublishAsync(command); var result = context.Result(); var response = GetResponse(result); return response; } private RolesDto GetResponse(IAppEntity result) { return RolesDto.FromApp(result, this); } } }