Skip to content
Snippets Groups Projects
Commit 7c66e091 authored by Petar Hristov's avatar Petar Hristov :speech_balloon:
Browse files

Merge branch 'Hotfix/2455-missingQuotaCheck' into 'master'

Fix: allow resource creation for quota case (coscine/issues#2455)

See merge request !151
parents 22b03a18 60b649de
No related branches found
No related tags found
1 merge request!151Fix: allow resource creation for quota case (coscine/issues#2455)
......@@ -309,12 +309,7 @@ namespace Coscine.Api.Resources.Controllers
orgs = new List<string> { "*" };
}
if (!resourceType.Enabled.HasValue
|| !resourceType.Enabled.Value
|| !orgs.SelectMany(x => ResourceTypeFactory
.Instance
.GetSpecificResourceTypes(x, ResourceTypes.Base.ResourceTypeStatus.Active))
.Any(x => x.SpecificTypeName == resourceType.SpecificType))
if (!Util.ResourceTypeHelper.IsResourceTypeUsable(resourceType, projectId, orgs))
{
return Unauthorized("The user is not authorized to add a new resource of this type to the selected project!");
}
......
......@@ -50,48 +50,18 @@ namespace Coscine.Api.Resources.Controllers
/// <summary>
/// Returns all enabled resource types according to user's affiliation and allocated quota in the project.
/// </summary>
/// <param name="id">Id of the project</param>
/// <param name="projectId">Id of the project</param>
/// <returns>List of ResourceTypeInformations for the enabled Resources</returns>
[Route("[controller]/types/{id}/-/enabled")]
public async Task<ActionResult<IEnumerable<ResourceTypeInformation>>> GetEnabledResourceTypes(Guid id)
[Route("[controller]/types/{projectId}/-/enabled")]
public async Task<ActionResult<IEnumerable<ResourceTypeInformation>>> GetEnabledResourceTypes(Guid projectId)
{
var user = _authenticator.GetUser();
var orgs = Util.OrganizationsHelper.GetOrganization(user).ToList();
if (orgs?.Any() != true)
{
orgs = new List<string> { "*" };
return Json(await CreateListOfResourceTypeInformation(Util.ResourceTypeHelper.GetAvailableResourceTypesForProject(projectId, orgs)));
}
var listOfAffiliatedResources = orgs.SelectMany(x => ResourceTypeFactory
.Instance
.GetSpecificResourceTypes(x, ResourceTypes.Base.ResourceTypeStatus.Active)
.Select(x => x.SpecificTypeName));
var listOfEnabledResources = listOfAffiliatedResources;
var listOfAllActiveResources = ResourceTypeFactory.Instance.GetSpecificResourceTypes(ResourceTypes.Base.ResourceTypeStatus.Active).Select(x => x.SpecificTypeName);
foreach (var resourceType in listOfAllActiveResources)
{
// check if the resource type is not in the list of affiliated ressource types and if so, if the project has reserved maxQuota for this resource type
if (!listOfAffiliatedResources.Contains(resourceType))
{
var resourceId = _resourceTypeModel.GetAllWhere(x => x.SpecificType == resourceType).FirstOrDefault().Id;
var quotas = _projectQuotaModel.GetAllWhere(x => x.ProjectId == id && x.ResourceTypeId == resourceId);
if (quotas.FirstOrDefault().MaxQuota > 0)
{
listOfEnabledResources = listOfEnabledResources.Append(resourceType).ToList();
}
}
}
var allEnabledResourceTypes = _resourceTypeModel.GetAllWhere((resourceType) => listOfEnabledResources.Contains(resourceType.SpecificType));
return Json(await CreateListOfResourceTypeInformation(allEnabledResourceTypes));
}
/// <summary>
/// Returns all enabled resource types.
/// </summary>
......@@ -117,7 +87,6 @@ namespace Coscine.Api.Resources.Controllers
return Json(await CreateListOfResourceTypeInformation(allEnabledResourceTypes));
}
/// <summary>
/// Returns all fields of the specified resource type.
/// </summary>
......
using Coscine.Database.DataModel;
using Coscine.Database.Models;
using Coscine.ResourceTypes;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Coscine.Api.Resources.Util
{
internal static class ResourceTypeHelper
{
private static readonly ResourceTypeModel _resourceTypeModel = new();
private static readonly ProjectQuotaModel _projectQuotaModel = new();
internal static IEnumerable<ResourceType> GetAvailableResourceTypesForProject(Guid projectId, IEnumerable<string> affiliatedOrganizaions)
{
if (affiliatedOrganizaions?.Any() != true)
{
affiliatedOrganizaions = new List<string> { "*" };
}
var listOfAffiliatedResources = affiliatedOrganizaions.SelectMany(x => ResourceTypeFactory
.Instance
.GetSpecificResourceTypes(x, ResourceTypes.Base.ResourceTypeStatus.Active)
.Select(x => x.SpecificTypeName));
var listOfEnabledResources = listOfAffiliatedResources;
foreach (var resourceType in ResourceTypeFactory
.Instance
.GetSpecificResourceTypes(ResourceTypes.Base.ResourceTypeStatus.Active)
.Select(x => x.SpecificTypeName))
{
// check if the resource type is not in the list of affiliated resource types and if so, if the project has reserved maxQuota for this resource type
if (!listOfAffiliatedResources.Contains(resourceType))
{
var resourceId = _resourceTypeModel.GetAllWhere(x => x.SpecificType == resourceType).FirstOrDefault().Id;
var quotas = _projectQuotaModel.GetAllWhere(x => x.ProjectId == projectId && x.ResourceTypeId == resourceId);
if (quotas.Any() && quotas.First().MaxQuota > 0)
{
listOfEnabledResources = listOfEnabledResources.Append(resourceType).ToList();
}
}
}
return _resourceTypeModel.GetAllWhere((resourceType) => listOfEnabledResources.Contains(resourceType.SpecificType));
}
internal static bool IsResourceTypeUsable(ResourceType resourceType, Guid projectId, IEnumerable<string> affiliatedOrganizations)
{
return resourceType.Enabled == true && GetAvailableResourceTypesForProject(projectId, affiliatedOrganizations)
.Select(x => x.Id)
.Contains(resourceType.Id);
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment