Skip to content
Snippets Groups Projects
Select Git revision
  • 744269395b0f781832bcd495f5b0405fe21ab1b4
  • master default protected
  • dev_2022
  • patch-1
  • develop
  • 50-use-ubuntus-libhidapi
  • issue-highLevelDispatch
  • issue-highLevelDesign
  • issue-motorStartBug
  • issue-commandLayerDesign
  • v1.0
  • v0.4-rc.13
  • v0.4-rc.12
  • v0.4-rc.11
  • v0.4-rc.10
  • v0.4-rc.9
  • v0.3-rc.8
  • v0.3-rc.7
  • v0.3-rc.6
  • v0.3-rc.5
  • v0.3-rc.4
  • v0.3-rc.3
  • v0.3-rc.2
  • v0.3-rc.1
  • v0.3-rc
  • v0.2
  • v0.1.1
  • v0.1
28 results

OutputStreamSend.class

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    PidController.cs 6.96 KiB
    using Coscine.Action;
    using Coscine.Action.EventArgs;
    using Coscine.Api.Pid.Models;
    using Coscine.Configuration;
    using Coscine.Database.DataModel;
    using Coscine.Database.Models;
    using Coscine.Database.ReturnObjects;
    using Coscine.Logging;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;
    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
    {
        /// <summary>
        /// This controller represents the actions to contact pid owner.
        /// </summary>
        public class PidController : Controller
        {
            private readonly ResourceModel _resourceModel;
            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.
            /// </summary>
            public PidController(ILogger<PidController> logger)
            {
                _configuration = Program.Configuration;
                _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>
            /// Sends a request to the pid owner.
            /// </summary>
            /// <param name="messageObject">Entry with the information for the email to the pid owner.</param>
            /// <returns>JSON object.</returns>
            [HttpPost("[controller]/sendMailToOwner")]
            public IActionResult SendMailToOwner([FromBody] MessageObject messageObject)
            {
                var resource = _resourceModel.GetById(new Guid(messageObject.Guid));
                var project = _projectModel.GetById(new Guid(messageObject.Guid));
                var user = new User()
                {
                    DisplayName = messageObject.Name,
                    EmailAddress = messageObject.Email
                };
                var placeholder = new JObject()
                {
                    ["requesterName"] = messageObject.Name,
                    ["requesterMail"] = messageObject.Email,
                    ["requestMessage"] = messageObject.Message,
                    ["pid"] = messageObject.Pid
                };
    
                PIDEventArgs pidEventArgs = new PIDEventArgs(_configuration)
                {
                    Requester = user,
                    SentCopy = messageObject.SendCopy,
                    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)
                    {
                        project = _projectModel.GetById(projectId.Value);
                        projectObject = _projectModel.CreateReturnObjectFromDatabaseObject(project);
    
                        var ownerId = _roleModel.GetAllWhere((x) => (x.DisplayName == "owner")).First().Id;
                        projectOwners = _projectRoleModel.GetAllWhere((x) => x.RoleId == ownerId && x.ProjectId == projectId.Value);
                    }
                }
                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)
            {
                string visibility = null;
                if (project != null && project.VisibilityId.HasValue) 
                {
                    visibility = _visibilityModel.GetById(project.VisibilityId.Value)?.DisplayName;
                }
                _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 = visibility,
                        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();
            }
        }
    }