Adds right and controller validation

This commit is contained in:
Jordan Brown
2020-03-24 23:31:11 -04:00
parent 7a5890aea1
commit b6862feac0
3 changed files with 18 additions and 5 deletions
@@ -1,6 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Tgstation.Server.Api.Models.Internal
{
@@ -26,10 +25,9 @@ namespace Tgstation.Server.Api.Models.Internal
public bool? Enabled { get; set; }
/// <summary>
/// The time interval in minutes the chat bot attempts to reconnect if <see cref="Enabled"/> and disconnected.
/// The time interval in minutes the chat bot attempts to reconnect if <see cref="Enabled"/> and disconnected. Must not be zero.
/// </summary>
[Required]
[NotMapped]
public uint? ReconnectionInterval { get; set; }
/// <summary>
@@ -57,5 +57,10 @@ namespace Tgstation.Server.Api.Rights
/// User can change <see cref="Models.Internal.ChatBot.Name"/>
/// </summary>
WriteName = 256,
/// <summary>
/// User can change <see cref="Models.Internal.ChatBot.ReconnectionInterval"/>
/// </summary>
WriteReconnectionInterval = 512,
}
}
@@ -91,6 +91,9 @@ namespace Tgstation.Server.Host.Controllers
return BadRequest(new ErrorMessage { Message = "Invalid provider!" });
}
if (model.ReconnectionInterval == 0)
return BadRequest(new ErrorMessage { Message = "ReconnectionInterval must not be zero!" });
if (!model.ValidateProviderChannelTypes())
return BadRequest(new ErrorMessage { Message = "One or more of channels aren't formatted correctly for the given provider!" });
@@ -105,6 +108,7 @@ namespace Tgstation.Server.Host.Controllers
Channels = model.Channels?.Select(x => ConvertApiChatChannel(x)).ToList() ?? new List<Models.ChatChannel>(), // important that this isn't null
InstanceId = Instance.Id,
Provider = model.Provider,
ReconnectionInterval = model.ReconnectionInterval
};
DatabaseContext.ChatBots.Add(dbModel);
@@ -219,12 +223,18 @@ namespace Tgstation.Server.Host.Controllers
[TgsAuthorize(ChatBotRights.WriteChannels | ChatBotRights.WriteConnectionString | ChatBotRights.WriteEnabled | ChatBotRights.WriteName | ChatBotRights.WriteProvider)]
[ProducesResponseType(200)]
[ProducesResponseType(typeof(Api.Models.ChatBot), 200)]
#pragma warning disable CA1506 // TODO: Decomplexify
#pragma warning disable CA1502 // TODO: Decomplexify
#pragma warning disable CA1506
public async Task<IActionResult> Update([FromBody] Api.Models.ChatBot model, CancellationToken cancellationToken)
#pragma warning restore CA1502
#pragma warning restore CA1506
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (model.ReconnectionInterval == 0)
return BadRequest(new ErrorMessage { Message = "ReconnectionInterval must not be zero!" });
if (model.Provider.HasValue && !model.ValidateProviderChannelTypes())
return BadRequest(new ErrorMessage { Message = "One or more of channels aren't formatted correctly for the given provider!" });
@@ -261,6 +271,7 @@ namespace Tgstation.Server.Host.Controllers
|| CheckModified(x => x.Enabled, ChatBotRights.WriteEnabled)
|| CheckModified(x => x.Name, ChatBotRights.WriteName)
|| CheckModified(x => x.Provider, ChatBotRights.WriteProvider)
|| CheckModified(x => x.ReconnectionInterval, ChatBotRights.WriteReconnectionInterval)
|| (model.Channels != null && !userRights.HasFlag(ChatBotRights.WriteChannels)))
return Forbid();
@@ -297,6 +308,5 @@ namespace Tgstation.Server.Host.Controllers
return Ok();
}
#pragma warning restore CA1506
}
}