From 7f981d8c5ee9f0319ee8a415e6dfb3e3162be22e Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Mon, 16 Apr 2018 21:21:43 -0400 Subject: [PATCH] TgsAuthorizeAttribute --- .../Models/Internal/ChatSettings.cs | 2 +- .../Rights/RightsHelper.cs | 2 +- src/Tgstation.Server.Api/Rights/RightsType.cs | 2 +- .../Controllers/HomeController.cs | 5 +- .../TgsAuthorizeAttribute.cs | 72 +++++++++++++++++++ 5 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/Tgstation.Server.Host/TgsAuthorizeAttribute.cs diff --git a/src/Tgstation.Server.Api/Models/Internal/ChatSettings.cs b/src/Tgstation.Server.Api/Models/Internal/ChatSettings.cs index b98d62b906..a672895d3c 100644 --- a/src/Tgstation.Server.Api/Models/Internal/ChatSettings.cs +++ b/src/Tgstation.Server.Api/Models/Internal/ChatSettings.cs @@ -8,7 +8,7 @@ namespace Tgstation.Server.Api.Models.Internal /// /// Manage the server chat bots /// - [Model(RightsType.Chat, RequiresInstance = true)] + [Model(RightsType.ChatSettings, RequiresInstance = true)] public class ChatSettings { /// diff --git a/src/Tgstation.Server.Api/Rights/RightsHelper.cs b/src/Tgstation.Server.Api/Rights/RightsHelper.cs index 8db36400f4..ab4e16de4e 100644 --- a/src/Tgstation.Server.Api/Rights/RightsHelper.cs +++ b/src/Tgstation.Server.Api/Rights/RightsHelper.cs @@ -20,7 +20,7 @@ namespace Tgstation.Server.Api.Rights { RightsType.Byond, typeof(ByondRights) }, { RightsType.DreamMaker, typeof(DreamMakerRights) }, { RightsType.DreamDaemon, typeof(DreamDaemonRights) }, - { RightsType.Chat, typeof(ChatSettingsRights) }, + { RightsType.ChatSettings, typeof(ChatSettingsRights) }, { RightsType.Configuration, typeof(ConfigurationRights) }, { RightsType.InstanceUser, typeof(InstanceUserRights) } }; diff --git a/src/Tgstation.Server.Api/Rights/RightsType.cs b/src/Tgstation.Server.Api/Rights/RightsType.cs index 5b4ff0722c..7ac7491661 100644 --- a/src/Tgstation.Server.Api/Rights/RightsType.cs +++ b/src/Tgstation.Server.Api/Rights/RightsType.cs @@ -32,7 +32,7 @@ /// /// /// - Chat, + ChatSettings, /// /// /// diff --git a/src/Tgstation.Server.Host/Controllers/HomeController.cs b/src/Tgstation.Server.Host/Controllers/HomeController.cs index 5260d1445b..d839f45017 100644 --- a/src/Tgstation.Server.Host/Controllers/HomeController.cs +++ b/src/Tgstation.Server.Host/Controllers/HomeController.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System; using System.Linq; @@ -50,7 +49,7 @@ namespace Tgstation.Server.Host.Controllers /// Returns the version of the /// /// - [Authorize] + [TgsAuthorize] [HttpGet] public JsonResult Home() => Json(Application.Version); diff --git a/src/Tgstation.Server.Host/TgsAuthorizeAttribute.cs b/src/Tgstation.Server.Host/TgsAuthorizeAttribute.cs new file mode 100644 index 0000000000..2281587fae --- /dev/null +++ b/src/Tgstation.Server.Host/TgsAuthorizeAttribute.cs @@ -0,0 +1,72 @@ +using System; +using Microsoft.AspNetCore.Authorization; +using Tgstation.Server.Api.Rights; + +namespace Tgstation.Server.Host +{ + /// + /// Helper for using the with the system + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] + sealed class TgsAuthorizeAttribute : AuthorizeAttribute + { + /// + /// Construct a + /// + public TgsAuthorizeAttribute() { } + + /// + /// Construct a for + /// + /// The rights required + public TgsAuthorizeAttribute(AdministrationRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights); + + /// + /// Construct a for + /// + /// The rights required + public TgsAuthorizeAttribute(InstanceManagerRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights); + + /// + /// Construct a for + /// + /// The rights required + public TgsAuthorizeAttribute(RepositoryRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights); + + /// + /// Construct a for + /// + /// The rights required + public TgsAuthorizeAttribute(ByondRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights); + + /// + /// Construct a for + /// + /// The rights required + public TgsAuthorizeAttribute(DreamMakerRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights); + + /// + /// Construct a for + /// + /// The rights required + public TgsAuthorizeAttribute(DreamDaemonRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights); + + /// + /// Construct a for + /// + /// The rights required + public TgsAuthorizeAttribute(ChatSettingsRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights); + + /// + /// Construct a for + /// + /// The rights required + public TgsAuthorizeAttribute(ConfigurationRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights); + + /// + /// Construct a for + /// + /// The rights required + public TgsAuthorizeAttribute(InstanceUserRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights); + } +}