SecurityHelper.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Master.Model;
  3. using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs;
  4. using Prism.Services.Dialogs;
  5. using System.Management;
  6. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.SetPasswordDialogModel;
  7. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  8. namespace PDF_Master.Helper
  9. {
  10. public class SecurityHelper
  11. {
  12. /// <summary>
  13. /// 由全局变量储存是否修改过密码
  14. /// </summary>
  15. public static bool IsPasswordChanged = false;
  16. /// <summary>
  17. /// 全局变量储存修改后的密码
  18. /// </summary>
  19. public static class PasswordInfo
  20. {
  21. public static string OpenPassword = null;
  22. public static string PermissionsPassword = null;
  23. public static CPDFPermissionsInfo CPDFPermissionsInfo = CreateDefaultPermissionsInfo();
  24. }
  25. public static bool CheckHaveAllPermissions(CPDFDocument document)
  26. {
  27. CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo();
  28. if (permissionsInfo.AllowsDocumentChanges &&
  29. permissionsInfo.AllowsPrinting &&
  30. permissionsInfo.AllowsHighQualityPrinting &&
  31. permissionsInfo.AllowsCopying &&
  32. permissionsInfo.AllowsDocumentAssembly &&
  33. permissionsInfo.AllowsFormFieldEntry &&
  34. permissionsInfo.AllowsCommenting||string.IsNullOrEmpty(document.FilePath) )
  35. {
  36. return true;
  37. }
  38. else
  39. {
  40. return false;
  41. }
  42. }
  43. public static bool CheckHaveSelectedPermissions(CPDFDocument document, EnumPermissionsSet enumPermissionsSet)
  44. {
  45. CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo();
  46. ///如果需求这个权限
  47. if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsDocumentChanges) == EnumPermissionsSet.StatusAllowsDocumentChanges) && (!permissionsInfo.AllowsDocumentChanges))
  48. {
  49. return false;
  50. }
  51. if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsPrinting) == EnumPermissionsSet.StatusAllowsPrinting) && (!permissionsInfo.AllowsPrinting))
  52. {
  53. return false;
  54. }
  55. if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsHighQualityPrinting) == EnumPermissionsSet.StatusAllowsHighQualityPrinting) && (!permissionsInfo.AllowsHighQualityPrinting))
  56. {
  57. return false;
  58. }
  59. if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsCopying) == EnumPermissionsSet.StatusAllowsCopying) && (!permissionsInfo.AllowsCopying))
  60. {
  61. return false;
  62. }
  63. if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsDocumentAssembly) == EnumPermissionsSet.StatusAllowsDocumentAssembly) && (!permissionsInfo.AllowsDocumentAssembly))
  64. {
  65. return false;
  66. }
  67. if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsFormFieldEntry) == EnumPermissionsSet.StatusAllowsFormFieldEntry) && (!permissionsInfo.AllowsDocumentAssembly))
  68. {
  69. return false;
  70. }
  71. if (((enumPermissionsSet & EnumPermissionsSet.StatusAllowsCommenting) == EnumPermissionsSet.StatusAllowsCommenting) && (!permissionsInfo.AllowsCommenting))
  72. {
  73. return false;
  74. }
  75. return true;
  76. }
  77. /// <summary>
  78. /// 该函数根据需要的密码类型进行验证
  79. /// </summary>
  80. /// <param name="document">文档对象</param>
  81. /// <param name="enumPasswordKind">密码类型枚举EnumPasswordKind</param>
  82. /// <param name="dialogService">该ViewModel的IDialogService对象</param>
  83. /// <returns>
  84. ///<para>
  85. ///返回值结构体状态:
  86. /// </para>
  87. /// <para>
  88. /// IsDiscryptied == true
  89. /// Password == null
  90. /// 已解锁且无需密码提权
  91. /// </para>
  92. /// <para>
  93. ///IsDiscryptied == true
  94. ///Password != null
  95. ///可解锁且密码存储在Password中
  96. /// </para>
  97. /// <para>
  98. /// IsDiscrypited == false
  99. /// 密码错误且不可解锁
  100. /// </para>
  101. /// </returns>
  102. public static VerifyPasswordResult VerifyPasswordByPasswordKind(CPDFDocument document, EnumPasswordKind enumPasswordKind, IDialogService dialogService)
  103. {
  104. VerifyPasswordResult verifyPasswordResult = new VerifyPasswordResult();
  105. if ((!document.IsLocked) &&
  106. ((enumPasswordKind == EnumPasswordKind.StatusOpenPassword) ||
  107. (enumPasswordKind == EnumPasswordKind.StatusPermissionsPassword) && CheckHaveAllPermissions(document))||string.IsNullOrEmpty(document.FilePath))
  108. {
  109. verifyPasswordResult.IsDiscryptied = true;
  110. verifyPasswordResult.Password = null;
  111. return verifyPasswordResult;
  112. }
  113. DialogParameters value = new DialogParameters();
  114. value.Add(ParameterNames.PasswordKind, enumPasswordKind);
  115. value.Add(ParameterNames.PDFDocument, document);
  116. dialogService.ShowDialog(DialogNames.CheckPasswordDialog, value, e =>
  117. {
  118. verifyPasswordResult.IsDiscryptied = e.Parameters.GetValue<bool>(ParameterNames.PasswordResult);
  119. verifyPasswordResult.Password = e.Parameters.GetValue<string>(ParameterNames.Password);
  120. });
  121. return verifyPasswordResult;
  122. }
  123. /// <summary>
  124. /// 该函数根据需要的权限类型子集进行验证
  125. /// </summary>
  126. /// <param name="document">文档对象</param>
  127. /// <param name="enumPermissionsSet">权限类型枚举子集EnumPermissionsSet,当权限为空时退化到开启密码</param>
  128. /// <param name="dialogService">该ViewModel的IDialogService对象</param>
  129. /// <returns>
  130. ///<para>
  131. ///返回值结构体状态:
  132. /// </para>
  133. /// <para>
  134. /// IsDiscryptied == true
  135. /// Password == null
  136. /// 已解锁且无需密码提权
  137. /// </para>
  138. /// <para>
  139. ///IsDiscryptied == true
  140. ///Password != null
  141. ///可解锁且密码存储在Password中
  142. /// </para>
  143. /// <para>
  144. /// IsDiscrypited == false
  145. /// 密码错误且不可解锁
  146. /// </para>
  147. /// </returns>
  148. public static VerifyPasswordResult VerifyPasswordForSelectedPermissions(CPDFDocument document, EnumPermissionsSet enumPermissionsSet, IDialogService dialogService)
  149. {
  150. VerifyPasswordResult verifyPasswordResult = new VerifyPasswordResult();
  151. if ((!document.IsLocked) && CheckHaveSelectedPermissions(document, enumPermissionsSet))
  152. {
  153. verifyPasswordResult.IsDiscryptied = true;
  154. verifyPasswordResult.Password = null;
  155. }
  156. else
  157. {
  158. DialogParameters value = new DialogParameters();
  159. if (CheckHaveSelectedPermissions(document, enumPermissionsSet))
  160. {
  161. value.Add(ParameterNames.PasswordKind, EnumPasswordKind.StatusOpenPassword);
  162. }
  163. else
  164. {
  165. value.Add(ParameterNames.PasswordKind, EnumPasswordKind.StatusPermissionsPassword);
  166. }
  167. value.Add(ParameterNames.PDFDocument, document);
  168. dialogService.ShowDialog(DialogNames.CheckPasswordDialog, value, e =>
  169. {
  170. verifyPasswordResult.IsDiscryptied = e.Parameters.GetValue<bool>(ParameterNames.PasswordResult);
  171. verifyPasswordResult.Password = e.Parameters.GetValue<string>(ParameterNames.Password);
  172. });
  173. }
  174. return verifyPasswordResult;
  175. }
  176. }
  177. public class VerifyPasswordResult
  178. {
  179. public bool IsDiscryptied;
  180. public string Password;
  181. }
  182. }