Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/view/classificationhelper.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 */
9
10
#include <sfx2/classificationhelper.hxx>
11
12
#include <map>
13
#include <algorithm>
14
#include <iterator>
15
16
#include <frozen/bits/defines.h>
17
#include <frozen/bits/elsa_std.h>
18
#include <frozen/unordered_map.h>
19
20
#include <com/sun/star/beans/XPropertyContainer.hpp>
21
#include <com/sun/star/beans/Property.hpp>
22
#include <com/sun/star/beans/XPropertySet.hpp>
23
#include <com/sun/star/document/XDocumentProperties.hpp>
24
#include <com/sun/star/xml/sax/Parser.hpp>
25
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
26
#include <com/sun/star/xml/sax/SAXParseException.hpp>
27
#include <com/sun/star/beans/PropertyAttribute.hpp>
28
29
#include <sal/log.hxx>
30
#include <i18nlangtag/languagetag.hxx>
31
#include <sfx2/infobar.hxx>
32
#include <comphelper/processfactory.hxx>
33
#include <unotools/configmgr.hxx>
34
#include <unotools/pathoptions.hxx>
35
#include <unotools/ucbstreamhelper.hxx>
36
#include <unotools/streamwrap.hxx>
37
#include <cppuhelper/implbase.hxx>
38
#include <sfx2/strings.hrc>
39
#include <sfx2/sfxresid.hxx>
40
#include <sfx2/viewfrm.hxx>
41
#include <tools/datetime.hxx>
42
#include <comphelper/diagnose_ex.hxx>
43
#include <unotools/datetime.hxx>
44
#include <vcl/svapp.hxx>
45
#include <vcl/settings.hxx>
46
#include <vcl/weld/weld.hxx>
47
#include <svl/fstathelper.hxx>
48
49
#include <o3tl/string_view.hxx>
50
#include <officecfg/Office/Common.hxx>
51
52
using namespace com::sun::star;
53
54
namespace
55
{
56
57
const OUString& PROP_BACNAME()
58
0
{
59
0
    static constexpr OUString sProp(u"BusinessAuthorizationCategory:Name"_ustr);
60
0
    return sProp;
61
0
}
62
63
const OUString& PROP_STARTVALIDITY()
64
0
{
65
0
    static constexpr OUString sProp(u"Authorization:StartValidity"_ustr);
66
0
    return sProp;
67
0
}
68
69
const OUString& PROP_NONE()
70
0
{
71
0
    static constexpr OUString sProp(u"None"_ustr);
72
0
    return sProp;
73
0
}
74
75
const OUString& PROP_IMPACTSCALE()
76
0
{
77
0
    static constexpr OUString sProp(u"Impact:Scale"_ustr);
78
0
    return sProp;
79
0
}
80
81
const OUString& PROP_IMPACTLEVEL()
82
0
{
83
0
    static constexpr OUString sProp(u"Impact:Level:Confidentiality"_ustr);
84
0
    return sProp;
85
0
}
86
87
const OUString& PROP_PREFIX_EXPORTCONTROL()
88
0
{
89
0
    static constexpr OUString sProp(u"urn:bails:ExportControl:"_ustr);
90
0
    return sProp;
91
0
}
92
93
const OUString& PROP_PREFIX_NATIONALSECURITY()
94
0
{
95
0
    static constexpr OUString sProp(u"urn:bails:NationalSecurity:"_ustr);
96
0
    return sProp;
97
0
}
98
99
/// Represents one category of a classification policy.
100
class SfxClassificationCategory
101
{
102
public:
103
    /// PROP_BACNAME() is stored separately for easier lookup.
104
    OUString m_aName;
105
    OUString m_aAbbreviatedName; //< An abbreviation to display instead of m_aName.
106
    OUString m_aIdentifier; //< The Identifier of this entry.
107
    size_t m_nConfidentiality; //< 0 is the lowest (least-sensitive).
108
    std::map<OUString, OUString> m_aLabels;
109
};
110
111
/// Parses a policy XML conforming to the TSCP BAF schema.
112
class SfxClassificationParser : public cppu::WeakImplHelper<xml::sax::XDocumentHandler>
113
{
114
public:
115
    std::vector<SfxClassificationCategory> m_aCategories;
116
    std::vector<OUString> m_aMarkings;
117
    std::vector<OUString> m_aIPParts;
118
    std::vector<OUString> m_aIPPartNumbers;
119
120
    OUString m_aPolicyAuthorityName;
121
    bool m_bInPolicyAuthorityName = false;
122
    OUString m_aPolicyName;
123
    bool m_bInPolicyName = false;
124
    OUString m_aProgramID;
125
    bool m_bInProgramID = false;
126
    OUString m_aScale;
127
    bool m_bInScale = false;
128
    OUString m_aConfidentalityValue;
129
    bool m_bInConfidentalityValue = false;
130
    OUString m_aIdentifier;
131
    bool m_bInIdentifier = false;
132
    OUString m_aValue;
133
    bool m_bInValue = false;
134
135
    /// Pointer to a value in m_aCategories, the currently parsed category.
136
    SfxClassificationCategory* m_pCategory = nullptr;
137
138
    SfxClassificationParser();
139
140
    void SAL_CALL startDocument() override;
141
142
    void SAL_CALL endDocument() override;
143
144
    void SAL_CALL startElement(const OUString& rName, const uno::Reference<xml::sax::XAttributeList>& xAttribs) override;
145
146
    void SAL_CALL endElement(const OUString& rName) override;
147
148
    void SAL_CALL characters(const OUString& rChars) override;
149
150
    void SAL_CALL ignorableWhitespace(const OUString& rWhitespaces) override;
151
152
    void SAL_CALL processingInstruction(const OUString& rTarget, const OUString& rData) override;
153
154
    void SAL_CALL setDocumentLocator(const uno::Reference<xml::sax::XLocator>& xLocator) override;
155
};
156
157
0
SfxClassificationParser::SfxClassificationParser() = default;
158
159
void SAL_CALL SfxClassificationParser::startDocument()
160
0
{
161
0
}
162
163
void SAL_CALL SfxClassificationParser::endDocument()
164
0
{
165
0
}
166
167
void SAL_CALL SfxClassificationParser::startElement(const OUString& rName, const uno::Reference<xml::sax::XAttributeList>& xAttribs)
168
0
{
169
0
    if (rName == "baf:PolicyAuthorityName")
170
0
    {
171
0
        m_aPolicyAuthorityName.clear();
172
0
        m_bInPolicyAuthorityName = true;
173
0
    }
174
0
    else if (rName == "baf:PolicyName")
175
0
    {
176
0
        m_aPolicyName.clear();
177
0
        m_bInPolicyName = true;
178
0
    }
179
0
    else if (rName == "baf:ProgramID")
180
0
    {
181
0
        m_aProgramID.clear();
182
0
        m_bInProgramID = true;
183
0
    }
184
0
    else if (rName == "baf:BusinessAuthorizationCategory")
185
0
    {
186
0
        const OUString aName = xAttribs->getValueByName(u"Name"_ustr);
187
0
        if (!m_pCategory && !aName.isEmpty())
188
0
        {
189
0
            OUString aIdentifier = xAttribs->getValueByName(u"Identifier"_ustr);
190
191
            // Create a new category and initialize it with the data that's true for all categories.
192
0
            m_aCategories.emplace_back();
193
0
            SfxClassificationCategory& rCategory = m_aCategories.back();
194
195
0
            rCategory.m_aName = aName;
196
            // Set the abbreviated name, if any, otherwise fallback on the full name.
197
0
            const OUString aAbbreviatedName = xAttribs->getValueByName(u"loextAbbreviatedName"_ustr);
198
0
            rCategory.m_aAbbreviatedName = !aAbbreviatedName.isEmpty() ? aAbbreviatedName : aName;
199
0
            rCategory.m_aIdentifier = aIdentifier;
200
201
0
            rCategory.m_aLabels[u"PolicyAuthority:Name"_ustr] = m_aPolicyAuthorityName;
202
0
            rCategory.m_aLabels[u"Policy:Name"_ustr] = m_aPolicyName;
203
0
            rCategory.m_aLabels[u"BusinessAuthorization:Identifier"_ustr] = m_aProgramID;
204
0
            rCategory.m_aLabels[u"BusinessAuthorizationCategory:Identifier"_ustr] = aIdentifier;
205
206
            // Also initialize defaults.
207
0
            rCategory.m_aLabels[u"PolicyAuthority:Identifier"_ustr] = PROP_NONE();
208
0
            rCategory.m_aLabels[u"PolicyAuthority:Country"_ustr] = PROP_NONE();
209
0
            rCategory.m_aLabels[u"Policy:Identifier"_ustr] = PROP_NONE();
210
0
            rCategory.m_aLabels[u"BusinessAuthorization:Name"_ustr] = PROP_NONE();
211
0
            rCategory.m_aLabels[u"BusinessAuthorization:Locator"_ustr] = PROP_NONE();
212
0
            rCategory.m_aLabels[u"BusinessAuthorizationCategory:Identifier:OID"_ustr] = PROP_NONE();
213
0
            rCategory.m_aLabels[u"BusinessAuthorizationCategory:Locator"_ustr] = PROP_NONE();
214
0
            rCategory.m_aLabels[u"BusinessAuthorization:Locator"_ustr] = PROP_NONE();
215
0
            rCategory.m_aLabels[u"MarkingPrecedence"_ustr] = PROP_NONE();
216
0
            rCategory.m_aLabels[u"Marking:general-summary"_ustr].clear();
217
0
            rCategory.m_aLabels[u"Marking:general-warning-statement"_ustr].clear();
218
0
            rCategory.m_aLabels[u"Marking:general-warning-statement:ext:2"_ustr].clear();
219
0
            rCategory.m_aLabels[u"Marking:general-warning-statement:ext:3"_ustr].clear();
220
0
            rCategory.m_aLabels[u"Marking:general-warning-statement:ext:4"_ustr].clear();
221
0
            rCategory.m_aLabels[u"Marking:general-distribution-statement"_ustr].clear();
222
0
            rCategory.m_aLabels[u"Marking:general-distribution-statement:ext:2"_ustr].clear();
223
0
            rCategory.m_aLabels[u"Marking:general-distribution-statement:ext:3"_ustr].clear();
224
0
            rCategory.m_aLabels[u"Marking:general-distribution-statement:ext:4"_ustr].clear();
225
0
            rCategory.m_aLabels[SfxClassificationHelper::PROP_DOCHEADER()].clear();
226
0
            rCategory.m_aLabels[SfxClassificationHelper::PROP_DOCFOOTER()].clear();
227
0
            rCategory.m_aLabels[SfxClassificationHelper::PROP_DOCWATERMARK()].clear();
228
0
            rCategory.m_aLabels[u"Marking:email-first-line-of-text"_ustr].clear();
229
0
            rCategory.m_aLabels[u"Marking:email-last-line-of-text"_ustr].clear();
230
0
            rCategory.m_aLabels[u"Marking:email-subject-prefix"_ustr].clear();
231
0
            rCategory.m_aLabels[u"Marking:email-subject-suffix"_ustr].clear();
232
0
            rCategory.m_aLabels[PROP_STARTVALIDITY()] = PROP_NONE();
233
0
            rCategory.m_aLabels[u"Authorization:StopValidity"_ustr] = PROP_NONE();
234
0
            m_pCategory = &rCategory;
235
0
        }
236
0
    }
237
0
    else if (rName == "loext:Marking")
238
0
    {
239
0
        OUString aName = xAttribs->getValueByName(u"Name"_ustr);
240
0
        m_aMarkings.push_back(aName);
241
0
    }
242
0
    else if (rName == "loext:IntellectualPropertyPart")
243
0
    {
244
0
        OUString aName = xAttribs->getValueByName(u"Name"_ustr);
245
0
        m_aIPParts.push_back(aName);
246
0
    }
247
0
    else if (rName == "loext:IntellectualPropertyPartNumber")
248
0
    {
249
0
        OUString aName = xAttribs->getValueByName(u"Name"_ustr);
250
0
        m_aIPPartNumbers.push_back(aName);
251
0
    }
252
0
    else if (rName == "baf:Scale")
253
0
    {
254
0
        m_aScale.clear();
255
0
        m_bInScale = true;
256
0
    }
257
0
    else if (rName == "baf:ConfidentalityValue")
258
0
    {
259
0
        m_aConfidentalityValue.clear();
260
0
        m_bInConfidentalityValue = true;
261
0
    }
262
0
    else if (rName == "baf:Identifier")
263
0
    {
264
0
        m_aIdentifier.clear();
265
0
        m_bInIdentifier = true;
266
0
    }
267
0
    else if (rName == "baf:Value")
268
0
    {
269
0
        m_aValue.clear();
270
0
        m_bInValue = true;
271
0
    }
272
0
}
273
274
void SAL_CALL SfxClassificationParser::endElement(const OUString& rName)
275
0
{
276
0
    if (rName == "baf:PolicyAuthorityName")
277
0
        m_bInPolicyAuthorityName = false;
278
0
    else if (rName == "baf:PolicyName")
279
0
        m_bInPolicyName = false;
280
0
    else if (rName == "baf:ProgramID")
281
0
        m_bInProgramID = false;
282
0
    else if (rName == "baf:BusinessAuthorizationCategory")
283
0
        m_pCategory = nullptr;
284
0
    else if (rName == "baf:Scale")
285
0
    {
286
0
        m_bInScale = false;
287
0
        if (m_pCategory)
288
0
            m_pCategory->m_aLabels[PROP_IMPACTSCALE()] = m_aScale;
289
0
    }
290
0
    else if (rName == "baf:ConfidentalityValue")
291
0
    {
292
0
        m_bInConfidentalityValue = false;
293
0
        if (m_pCategory)
294
0
        {
295
0
            std::map<OUString, OUString>& rLabels = m_pCategory->m_aLabels;
296
0
            rLabels[PROP_IMPACTLEVEL()] = m_aConfidentalityValue;
297
0
            m_pCategory->m_nConfidentiality = m_aConfidentalityValue.toInt32(); // 0-based class sensitivity; 0 is lowest.
298
            // Set the two other type of levels as well, if they're not set
299
            // yet: they're optional in BAF, but not in BAILS.
300
0
            rLabels.try_emplace(u"Impact:Level:Integrity"_ustr, m_aConfidentalityValue);
301
0
            rLabels.try_emplace(u"Impact:Level:Availability"_ustr, m_aConfidentalityValue);
302
0
        }
303
0
    }
304
0
    else if (rName == "baf:Identifier")
305
0
        m_bInIdentifier = false;
306
0
    else if (rName == "baf:Value")
307
0
    {
308
0
        if (m_pCategory)
309
0
        {
310
0
            if (m_aIdentifier == "Document: Header")
311
0
                m_pCategory->m_aLabels[SfxClassificationHelper::PROP_DOCHEADER()] = m_aValue;
312
0
            else if (m_aIdentifier == "Document: Footer")
313
0
                m_pCategory->m_aLabels[SfxClassificationHelper::PROP_DOCFOOTER()] = m_aValue;
314
0
            else if (m_aIdentifier == "Document: Watermark")
315
0
                m_pCategory->m_aLabels[SfxClassificationHelper::PROP_DOCWATERMARK()] = m_aValue;
316
0
        }
317
0
    }
318
0
}
319
320
void SAL_CALL SfxClassificationParser::characters(const OUString& rChars)
321
0
{
322
0
    if (m_bInPolicyAuthorityName)
323
0
        m_aPolicyAuthorityName += rChars;
324
0
    else if (m_bInPolicyName)
325
0
        m_aPolicyName += rChars;
326
0
    else if (m_bInProgramID)
327
0
        m_aProgramID += rChars;
328
0
    else if (m_bInScale)
329
0
        m_aScale += rChars;
330
0
    else if (m_bInConfidentalityValue)
331
0
        m_aConfidentalityValue += rChars;
332
0
    else if (m_bInIdentifier)
333
0
        m_aIdentifier += rChars;
334
0
    else if (m_bInValue)
335
0
        m_aValue += rChars;
336
0
}
337
338
void SAL_CALL SfxClassificationParser::ignorableWhitespace(const OUString& /*rWhitespace*/)
339
0
{
340
0
}
341
342
void SAL_CALL SfxClassificationParser::processingInstruction(const OUString& /*rTarget*/, const OUString& /*rData*/)
343
0
{
344
0
}
345
346
void SAL_CALL SfxClassificationParser::setDocumentLocator(const uno::Reference<xml::sax::XLocator>& /*xLocator*/)
347
0
{
348
0
}
349
350
} // anonymous namespace
351
352
/// Implementation details of SfxClassificationHelper.
353
class SfxClassificationHelper::Impl
354
{
355
public:
356
    /// Selected categories, one category for each policy type.
357
    std::map<SfxClassificationPolicyType, SfxClassificationCategory> m_aCategory;
358
    /// Possible categories of a policy to choose from.
359
    std::vector<SfxClassificationCategory> m_aCategories;
360
    std::vector<OUString> m_aMarkings;
361
    std::vector<OUString> m_aIPParts;
362
    std::vector<OUString> m_aIPPartNumbers;
363
364
    uno::Reference<document::XDocumentProperties> m_xDocumentProperties;
365
366
    bool m_bUseLocalized;
367
368
    explicit Impl(uno::Reference<document::XDocumentProperties> xDocumentProperties, bool bUseLocalized);
369
    void parsePolicy();
370
    /// Synchronize m_aLabels back to the document properties.
371
    void pushToDocumentProperties();
372
    /// Set the classification start date to the system time.
373
    void setStartValidity(SfxClassificationPolicyType eType);
374
};
375
376
SfxClassificationHelper::Impl::Impl(uno::Reference<document::XDocumentProperties> xDocumentProperties, bool bUseLocalized)
377
0
    : m_xDocumentProperties(std::move(xDocumentProperties))
