Various fixes

This commit is contained in:
Jordan Brown
2018-07-22 12:04:26 -04:00
parent a5e3f68cc2
commit f0cd3df98a
7 changed files with 38 additions and 30 deletions
@@ -121,13 +121,10 @@ namespace Tgstation.Server.Host.Controllers
/// <inheritdoc />
[TgsAuthorize(ChatSettingsRights.Delete)]
public override async Task<IActionResult> Delete([FromBody] Api.Models.ChatSettings model, CancellationToken cancellationToken)
public override async Task<IActionResult> Delete(long id, CancellationToken cancellationToken)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
var instance = instanceManager.GetInstance(Instance);
await Task.WhenAll(instance.Chat.DeleteConnection(model.Id, cancellationToken), DatabaseContext.ChatSettings.Where(x => x.Id == model.Id).DeleteAsync(cancellationToken)).ConfigureAwait(false);
await Task.WhenAll(instance.Chat.DeleteConnection(id, cancellationToken), DatabaseContext.ChatSettings.Where(x => x.Id == id).DeleteAsync(cancellationToken)).ConfigureAwait(false);
return Ok();
}
@@ -49,6 +49,7 @@ namespace Tgstation.Server.Host.Controllers
[TgsAuthorize(DreamDaemonRights.Start)]
public override async Task<IActionResult> Create([FromBody] DreamDaemon model, CancellationToken cancellationToken)
{
//alias for launching DD
var instance = instanceManager.GetInstance(Instance);
if (instance.Watchdog.Running)
@@ -113,8 +114,9 @@ namespace Tgstation.Server.Host.Controllers
/// <inheritdoc />
[TgsAuthorize(DreamDaemonRights.Shutdown)]
public override async Task<IActionResult> Delete([FromBody] DreamDaemon model, CancellationToken cancellationToken)
public override async Task<IActionResult> Delete(long id, CancellationToken cancellationToken)
{
//alias for stopping DD
var instance = instanceManager.GetInstance(Instance);
if (!instance.Watchdog.Running)
@@ -128,6 +130,7 @@ namespace Tgstation.Server.Host.Controllers
[TgsAuthorize(DreamDaemonRights.SetAutoStart | DreamDaemonRights.SetPorts | DreamDaemonRights.SetSecurity | DreamDaemonRights.SetWebClient | DreamDaemonRights.SoftRestart | DreamDaemonRights.SoftShutdown | DreamDaemonRights.Start | DreamDaemonRights.SetStartupTimeout)]
public override async Task<IActionResult> Update([FromBody] DreamDaemon model, CancellationToken cancellationToken)
{
//alias for changing DD settings
var current = await DatabaseContext.Instances.Where(x => x.Id == Instance.Id).Select(x => x.DreamDaemonSettings).FirstAsync(cancellationToken).ConfigureAwait(false);
var userRights = (DreamDaemonRights)AuthenticationContext.GetRight(RightsType.DreamDaemon);
@@ -75,13 +75,20 @@ namespace Tgstation.Server.Host.Controllers
/// <inheritdoc />
[TgsAuthorize(DreamMakerRights.CancelCompile)]
public override async Task<IActionResult> Delete([FromBody] Api.Models.DreamMaker model, CancellationToken cancellationToken)
public override async Task<IActionResult> Delete(long id, CancellationToken cancellationToken)
{
//alias for cancelling the latest job
var job = await DatabaseContext.CompileJobs.OrderByDescending(x => x.Job.StartedAt).Select(x => new Job { Id = x.Job.Id, StoppedAt = x.Job.StoppedAt }).FirstAsync(cancellationToken).ConfigureAwait(false);
if (job.StoppedAt != null)
return StatusCode((int)HttpStatusCode.Gone);
await jobManager.CancelJob(job, AuthenticationContext.User, cancellationToken).ConfigureAwait(false);
try
{
await jobManager.CancelJob(job, AuthenticationContext.User, cancellationToken).ConfigureAwait(false);
}
catch (InvalidOperationException) //job already stopped
{
return StatusCode((int)HttpStatusCode.Gone);
}
return Ok();
}
@@ -140,7 +140,7 @@ namespace Tgstation.Server.Host.Controllers
public override async Task<IActionResult> GetId(long id, CancellationToken cancellationToken)
{
//this functions as userId
var user = await DatabaseContext.Instances.Where(x => x.Id == id).SelectMany(x => x.InstanceUsers).Where(x => x.UserId == id).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
var user = await DatabaseContext.Instances.Where(x => x.Id == Instance.Id).SelectMany(x => x.InstanceUsers).Where(x => x.UserId == id).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
if (user == default)
return NotFound();
return Json(user.ToApi());
@@ -148,13 +148,11 @@ namespace Tgstation.Server.Host.Controllers
/// <inheritdoc />
[TgsAuthorize(AdministrationRights.EditUsers)]
public override async Task<IActionResult> Delete([FromBody] Api.Models.InstanceUser model, CancellationToken cancellationToken)
public override async Task<IActionResult> Delete(long id, CancellationToken cancellationToken)
{
var test = StandardModelChecks(model);
if (test != null)
return test;
//id is actually UserId
await DatabaseContext.Instances.Where(x => x.Id == Instance.Id).SelectMany(x => x.InstanceUsers).Where(x => x.UserId == model.UserId).DeleteAsync(cancellationToken).ConfigureAwait(false);
await DatabaseContext.Instances.Where(x => x.Id == Instance.Id).SelectMany(x => x.InstanceUsers).Where(x => x.UserId == id).DeleteAsync(cancellationToken).ConfigureAwait(false);
return Ok();
}
}
@@ -53,10 +53,10 @@ namespace Tgstation.Server.Host.Controllers
/// <inheritdoc />
[TgsAuthorize]
public override async Task<IActionResult> Delete([FromBody] Api.Models.Job model, CancellationToken cancellationToken)
public override async Task<IActionResult> Delete(long id, CancellationToken cancellationToken)
{
//don't care if an instance post or not at this point
var job = await DatabaseContext.Jobs.Where(x => x.Id == model.Id).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
var job = await DatabaseContext.Jobs.Where(x => x.Id == id).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
if (job == default(Job))
return NotFound();
@@ -46,8 +46,8 @@ namespace Tgstation.Server.Host.Controllers
/// <summary>
/// Attempt to get a specific a <typeparamref name="TModel"/>
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
/// <param name="id">The ID of the model to get</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the operation</returns>
[HttpGet("/{0}")]
public virtual Task<IActionResult> GetId(long id, CancellationToken cancellationToken) => Task.FromResult((IActionResult)NotFound());
@@ -62,13 +62,13 @@ namespace Tgstation.Server.Host.Controllers
public virtual Task<IActionResult> Update([FromBody]TModel model, CancellationToken cancellationToken) => Task.FromResult((IActionResult)NotFound());
/// <summary>
/// Attempt to delete a <paramref name="model"/>
/// Attempt to delete a model with a particular <paramref name="id"/>
/// </summary>
/// <param name="model">The <typeparamref name="TModel"/> being deleted</param>
/// <param name="id">The ID of the model to delete</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the operation</returns>
[HttpDelete]
public virtual Task<IActionResult> Delete([FromBody]TModel model, CancellationToken cancellationToken) => Task.FromResult((IActionResult)NotFound());
public virtual Task<IActionResult> Delete(long id, CancellationToken cancellationToken) => Task.FromResult((IActionResult)NotFound());
/// <summary>
/// Attempt to list entries of the <typeparamref name="TModel"/>
+13 -10
View File
@@ -149,16 +149,19 @@ namespace Tgstation.Server.Host.Core
/// <inheritdoc />
public async Task CancelJob(Job job, User user, CancellationToken cancellationToken)
{
if (user != null)
using (var scope = serviceProvider.CreateScope())
{
var databaseContext = scope.ServiceProvider.GetRequiredService<IDatabaseContext>();
job = new Job { Id = job.Id };
databaseContext.Jobs.Attach(job);
job.CancelledBy = user;
await databaseContext.Save(cancellationToken).ConfigureAwait(false);
}
CheckGetJob(job).Cancel();
if (job == null)
throw new ArgumentNullException(nameof(job));
if (user == null)
throw new ArgumentNullException(nameof(user));
CheckGetJob(job).Cancel(); //this will ensure the db update is only done once
using (var scope = serviceProvider.CreateScope())
{
var databaseContext = scope.ServiceProvider.GetRequiredService<IDatabaseContext>();
job = new Job { Id = job.Id };
databaseContext.Jobs.Attach(job);
job.CancelledBy = user;
await databaseContext.Save(cancellationToken).ConfigureAwait(false);
}
}
}
}