🐛 Fix bug while searching for the user sid

This commit is contained in:
Gerrit 2023-05-30 17:22:56 +02:00
parent c7d2a2c4fe
commit 5a460e1ba0
16 changed files with 350 additions and 45 deletions

View file

@ -2,12 +2,14 @@
namespace AmagnoVirtualPrinter.Agent.Core.Model
{
public struct SessionInfo : ISessionInfo
public class SessionInfo : ISessionInfo
{
public int Id { get; set; }
public string Desktop { get; set; }
public string Sid { get; set; }
public bool FoundDomain { get; set; }
}
}

View file

@ -41,6 +41,7 @@
<Compile Include="Misc\Job.cs" />
<Compile Include="Misc\JobFactory.cs" />
<Compile Include="Misc\JobService.cs" />
<Compile Include="Misc\PrintJobReader.cs" />
<Compile Include="Misc\VirtualTcpInputPrinter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AmagnoVirtualPrinterService.cs" />

View file

@ -62,6 +62,7 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
if (ghostScriptExe == null)
{
_logger.Error("GhostScript not found.");
throw new PostScriptConversionException("GhostScript not found. Please place local variable.");
}

View file

@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Printing;
using System.Security.Principal;
using AmagnoVirtualPrinter.Agent.Core.Enums;
using AmagnoVirtualPrinter.Agent.Core.Interfaces;
@ -67,13 +66,15 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
{
var now = DateTime.Now;
var jobInfo = GetCurrentPrintJobs(printerName).FirstOrDefault();
var jobInfo = PrintJobReader.GetCurrentPrintJobs(printerName).FirstOrDefault();
if (jobInfo == null)
{
throw new InvalidOperationException();
}
var session = GetCurrentSessions(jobInfo).FirstOrDefault();
var sessions = GetCurrentSessions(jobInfo).ToArray();
var session = sessions.FirstOrDefault(s => s.FoundDomain) ?? sessions.FirstOrDefault();
var config = _registryRepository.GetRegistryConfig();
var userConfig = _registryRepository.GetUserRegistryConfig(session.Sid);
var root = _directoryHelper.GetOutputDirectory(userConfig);
@ -144,11 +145,11 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
{
var domain = job.DomainName;
var machine = job.MachineName?.TrimStart('\\');
var user = job.UserName;
var username = job.UserName;
LogDebug($"Searching for session of {domain}\\{user} on {machine} !");
LogDebug($"Searching for session of {domain}\\{username} on {machine} !");
if (domain == null || machine == null || user == null)
if (domain == null || machine == null || username == null)
{
yield break;
}
@ -159,7 +160,7 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
var sessions = server.GetSessions().Where(s => s.UserName != null && s.DomainName != null);
foreach (var session in sessions)
{
if (!session.UserName.Equals(user, cmp))
if (!session.UserName.Equals(username, cmp))
{
continue;
}
@ -168,7 +169,7 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
var isDomainUser = session.DomainName.Equals(domain, cmp);
if (!isSingleUser && !isDomainUser)
{
continue;
LogWarn("Found Session {sessionId} for {username} but its not of domain {domain} or {machine}", session.SessionId, username, domain, machine);
}
var sessionId = session.SessionId;
@ -178,47 +179,13 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
{
Id = sessionId,
Desktop = desktopName,
Sid = account.Translate(typeof(SecurityIdentifier)).Value
Sid = account.Translate(typeof(SecurityIdentifier)).Value,
FoundDomain = isSingleUser || isDomainUser
};
}
}
}
[ItemNotNull]
private IEnumerable<IJobInfo> GetCurrentPrintJobs(string printerName)
{
using (var server = new LocalPrintServer())
{
using (var queue = server.GetPrintQueue(printerName))
{
using (var jobs = queue.GetPrintJobInfoCollection())
{
foreach (var job in jobs)
{
using (job)
{
var id = job.JobIdentifier;
var machine = server.Name;
var domain = Environment.UserDomainName;
var user = job.Submitter;
var name = job.Name;
yield return new JobInfo
{
JobId = id,
Name = name,
DomainName = domain,
MachineName = machine,
UserName = user,
Status = job.JobStatus,
DeviceName = queue.Name
};
}
}
}
}
}
}
[NotNull]
private string GetRawFileExtension(IntermediateFormat format)
{
@ -242,5 +209,10 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
{
_logger.Error(exception, message, args);
}
private void LogWarn(string message, params object[] args)
{
_logger.Warn(message, args);
}
}
}

View file

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Printing;
using AmagnoVirtualPrinter.Agent.Core.Interfaces;
using AmagnoVirtualPrinter.Agent.Core.Model;
using JetBrains.Annotations;
namespace AmagnoVirtualPrinter.Agent.Lib.Misc
{
public class PrintJobReader
{
[ItemNotNull]
public static IEnumerable<IJobInfo> GetCurrentPrintJobs(string printerName)
{
using (var server = new LocalPrintServer())
{
using (var queue = server.GetPrintQueue(printerName))
{
using (var jobs = queue.GetPrintJobInfoCollection())
{
foreach (var job in jobs)
{
using (job)
{
var id = job.JobIdentifier;
var machine = server.Name;
var domain = Environment.UserDomainName;
var user = job.Submitter;
var name = job.Name;
yield return new JobInfo
{
JobId = id,
Name = name,
DomainName = domain,
MachineName = machine,
UserName = user,
Status = job.JobStatus,
DeviceName = queue.Name
};
}
}
}
}
}
}
}
}

View file

@ -4,6 +4,7 @@ using System.Linq;
using AmagnoVirtualPrinter.Agent.Core.Enums;
using AmagnoVirtualPrinter.Agent.Core.Interfaces;
using AmagnoVirtualPrinter.Agent.Core.Model;
using AmagnoVirtualPrinter.Logging;
using JetBrains.Annotations;
using Microsoft.Win32;
@ -13,6 +14,8 @@ namespace AmagnoVirtualPrinter.Utils
public class RegistryRepository : IRegistryRepository
{
private const short DefaultServerPort = 9101;
private static readonly IVirtualPrinterLogger<RegistryRepository> Logger = new VirtualPrinterLogger<RegistryRepository>();
public bool TryGetGhostscriptPath(out string path)
{
@ -104,6 +107,8 @@ namespace AmagnoVirtualPrinter.Utils
public IUserConfig GetUserRegistryConfig(string sid)
{
Logger.Trace("GetUserRegistryConfig for {sid}", sid);
var regView = GetRegistryView();
var userConfig = new UserRegistryConfig();