using ComPDFKit.PDFDocument;
using PDF_Office.Model;
using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
using Prism.Services.Dialogs;
using System.Management;
using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.SetPasswordDialogModel;
using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
namespace PDF_Office.Helper
{
public class SecurityHelper
{
///
/// 由全局变量储存是否修改过密码
///
public static bool IsPasswordChanged = false;
///
/// 全局变量储存修改后的密码
///
public static class PasswordInfo
{
public static string OpenPassword = null;
public static string PermissionsPassword = null;
public static CPDFPermissionsInfo CPDFPermissionsInfo = CreateDefaultPermissionsInfo();
}
public static bool CheckHaveAllPermissions(CPDFDocument document)
{
CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo();
if (permissionsInfo.AllowsDocumentChanges &&
permissionsInfo.AllowsPrinting &&
permissionsInfo.AllowsHighQualityPrinting &&
permissionsInfo.AllowsCopying &&
permissionsInfo.AllowsDocumentAssembly &&
permissionsInfo.AllowsFormFieldEntry &&
permissionsInfo.AllowsCommenting||string.IsNullOrEmpty(document.FilePath) )
{
return true;
}
else
{
return false;
}
}
public static bool CheckHaveSelectedPermissions(CPDFDocument document, EnumPermissionsSet enumPermissionsSet)
{
CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo();
///如果需求这个权限
if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsDocumentChanges) == EnumPermissionsSet.StatusAllowsDocumentChanges) && (!permissionsInfo.AllowsDocumentChanges))
{
return false;
}
if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsPrinting) == EnumPermissionsSet.StatusAllowsPrinting) && (!permissionsInfo.AllowsPrinting))
{
return false;
}
if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsHighQualityPrinting) == EnumPermissionsSet.StatusAllowsHighQualityPrinting) && (!permissionsInfo.AllowsHighQualityPrinting))
{
return false;
}
if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsCopying) == EnumPermissionsSet.StatusAllowsCopying) && (!permissionsInfo.AllowsCopying))
{
return false;
}
if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsDocumentAssembly) == EnumPermissionsSet.StatusAllowsDocumentAssembly) && (!permissionsInfo.AllowsDocumentAssembly))
{
return false;
}
if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsFormFieldEntry) == EnumPermissionsSet.StatusAllowsFormFieldEntry) && (!permissionsInfo.AllowsDocumentAssembly))
{
return false;
}
if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsCommenting) == EnumPermissionsSet.StatusAllowsCommenting) && (!permissionsInfo.AllowsCommenting))
{
return false;
}
return true;
}
///
/// 该函数根据需要的密码类型进行验证
///
/// 文档对象
/// 密码类型枚举EnumPasswordKind
/// 该ViewModel的IDialogService对象
///
///
///返回值结构体状态:
///
///
/// IsDiscryptied == true
/// Password == null
/// 已解锁且无需密码提权
///
///
///IsDiscryptied == true
///Password != null
///可解锁且密码存储在Password中
///
///
/// IsDiscrypited == false
/// 密码错误且不可解锁
///
///
public static VerifyPasswordResult VerifyPasswordByPasswordKind(CPDFDocument document, EnumPasswordKind enumPasswordKind, IDialogService dialogService)
{
VerifyPasswordResult verifyPasswordResult = new VerifyPasswordResult();
if ((!document.IsLocked) &&
((enumPasswordKind == EnumPasswordKind.StatusOpenPassword) ||
(enumPasswordKind == EnumPasswordKind.StatusPermissionsPassword) && CheckHaveAllPermissions(document)))
{
verifyPasswordResult.IsDiscryptied = true;
verifyPasswordResult.Password = null;
return verifyPasswordResult;
}
DialogParameters value = new DialogParameters();
value.Add(ParameterNames.PasswordKind, enumPasswordKind);
value.Add(ParameterNames.PDFDocument, document);
dialogService.ShowDialog(DialogNames.CheckPasswordDialog, value, e =>
{
verifyPasswordResult.IsDiscryptied = e.Parameters.GetValue(ParameterNames.PasswordResult);
verifyPasswordResult.Password = e.Parameters.GetValue(ParameterNames.Password);
});
return verifyPasswordResult;
}
///
/// 该函数根据需要的权限类型子集进行验证
///
/// 文档对象
/// 权限类型枚举子集EnumPermissionsSet,当权限为空时退化到开启密码
/// 该ViewModel的IDialogService对象
///
///
///返回值结构体状态:
///
///
/// IsDiscryptied == true
/// Password == null
/// 已解锁且无需密码提权
///
///
///IsDiscryptied == true
///Password != null
///可解锁且密码存储在Password中
///
///
/// IsDiscrypited == false
/// 密码错误且不可解锁
///
///
public static VerifyPasswordResult VerifyPasswordForSelectedPermissions(CPDFDocument document, EnumPermissionsSet enumPermissionsSet, IDialogService dialogService)
{
VerifyPasswordResult verifyPasswordResult = new VerifyPasswordResult();
if ((!document.IsLocked) && CheckHaveSelectedPermissions(document, enumPermissionsSet))
{
verifyPasswordResult.IsDiscryptied = true;
verifyPasswordResult.Password = null;
return verifyPasswordResult;
}
DialogParameters value = new DialogParameters();
if (enumPermissionsSet == EnumPermissionsSet.None)
{
value.Add(ParameterNames.PasswordKind, EnumPasswordKind.StatusOpenPassword);
}
else
{
value.Add(ParameterNames.PasswordKind, EnumPasswordKind.StatusPermissionsPassword);
}
value.Add(ParameterNames.PDFDocument, document);
dialogService.ShowDialog(DialogNames.CheckPasswordDialog, value, e =>
{
verifyPasswordResult.IsDiscryptied = e.Parameters.GetValue(ParameterNames.PasswordResult);
verifyPasswordResult.Password = e.Parameters.GetValue(ParameterNames.Password);
});
return verifyPasswordResult;
}
}
public class VerifyPasswordResult
{
public bool IsDiscryptied;
public string Password;
}
}