desktop/qa/desktop_lib/test_desktop_lib.cxx |    9 +++--
 sw/source/uibase/shells/textfld.cxx         |   45 ++++++++++++++++++++++------
 2 files changed, 41 insertions(+), 13 deletions(-)

New commits:
commit f93d46429c68770dd432fa6e7edcfc876891f8e1
Author:     Jaume Pujantell <jaume.pujant...@collabora.com>
AuthorDate: Thu Aug 29 15:32:01 2024 +0200
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Tue Sep 3 06:40:41 2024 +0200

    lok: sw: restrict deletions on comment only mode
    
    On Writer, when in readonly mode with comments activated restrict
    the hability to delete comments to only those made by the same user.
    
    Change-Id: Iaefb1166c680726c9c73a305ef67be812d3b97d2
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/172596
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx 
b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index cfacc5cc4086..d013de5cf20f 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -2677,10 +2677,11 @@ void DesktopLOKTest::testCommentsInReadOnlyMode()
     // Comments callback are emitted only if tiled annotations are off
     comphelper::LibreOfficeKit::setTiledAnnotations(false);
     LibLODocument_Impl* pDocument = loadDoc("blank_text.odt");
-    pDocument->m_pDocumentClass->initializeForRendering(pDocument, "{}");
+    OString aUserName("LOK User1"_ostr);
+    OString aArguments = "{\".uno:Author\":{\"type\":\"string\",\"value\":\"" 
+ aUserName + "\"}}";
+    pDocument->m_pDocumentClass->initializeForRendering(pDocument, 
aArguments.getStr());
 
-    int viewId = pDocument->m_pDocumentClass->createView(pDocument);
-    pDocument->m_pDocumentClass->setView(pDocument, viewId);
+    int viewId = pDocument->m_pDocumentClass->getView(pDocument);
 
     SfxLokHelper::setViewReadOnly(viewId, true);
     SfxLokHelper::setAllowChangeComments(viewId, true);
@@ -2694,7 +2695,7 @@ void DesktopLOKTest::testCommentsInReadOnlyMode()
     {
         tools::JsonWriter aJson;
         addParameter(aJson, "Text", "string", "Comment");
-        addParameter(aJson, "Author", "string", "LOK User1");
+        addParameter(aJson, "Author", "string", aUserName);
         aCommandArgs = aJson.finishAndGetAsOString();
     }
 
diff --git a/sw/source/uibase/shells/textfld.cxx 
b/sw/source/uibase/shells/textfld.cxx
index 6e3ff742cf20..20f21bb92355 100644
--- a/sw/source/uibase/shells/textfld.cxx
+++ b/sw/source/uibase/shells/textfld.cxx
@@ -114,6 +114,23 @@ static OUString lcl_BuildTitleWithRedline( const 
SwRangeRedline *pRedline )
     return sTitle + SwResId(pResId);
 }
 
