Skip to content
Snippets Groups Projects
Commit b3853fdc authored by Marcel Nellesen's avatar Marcel Nellesen
Browse files

Merge branch 'Product/1188-LoggingExtended' into 'Sprint/2021-05'

Product/1188 logging extended

See merge request !26
parents a64f5a0b b66353d8
No related branches found
No related tags found
2 merge requests!26Product/1188 logging extended,!22Sprint/2021 05
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();
}
}
}
......@@ -19,5 +19,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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment