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;
///
/// Редактор разрешений пользователей для админки
///
[Route("api/admin/permission")]
[ApiController]
[Authorize]
public class AdminPermissionController : CrudController>
{
public AdminPermissionController(ICrudRepository 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();
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()?.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);
}
}