378
0
    , m_bUseLocalized(bUseLocalized)
379
0
{
380
0
    parsePolicy();
381
0
}
382
383
void SfxClassificationHelper::Impl::parsePolicy()
384
0
{
385
0
    const uno::Reference<uno::XComponentContext>& xComponentContext = comphelper::getProcessComponentContext();
386
0
    SvtPathOptions aOptions;
387
0
    OUString aPath = aOptions.GetClassificationPath();
388
389
    // See if there is a localized variant next to the configured XML.
390
0
    OUString aExtension(u".xml"_ustr);
391
0
    if (aPath.endsWith(aExtension) && m_bUseLocalized)
392
0
    {
393
0
        std::u16string_view aBase = aPath.subView(0, aPath.getLength() - aExtension.getLength());
394
0
        const LanguageTag& rLanguageTag = Application::GetSettings().GetLanguageTag();
395
        // Expected format is "<original path>_xx-XX.xml".
396
0
        OUString aLocalized = OUString::Concat(aBase) + "_" + rLanguageTag.getBcp47() + aExtension;
397
0
        if (FStatHelper::IsDocument(aLocalized))
398
0
            aPath = aLocalized;
399
0
    }
400
401
0
    xml::sax::InputSource aParserInput;
402
0
    std::unique_ptr<SvStream> pStream = utl::UcbStreamHelper::CreateStream(aPath, StreamMode::READ);
403
0
    aParserInput.aInputStream.set(new utl::OStreamWrapper(std::move(pStream)));
404
405
0
    uno::Reference<xml::sax::XParser> xParser = xml::sax::Parser::create(xComponentContext);
406
0
    rtl::Reference<SfxClassificationParser> xClassificationParser(new SfxClassificationParser());
407
0
    xParser->setDocumentHandler(xClassificationParser);
408
0
    try
409
0
    {
410
0
        xParser->parseStream(aParserInput);
411
0
    }
412
0
    catch (const xml::sax::SAXParseException&)
413
0
    {
414
0
        TOOLS_WARN_EXCEPTION("sfx.view", "parsePolicy() failed");
415
0
    }
416
0
    m_aCategories = xClassificationParser->m_aCategories;
417
0
    m_aMarkings = xClassificationParser->m_aMarkings;
418
0
    m_aIPParts = xClassificationParser->m_aIPParts;
419
0
    m_aIPPartNumbers = xClassificationParser->m_aIPPartNumbers;
420
0
}
421
422
static bool lcl_containsProperty(const uno::Sequence<beans::Property>& rProperties, std::u16string_view rName)
423
0
{
424
0
    return std::any_of(rProperties.begin(), rProperties.end(), [&](const beans::Property& rProperty)
425
0
    {
426
0
        return rProperty.Name == rName;
427
0
    });
428
0
}
429
430
void SfxClassificationHelper::Impl::setStartValidity(SfxClassificationPolicyType eType)
431
0
{
432
0
    auto itCategory = m_aCategory.find(eType);
433
0
    if (itCategory == m_aCategory.end())
434
0
        return;
435
436
0
    SfxClassificationCategory& rCategory = itCategory->second;
437
0
    auto it = rCategory.m_aLabels.find(policyTypeToString(eType) + PROP_STARTVALIDITY());
438
0
    if (it != rCategory.m_aLabels.end())
439
0
    {
440
0
        if (it->second == PROP_NONE())
441
0
        {
442
            // The policy left the start date unchanged, replace it with the system time.
443
0
            util::DateTime aDateTime = DateTime(DateTime::SYSTEM).GetUNODateTime();
444
0
            it->second = utl::toISO8601(aDateTime);
445
0
        }
446
0
    }
447
0
}
448
449
void SfxClassificationHelper::Impl::pushToDocumentProperties()
450
0
{
451
0
    uno::Reference<beans::XPropertyContainer> xPropertyContainer = m_xDocumentProperties->getUserDefinedProperties();
452
0
    uno::Reference<beans::XPropertySet> xPropertySet(xPropertyContainer, uno::UNO_QUERY);
453
0
    uno::Sequence<beans::Property> aProperties = xPropertySet->getPropertySetInfo()->getProperties();
454
0
    for (auto& rPair : m_aCategory)
455
0
    {
456
0
        SfxClassificationPolicyType eType = rPair.first;
457
0
        SfxClassificationCategory& rCategory = rPair.second;
458
0
        std::map<OUString, OUString> aLabels = rCategory.m_aLabels;
459
0
        aLabels[policyTypeToString(eType) + PROP_BACNAME()] = rCategory.m_aName;
460
0
        for (const auto& rLabel : aLabels)
461
0
        {
462
0
            try
463
0
            {
464
0
                if (lcl_containsProperty(aProperties, rLabel.first))
465
0
                    xPropertySet->setPropertyValue(rLabel.first, uno::Any(rLabel.second));
466
0
                else
467
0
                    xPropertyContainer->addProperty(rLabel.first, beans::PropertyAttribute::REMOVABLE, uno::Any(rLabel.second));
468
0
            }
469
0
            catch (const uno::Exception&)
470
0
            {
471
0
                TOOLS_WARN_EXCEPTION("sfx.view", "pushDocumentProperties() failed for property " << rLabel.first);
472
0
            }
473
0
        }
474
0
    }
475
0
}
476
477
bool SfxClassificationHelper::IsClassified(const uno::Reference<document::XDocumentProperties>& xDocumentProperties)
478
0
{
479
0
    uno::Reference<beans::XPropertyContainer> xPropertyContainer = xDocumentProperties->getUserDefinedProperties();
480
0
    if (!xPropertyContainer.is())
481
0
        return false;
482
483
0
    uno::Reference<beans::XPropertySet> xPropertySet(xPropertyContainer, uno::UNO_QUERY);
484
0
    const uno::Sequence<beans::Property> aProperties = xPropertySet->getPropertySetInfo()->getProperties();
485
0
    for (const beans::Property& rProperty : aProperties)
486
0
    {
487
0
        if (rProperty.Name.startsWith("urn:bails:"))
488
0
            return true;
489
0
    }
490
491
0
    return false;
492
0
}
493
494
SfxClassificationCheckPasteResult SfxClassificationHelper::CheckPaste(const uno::Reference<document::XDocumentProperties>& xSource,
495
        const uno::Reference<document::XDocumentProperties>& xDestination)
496
0
{
497
0
    if (!SfxClassificationHelper::IsClassified(xSource))
498
        // No classification on the source side. Return early, regardless the
499
        // state of the destination side.
500
0
        return SfxClassificationCheckPasteResult::None;
501
502
0
    if (!SfxClassificationHelper::IsClassified(xDestination))
503
0
    {
504
        // Paste from a classified document to a non-classified one -> deny.
505
0
        return SfxClassificationCheckPasteResult::TargetDocNotClassified;
506
0
    }
507
508
    // Remaining case: paste between two classified documents.
509
0
    SfxClassificationHelper aSource(xSource);
510
0
    SfxClassificationHelper aDestination(xDestination);
511
0
    if (aSource.GetImpactScale() != aDestination.GetImpactScale())
512
        // It's possible to compare them if they have the same scale.
513
0
        return SfxClassificationCheckPasteResult::None;
514
515
0
    if (aSource.GetImpactLevel() > aDestination.GetImpactLevel())
516
        // Paste from a doc that has higher classification -> deny.
517
0
        return SfxClassificationCheckPasteResult::DocClassificationTooLow;
518
519
0
    return SfxClassificationCheckPasteResult::None;
520
0
}
521
522
bool SfxClassificationHelper::ShowPasteInfo(SfxClassificationCheckPasteResult eResult)
523
0
{
524
0
    switch (eResult)
525
0
    {
526
0
    case SfxClassificationCheckPasteResult::None:
527
0
    {
528
0
        return true;
529
0
    }
530
0
    break;
531
0
    case SfxClassificationCheckPasteResult::TargetDocNotClassified:
532
0
    {
533
0
        if (!Application::IsHeadlessModeEnabled())
534
0
        {
535
0
            std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(nullptr,
536
0
                                                                     VclMessageType::Info, VclButtonsType::Ok,
537
0
                                                                     SfxResId(STR_TARGET_DOC_NOT_CLASSIFIED)));
538
0
            xBox->run();
539
0
        }
540
0
        return false;
541
0
    }
542
0
    break;
543
0
    case SfxClassificationCheckPasteResult::DocClassificationTooLow:
544
0
    {
545
0
        if (!Application::IsHeadlessModeEnabled())
546
0
        {
547
0
            std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(nullptr,
548
0
                                                                     VclMessageType::Info, VclButtonsType::Ok,
549
0
                                                                     SfxResId(STR_DOC_CLASSIFICATION_TOO_LOW)));
550
0
            xBox->run();
551
0
        }
552
0
        return false;
553
0
    }
