Ver Fonte

安全 - 本路径保存、鉴权提权

liuaoran há 1 ano atrás
pai
commit
4fdcc9c915

+ 15 - 15
PDF Office/Helper/SecurityHelper.cs

@@ -159,25 +159,25 @@ namespace PDF_Office.Helper
             {
                 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);
+                DialogParameters value = new DialogParameters();
+                if (CheckHaveSelectedPermissions(document, enumPermissionsSet))
+                {
+                    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);
+                });
             }
-            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;
         }
     }

+ 19 - 2
PDF Office/Model/Dialog/ToolsDialogs/SaftyDialogs/CheckPasswordDialogModel.cs

@@ -1,4 +1,5 @@
-using System;
+using ComPDFKit.PDFDocument;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -8,7 +9,23 @@ namespace PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs
 {
     public class CheckPasswordDialogModel
     {
+        static public Dictionary<EnumPermissionsSet, bool> InitHavePermissionsInfo(CPDFDocument document)
+        {
+            Dictionary<EnumPermissionsSet, bool> GetHavePermissionsFromPermissionsSet = new Dictionary<EnumPermissionsSet, bool>();
+            GetHavePermissionsFromPermissionsSet.Clear();
+            GetHavePermissionsFromPermissionsSet.Add(EnumPermissionsSet.StatusAllowsDocumentChanges, document.GetPermissionsInfo().AllowsDocumentChanges);
+            GetHavePermissionsFromPermissionsSet.Add(EnumPermissionsSet.StatusAllowsPrinting, document.GetPermissionsInfo().AllowsPrinting);
+            GetHavePermissionsFromPermissionsSet.Add(EnumPermissionsSet.StatusAllowsHighQualityPrinting, document.GetPermissionsInfo().AllowsHighQualityPrinting);
+            GetHavePermissionsFromPermissionsSet.Add(EnumPermissionsSet.StatusAllowsCopying, document.GetPermissionsInfo().AllowsCopying);
+            GetHavePermissionsFromPermissionsSet.Add(EnumPermissionsSet.StatusAllowsDocumentAssembly, document.GetPermissionsInfo().AllowsDocumentAssembly);
+            GetHavePermissionsFromPermissionsSet.Add(EnumPermissionsSet.StatusAllowsFormFieldEntry, document.GetPermissionsInfo().AllowsFormFieldEntry);
+            GetHavePermissionsFromPermissionsSet.Add(EnumPermissionsSet.StatusAllowsCommenting, document.GetPermissionsInfo().AllowsCommenting);
+            return GetHavePermissionsFromPermissionsSet;
+        }
     }
+
+
+
     public enum EnumPermissionsSet
     {
         None = 0,
@@ -20,5 +37,5 @@ namespace PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs
         StatusAllowsFormFieldEntry  = 1<<6,
         StatusAllowsCommenting = 1<<7
     }
-    
+
 }

+ 8 - 4
PDF Office/ViewModels/Dialog/HomePageToolsDialogs/HomePagePrinter/HomePagePrinterDialogViewModel.cs

@@ -1024,10 +1024,14 @@ namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
                 if (result.Password != null)
                 {
                     string filePath = PDFViewer.Document.FilePath;
-                    PDFViewer.CloseDocument();
-                    PDFViewer.InitDocument(filePath);
-                    PDFViewer.Document.UnlockWithPassword(result.Password);
-                    PDFViewer.Load();
+                    if (PDFViewer.Document.IsLocked)
+                    {
+                        PDFViewer.Document.UnlockWithPassword(result.Password);
+                    }
+                    else
+                    {
+                        PDFViewer.Document.CheckOwnerPassword(result.Password);
+                    }
                 }
                 ///TODO:
                 ///此处填入需要执行的代码

+ 3 - 0
PDF Office/ViewModels/Dialog/ToolsDialogs/SaftyDialogs/CheckPasswordDialogViewModel.cs

@@ -1,10 +1,13 @@
 using ComPDFKit.PDFDocument;
+using Microsoft.Office.Interop.Word;
 using PDF_Office.Helper;
 using PDF_Office.Model;
+using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
 using Prism.Commands;
 using Prism.Mvvm;
 using Prism.Services.Dialogs;
 using System;
+using System.Collections.Generic;
 using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
 
 using Visibility = System.Windows.Visibility;

+ 39 - 32
PDF Office/ViewModels/ViewContentViewModel.cs

@@ -1992,43 +1992,45 @@ namespace PDF_Office.ViewModels
                     {
                         string currentPath = PDFViewer.Document.FilePath;
                         string tempPath = PDFViewer.Document.FilePath + ".temp.pdf";
-                        PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
-
-                        PDFViewer.Document.WriteToFilePath(tempPath);
-                        PDFViewer.CloseDocument();
-                        PDFViewer.InitDocument(tempPath);
-                        if (!string.IsNullOrEmpty(permissionsPassword))
+                        if (PDFViewer.Document.Descrypt(tempPath))
                         {
-                            PDFViewer.Document.UnlockWithPassword(permissionsPassword);
+                            PDFViewer.CloseDocument();
+                            PDFViewer.InitDocument(tempPath);
+                            PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
+                            if (PDFViewer.Document.WriteToFilePath(currentPath))
+                            {
+                                PDFViewer.CloseDocument();
+                                PDFViewer.InitDocument(currentPath);
+                                if (!string.IsNullOrEmpty(openPassword))
+                                {
+                                    PDFViewer.Document.UnlockWithPassword(openPassword);
+                                }
+
+                                if (!string.IsNullOrEmpty(permissionsPassword))
+                                {
+                                    PDFViewer.Document.CheckOwnerPassword(permissionsPassword);
+                                }
+                                try
+                                {
+                                    PDFViewer.Load();
+                                }
+                                catch
+                                {
+                                }
+                                PDFViewer.UndoManager.CanSave = false;
+                                SecurityInfo.IsPasswordChanged = false;
+                            }
+                            else
+                            {
+                                return false;
+                            }
+                            System.IO.File.Delete(tempPath);
                         }
-                        else if (!string.IsNullOrEmpty(openPassword))
+                        else
                         {
-                            PDFViewer.Document.UnlockWithPassword(openPassword);
+                            return false;
                         }
-                        PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
-
-                        PDFViewer.Document.WriteToFilePath(currentPath);
-                        PDFViewer.CloseDocument();
-                        PDFViewer.InitDocument(currentPath);
-                        System.IO.File.Delete(tempPath);
 
-                        if (!string.IsNullOrEmpty(permissionsPassword))
-                        {
-                            PDFViewer.Document.UnlockWithPassword(permissionsPassword);
-                        }
-                        else if (!string.IsNullOrEmpty(openPassword))
-                        {
-                            PDFViewer.Document.UnlockWithPassword(openPassword);
-                        }
-                        try
-                        {
-                            PDFViewer.Load();
-                        }
-                        catch
-                        {
-                        }
-                        PDFViewer.UndoManager.CanSave = false;
-                        SecurityInfo.IsPasswordChanged = false;
                         return true;
                     }
                     catch
@@ -2120,6 +2122,10 @@ namespace PDF_Office.ViewModels
                 return result;
             }
             catch { return false; }
+            finally
+            {
+                ShowLeftTip(false);
+            }
         }
 
         /// <summary>
@@ -2145,6 +2151,7 @@ namespace PDF_Office.ViewModels
                 }
                 else
                 {
+
                     result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
                     if (result)
                     {