Skip to content
Snippets Groups Projects
Commit 12cd6b0a authored by Benedikt Heinrichs's avatar Benedikt Heinrichs Committed by Petar Hristov
Browse files

Update: Overhaul PID handling

parent 91f0ea59
Branches
Tags
2 merge requests!38Release: Sprint/2022 17 :robot:,!37Update: Overhaul PID handling
...@@ -48,6 +48,64 @@ namespace Coscine.Api.Pid.Controllers ...@@ -48,6 +48,64 @@ namespace Coscine.Api.Pid.Controllers
_visibilityModel = new VisibilityModel(); _visibilityModel = new VisibilityModel();
} }
/// <summary>
/// Returns a 200, if a PID is valid.
/// </summary>
/// <param name="pid">PID</param>
/// <returns>200 if successful, 400 if pid is badly formatted, 403 if project or resource is deleted, 404 if not exists.</returns>
[HttpGet("[controller]/valid/{pid}")]
public IActionResult IsValid(string pid)
{
var id = Uri.UnescapeDataString(pid);
if (id.Contains('/'))
{
id = id[(id.IndexOf("/") + 1)..];
}
else
{
return BadRequest($"PID {pid} is badly formatted.");
}
if (id.Contains('@'))
{
id = id[..id.IndexOf("@")];
}
if (id.Contains('/'))
{
id = id[..id.IndexOf("/")];
}
Resource resource = null;
try
{
resource = _resourceModel.GetByIdIncludingDeleted(new Guid(id));
}
catch
{
}
Project project = null;
try
{
project = _projectModel.GetByIdIncludingDeleted(new Guid(id));
}
catch
{
}
if (resource == null && project == null)
{
return NotFound($"No project/resource with PID {pid} exists.");
}
if ((resource != null && resource.Deleted) || (project != null && project.Deleted))
{
return Forbid();
}
return Ok();
}
/// <summary> /// <summary>
/// Sends a request to the pid owner. /// Sends a request to the pid owner.
/// </summary> /// </summary>
...@@ -56,8 +114,38 @@ namespace Coscine.Api.Pid.Controllers ...@@ -56,8 +114,38 @@ namespace Coscine.Api.Pid.Controllers
[HttpPost("[controller]/sendMailToOwner")] [HttpPost("[controller]/sendMailToOwner")]
public IActionResult SendMailToOwner([FromBody] MessageObject messageObject) public IActionResult SendMailToOwner([FromBody] MessageObject messageObject)
{ {
var resource = _resourceModel.GetById(new Guid(messageObject.Guid)); Resource resource = null;
var project = _projectModel.GetById(new Guid(messageObject.Guid)); try
{
resource = _resourceModel.GetByIdIncludingDeleted(new Guid(messageObject.Guid));
}
catch
{
}
Project project = null;
try
{
project = _projectModel.GetByIdIncludingDeleted(new Guid(messageObject.Guid));
}
catch
{
}
if (resource == null && project == null)
{
// Log the error.
// ProjectId and ResourceId will be null and indicate an error in the logs.
LogAnalyticsPidEnquiry(null, null, null, null, null, null, null, null);
return NotFound($"No project/resource with PID {messageObject.Pid} exists.");
}
if ((resource != null && resource.Deleted) || (project != null && project.Deleted))
{
return Forbid();
}
var user = new User() var user = new User()
{ {
DisplayName = messageObject.Name, DisplayName = messageObject.Name,
...@@ -108,13 +196,6 @@ namespace Coscine.Api.Pid.Controllers ...@@ -108,13 +196,6 @@ namespace Coscine.Api.Pid.Controllers
var ownerId = _roleModel.GetAllWhere((x) => (x.DisplayName == "owner")).First().Id; var ownerId = _roleModel.GetAllWhere((x) => (x.DisplayName == "owner")).First().Id;
projectOwners = _projectRoleModel.GetAllWhere((x) => x.RoleId == ownerId && x.ProjectId == project.Id); projectOwners = _projectRoleModel.GetAllWhere((x) => x.RoleId == ownerId && x.ProjectId == project.Id);
} }
else
{
// Log the error.
// ProjectId and ResourceId will be null and indicate an error in the logs.
LogAnalyticsPidEnquiry(null, null, null, null, null, null, null, null);
return BadRequest("No project/resource with this pid exists.");
}
if (CoscineLoggerConfiguration.IsLogLevelActivated(LogType.Analytics)) if (CoscineLoggerConfiguration.IsLogLevelActivated(LogType.Analytics))
{ {
......
using System; namespace Coscine.Api.Pid.Models
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.Api.Pid.Models
{ {
/// <summary> /// <summary>
/// Message Object. /// Message Object.
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Coscine.Action" Version="2.*-*" /> <PackageReference Include="Coscine.Action" Version="3.*-*" />
<PackageReference Include="Coscine.ApiCommons" Version="2.*-*" /> <PackageReference Include="Coscine.ApiCommons" Version="2.*-*" />
<PackageReference Include="Coscine.Database" Version="2.*-*" /> <PackageReference Include="Coscine.Database" Version="2.*-*" />
<PackageReference Include="Coscine.Logging" Version="2.*-*" /> <PackageReference Include="Coscine.Logging" Version="2.*-*" />
......
using Coscine.ApiCommons; using Coscine.ApiCommons;
using Coscine.Configuration; using Coscine.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.Api.Pid namespace Coscine.Api.Pid
{ {
......
using Coscine.ApiCommons; using Coscine.ApiCommons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.Api.Pid namespace Coscine.Api.Pid
{ {
/// <summary> /// <summary>
/// Standard Startup class. /// Standard Startup class.
/// </summary> /// </summary>
public class Startup : AbstractDefaultStartup public class Startup : AbstractStartup
{ {
/// <summary> /// <summary>
/// Standard Startup constructor. /// Standard Startup constructor.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment