Procházet zdrojové kódy

安全 -校验弹窗 打印 - 部分bug

liuaoran před 2 roky
rodič
revize
1ab3576dd9

+ 95 - 1
PDF Office/Helper/SecurityHelper.cs

@@ -1,5 +1,6 @@
 using ComPDFKit.PDFDocument;
 using PDF_Office.Model;
+using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
 using Prism.Services.Dialogs;
 using System;
 using System.Collections.Generic;
@@ -31,7 +32,42 @@ namespace PDF_Office.Helper
             }
         }
 
-        public static bool VerifyPassword(CPDFDocument document ,EnumPasswordKind enumPasswordKind, IDialogService dialogService)
+        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;
+        }
+
+        public static bool VerifyPasswordOnlyBool(CPDFDocument document ,EnumPasswordKind enumPasswordKind, IDialogService dialogService)
         {
             if (enumPasswordKind == EnumPasswordKind.StatusOpenPassword)
             {
@@ -61,5 +97,63 @@ namespace PDF_Office.Helper
             });
             return isDiscryptied;
         }
+
+        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.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 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;
     }
 }

+ 12 - 1
PDF Office/Model/Dialog/ToolsDialogs/SaftyDialogs/CheckPasswordDialogModel.cs

@@ -9,5 +9,16 @@ namespace PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs
     public class CheckPasswordDialogModel
     {
     }
-
+    public enum EnumPermissionsSet
+    {
+        None = 0,
+        StatusAllowsDocumentChanges = 1<<1,
+        StatusAllowsPrinting = 1 <<2,
+        StatusAllowsHighQualityPrinting = 1<<3,
+        StatusAllowsCopying = 1<<4,
+        StatusAllowsDocumentAssembly = 1<<5,
+        StatusAllowsFormFieldEntry  = 1<<6,
+        StatusAllowsCommenting = 1<<7
+    }
+    
 }

+ 1 - 0
PDF Office/Model/Dialog/ToolsDialogs/SaftyDialogs/DeleteSafetySettintgsModel.cs

@@ -20,5 +20,6 @@ namespace PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs
             StatusOpenPassword,
             StatusPermissionsPassword
         }
+
     }
 }

+ 32 - 1
PDF Office/StyleAndTemplateList.txt

@@ -55,4 +55,35 @@
 位置:PDF Office\CustomControl\BatchStatus.xaml
 用途:批量处理进度显示
 使用方法:StatusValue{设置等于,"complete"(显示绿色背景勾形符号)、"error"(显示红色背景感叹符号)、
-"wait"(显示等待闹钟)、"为数字时"(显示进度条)}
+"wait"(显示等待闹钟)、"为数字时"(显示进度条)}
+
+8.CheckPasswordDialog
+位置:PDF Office\Views\Dialog\ToolsDialogs\SaftyDialogs\CheckPasswordDialog.xaml
+用途:  密码校验
+使用方法
+```C# 
+  VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
+  //或 VerifyPasswordResult result = SecurityHelper.VerifyPasswordForSelectedPermissions(PDFViewer.Document, PermissionsSubset, dialogs);
+                if (result.IsDiscryptied)
+                {
+                    if (result.Password != null)
+                    {
+                        string filePath = PDFViewer.Document.FilePath;
+                        PDFViewer.Document.Release();
+                        PDFViewer.InitDocument(filePath);
+                        PDFViewer.Document.UnlockWithPassword(result.Password);
+                    }
+                   ///TODO:
+                   ///此处填入需要执行的代码
+                }
+```
+备注:  
+SecurityHelper.VerifyPasswordByPasswordKind需要传入的参数:PDFViewer.Document,需要验证的密码类型(开启密码/权限密码),当前ViewModel的IDialogService对象
+ecurityHelper.VerifyPasswordForSelectedPermissions需要传入的参数:PDFViewer.Document, 需要验证的权限子集(形如 枚举1|枚举2|枚举3),当前ViewModel的IDialogService对象。
+返回值result对象包含两个成员:IsDiscryptied标志是否可以解锁/提权,Password对象标志当前正确的密码,当密码为空时表示当前无需输入。
+IsDiscryptied == false  解锁/提权失败,拒绝下一步
+IsDiscryptied == true password == null; 当前无需解锁/提权,不会显示输入密码弹窗,直接进行下一步
+IsDiscryptied == true  password != null; 提权/解锁成功,共用当前PDFView的同权限级别操作全部开放,不再需要校验
+* 下一步 *指解锁后的具体业务操作,例如唤起需求复制权限的弹窗等
+
+

+ 3 - 1
PDF Office/ViewModels/Dialog/HomePageToolsDialogs/HomePagePrinter/HomePagePrinterDialogViewModel.cs

@@ -394,7 +394,7 @@ namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
                     {
                         PrintOrientationIndex = 1;
                     }
-                    this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); 
+                    this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
                 }
             }
             else
@@ -897,6 +897,8 @@ namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
                 {
                     isCurrentPage = false;
                 }
