comphelper/source/misc/lok.cxx              |   27 +++++++++++++++++++++++++++
 desktop/qa/desktop_lib/test_desktop_lib.cxx |    6 ++++--
 desktop/source/lib/init.cxx                 |   21 +++++++++++++++++++++
 include/LibreOfficeKit/LibreOfficeKit.h     |    9 +++++++++
 include/LibreOfficeKit/LibreOfficeKit.hxx   |   10 ++++++++++
 include/comphelper/lok.hxx                  |    4 ++++
 include/sfx2/lokhelper.hxx                  |    2 ++
 include/sfx2/viewsh.hxx                     |    5 +++++
 sfx2/source/control/unoctitm.cxx            |   17 +++++++++++++++++
 sfx2/source/view/lokhelper.cxx              |    7 +++++++
 sfx2/source/view/viewsh.cxx                 |    1 +
 11 files changed, 107 insertions(+), 2 deletions(-)

New commits:
commit 5393f9bf41f4a35385526205bb51f9b29bf8c20d
Author:     Pranam Lashkari <[email protected]>
AuthorDate: Mon Sep 13 23:05:40 2021 +0530
Commit:     Tor Lillqvist <[email protected]>
CommitDate: Mon Oct 18 14:03:29 2021 +0200

    LOK: introduce way to restrict uno commands
    
    With this new API we can define which uno commands to restrict their 
functionality
    
    Change-Id: I9f3fd659d373e56542c5323922a53564f1cfb27b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122049
    Tested-by: Jenkins CollaboraOffice <[email protected]>
    Reviewed-by: Tor Lillqvist <[email protected]>

diff --git a/comphelper/source/misc/lok.cxx b/comphelper/source/misc/lok.cxx
index a7b77ea0a3ae..659765f802da 100644
--- a/comphelper/source/misc/lok.cxx
+++ b/comphelper/source/misc/lok.cxx
@@ -37,6 +37,8 @@ static Compat g_eCompatFlags(Compat::none);
 
 static std::vector<OUString> g_vFreemiumDenyList;
 
+static std::vector<OUString> g_vRestrictedCommandList;
+
 namespace
 {
 
@@ -312,6 +314,31 @@ bool isCommandFreemiumDenied(const OUString& command)
     return std::find(g_vFreemiumDenyList.begin(), g_vFreemiumDenyList.end(), 
command) != g_vFreemiumDenyList.end();
 }
 
+void setRestrictedCommandList(const char* restrictedCommandList)
+{
+    if(!g_vRestrictedCommandList.empty())
+        return;
+
+    OUString RestrictedListString(restrictedCommandList, 
strlen(restrictedCommandList), RTL_TEXTENCODING_UTF8);
+
+    OUString command = RestrictedListString.getToken(0, ' ');
+    for (size_t i = 1; !command.isEmpty(); i++)
+    {
+        g_vRestrictedCommandList.emplace_back(command);
+        command = RestrictedListString.getToken(i, ' ');
+    }
+}
+
+const std::vector<OUString>& getRestrictedCommandList()
+{
+    return g_vRestrictedCommandList;
+}
+
+bool isRestrictedCommand(const OUString& command)
+{
+    return std::find(g_vRestrictedCommandList.begin(), 
g_vRestrictedCommandList.end(), command) != g_vRestrictedCommandList.end();
+}
+
 } // namespace
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx 
b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 13c6c6938c0a..e66ebb159cb3 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -3549,11 +3549,13 @@ void DesktopLOKTest::testABI()
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(61), offsetof(struct 
_LibreOfficeKitDocumentClass, sendFormFieldEvent));
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(62), offsetof(struct 
_LibreOfficeKitDocumentClass, setFreemiumDenyList));
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(63), offsetof(struct 
_LibreOfficeKitDocumentClass, setFreemiumView));
-    CPPUNIT_ASSERT_EQUAL(documentClassOffset(64), offsetof(struct 
_LibreOfficeKitDocumentClass, renderSearchResult));
+    CPPUNIT_ASSERT_EQUAL(documentClassOffset(64), offsetof(struct 
_LibreOfficeKitDocumentClass, setRestrictedCommandList));
+    CPPUNIT_ASSERT_EQUAL(documentClassOffset(65), offsetof(struct 
_LibreOfficeKitDocumentClass, setRestrictedView));
+    CPPUNIT_ASSERT_EQUAL(documentClassOffset(66), offsetof(struct 
_LibreOfficeKitDocumentClass, renderSearchResult));
 
     // Extending is fine, update this, and add new assert for the offsetof the
     // new method
-    CPPUNIT_ASSERT_EQUAL(documentClassOffset(65), sizeof(struct 
_LibreOfficeKitDocumentClass));
+    CPPUNIT_ASSERT_EQUAL(documentClassOffset(67), sizeof(struct 
_LibreOfficeKitDocumentClass));
 }
 
 CPPUNIT_TEST_SUITE_REGISTRATION(DesktopLOKTest);
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 2f490ce2b5af..93eaa3e78f42 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -1083,6 +1083,13 @@ static void doc_setFreemiumView(LibreOfficeKitDocument* 
pThis,
                                 int nViewId,
                                 bool isFreemium);
 
