TgsAuthorizeAttribute

This commit is contained in:
Jordan Brown
2018-04-16 21:21:43 -04:00
parent cdd318524a
commit 7f981d8c5e
5 changed files with 77 additions and 6 deletions
@@ -8,7 +8,7 @@ namespace Tgstation.Server.Api.Models.Internal
/// <summary>
/// Manage the server chat bots
/// </summary>
[Model(RightsType.Chat, RequiresInstance = true)]
[Model(RightsType.ChatSettings, RequiresInstance = true)]
public class ChatSettings
{
/// <summary>
@@ -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) }
};
@@ -32,7 +32,7 @@
/// <summary>
/// <see cref="ChatSettingsRights"/>
/// </summary>
Chat,
ChatSettings,
/// <summary>
/// <see cref="ConfigurationRights"/>
/// </summary>
@@ -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 <see cref="Application"/>
/// </summary>
/// <returns><see cref="Application.Version"/></returns>
[Authorize]
[TgsAuthorize]
[HttpGet]
public JsonResult Home() => Json(Application.Version);
@@ -0,0 +1,72 @@
using System;
using Microsoft.AspNetCore.Authorization;
using Tgstation.Server.Api.Rights;
namespace Tgstation.Server.Host
{
/// <summary>
/// Helper for using the <see cref="AuthorizeAttribute"/> with the <see cref="Api.Rights"/> system
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
sealed class TgsAuthorizeAttribute : AuthorizeAttribute
{
/// <summary>
/// Construct a <see cref="TgsAuthorizeAttribute"/>
/// </summary>
public TgsAuthorizeAttribute() { }
/// <summary>
/// Construct a <see cref="TgsAuthorizeAttribute"/> for <see cref="AdministrationRights"/>
/// </summary>
/// <param name="requiredRights">The rights required</param>
public TgsAuthorizeAttribute(AdministrationRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights);
/// <summary>
/// Construct a <see cref="TgsAuthorizeAttribute"/> for <see cref="InstanceManagerRights"/>
/// </summary>
/// <param name="requiredRights">The rights required</param>
public TgsAuthorizeAttribute(InstanceManagerRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights);
/// <summary>
/// Construct a <see cref="TgsAuthorizeAttribute"/> for <see cref="RepositoryRights"/>
/// </summary>
/// <param name="requiredRights">The rights required</param>
public TgsAuthorizeAttribute(RepositoryRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights);
/// <summary>
/// Construct a <see cref="TgsAuthorizeAttribute"/> for <see cref="ByondRights"/>
/// </summary>
/// <param name="requiredRights">The rights required</param>
public TgsAuthorizeAttribute(ByondRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights);
/// <summary>
/// Construct a <see cref="TgsAuthorizeAttribute"/> for <see cref="DreamMakerRights"/>
/// </summary>
/// <param name="requiredRights">The rights required</param>
public TgsAuthorizeAttribute(DreamMakerRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights);
/// <summary>
/// Construct a <see cref="TgsAuthorizeAttribute"/> for <see cref="DreamDaemonRights"/>
/// </summary>
/// <param name="requiredRights">The rights required</param>
public TgsAuthorizeAttribute(DreamDaemonRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights);
/// <summary>
/// Construct a <see cref="TgsAuthorizeAttribute"/> for <see cref="ChatSettingsRights"/>
/// </summary>
/// <param name="requiredRights">The rights required</param>
public TgsAuthorizeAttribute(ChatSettingsRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights);
/// <summary>
/// Construct a <see cref="TgsAuthorizeAttribute"/> for <see cref="ConfigurationRights"/>
/// </summary>
/// <param name="requiredRights">The rights required</param>
public TgsAuthorizeAttribute(ConfigurationRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights);
/// <summary>
/// Construct a <see cref="TgsAuthorizeAttribute"/> for <see cref="InstanceUserRights"/>
/// </summary>
/// <param name="requiredRights">The rights required</param>
public TgsAuthorizeAttribute(InstanceUserRights requiredRights) => Roles = RightsHelper.RoleName(requiredRights);
}
}