123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- 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
- {
- /// <summary>
- /// 由全局变量储存是否修改过密码
- /// </summary>
- public static bool IsPasswordChanged = false;
- /// <summary>
- /// 全局变量储存修改后的密码
- /// </summary>
- 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;
- }
- /// <summary>
- /// 该函数根据需要的密码类型进行验证
- /// </summary>
- /// <param name="document">文档对象</param>
- /// <param name="enumPasswordKind">密码类型枚举EnumPasswordKind</param>
- /// <param name="dialogService">该ViewModel的IDialogService对象</param>
- /// <returns>
- ///<para>
- ///返回值结构体状态:
- /// </para>
- /// <para>
- /// IsDiscryptied == true
- /// Password == null
- /// 已解锁且无需密码提权
- /// </para>
- /// <para>
- ///IsDiscryptied == true
- ///Password != null
- ///可解锁且密码存储在Password中
- /// </para>
- /// <para>
- /// IsDiscrypited == false
- /// 密码错误且不可解锁
- /// </para>
- /// </returns>
- 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))||string.IsNullOrEmpty(document.FilePath))
- {
- 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<bool>(ParameterNames.PasswordResult);
- verifyPasswordResult.Password = e.Parameters.GetValue<string>(ParameterNames.Password);
-
- });
- return verifyPasswordResult;
- }
- /// <summary>
- /// 该函数根据需要的权限类型子集进行验证
- /// </summary>
- /// <param name="document">文档对象</param>
- /// <param name="enumPermissionsSet">权限类型枚举子集EnumPermissionsSet,当权限为空时退化到开启密码</param>
- /// <param name="dialogService">该ViewModel的IDialogService对象</param>
- /// <returns>
- ///<para>
- ///返回值结构体状态:
- /// </para>
- /// <para>
- /// IsDiscryptied == true
- /// Password == null
- /// 已解锁且无需密码提权
- /// </para>
- /// <para>
- ///IsDiscryptied == true
- ///Password != null
- ///可解锁且密码存储在Password中
- /// </para>
- /// <para>
- /// IsDiscrypited == false
- /// 密码错误且不可解锁
- /// </para>
- /// </returns>
- 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<bool>(ParameterNames.PasswordResult);
- verifyPasswordResult.Password = e.Parameters.GetValue<string>(ParameterNames.Password);
- });
- return verifyPasswordResult;
- }
- }
- public class VerifyPasswordResult
- {
- public bool IsDiscryptied;
- public string Password;
- }
- }
|