+                System.Windows.Size pageSize = PDFViewer.Document.GetPageSize(0);
+
                 NavigationParameters param = new NavigationParameters();
                 NavigateDocumentRegion();
                 NavigateModRegion();

+ 4 - 2
PDF Office/ViewModels/Dialog/HomePageToolsDialogs/HomePagePrinter/HomePagePrinterDocumentContentViewModel.cs

@@ -335,7 +335,7 @@ namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
                 }
                 catch
                 {
-                    MessageBoxEx.Show("另一个进程正在占用该打印机");
+                    MessageBoxEx.Show("另一个进程正在占用该打印机或发生未知错误");
                 }
             }
         }
@@ -654,7 +654,7 @@ namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
                             string sign = null;
                             if (posterInfo.Label == null || posterInfo.Label == "")
                             {
-                                sign = string.Format("({0},{1}){2}.pdf {3} {4}", HorizonPaperIndex + 1, VerticalPaperIndex + 1, PrintIndex + 1, System.DateTime.Today.ToString("yyyy-MM-dd"), System.DateTime.Now.ToString("HH:mm:ss"));
+                                sign = string.Format("({0},{1})  -{2} -{3}.pdf {4} {5}", HorizonPaperIndex + 1, VerticalPaperIndex + 1, PrintIndex + 1, PDFViewer.Document.FileName, System.DateTime.Today.ToString("yyyy/MM/dd"), System.DateTime.Now.ToString("HH:mm:ss"));
                             }
                             else
                             {
@@ -716,6 +716,8 @@ namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
                     }
                     else
                     {
+                        HorizonPaperIndex = 0;
+                        VerticalPaperIndex = 0;
                         e.HasMorePages = false;
                     }
                 }

+ 3 - 2
PDF Office/ViewModels/Dialog/HomePageToolsDialogs/HomePagePrinter/HomePagePrinterModPosterContentViewModel.cs

@@ -1,4 +1,5 @@
-using Microsoft.Office.Interop.Word;
+using ComPDFKitViewer.PdfViewer;
+using Microsoft.Office.Interop.Word;
 using PDF_Office.EventAggregators;
 using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
 using Prism.Commands;
@@ -343,7 +344,7 @@ namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
 
         public void OnNavigatedTo(NavigationContext navigationContext)
         {
-            DefaultLabel = "(column, row) 1.pdf "+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
+            DefaultLabel = "(Column, Row) -Page -xxx.pdf - yyyy/mm/dd hh-mm-ss";
             navigationContext.Parameters.TryGetValue<string>("Unicode", out Unicode);
             SendPosterInfo();
         }

+ 2 - 1
PDF Office/ViewModels/Dialog/ToolsDialogs/SaftyDialogs/CheckPasswordDialogViewModel.cs

@@ -147,7 +147,8 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
         private void Cancel()
         {
             var dialogResult = new DialogResult(ButtonResult.Cancel);
-            dialogResult.Parameters.Add("CheckPassword", false);
+            dialogResult.Parameters.Add(ParameterNames.Password, null);
+            dialogResult.Parameters.Add(ParameterNames.PasswordResult, false);
             RequestClose.Invoke(dialogResult);
         }
 

+ 19 - 2
PDF Office/ViewModels/Tools/ToolsBarContentViewModel.cs

@@ -3,6 +3,7 @@ using PDF_Office.CustomControl;
 using PDF_Office.EventAggregators;
 using PDF_Office.Helper;
 using PDF_Office.Model;
+using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
 using Prism.Commands;
 using Prism.Events;
 using Prism.Mvvm;
@@ -77,8 +78,16 @@ namespace PDF_Office.ViewModels.Tools
 
         private void OpenSetPasswordDialog()
         {
-            if (SecurityHelper.VerifyPassword(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs))
+            VerifyPasswordResult result = SecurityHelper.VerifyPasswordForSelectedPermissions(PDFViewer.Document, EnumPermissionsSet.StatusAllowsPrinting|EnumPermissionsSet.StatusAllowsHighQualityPrinting, dialogs);
+            if (result.IsDiscryptied)
             {
+                if (result.Password != null)
+                {
+                    string filePath = PDFViewer.Document.FilePath;
+                    PDFViewer.Document.Release();
+                    PDFViewer.InitDocument(filePath);
+                    PDFViewer.Document.UnlockWithPassword(result.Password);
+                }
                 DialogParameters value = new DialogParameters();
                 value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
                 dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e => { });
@@ -93,8 +102,16 @@ namespace PDF_Office.ViewModels.Tools
             }
             else
             {
-                if (SecurityHelper.VerifyPassword(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs))
+                VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
+                if (result.IsDiscryptied)
                 {
+                    if (result.Password != null)
+                    {
+                        string filePath = PDFViewer.Document.FilePath;
+                        PDFViewer.Document.Release();
+                        PDFViewer.InitDocument(filePath);
+                        PDFViewer.Document.UnlockWithPassword(result.Password);
+                    }
                     DialogParameters value = new DialogParameters();
                     value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
                     dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });