Skip to content
Snippets Groups Projects

Update: Overhaul PID handling

Merged Benedikt Heinrichs requested to merge Issue/1321-pidEnquiryOverhaul into dev
Files
5
@@ -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))
{
{
@@ -126,7 +207,7 @@ namespace Coscine.Api.Pid.Controllers
@@ -126,7 +207,7 @@ namespace Coscine.Api.Pid.Controllers
}
}
private void LogAnalyticsPidEnquiry(Project project, Resource resource, string applicationsProfile, string license,
private void LogAnalyticsPidEnquiry(Project project, Resource resource, string applicationsProfile, string license,
IEnumerable<DisciplineObject> disciplines, IEnumerable<OrganizationObject> organizations, IEnumerable<ProjectRole> owners, string email)
IEnumerable<DisciplineObject> disciplines, IEnumerable<OrganizationObject> organizations, IEnumerable<ProjectRole> owners, string email)
{
{
string visibility = null;
string visibility = null;
if (project != null && project.VisibilityId.HasValue)
if (project != null && project.VisibilityId.HasValue)
Loading