+static bool lcl_canUserModifyAnnotation(const SwView& rView, 
std::u16string_view sAuthor)
+{
+    return !comphelper::LibreOfficeKit::isActive() || 
!rView.IsLokReadOnlyView()
+           || sAuthor == rView.GetRedlineAuthor();
+}
+
+static bool lcl_canUserModifyAnnotation(const SwView& rView,
+                                        const sw::annotation::SwAnnotationWin* 
pAnnotationWin)
+{
+    return lcl_canUserModifyAnnotation(rView, pAnnotationWin->GetAuthor());
+}
+
+static bool lcl_canUserModifyAnnotation(const SwView& rView, sal_uInt32 
nPostItId)
+{
+    return lcl_canUserModifyAnnotation(rView, 
rView.GetPostItMgr()->GetAnnotationWin(nPostItId));
+}
+
 void SwTextShell::ExecField(SfxRequest &rReq)
 {
     SwWrtShell& rSh = GetShell();
@@ -385,12 +402,17 @@ void SwTextShell::ExecField(SfxRequest &rReq)
             const SvxPostItIdItem* pIdItem = 
rReq.GetArg<SvxPostItIdItem>(SID_ATTR_POSTIT_ID);
             if (pIdItem && !pIdItem->GetValue().isEmpty() && 
GetView().GetPostItMgr())
             {
-                
GetView().GetPostItMgr()->Delete(pIdItem->GetValue().toUInt32());
+                sal_uInt32 nPostItId = pIdItem->GetValue().toUInt32();
+                if (lcl_canUserModifyAnnotation(GetView(), nPostItId))
+                    GetView().GetPostItMgr()->Delete(nPostItId);
             }
             else if ( GetView().GetPostItMgr() &&
                       GetView().GetPostItMgr()->HasActiveSidebarWin() )
             {
-                GetView().GetPostItMgr()->DeleteActiveSidebarWin();
+                sw::annotation::SwAnnotationWin* pAnnotationWin
+                    = GetView().GetPostItMgr()->GetActiveSidebarWin();
+                if (lcl_canUserModifyAnnotation(GetView(), pAnnotationWin))
+                    GetView().GetPostItMgr()->DeleteActiveSidebarWin();
             }
             break;
         }
@@ -399,12 +421,16 @@ void SwTextShell::ExecField(SfxRequest &rReq)
             const SvxPostItIdItem* pIdItem = 
rReq.GetArg<SvxPostItIdItem>(SID_ATTR_POSTIT_ID);
             if (pIdItem && !pIdItem->GetValue().isEmpty() && 
GetView().GetPostItMgr())
             {
-                
GetView().GetPostItMgr()->DeleteCommentThread(pIdItem->GetValue().toUInt32());
+                sal_uInt32 nPostItId = pIdItem->GetValue().toUInt32();
+                if (lcl_canUserModifyAnnotation(GetView(), nPostItId))
+                    GetView().GetPostItMgr()->DeleteCommentThread(nPostItId);
             }
-            else if ( GetView().GetPostItMgr() &&
-                        GetView().GetPostItMgr()->HasActiveSidebarWin() )
+            else if (GetView().GetPostItMgr() && 
GetView().GetPostItMgr()->HasActiveSidebarWin())
             {
-                GetView().GetPostItMgr()->DeleteActiveSidebarWin();
+                sw::annotation::SwAnnotationWin* pAnnotationWin
+                    = GetView().GetPostItMgr()->GetActiveSidebarWin();
+                if (lcl_canUserModifyAnnotation(GetView(), pAnnotationWin))
+                    GetView().GetPostItMgr()->DeleteActiveSidebarWin();
             }
             break;
         }
@@ -440,8 +466,9 @@ void SwTextShell::ExecField(SfxRequest &rReq)
         case FN_DELETE_NOTE_AUTHOR:
         {
             const SfxStringItem* pNoteItem = rReq.GetArg<SfxStringItem>(nSlot);
-            if ( pNoteItem && GetView().GetPostItMgr() )
-                GetView().GetPostItMgr()->Delete( pNoteItem->GetValue() );
+            if (pNoteItem && GetView().GetPostItMgr()
+                && lcl_canUserModifyAnnotation(GetView(), 
pNoteItem->GetValue()))
+                GetView().GetPostItMgr()->Delete(pNoteItem->GetValue());
         }
         break;
         case FN_HIDE_NOTE:
@@ -512,7 +539,7 @@ void SwTextShell::ExecField(SfxRequest &rReq)
                     sText = pTextItem->GetValue();
 
                 sw::annotation::SwAnnotationWin* pAnnotationWin = 
GetView().GetPostItMgr()->GetAnnotationWin(pIdItem->GetValue().toUInt32());
-                if (pAnnotationWin)
+                if (pAnnotationWin && lcl_canUserModifyAnnotation(GetView(), 
pAnnotationWin))
                 {
                     pAnnotationWin->UpdateText(sText);
 

Reply via email to