// Copyright Epic Games, Inc. All Rights Reserved.
using HordeServer.Api;
using HordeServer.Models;
using HordeServer.Services;
using HordeServer.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace HordeServer.Controllers
{
///
/// Controller for the /api/v1/credentials endpoint
///
[ApiController]
[Authorize]
[Route("[controller]")]
public class CredentialsController : ControllerBase
{
///
/// Singleton instance of the ACL service
///
private readonly AclService AclService;
///
/// Singleton instance of the credential service
///
private readonly CredentialService CredentialService;
///
/// Constructor
///
/// The ACL service
/// The credential service
public CredentialsController(AclService AclService, CredentialService CredentialService)
{
this.AclService = AclService;
this.CredentialService = CredentialService;
}
///
/// Creates a new credential
///
/// Parameters for the new credential.
/// Http result code
[HttpPost]
[Route("/api/v1/credentials")]
public async Task> CreateCredentialAsync([FromBody] CreateProjectRequest Create)
{
if(!await AclService.AuthorizeAsync(AclAction.CreateCredential, User))
{
return Forbid();
}
Credential NewCredential = await CredentialService.CreateCredentialAsync(Create.Name, Create.Properties);
return new CreateCredentialResponse(NewCredential.Id.ToString());
}
///
/// Query all the credentials
///
/// Id of the credential to get information about
/// Filter for the properties to return
/// Information about all the credentials
[HttpGet]
[Route("/api/v1/credentials")]
[ProducesResponseType(typeof(List), 200)]
public async Task> FindCredentialAsync([FromQuery] string? Name = null, [FromQuery] PropertyFilter? Filter = null)
{
if (!await AclService.AuthorizeAsync(AclAction.ListCredentials, User))
{
return Forbid();
}
List Credentials = await CredentialService.FindCredentialsAsync(Name);
GlobalPermissionsCache Cache = new GlobalPermissionsCache();
List