Skip to content

Commit

Permalink
Add permissions stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
SylveonDeko committed Aug 3, 2024
1 parent d5f005a commit 2183b88
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 26 deletions.
5 changes: 2 additions & 3 deletions src/Mewdeko/Controllers/AfkController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel.DataAnnotations;
using Mewdeko.Common.TypeReaders.Models;
using Mewdeko.Common.TypeReaders.Models;
using Mewdeko.Database.DbContextStuff;
using Mewdeko.Modules.Afk.Services;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -12,7 +11,7 @@ namespace Mewdeko.Controllers;
/// </summary>
/// <param name="afk">The afk service</param>
[ApiController]
[Route("api/[controller]/{guildId}")]
[Route("botapi/[controller]/{guildId}")]
public class AfkController(AfkService afk, DiscordShardedClient client, DbContextProvider provider) : Controller
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Mewdeko/Controllers/BotStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Mewdeko.Controllers;
/// Endpoint for getting status such as guild count, bot version, etc
/// </summary>
[ApiController]
[Route("api/[controller]")]
[Route("botapi/[controller]")]
public class BotStatus(DiscordShardedClient client, StatsService statsService, CommandService commandService) : Controller
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Mewdeko/Controllers/ChatTriggersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Mewdeko.Controllers;
/// Api endpoint to for chat triggers for guilds
/// </summary>
[ApiController]
[Route("api/[controller]/{guildId}")]
[Route("botapi/[controller]/{guildId}")]
public class ChatTriggersController(ChatTriggersService service) : Controller
{

Expand Down
2 changes: 1 addition & 1 deletion src/Mewdeko/Controllers/ClientOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Mewdeko.Controllers;
/// Api endpoint for operations done via the discord client, such as getting roles, users, guilds
/// </summary>
[ApiController]
[Route("api/[controller]")]
[Route("botapi/[controller]")]
public class ClientOperations(DiscordShardedClient client) : Controller
{

Expand Down
2 changes: 1 addition & 1 deletion src/Mewdeko/Controllers/GuildConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Mewdeko.Controllers;
/// </summary>
/// <param name="service"></param>
[ApiController]
[Route("api/[controller]/{guildId}")]
[Route("botapi/[controller]/{guildId}")]
public class GuildConfigController(GuildSettingsService service) : Controller
{
/// <summary>
Expand Down
67 changes: 65 additions & 2 deletions src/Mewdeko/Controllers/PermissionsController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Discord.Commands;
using Mewdeko.Modules.Administration.Services;
using Mewdeko.Modules.Permissions.Services;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -8,8 +9,8 @@ namespace Mewdeko.Controllers;
/// Controller for setting permissions for commands and triggers
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class PermissionsController(PermissionService permissionService, DiscordPermOverrideService dpoService) : Controller
[Route("botapi/[controller]")]
public class PermissionsController(PermissionService permissionService, DiscordPermOverrideService dpoService, CommandService cmdServ) : Controller
{
/// <summary>
/// Gets all dpos for a guild
Expand All @@ -23,6 +24,40 @@ public async Task<IActionResult> GetPermissionOverridesForGuildAsync(ulong guild
return Ok(overrides);
}

/// <summary>
/// Add a discord permission override
/// </summary>
/// <param name="guildId"></param>
/// <param name="commandName"></param>
/// <param name="permissions"></param>
/// <returns></returns>
[HttpPost("dpo/{guildId}")]
public async Task<IActionResult> AddDpo(ulong guildId, [FromBody] DpoRequest request)
{
var com = cmdServ.Search(request.Command);
if (!com.IsSuccess)
return BadRequest(com);
var perms = (GuildPermission)request.Permissions;
await dpoService.AddOverride(guildId, request.Command, perms);
return Ok(dpoService);
}

/// <summary>
/// Remove a dpo
/// </summary>
/// <param name="guildId"></param>
/// <param name="commandName"></param>
/// <returns></returns>
[HttpDelete("dpo/{guildId}/{commandName}")]
public async Task<IActionResult> RemoveDpo(ulong guildId, string commandName)
{
var com = cmdServ.Search(commandName);
if (!com.IsSuccess)
return BadRequest(com);
await dpoService.RemoveOverride(guildId, commandName);
return Ok();
}

/// <summary>
/// Gets regular permissions for a guild
/// </summary>
Expand All @@ -34,4 +69,32 @@ public async Task<IActionResult> GetPermissionsForGuildAsync(ulong guildId)
var perms = await permissionService.GetCacheFor(guildId);
return Ok(perms);
}

/// <summary>
/// E
/// </summary>
public class Stupidity
{
/// <summary>
/// E
/// </summary>
public string Command { get; set; }
}

/// <summary>
/// E
/// </summary>
public class DpoRequest
{
/// <summary>
/// e
/// </summary>
public string Command { get; set; }
/// <summary>
/// e
/// </summary>
public ulong Permissions { get; set; }
}


}
2 changes: 1 addition & 1 deletion src/Mewdeko/Controllers/SuggestionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Mewdeko.Controllers;
/// </summary>
/// <param name="service"></param>
[ApiController]
[Route("api/[controller]/{guildId}")]
[Route("botapi/[controller]/{guildId}")]
public class SuggestionsController(
SuggestionsService service,
DbContextProvider provider,
Expand Down
31 changes: 17 additions & 14 deletions src/Mewdeko/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public class Program
/// <returns>A <see cref="Task" /> representing the asynchronous operation of running the application.</returns>
public static async Task Main(string[] args)
{
var settings = new HostApplicationBuilderSettings
{
ApplicationName = "Mewdeko"
};
var builder = WebApplication.CreateBuilder();
var services = builder.Services;
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
Expand Down Expand Up @@ -164,11 +160,18 @@ await migrationService.ApplyMigrations(

services.AddSingleton<Mewdeko>()
.AddHostedService<MewdekoService>();
services.AddControllers();
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});;
services.AddEndpointsApiExplorer();
services.AddAuthorization();
services.AddSwaggerGen();
services.AddTransient<IApiKeyValidation, ApiKeyValidation>();
if (!credentials.SkipApiKey)
{
services.AddTransient<IApiKeyValidation, ApiKeyValidation>();
services.AddAuthorization();
}

builder.WebHost.UseUrls($"http://localhost:{credentials.ApiPortHttp}");

Expand All @@ -191,13 +194,13 @@ await migrationService.ApplyMigrations(

app.MapControllers();

// app.Use(async (context, next) =>
// {
// var logger = context.RequestServices.GetRequiredService<ILogger<Mewdeko>>();
// logger.LogInformation($"Request received: {context.Request.Method} {context.Request.Path}");
// logger.LogInformation($"Request body: {await new StreamReader(context.Request.Body).ReadToEndAsync()}");
// await next();
// });
app.Use(async (context, next) =>
{
var logger = context.RequestServices.GetRequiredService<ILogger<Mewdeko>>();
logger.LogInformation($"Request received: {context.Request.Method} {context.Request.Path}");
logger.LogInformation($"Request body: {await new StreamReader(context.Request.Body).ReadToEndAsync()}");
await next();
});

foreach (var address in app.Urls)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Mewdeko/Services/LogSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public static class LogSetup
public static ILogger SetupLogger(object source)
{
var logger = Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft", LogEventLevel.Verbose)
.MinimumLevel.Override("System", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Debug)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Verbose)
.Enrich.FromLogContext()
.WriteTo.Console(LogEventLevel.Information,
theme: AnsiConsoleTheme.Code,
Expand Down

0 comments on commit 2183b88

Please sign in to comment.