DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/AdminPermissionController.cs

62 lines
2.1 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace AsbCloudWebApi.Controllers
{
/// <summary>
/// Ðåäàêòîð ðàçðåøåíèé ïîëüçîâàòåëåé äëÿ àäìèíêè
/// </summary>
[Route("api/admin/permission")]
[ApiController]
[Authorize]
public class AdminPermissionController : CrudController<PermissionDto, ICrudRepository<PermissionDto>>
{
public AdminPermissionController(ICrudRepository<PermissionDto> service)
: base(service)
{ }
[HttpGet("Autogenerated")]
[Permission]
public IActionResult GetAutogenerated()
{
var controllers = Assembly.GetExecutingAssembly().GetTypes().
Where(type => typeof(ControllerBase).IsAssignableFrom(type) && !type.IsAbstract)
.ToList();
var permissions = new SortedSet<string>();
foreach (var controller in controllers)
{
var methods = controller.GetMethods().Where(m => m.IsPublic);
var controllerName = controller.Name.Replace("Controller", "");
foreach (var method in methods)
{
var httpMethod = method.GetCustomAttribute<HttpMethodAttribute>()?.HttpMethods?.First().ToLower();
switch (httpMethod)
{
case "get":
case "delete":
permissions.Add($"{controllerName}.{httpMethod.ToLower()}");
continue;
case "post":
case "put":
case "patch":
permissions.Add($"{controllerName}.edit");
continue;
default:
continue;
}
}
}
return Ok(permissions);
}
}
}