SecurityHelper.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 != null&& dialogService!=null)
  106. {
  107. if ((!document.IsLocked) &&
  108. ((enumPasswordKind == EnumPasswordKind.StatusOpenPassword) ||
  109. (enumPasswordKind == EnumPasswordKind.StatusPermissionsPassword) && CheckHaveAllPermissions(document)) || string.IsNullOrEmpty(document.FilePath))
  110. {
  111. verifyPasswordResult.IsDiscryptied = true;
  112. verifyPasswordResult.Password = null;
  113. return verifyPasswordResult;
  114. }
  115. DialogParameters value = new DialogParameters();
  116. value.Add(ParameterNames.PasswordKind, enumPasswordKind);
  117. value.Add(ParameterNames.PDFDocument, document);
  118. dialogService.ShowDialog(DialogNames.CheckPasswordDialog, value, e =>
  119. {
  120. verifyPasswordResult.IsDiscryptied = e.Parameters.GetValue<bool>(ParameterNames.PasswordResult);
  121. verifyPasswordResult.Password = e.Parameters.GetValue<string>(ParameterNames.Password);
  122. });
  123. }
  124. return verifyPasswordResult;
  125. }
  126. /// <summary>
  127. /// 该函数根据需要的权限类型子集进行验证
  128. /// </summary>
  129. /// <param name="document">文档对象</param>
  130. /// <param name="enumPermissionsSet">权限类型枚举子集EnumPermissionsSet,当权限为空时退化到开启密码</param>
  131. /// <param name="dialogService">该ViewModel的IDialogService对象</param>
  132. /// <returns>
  133. ///<para>
  134. ///返回值结构体状态:
  135. /// </para>
  136. /// <para>
  137. /// IsDiscryptied == true
  138. /// Password == null
  139. /// 已解锁且无需密码提权
  140. /// </para>
  141. /// <para>
  142. ///IsDiscryptied == true
  143. ///Password != null
  144. ///可解锁且密码存储在Password中
  145. /// </para>
  146. /// <para>
  147. /// IsDiscrypited == false
  148. /// 密码错误且不可解锁
  149. /// </para>
  150. /// </returns>
  151. public static VerifyPasswordResult VerifyPasswordForSelectedPermissions(CPDFDocument document, EnumPermissionsSet enumPermissionsSet, IDialogService dialogService)
  152. {
  153. VerifyPasswordResult verifyPasswordResult = new VerifyPasswordResult();
  154. if ((!document.IsLocked) && CheckHaveSelectedPermissions(document, enumPermissionsSet))
  155. {
  156. verifyPasswordResult.IsDiscryptied = true;
  157. verifyPasswordResult.Password = null;
  158. }
  159. else
  160. {
  161. DialogParameters value = new DialogParameters();
  162. if (CheckHaveSelectedPermissions(document, enumPermissionsSet))
  163. {
  164. value.Add(ParameterNames.PasswordKind, EnumPasswordKind.StatusOpenPassword);
  165. }
  166. else
  167. {
  168. value.Add(ParameterNames.PasswordKind, EnumPasswordKind.StatusPermissionsPassword);
  169. }
  170. value.Add(ParameterNames.PDFDocument, document);
  171. dialogService.ShowDialog(DialogNames.CheckPasswordDialog, value, e =>
  172. {
  173. verifyPasswordResult.IsDiscryptied = e.Parameters.GetValue<bool>(ParameterNames.PasswordResult);
  174. verifyPasswordResult.Password = e.Parameters.GetValue<string>(ParameterNames.Password);
  175. });
  176. }
  177. return verifyPasswordResult;
  178. }
  179. }
  180. public class VerifyPasswordResult
  181. {
  182. public bool IsDiscryptied;
  183. public string Password;
  184. }
  185. }