diff --git a/src/Pid/Controllers/PidController.cs b/src/Pid/Controllers/PidController.cs index 9fc8aa28162f199e35595088516a372dbf789e12..e1a6acd6278babf35f13d3b02e67b4dfef16b189 100644 --- a/src/Pid/Controllers/PidController.cs +++ b/src/Pid/Controllers/PidController.cs @@ -1,20 +1,19 @@ -using Coscine.Database.Models; -using Coscine.Database.ReturnObjects; -using Coscine.ApiCommons; -using Coscine.ApiCommons.Factories; -using Microsoft.AspNetCore.Mvc; -using System; -using System.Linq; +using Coscine.Action; +using Coscine.Action.EventArgs; +using Coscine.Api.Pid.Models; using Coscine.Configuration; -using Microsoft.AspNetCore.Authorization; -using Newtonsoft.Json.Linq; using Coscine.Database.DataModel; -using Coscine.Database.Util; +using Coscine.Database.Models; +using Coscine.Database.ReturnObjects; using Coscine.Logging; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using Coscine.Api.Pid.Models; -using Coscine.Action; -using Coscine.Action.EventArgs; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; namespace Coscine.Api.Pid.Controllers { @@ -27,6 +26,11 @@ namespace Coscine.Api.Pid.Controllers private readonly ProjectModel _projectModel; private readonly IConfiguration _configuration; private readonly Emitter _emitter; + private readonly CoscineLogger _coscineLogger; + private readonly ProjectRoleModel _projectRoleModel; + private readonly RoleModel _roleModel; + private readonly ProjectResourceModel _projectResourceModel; + private readonly VisibilityModel _visibilityModel; /// <summary> /// PidController constructor for contacting the pid owner. @@ -37,6 +41,11 @@ namespace Coscine.Api.Pid.Controllers _emitter = new Emitter(_configuration); _resourceModel = new ResourceModel(); _projectModel = new ProjectModel(); + _coscineLogger = new CoscineLogger(logger); + _projectRoleModel = new ProjectRoleModel(); + _roleModel = new RoleModel(); + _projectResourceModel = new ProjectResourceModel(); + _visibilityModel = new VisibilityModel(); } /// <summary> @@ -69,23 +78,82 @@ namespace Coscine.Api.Pid.Controllers Placeholder = placeholder }; + ResourceObject resourceObject = null; + ProjectObject projectObject = null; + IEnumerable<ProjectRole> projectOwners = null; + if (resource != null) { placeholder["resourceName"] = resource.DisplayName; pidEventArgs.Resource = resource; + resourceObject = _resourceModel.CreateReturnObjectFromDatabaseObject(resource); + + var projectId = _projectResourceModel.GetProjectForResource(resource.Id); + + if (projectId.HasValue) + { + projectObject = _projectModel.CreateReturnObjectFromDatabaseObject(_projectModel.GetById(projectId.Value)); + + var ownerId = _roleModel.GetAllWhere((x) => (x.DisplayName == "owner")).First().Id; + projectOwners = _projectRoleModel.GetAllWhere((x) => x.RoleId == ownerId && x.ProjectId == project.Id); + } } else if (project != null) { placeholder["projectName"] = project.DisplayName; pidEventArgs.Project = project; - } + projectObject = _projectModel.CreateReturnObjectFromDatabaseObject(project); + + var ownerId = _roleModel.GetAllWhere((x) => (x.DisplayName == "owner")).First().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)) + { + LogAnalyticsPidEnquiry(project, resource, resourceObject?.ApplicationProfile, resourceObject?.License?.DisplayName, projectObject?.Disciplines, projectObject?.Organizations, projectOwners, messageObject.Email); + } + _emitter.EmitPIDOwnerContact(pidEventArgs); return Json(new JObject { ["status"] = "ok" }); } + + private void LogAnalyticsPidEnquiry(Project project, Resource resource, string applicationsProfile, string license, + IEnumerable<DisciplineObject> disciplines, IEnumerable<OrganizationObject> organizations, IEnumerable<ProjectRole> owners, string email) + { + _coscineLogger.AnalyticsLog( + new AnalyticsLogObject + { + Type = "Action", + Operation = "PID Enquiry", + ProjectId = project?.Id.ToString(), + ResourceId = resource?.Id.ToString(), + ApplicationsProfile = applicationsProfile, + License = license, + Disciplines = disciplines?.Select(x => x.DisplayNameEn).ToList(), + Organizations = organizations?.Select(x => x.DisplayName).ToList(), + Visibility = project.VisibilityId.HasValue ? _visibilityModel.GetById(project.VisibilityId.Value)?.DisplayName : null, + UserList = owners?.Select(x => x.UserId.ToString()).ToList(), + ExternalInfo = email == null ? null : HashMail(email), + }); + } + + private static string HashMail(string email) + { + using var sha256Hash = SHA256.Create(); + var data = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(email)); + var stringBuilder = new StringBuilder(); + for (int i = 0; i < data.Length; i++) + { + stringBuilder.Append(data[i].ToString("x2")); + } + return stringBuilder.ToString(); + } } } diff --git a/src/Pid/Pid.csproj b/src/Pid/Pid.csproj index c0f9600725d6a7ff55406ddeecc0468a7df5d4b3..83bf7ee84b4f702ee2d45efe46dd8b6f991e7e38 100644 --- a/src/Pid/Pid.csproj +++ b/src/Pid/Pid.csproj @@ -18,5 +18,7 @@ <ItemGroup> <PackageReference Include="Coscine.Action" Version="2.*-*" /> <PackageReference Include="Coscine.ApiCommons" Version="2.*-*" /> + <PackageReference Include="Coscine.Database" Version="2.*-*" /> + <PackageReference Include="Coscine.Logging" Version="2.*-*" /> </ItemGroup> </Project> \ No newline at end of file