From 9ae846f88a4bfe8835417d608b25381d08b5c7c7 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 13 May 2021 23:01:11 +0200 Subject: [PATCH] Starting a config apio --- .../Models/Attributes/PermissionAttribute.cs | 7 +++- Kyoo.Common/Module.cs | 8 ++++ Kyoo/Program.cs | 2 + Kyoo/Views/ConfigurationApi.cs | 41 +++++++++++++++++++ Kyoo/settings.json | 4 +- 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 Kyoo/Views/ConfigurationApi.cs diff --git a/Kyoo.Common/Models/Attributes/PermissionAttribute.cs b/Kyoo.Common/Models/Attributes/PermissionAttribute.cs index b34fb48b..40228782 100644 --- a/Kyoo.Common/Models/Attributes/PermissionAttribute.cs +++ b/Kyoo.Common/Models/Attributes/PermissionAttribute.cs @@ -7,12 +7,17 @@ namespace Kyoo.Models.Permissions /// /// The kind of permission needed. /// + /// + /// The admin kind is used for configuration or security sensitive permissions to allow one + /// to use an overall permission without compromising security. + /// public enum Kind { Read, Write, Create, - Delete + Delete, + Admin } /// diff --git a/Kyoo.Common/Module.cs b/Kyoo.Common/Module.cs index c1c09165..ea87b604 100644 --- a/Kyoo.Common/Module.cs +++ b/Kyoo.Common/Module.cs @@ -1,5 +1,6 @@ using System; using Kyoo.Controllers; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Kyoo @@ -62,5 +63,12 @@ namespace Kyoo services.Add(ServiceDescriptor.Describe(typeof(T), typeof(T2), lifetime)); return services.AddRepository(lifetime); } + + public static IServiceCollection AddConfiguration(this IServiceCollection services, IConfiguration config, string path) + { + services.Configure(config.GetSection(path)); + services.AddSingleton(new ConfigReference(path, typeof(T))); + return services; + } } } \ No newline at end of file diff --git a/Kyoo/Program.cs b/Kyoo/Program.cs index 9fecc61b..16b6d098 100644 --- a/Kyoo/Program.cs +++ b/Kyoo/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; @@ -18,6 +19,7 @@ namespace Kyoo /// Main function of the program /// /// Command line arguments + [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse")] public static async Task Main(string[] args) { if (!File.Exists("./settings.json")) diff --git a/Kyoo/Views/ConfigurationApi.cs b/Kyoo/Views/ConfigurationApi.cs new file mode 100644 index 00000000..2dfbc463 --- /dev/null +++ b/Kyoo/Views/ConfigurationApi.cs @@ -0,0 +1,41 @@ +using Kyoo.Models.Permissions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; + +namespace Kyoo.Api +{ + /// + /// An API to retrieve or edit configuration settings + /// + [Route("api/config")] + [Route("api/configuration")] + [ApiController] + public class ConfigurationApi : Controller + { + /// + /// The configuration to retrieve and edit. + /// + private readonly IConfiguration _config; + + /// + /// Create a new using the given configuration. + /// + /// The configuration to use. + public ConfigurationApi(IConfiguration config) + { + _config = config; + } + + /// + /// Get a permission from it's slug. + /// + /// The permission to retrieve. You can use __ to get a child value. + /// The associate value or list of values. + [HttpGet("{slug}")] + [Permission(nameof(ConfigurationApi), Kind.Admin)] + public ActionResult GetConfiguration(string slug) + { + return _config[slug]; + } + } +} \ No newline at end of file diff --git a/Kyoo/settings.json b/Kyoo/settings.json index fed2bd58..114636fe 100644 --- a/Kyoo/settings.json +++ b/Kyoo/settings.json @@ -32,8 +32,8 @@ "password": "passphrase" }, "permissions": { - "default": ["overall.read", "overall.write", "overall.create", "overall.delete"], - "newUser": ["overall.read", "overall.write", "overall.create", "overall.delete"] + "default": ["overall.read", "overall.write", "overall.create", "overall.delete", "overall.admin"], + "newUser": ["overall.read", "overall.write", "overall.create", "overall.delete", "overall.admin"] }, "profilePicturePath": "users/", "clients": []