+static void doc_setRestrictedCommandList(LibreOfficeKitDocument* pThis,
+                                    const char* restrictedCommandList);
+
+static void doc_setRestrictedView(LibreOfficeKitDocument* pThis,
+                                int nViewId,
+                                bool isRestricted);
+
 static void doc_postWindowExtTextInputEvent(LibreOfficeKitDocument* pThis,
                                             unsigned nWindowId,
                                             int nType,
@@ -1376,6 +1383,9 @@ LibLODocument_Impl::LibLODocument_Impl(const 
uno::Reference <css::lang::XCompone
         m_pDocumentClass->setFreemiumDenyList = doc_setFreemiumDenyList;
         m_pDocumentClass->setFreemiumView = doc_setFreemiumView;
 
+        m_pDocumentClass->setRestrictedCommandList = 
doc_setRestrictedCommandList;
+        m_pDocumentClass->setRestrictedView = doc_setRestrictedView;
+
         gDocumentClass = m_pDocumentClass;
     }
     pClass = m_pDocumentClass.get();
@@ -3691,6 +3701,17 @@ static void doc_setFreemiumView(LibreOfficeKitDocument* 
/*pThis*/, int nViewId,
     SfxLokHelper::setFreemiumView(nViewId, isFreemium);
 }
 
+static void doc_setRestrictedCommandList(LibreOfficeKitDocument* /*pThis*/, 
const char* restrictedCommandList)
+{
+    
comphelper::LibreOfficeKit::setRestrictedCommandList(restrictedCommandList);
+}
+
+static void doc_setRestrictedView(LibreOfficeKitDocument* /*pThis*/, int 
nViewId, bool isRestricted)
+{
+    SolarMutexGuard aGuard;
+    SfxLokHelper::setRestrictedView(nViewId, isRestricted);
+}
+
 static void doc_postWindowExtTextInputEvent(LibreOfficeKitDocument* pThis, 
unsigned nWindowId, int nType, const char* pText)
 {
     comphelper::ProfileZone aZone("doc_postWindowExtTextInputEvent");
diff --git a/include/LibreOfficeKit/LibreOfficeKit.h 
b/include/LibreOfficeKit/LibreOfficeKit.h
index b6073feaeeeb..6a94583b8bf3 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.h
+++ b/include/LibreOfficeKit/LibreOfficeKit.h
@@ -467,6 +467,15 @@ struct _LibreOfficeKitDocumentClass
                             int nViewId,
                             bool isFreemium);
 
+    /// @see lok::Document::setRestrictedCommandList
+    void (*setRestrictedCommandList) (LibreOfficeKitDocument* pThis,
+                                const char* restrictedCommandList);
+
+    /// @see lok::Document::setRestrictedView
+    void (*setRestrictedView) (LibreOfficeKitDocument* pThis,
+                            int nViewId,
+                            bool isRestricted);
+
     /// @see lok::Document::renderSearchResult
     bool (*renderSearchResult) (LibreOfficeKitDocument* pThis,
                                 const char* pSearchResult,
diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx 
b/include/LibreOfficeKit/LibreOfficeKit.hxx
index ded1cc3da6a9..14170ed361a7 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.hxx
+++ b/include/LibreOfficeKit/LibreOfficeKit.hxx
@@ -797,6 +797,16 @@ public:
         mpDoc->pClass->setFreemiumView(mpDoc, nViewId, isFreemium);
     }
 
+    void setRestrictedCommandList(const char* restrictedCommandList)
+    {
+        mpDoc->pClass->setRestrictedCommandList(mpDoc, restrictedCommandList);
+    }
+
+    void setRestrictedView(int nViewId, bool isRestricted)
+    {
+        mpDoc->pClass->setRestrictedView(mpDoc, nViewId, isRestricted);
+    }
+
     /**
      * Render input search result to a bitmap buffer.
      *
diff --git a/include/comphelper/lok.hxx b/include/comphelper/lok.hxx
index d68fabb2e600..847c56012565 100644
--- a/include/comphelper/lok.hxx
+++ b/include/comphelper/lok.hxx
@@ -113,6 +113,10 @@ COMPHELPER_DLLPUBLIC void statusIndicatorFinish();
 COMPHELPER_DLLPUBLIC void setFreemiumDenyList(const char* freemiumDenyList);
 COMPHELPER_DLLPUBLIC const std::vector<OUString>& getFreemiumDenyList();
 COMPHELPER_DLLPUBLIC bool isCommandFreemiumDenied(const OUString& command);
+
+COMPHELPER_DLLPUBLIC void setRestrictedCommandList(const char* 
restrictedCommandList);
+COMPHELPER_DLLPUBLIC const std::vector<OUString>& getRestrictedCommandList();
+COMPHELPER_DLLPUBLIC bool isRestrictedCommand(const OUString& command);
 }
 
 #endif // INCLUDED_COMPHELPER_LOK_HXX
diff --git a/include/sfx2/lokhelper.hxx b/include/sfx2/lokhelper.hxx
index 390b024757cf..76f822a1af5f 100644
--- a/include/sfx2/lokhelper.hxx
+++ b/include/sfx2/lokhelper.hxx
@@ -64,6 +64,8 @@ public:
     static bool getViewIds(int nDocId, int* pArray, size_t nSize);
     /// Set View Freemium
     static void setFreemiumView(int nViewId, bool isFreemium);
+    /// Set View Restricted
+    static void setRestrictedView(int nViewId, bool isRestricted);
     /// Get the document id for a view
     static int getDocumentIdOfView(int nViewId);
     /// Get the default language that should be used for views
diff --git a/include/sfx2/viewsh.hxx b/include/sfx2/viewsh.hxx
index 19cae3d21d12..72091daa2153 100644
--- a/include/sfx2/viewsh.hxx
+++ b/include/sfx2/viewsh.hxx
@@ -166,6 +166,7 @@ friend class SfxPrinterController;
     LanguageTag                 maLOKLocale;
     LOKDeviceFormFactor         maLOKDeviceFormFactor;
     bool                        mbLOKIsFreemiumView;
+    bool                        mbLOKIsRestrictedView;
 
     /// Used to set the DocId at construction time. See SetCurrentDocId.
     static ViewShellDocId       mnCurrentDocId;
@@ -395,6 +396,10 @@ public:
     // Fremium view settings
     void setFreemiumView(bool isFreemium) { mbLOKIsFreemiumView = isFreemium; }
     bool isFreemiumView() { return mbLOKIsFreemiumView; }
+
+    // Restricted view setting
+    void setRestrictedView(bool isRestricted) { mbLOKIsRestrictedView = 
isRestricted; }
+    bool isRestrictedView() { return mbLOKIsRestrictedView; }
 };
 
 
diff --git a/sfx2/source/control/unoctitm.cxx b/sfx2/source/control/unoctitm.cxx
index 752bdf99ead1..fe52ed3ff9fb 100644
--- a/sfx2/source/control/unoctitm.cxx
+++ b/sfx2/source/control/unoctitm.cxx
@@ -662,6 +662,23 @@ void SfxDispatchController_Impl::dispatch( const 
css::util::URL& aURL,
         return;
     }
 
+    if (comphelper::LibreOfficeKit::isActive() &&
+        SfxViewShell::Current()->isRestrictedView() &&
+        comphelper::LibreOfficeKit::isRestrictedCommand(aURL.Complete))
+    {
+        boost::property_tree::ptree aTree;
+        aTree.put("code", "");
+        aTree.put("kind", "restricted");
+        aTree.put("cmd", aURL.Complete);
+        aTree.put("message", "Blocked restricted feature");
+        aTree.put("viewID", SfxViewShell::Current()->GetViewShellId().get());
+
+        std::stringstream aStream;
+        boost::property_tree::write_json(aStream, aTree);
+        
SfxViewShell::Current()->libreOfficeKitViewCallback(LOK_CALLBACK_ERROR, 
aStream.str().c_str());
+        return;
+    }
+
     if (
         !(pDispatch &&
         (
diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index 4f046bebf6eb..2e9be1d80c2f 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -745,6 +745,13 @@ void SfxLokHelper::setFreemiumView(int nViewId, bool 
isFreemium)
         pViewShell->setFreemiumView(isFreemium);
 }
 
+void SfxLokHelper::setRestrictedView(int nViewId, bool isRestricted)
+{
+    SfxViewShell* pViewShell = SfxLokHelper::getViewOfId(nViewId);
+
+    if(pViewShell)
+        pViewShell->setRestrictedView(isRestricted);
+}
 void SfxLokHelper::postExtTextEventAsync(const VclPtr<vcl::Window> &xWindow,
                                          int nType, const OUString &rText)
 {
diff --git a/sfx2/source/view/viewsh.cxx b/sfx2/source/view/viewsh.cxx
index 31b972d2d21a..98ebe7bde3cf 100644
--- a/sfx2/source/view/viewsh.cxx
+++ b/sfx2/source/view/viewsh.cxx
@@ -1078,6 +1078,7 @@ SfxViewShell::SfxViewShell
 ,   maLOKLocale(LANGUAGE_NONE)
 ,   maLOKDeviceFormFactor(LOKDeviceFormFactor::UNKNOWN)
 ,   mbLOKIsFreemiumView(false)
+,   mbLOKIsRestrictedView(false)
 {
     SetMargin( pViewFrame->GetMargin_Impl() );
 

Reply via email to