554
0
    break;
555
0
    }
556
557
0
    return true;
558
0
}
559
560
SfxClassificationHelper::SfxClassificationHelper(const uno::Reference<document::XDocumentProperties>& xDocumentProperties, bool bUseLocalizedPolicy)
561
0
    : m_pImpl(std::make_unique<Impl>(xDocumentProperties, bUseLocalizedPolicy))
562
0
{
563
0
    if (!xDocumentProperties.is())
564
0
        return;
565
566
0
    uno::Reference<beans::XPropertyContainer> xPropertyContainer = xDocumentProperties->getUserDefinedProperties();
567
0
    if (!xPropertyContainer.is())
568
0
        return;
569
570
0
    uno::Reference<beans::XPropertySet> xPropertySet(xPropertyContainer, uno::UNO_QUERY);
571
0
    const uno::Sequence<beans::Property> aProperties = xPropertySet->getPropertySetInfo()->getProperties();
572
0
    for (const beans::Property& rProperty : aProperties)
573
0
    {
574
0
        if (!rProperty.Name.startsWith("urn:bails:"))
575
0
            continue;
576
577
0
        uno::Any aAny = xPropertySet->getPropertyValue(rProperty.Name);
578
0
        OUString aValue;
579
0
        if (aAny >>= aValue)
580
0
        {
581
0
            SfxClassificationPolicyType eType = stringToPolicyType(rProperty.Name);
582
0
            OUString aPrefix = policyTypeToString(eType);
583
0
            if (!rProperty.Name.startsWith(aPrefix))
584
                // It's a prefix we did not recognize, ignore.
585
0
                continue;
586
587
            //TODO: Support abbreviated names(?)
588
0
            if (rProperty.Name == Concat2View(aPrefix + PROP_BACNAME()))
589
0
                m_pImpl->m_aCategory[eType].m_aName = aValue;
590
0
            else
591
0
                m_pImpl->m_aCategory[eType].m_aLabels[rProperty.Name] = aValue;
592
0
        }
593
0
    }
594
0
}
595
596
0
SfxClassificationHelper::~SfxClassificationHelper() = default;
597
598
std::vector<OUString> const & SfxClassificationHelper::GetMarkings() const
599
0
{
600
0
    return m_pImpl->m_aMarkings;
601
0
}
602
603
std::vector<OUString> const & SfxClassificationHelper::GetIntellectualPropertyParts() const
604
0
{
605
0
    return m_pImpl->m_aIPParts;
606
0
}
607
608
std::vector<OUString> const & SfxClassificationHelper::GetIntellectualPropertyPartNumbers() const
609
0
{
610
0
    return m_pImpl->m_aIPPartNumbers;
611
0
}
612
613
const OUString& SfxClassificationHelper::GetBACName(SfxClassificationPolicyType eType) const
614
0
{
615
0
    return m_pImpl->m_aCategory[eType].m_aName;
616
0
}
617
618
const OUString& SfxClassificationHelper::GetAbbreviatedBACName(const OUString& sFullName)
619
0
{
620
0
    for (const auto& category : m_pImpl->m_aCategories)
621
0
    {
622
0
        if (category.m_aName == sFullName)
623
0
            return category.m_aAbbreviatedName;
624
0
    }
625
626
0
    return sFullName;
627
0
}
628
629
OUString SfxClassificationHelper::GetBACNameForIdentifier(std::u16string_view sIdentifier)
630
0
{
631
0
    if (sIdentifier.empty())
632
0
        return u""_ustr;
633
634
0
    for (const auto& category : m_pImpl->m_aCategories)
635
0
    {
636
0
        if (category.m_aIdentifier == sIdentifier)
637
0
            return category.m_aName;
638
0
    }
639
640
0
    return u""_ustr;
641
0
}
642
643
OUString SfxClassificationHelper::GetHigherClass(const OUString& first, const OUString& second)
644
0
{
645
0
    size_t nFirstConfidentiality = 0;
646
0
    size_t nSecondConfidentiality = 0;
647
0
    for (const auto& category : m_pImpl->m_aCategories)
648
0
    {
649
0
        if (category.m_aName == first)
650
0
            nFirstConfidentiality = category.m_nConfidentiality;
651
0
        if (category.m_aName == second)
652
0
            nSecondConfidentiality = category.m_nConfidentiality;
653
0
    }
654
655
0
    return nFirstConfidentiality >= nSecondConfidentiality ? first : second;
656
0
}
657
658
bool SfxClassificationHelper::HasImpactLevel()
659
0
{
660
0
    auto itCategory = m_pImpl->m_aCategory.find(SfxClassificationPolicyType::IntellectualProperty);
661
0
    if (itCategory == m_pImpl->m_aCategory.end())
662
0
        return false;
663
664
0
    SfxClassificationCategory& rCategory = itCategory->second;
665
0
    auto it = rCategory.m_aLabels.find(PROP_PREFIX_INTELLECTUALPROPERTY() + PROP_IMPACTSCALE());
666
0
    if (it == rCategory.m_aLabels.end())
667
0
        return false;
668
669
0
    it = rCategory.m_aLabels.find(PROP_PREFIX_INTELLECTUALPROPERTY() + PROP_IMPACTLEVEL());
670
0
    return it != rCategory.m_aLabels.end();
671
0
}
672
673
bool SfxClassificationHelper::HasDocumentHeader()
674
0
{
675
0
    auto itCategory = m_pImpl->m_aCategory.find(SfxClassificationPolicyType::IntellectualProperty);
676
0
    if (itCategory == m_pImpl->m_aCategory.end())
677
0
        return false;
678
679
0
    SfxClassificationCategory& rCategory = itCategory->second;
680
0
    auto it = rCategory.m_aLabels.find(PROP_PREFIX_INTELLECTUALPROPERTY() + PROP_DOCHEADER());
681
0
    return it != rCategory.m_aLabels.end() && !it->second.isEmpty();
682
0
}
683
684
bool SfxClassificationHelper::HasDocumentFooter()
685
0
{
686
0
    auto itCategory = m_pImpl->m_aCategory.find(SfxClassificationPolicyType::IntellectualProperty);
687
0
    if (itCategory == m_pImpl->m_aCategory.end())
688
0
        return false;
689
690
0
    SfxClassificationCategory& rCategory = itCategory->second;
691
0
    auto it = rCategory.m_aLabels.find(PROP_PREFIX_INTELLECTUALPROPERTY() + PROP_DOCFOOTER());
692
0
    return it != rCategory.m_aLabels.end() && !it->second.isEmpty();
693
0
}
694
695
InfobarType SfxClassificationHelper::GetImpactLevelType()
696
0
{
697
0
    InfobarType aRet;
698
699
0
    aRet = InfobarType::WARNING;
700
701
0
    auto itCategory = m_pImpl->m_aCategory.find(SfxClassificationPolicyType::IntellectualProperty);
702
0
    if (itCategory == m_pImpl->m_aCategory.end())
703
0
        return aRet;
704
705
0
    SfxClassificationCategory& rCategory = itCategory->second;
706
0
    auto it = rCategory.m_aLabels.find(PROP_PREFIX_INTELLECTUALPROPERTY() + PROP_IMPACTSCALE());
707
0
    if (it == rCategory.m_aLabels.end())
708
0
        return aRet;
709
0
    OUString aScale = it->second;
710
711
0
    it = rCategory.m_aLabels.find(PROP_PREFIX_INTELLECTUALPROPERTY() + PROP_IMPACTLEVEL());
712
0
    if (it == rCategory.m_aLabels.end())
713
0
        return aRet;
714
0
    OUString aLevel = it->second;
715
716
    // The spec defines two valid scale values: FIPS-199 and UK-Cabinet.
717
0
    if (aScale == "UK-Cabinet")
718
0
    {
719
0
        if (aLevel == "0")
720
0
            aRet = InfobarType::SUCCESS;
721
0
        else if (aLevel == "1")
722
0
            aRet = InfobarType::WARNING;
723
0
        else if (aLevel == "2")
724
0
            aRet = InfobarType::WARNING;
725
0
        else if (aLevel == "3")
726
0
            aRet = InfobarType::DANGER;
727
0
    }
728
0
    else if (aScale == "FIPS-199")
729
0
    {
730
0
        if (aLevel == "Low")
731
0
            aRet = InfobarType::SUCCESS;
732
0
        else if (aLevel == "Moderate")
733
0
            aRet = InfobarType::WARNING;
734
0
        else if (aLevel == "High")
735
0
            aRet = InfobarType::DANGER;
736
0
    }
737
0
    return aRet;
738
0
}
739
740
sal_Int32 SfxClassificationHelper::GetImpactLevel()
741
0
{
742
0
    sal_Int32 nRet = -1;
743
744
0
    auto itCategory = m_pImpl->m_aCategory.find(SfxClassificationPolicyType::IntellectualProperty);
745
0
    if (itCategory == m_pImpl->m_aCategory.end())
746
0
        return nRet;
747
748
0
    SfxClassificationCategory& rCategory = itCategory->second;
749
0
    auto it = rCategory.m_aLabels.find(PROP_PREFIX_INTELLECTUALPROPERTY() + PROP_IMPACTSCALE());
750
0
    if (it == rCategory.m_aLabels.end())
751
0
        return nRet;
752
0
    OUString aScale = it->second;
753
754
0
    it = rCategory.m_aLabels.find(PROP_PREFIX_INTELLECTUALPROPERTY() + PROP_IMPACTLEVEL());
755
0
    if (it == rCategory.m_aLabels.end())
756
0
        return nRet;
757
0
    OUString aLevel = it->second;
758
759
0
    if (aScale == "UK-Cabinet")
760
0
    {
761
0
        sal_Int32 nValue = aLevel.toInt32();
762
0
        if (nValue < 0 || nValue > 3)
763
0
            return nRet;
764
0
        nRet = nValue;
765
0
    }
766
0
    else if (aScale == "FIPS-199")
767
0
    {
768
0
        static auto constexpr aValues = frozen::make_unordered_map<std::u16string_view, sal_Int32>(
769
0
        {
770
0
            { u"Low", 0 },
771
0
            { u"Moderate", 1 },
772
0
            { u"High", 2 }
773
0
        });
774
0
        auto itValues = aValues.find(aLevel);
775
0
        if (itValues == aValues.end())
776
0
            return nRet;
777
0
        nRet = itValues->second;
778
0
    }
779
780
0
    return nRet;
781
0
}
782
783
OUString SfxClassificationHelper::GetImpactScale()
784
0
{
785
0
    auto itCategory = m_pImpl->m_aCategory.find(SfxClassificationPolicyType::IntellectualProperty);
786
0
    if (itCategory == m_pImpl->m_aCategory.end())
787
0
        return OUString();
788
789
0
    SfxClassificationCategory& rCategory = itCategory->second;
790
0
    auto it = rCategory.m_aLabels.find(PROP_PREFIX_INTELLECTUALPROPERTY() + PROP_IMPACTSCALE());
791
0
    if (it != rCategory.m_aLabels.end())
792
0
        return it->second;
793
794
0
    return OUString();
795
0
}
796
797
OUString SfxClassificationHelper::GetDocumentWatermark()
798
0
{
799
0
    auto itCategory = m_pImpl->m_aCategory.find(SfxClassificationPolicyType::IntellectualProperty);
800
0
    if (itCategory == m_pImpl->m_aCategory.end())
801
0
        return OUString();
802
803
0
    SfxClassificationCategory& rCategory = itCategory->second;
804
0
    auto it = rCategory.m_aLabels.find(PROP_PREFIX_INTELLECTUALPROPERTY() + PROP_DOCWATERMARK());
805
0
    if (it != rCategory.m_aLabels.end())
806
0
        return it->second;
807
808
0
    return OUString();
809
0
}
810
811
std::vector<OUString> SfxClassificationHelper::GetBACNames()
812
0
{
813
0
    if (m_pImpl->m_aCategories.empty())
814
0
        m_pImpl->parsePolicy();
815
816
0
    std::vector<OUString> aRet;
817
0
    std::transform(m_pImpl->m_aCategories.begin(), m_pImpl->m_aCategories.end(), std::back_inserter(aRet), [](const SfxClassificationCategory& rCategory)
818
0
    {
819
0
        return rCategory.m_aName;
820
0
    });
821
0
    return aRet;
822
0
}
823
824
std::vector<OUString> SfxClassificationHelper::GetBACIdentifiers()
825
0
{
826
0
    if (m_pImpl->m_aCategories.empty())
827
0
        m_pImpl->parsePolicy();
828
829
0
    std::vector<OUString> aRet;
830
0
    std::transform(m_pImpl->m_aCategories.begin(), m_pImpl->m_aCategories.end(), std::back_inserter(aRet), [](const SfxClassificationCategory& rCategory)
831
0
    {
832
0
        return rCategory.m_aIdentifier;
833
0
    });
834
0
    return aRet;
835
0
}
836
837
std::vector<OUString> SfxClassificationHelper::GetAbbreviatedBACNames()
838
0
{
839
0
    if (m_pImpl->m_aCategories.empty())
840
0
        m_pImpl->parsePolicy();
841
842
0
    std::vector<OUString> aRet;
843
0
    std::transform(m_pImpl->m_aCategories.begin(), m_pImpl->m_aCategories.end(), std::back_inserter(aRet), [](const SfxClassificationCategory& rCategory)
844
0
    {
845
0
        return rCategory.m_aAbbreviatedName;
846
0
    });
847
0
    return aRet;
848
0
}
849
850
void SfxClassificationHelper::SetBACName(const OUString& rName, SfxClassificationPolicyType eType)
851
0
{
852
0
    if (m_pImpl->m_aCategories.empty())
853
0
        m_pImpl->parsePolicy();
854
855
0
    auto it = std::find_if(m_pImpl->m_aCategories.begin(), m_pImpl->m_aCategories.end(), [&](const SfxClassificationCategory& rCategory)
856
0
    {
857
0
        return rCategory.m_aName == rName;
858
0
    });
859
0
    if (it == m_pImpl->m_aCategories.end())
860
0
    {
861
0
        SAL_WARN("sfx.view", "'" << rName << "' is not a recognized category name");
862
0
        return;
863
0
    }
864
865
0
    m_pImpl->m_aCategory[eType].m_aName = it->m_aName;
866
0
    m_pImpl->m_aCategory[eType].m_aAbbreviatedName = it->m_aAbbreviatedName;
867
0
    m_pImpl->m_aCategory[eType].m_nConfidentiality = it->m_nConfidentiality;
868
0
    m_pImpl->m_aCategory[eType].m_aLabels.clear();
869
0
    const OUString& rPrefix = policyTypeToString(eType);
870
0
    for (const auto& rLabel : it->m_aLabels)
871
0
        m_pImpl->m_aCategory[eType].m_aLabels[rPrefix + rLabel.first] = rLabel.second;
872
873
0
    m_pImpl->setStartValidity(eType);
874
0
    m_pImpl->pushToDocumentProperties();
875
0
    SfxViewFrame* pViewFrame = SfxViewFrame::Current();
876
0
    if (!pViewFrame)
877
0
        return;
878
879
0
    UpdateInfobar(*pViewFrame);
880
0
}
881
882
void SfxClassificationHelper::UpdateInfobar(SfxViewFrame& rViewFrame)
883
0
{
884
0
    OUString aBACName = GetBACName(SfxClassificationPolicyType::IntellectualProperty);
885
0
    bool bImpactLevel = HasImpactLevel();
886
0
    if (!aBACName.isEmpty() && bImpactLevel)
887
0
    {
888
0
        OUString aMessage = SfxResId(STR_CLASSIFIED_DOCUMENT);
889
0
        aMessage = aMessage.replaceFirst("%1", aBACName);
890
891
0
        rViewFrame.RemoveInfoBar(u"classification");
892
0
        rViewFrame.AppendInfoBar(u"classification"_ustr, u""_ustr, aMessage, GetImpactLevelType());
893
0
    }
894
0
}
895
896
SfxClassificationPolicyType SfxClassificationHelper::stringToPolicyType(std::u16string_view rType)
897
0
{
898
0
    if (o3tl::starts_with(rType, PROP_PREFIX_EXPORTCONTROL()))
899
0
        return SfxClassificationPolicyType::ExportControl;
900
0
    else if (o3tl::starts_with(rType, PROP_PREFIX_NATIONALSECURITY()))
901
0
        return SfxClassificationPolicyType::NationalSecurity;
902
0
    else
903
0
        return SfxClassificationPolicyType::IntellectualProperty;
904
0
}
905
906
const OUString& SfxClassificationHelper::policyTypeToString(SfxClassificationPolicyType eType)
907
4.57k
{
908
4.57k
    switch (eType)
909
4.57k
    {
910
0
    case SfxClassificationPolicyType::ExportControl:
911
0
        return PROP_PREFIX_EXPORTCONTROL();
912
0
    case SfxClassificationPolicyType::NationalSecurity:
913
0
        return PROP_PREFIX_NATIONALSECURITY();
914
4.57k
    case SfxClassificationPolicyType::IntellectualProperty:
915
4.57k
        break;
916
4.57k
    }
917
918
4.57k
    return PROP_PREFIX_INTELLECTUALPROPERTY();
919
4.57k
}
920
921
const OUString& SfxClassificationHelper::PROP_DOCHEADER()
922
0
{
923
0
    static constexpr OUString sProp(u"Marking:document-header"_ustr);
924
0
    return sProp;
925
0
}
926
927
const OUString& SfxClassificationHelper::PROP_DOCFOOTER()
928
0
{
929
0
    static constexpr OUString sProp(u"Marking:document-footer"_ustr);
930
0
    return sProp;
931
0
}
932
933
const OUString& SfxClassificationHelper::PROP_DOCWATERMARK()
934
0
{
935
0
    static constexpr OUString sProp(u"Marking:document-watermark"_ustr);
936
0
    return sProp;
937
0
}
938
939
const OUString& SfxClassificationHelper::PROP_PREFIX_INTELLECTUALPROPERTY()
940
4.57k
{
941
4.57k
    static constexpr OUString sProp(u"urn:bails:IntellectualProperty:"_ustr);
942
4.57k
    return sProp;
943
4.57k
}
944
945
SfxClassificationPolicyType SfxClassificationHelper::getPolicyType()
946
4.57k
{
947
4.57k
    if (comphelper::IsFuzzing())
948
4.57k
        return SfxClassificationPolicyType::IntellectualProperty;
949
0
    sal_Int32 nPolicyTypeNumber = officecfg::Office::Common::Classification::Policy::get();
950
0
    auto eType = static_cast<SfxClassificationPolicyType>(nPolicyTypeNumber);
951
0
    return eType;
952
4.57k
}
953
954
namespace sfx
955
{
956
957
namespace
958
{
959
960
OUString getProperty(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer,
961
                     OUString const& rName)
962
0
{
963
0
    try
964
0
    {
965
0
        uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY);
966
0
        return xPropertySet->getPropertyValue(rName).get<OUString>();
967
0
    }
968
0
    catch (const css::uno::Exception&)
969
0
    {
970
0
    }
971
972
0
    return OUString();
973
0
}
974
975
} // end anonymous namespace
976
977
sfx::ClassificationCreationOrigin getCreationOriginProperty(uno::Reference<beans::XPropertyContainer> const & rxPropertyContainer,
978
                                                            sfx::ClassificationKeyCreator const & rKeyCreator)
979
0
{
980
0
    OUString sValue = getProperty(rxPropertyContainer, rKeyCreator.makeCreationOriginKey());
981
0
    if (sValue.isEmpty())
982
0
        return sfx::ClassificationCreationOrigin::NONE;
983
984
0
    return (sValue == "BAF_POLICY")
985
0
                ? sfx::ClassificationCreationOrigin::BAF_POLICY
986
0
                : sfx::ClassificationCreationOrigin::MANUAL;
987
0
}
988
989
}
990
991
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */