// ========================================================================== // 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.Pipeline; using Squidex.Shared; 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 = RolesDto.FromApp(App); Response.Headers[HeaderNames.ETag] = App.Version.ToString(); 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 async Task GetPermissions(string app) { var response = await permissionsProvider.GetPermissionsAsync(App); Response.Headers[HeaderNames.ETag] = string.Join(";", response).Sha256Base64(); return Ok(response); } /// /// Add role to app. /// /// The name of the app. /// Role object that needs to be added to the app. /// /// 200 => User assigned to app. /// 400 => Role name already in use. /// 404 => App not found. /// [HttpPost] [Route("apps/{app}/roles/")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiPermission(Permissions.AppRolesCreate)] [ApiCosts(1)] public async Task PostRole(string app, [FromBody] AddRoleDto request) { var command = request.ToCommand(); var context = await CommandBus.PublishAsync(command); return NoContent(); } /// /// 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. /// /// 204 => Role updated. /// 400 => Role request not valid. /// 404 => Role or app not found. /// [HttpPut] [Route("apps/{app}/roles/{role}/")] [ApiPermission(Permissions.AppRolesUpdate)] [ApiCosts(1)] public async Task UpdateRole(string app, string role, [FromBody] UpdateRoleDto request) { await CommandBus.PublishAsync(request.ToCommand(role)); return NoContent(); } /// /// Remove role from app. /// /// The name of the app. /// The name of the role. /// /// 204 => 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/{role}/")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiPermission(Permissions.AppRolesDelete)] [ApiCosts(1)] public async Task DeleteRole(string app, string role) { await CommandBus.PublishAsync(new DeleteRole { Name = role }); return NoContent(); } } }