Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sc / inc / tabprotection.hxx
blobc174957f1e1845b7cd8558df09e4a8af807d5c21
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SC_INC_TABPROTECTION_HXX
21 #define INCLUDED_SC_INC_TABPROTECTION_HXX
23 #include <sal/types.h>
24 #include <com/sun/star/uno/Sequence.hxx>
26 #include "global.hxx"
27 #include "rangelst.hxx"
28 #include <memory>
30 class ScDocument;
31 class ScTableProtectionImpl;
33 enum ScPasswordHash
35 PASSHASH_SHA1 = 0,
36 PASSHASH_SHA1_UTF8, // tdf#115483 this is UTF8, previous one is wrong UTF16
37 PASSHASH_SHA256,
38 PASSHASH_XL,
39 PASSHASH_UNSPECIFIED
42 /// OOXML password definitions: algorithmName, hashValue, saltValue, spinCount
43 struct ScOoxPasswordHash
45 OUString maAlgorithmName; /// "SHA-512", ...
46 OUString maHashValue; /// base64 encoded hash value
47 OUString maSaltValue; /// base64 encoded salt value
48 sal_uInt32 mnSpinCount; /// spin count, iteration runs
50 ScOoxPasswordHash() : mnSpinCount(0) {}
51 bool hasPassword() const { return !maHashValue.isEmpty(); }
52 void clear()
54 // Keep algorithm and spin count.
55 maHashValue.clear();
56 maSaltValue.clear();
58 bool verifyPassword( const OUString& aPassText ) const;
61 namespace ScPassHashHelper
63 /** Check for the compatibility of all password hashes. If there is at
64 * least one hash that needs to be regenerated, it returns true. If all
65 * hash values are compatible with the specified hash type, then it
66 * returns false. */
67 bool needsPassHashRegen(const ScDocument& rDoc, ScPasswordHash eHash1, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED);
69 OUString getHashURI(ScPasswordHash eHash);
71 ScPasswordHash getHashTypeFromURI(const OUString& rURI);
74 class SAL_NO_VTABLE ScPassHashProtectable
76 public:
77 virtual ~ScPassHashProtectable() = 0;
79 virtual bool isProtected() const = 0;
80 virtual bool isProtectedWithPass() const = 0;
81 virtual void setProtected(bool bProtected) = 0;
83 virtual bool isPasswordEmpty() const = 0;
84 virtual bool hasPasswordHash(ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) const = 0;
85 virtual void setPassword(const OUString& aPassText) = 0;
86 virtual css::uno::Sequence<sal_Int8> getPasswordHash(
87 ScPasswordHash eHash, ScPasswordHash eHas2 = PASSHASH_UNSPECIFIED) const = 0;
88 virtual const ScOoxPasswordHash& getPasswordHash() const = 0;
89 virtual void setPasswordHash(
90 const css::uno::Sequence<sal_Int8>& aPassword,
91 ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) = 0;
92 virtual void setPasswordHash( const OUString& rAlgorithmName, const OUString& rHashValue,
93 const OUString& rSaltValue, sal_uInt32 nSpinCount ) = 0;
94 virtual bool verifyPassword(const OUString& aPassText) const = 0;
97 class SC_DLLPUBLIC ScDocProtection : public ScPassHashProtectable
99 public:
100 enum Option
102 STRUCTURE = 0,
103 WINDOWS,
104 NONE ///< last item - used to resize the vector
107 explicit ScDocProtection();
108 explicit ScDocProtection(const ScDocProtection& r);
109 virtual ~ScDocProtection() override;
111 virtual bool isProtected() const override;
112 virtual bool isProtectedWithPass() const override;
113 virtual void setProtected(bool bProtected) override;
115 virtual bool isPasswordEmpty() const override;
116 virtual bool hasPasswordHash(ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) const override;
117 virtual void setPassword(const OUString& aPassText) override;
118 virtual css::uno::Sequence<sal_Int8> getPasswordHash(
119 ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) const override;
120 virtual const ScOoxPasswordHash& getPasswordHash() const override;
121 virtual void setPasswordHash(
122 const css::uno::Sequence<sal_Int8>& aPassword,
123 ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) override;
124 virtual void setPasswordHash( const OUString& rAlgorithmName, const OUString& rHashValue,
125 const OUString& rSaltValue, sal_uInt32 nSpinCount ) override;
126 virtual bool verifyPassword(const OUString& aPassText) const override;
128 bool isOptionEnabled(Option eOption) const;
129 void setOption(Option eOption, bool bEnabled);
131 private:
132 std::unique_ptr<ScTableProtectionImpl> mpImpl;
135 /** Container for the Excel EnhancedProtection feature.
137 struct ScEnhancedProtection
139 ScRangeListRef maRangeList;
140 sal_uInt32 mnAreserved;
141 sal_uInt32 mnPasswordVerifier;
142 OUString maTitle;
143 ::std::vector< sal_uInt8 > maSecurityDescriptor; // imported as raw BIFF data
144 OUString maSecurityDescriptorXML; // imported from OOXML
145 ScOoxPasswordHash maPasswordHash;
147 ScEnhancedProtection() : mnAreserved(0), mnPasswordVerifier(0) {}
149 bool hasSecurityDescriptor() const
151 return !maSecurityDescriptor.empty() || !maSecurityDescriptorXML.isEmpty();
154 bool hasPassword() const
156 return mnPasswordVerifier != 0 || maPasswordHash.hasPassword();
160 /** sheet protection state container
162 * This class stores sheet's protection state: 1) whether the protection
163 * is on, 2) password and/or password hash, and 3) any associated
164 * protection options. This class is also used as a protection state
165 * container for the undo/redo stack, in which case the password, hash and
166 * the options need to be preserved even when the protection flag is
167 * off. */
168 class SC_DLLPUBLIC ScTableProtection : public ScPassHashProtectable
170 public:
171 enum Option
173 AUTOFILTER = 0,
174 DELETE_COLUMNS,
175 DELETE_ROWS,
176 FORMAT_CELLS,
177 FORMAT_COLUMNS,
178 FORMAT_ROWS,
179 INSERT_COLUMNS,
180 INSERT_HYPERLINKS,
181 INSERT_ROWS,
182 OBJECTS,
183 PIVOT_TABLES,
184 SCENARIOS,
185 SELECT_LOCKED_CELLS,
186 SELECT_UNLOCKED_CELLS,
187 SORT,
188 NONE ///< last item - used to resize the vector
191 explicit ScTableProtection();
192 explicit ScTableProtection(const ScTableProtection& r);
193 virtual ~ScTableProtection() override;
195 virtual bool isProtected() const override;
196 virtual bool isProtectedWithPass() const override;
197 virtual void setProtected(bool bProtected) override;
199 virtual bool isPasswordEmpty() const override;
200 virtual bool hasPasswordHash(ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) const override;
201 virtual void setPassword(const OUString& aPassText) override;
202 virtual css::uno::Sequence<sal_Int8> getPasswordHash(
203 ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) const override;
204 virtual const ScOoxPasswordHash& getPasswordHash() const override;
205 virtual void setPasswordHash(
206 const css::uno::Sequence<sal_Int8>& aPassword,
207 ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) override;
208 virtual void setPasswordHash( const OUString& rAlgorithmName, const OUString& rHashValue,
209 const OUString& rSaltValue, sal_uInt32 nSpinCount ) override;
210 virtual bool verifyPassword(const OUString& aPassText) const override;
212 bool isOptionEnabled(Option eOption) const;
213 void setOption(Option eOption, bool bEnabled);
215 void setEnhancedProtection( const ::std::vector< ScEnhancedProtection > & rProt );
216 const ::std::vector< ScEnhancedProtection > & getEnhancedProtection() const;
217 bool updateReference( UpdateRefMode, const ScDocument*, const ScRange& rWhere, SCCOL nDx, SCROW nDy, SCTAB nDz );
218 bool isBlockEditable( const ScRange& rRange ) const;
219 bool isSelectionEditable( const ScRangeList& rRangeList ) const;
221 private:
222 std::unique_ptr<ScTableProtectionImpl> mpImpl;
225 #endif
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */