[9580] | 1 | /*******************************************************************************
|
---|
| 2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
| 3 | *
|
---|
| 4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
| 5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
| 6 | *
|
---|
| 7 | * Copyright (C) 2011 Jan Källman
|
---|
| 8 | *
|
---|
| 9 | * This library is free software; you can redistribute it and/or
|
---|
| 10 | * modify it under the terms of the GNU Lesser General Public
|
---|
| 11 | * License as published by the Free Software Foundation; either
|
---|
| 12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
| 13 |
|
---|
| 14 | * This library is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 17 | * See the GNU Lesser General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
| 20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
| 21 | *
|
---|
| 22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
| 23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
| 24 | *
|
---|
| 25 | * Code change notes:
|
---|
| 26 | *
|
---|
| 27 | * Author Change Date
|
---|
| 28 | * ******************************************************************************
|
---|
| 29 | * Jan Källman Initial Release 2010-03-14
|
---|
| 30 | * Jan Källman License changed GPL-->LGPL 2011-12-27
|
---|
| 31 | *******************************************************************************/
|
---|
| 32 | using System;
|
---|
| 33 | using System.Collections.Generic;
|
---|
| 34 | using System.Text;
|
---|
| 35 | using System.Xml;
|
---|
| 36 | using System.Security.Cryptography;
|
---|
| 37 | using OfficeOpenXml.Utils;
|
---|
| 38 |
|
---|
| 39 | namespace OfficeOpenXml
|
---|
| 40 | {
|
---|
| 41 | /// <summary>
|
---|
| 42 | /// Sheet protection
|
---|
| 43 | ///<seealso cref="ExcelEncryption"/>
|
---|
| 44 | ///<seealso cref="ExcelProtection"/>
|
---|
| 45 | /// </summary>
|
---|
| 46 | public sealed class ExcelSheetProtection : XmlHelper
|
---|
| 47 | {
|
---|
| 48 | internal ExcelSheetProtection (XmlNamespaceManager nsm, XmlNode topNode,ExcelWorksheet ws) :
|
---|
| 49 | base(nsm, topNode)
|
---|
| 50 | {
|
---|
| 51 | SchemaNodeOrder = ws.SchemaNodeOrder;
|
---|
| 52 | }
|
---|
| 53 | private const string _isProtectedPath="d:sheetProtection/@sheet";
|
---|
| 54 | /// <summary>
|
---|
| 55 | /// If the worksheet is protected.
|
---|
| 56 | /// </summary>
|
---|
| 57 | public bool IsProtected
|
---|
| 58 | {
|
---|
| 59 | get
|
---|
| 60 | {
|
---|
| 61 | return GetXmlNodeBool(_isProtectedPath, false);
|
---|
| 62 | }
|
---|
| 63 | set
|
---|
| 64 | {
|
---|
| 65 | SetXmlNodeBool(_isProtectedPath, value, false);
|
---|
| 66 | if (value)
|
---|
| 67 | {
|
---|
| 68 | AllowEditObject = true;
|
---|
| 69 | AllowEditScenarios = true;
|
---|
| 70 | }
|
---|
| 71 | else
|
---|
| 72 | {
|
---|
| 73 | DeleteAllNode(_isProtectedPath); //delete the whole sheetprotection node
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | private const string _allowSelectLockedCellsPath = "d:sheetProtection/@selectLockedCells";
|
---|
| 78 | /// <summary>
|
---|
| 79 | /// Allow users to select locked cells
|
---|
| 80 | /// </summary>
|
---|
| 81 | public bool AllowSelectLockedCells
|
---|
| 82 | {
|
---|
| 83 | get
|
---|
| 84 | {
|
---|
| 85 | return !GetXmlNodeBool(_allowSelectLockedCellsPath, false);
|
---|
| 86 | }
|
---|
| 87 | set
|
---|
| 88 | {
|
---|
| 89 | SetXmlNodeBool(_allowSelectLockedCellsPath, !value, false);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | private const string _allowSelectUnlockedCellsPath = "d:sheetProtection/@selectUnlockedCells";
|
---|
| 93 | /// <summary>
|
---|
| 94 | /// Allow users to select unlocked cells
|
---|
| 95 | /// </summary>
|
---|
| 96 | public bool AllowSelectUnlockedCells
|
---|
| 97 | {
|
---|
| 98 | get
|
---|
| 99 | {
|
---|
| 100 | return !GetXmlNodeBool(_allowSelectUnlockedCellsPath, false);
|
---|
| 101 | }
|
---|
| 102 | set
|
---|
| 103 | {
|
---|
| 104 | SetXmlNodeBool(_allowSelectUnlockedCellsPath, !value, false);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | private const string _allowObjectPath="d:sheetProtection/@objects";
|
---|
| 108 | /// <summary>
|
---|
| 109 | /// Allow users to edit objects
|
---|
| 110 | /// </summary>
|
---|
| 111 | public bool AllowEditObject
|
---|
| 112 | {
|
---|
| 113 | get
|
---|
| 114 | {
|
---|
| 115 | return !GetXmlNodeBool(_allowObjectPath, false);
|
---|
| 116 | }
|
---|
| 117 | set
|
---|
| 118 | {
|
---|
| 119 | SetXmlNodeBool(_allowObjectPath, !value, false);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | private const string _allowScenariosPath="d:sheetProtection/@scenarios";
|
---|
| 123 | /// <summary>
|
---|
| 124 | /// Allow users to edit senarios
|
---|
| 125 | /// </summary>
|
---|
| 126 | public bool AllowEditScenarios
|
---|
| 127 | {
|
---|
| 128 | get
|
---|
| 129 | {
|
---|
| 130 | return !GetXmlNodeBool(_allowScenariosPath, false);
|
---|
| 131 | }
|
---|
| 132 | set
|
---|
| 133 | {
|
---|
| 134 | SetXmlNodeBool(_allowScenariosPath, !value, false);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | private const string _allowFormatCellsPath="d:sheetProtection/@formatCells";
|
---|
| 138 | /// <summary>
|
---|
| 139 | /// Allow users to format cells
|
---|
| 140 | /// </summary>
|
---|
| 141 | public bool AllowFormatCells
|
---|
| 142 | {
|
---|
| 143 | get
|
---|
| 144 | {
|
---|
| 145 | return !GetXmlNodeBool(_allowFormatCellsPath, true);
|
---|
| 146 | }
|
---|
| 147 | set
|
---|
| 148 | {
|
---|
| 149 | SetXmlNodeBool(_allowFormatCellsPath, !value, true );
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | private const string _allowFormatColumnsPath = "d:sheetProtection/@formatColumns";
|
---|
| 153 | /// <summary>
|
---|
| 154 | /// Allow users to Format columns
|
---|
| 155 | /// </summary>
|
---|
| 156 | public bool AllowFormatColumns
|
---|
| 157 | {
|
---|
| 158 | get
|
---|
| 159 | {
|
---|
| 160 | return !GetXmlNodeBool(_allowFormatColumnsPath, true);
|
---|
| 161 | }
|
---|
| 162 | set
|
---|
| 163 | {
|
---|
| 164 | SetXmlNodeBool(_allowFormatColumnsPath, !value, true);
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | private const string _allowFormatRowsPath = "d:sheetProtection/@formatRows";
|
---|
| 168 | /// <summary>
|
---|
| 169 | /// Allow users to Format rows
|
---|
| 170 | /// </summary>
|
---|
| 171 | public bool AllowFormatRows
|
---|
| 172 | {
|
---|
| 173 | get
|
---|
| 174 | {
|
---|
| 175 | return !GetXmlNodeBool(_allowFormatRowsPath, true);
|
---|
| 176 | }
|
---|
| 177 | set
|
---|
| 178 | {
|
---|
| 179 | SetXmlNodeBool(_allowFormatRowsPath, !value, true);
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | private const string _allowInsertColumnsPath = "d:sheetProtection/@insertColumns";
|
---|
| 184 | /// <summary>
|
---|
| 185 | /// Allow users to insert columns
|
---|
| 186 | /// </summary>
|
---|
| 187 | public bool AllowInsertColumns
|
---|
| 188 | {
|
---|
| 189 | get
|
---|
| 190 | {
|
---|
| 191 | return !GetXmlNodeBool(_allowInsertColumnsPath, true);
|
---|
| 192 | }
|
---|
| 193 | set
|
---|
| 194 | {
|
---|
| 195 | SetXmlNodeBool(_allowInsertColumnsPath, !value, true);
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | private const string _allowInsertRowsPath = "d:sheetProtection/@insertRows";
|
---|
| 200 | /// <summary>
|
---|
| 201 | /// Allow users to Format rows
|
---|
| 202 | /// </summary>
|
---|
| 203 | public bool AllowInsertRows
|
---|
| 204 | {
|
---|
| 205 | get
|
---|
| 206 | {
|
---|
| 207 | return !GetXmlNodeBool(_allowInsertRowsPath, true);
|
---|
| 208 | }
|
---|
| 209 | set
|
---|
| 210 | {
|
---|
| 211 | SetXmlNodeBool(_allowInsertRowsPath, !value, true);
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | private const string _allowInsertHyperlinksPath = "d:sheetProtection/@insertHyperlinks";
|
---|
| 215 | /// <summary>
|
---|
| 216 | /// Allow users to insert hyperlinks
|
---|
| 217 | /// </summary>
|
---|
| 218 | public bool AllowInsertHyperlinks
|
---|
| 219 | {
|
---|
| 220 | get
|
---|
| 221 | {
|
---|
| 222 | return !GetXmlNodeBool(_allowInsertHyperlinksPath, true);
|
---|
| 223 | }
|
---|
| 224 | set
|
---|
| 225 | {
|
---|
| 226 | SetXmlNodeBool(_allowInsertHyperlinksPath, !value, true);
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | private const string _allowDeleteColumns = "d:sheetProtection/@deleteColumns";
|
---|
| 230 | /// <summary>
|
---|
| 231 | /// Allow users to delete columns
|
---|
| 232 | /// </summary>
|
---|
| 233 | public bool AllowDeleteColumns
|
---|
| 234 | {
|
---|
| 235 | get
|
---|
| 236 | {
|
---|
| 237 | return !GetXmlNodeBool(_allowDeleteColumns, true);
|
---|
| 238 | }
|
---|
| 239 | set
|
---|
| 240 | {
|
---|
| 241 | SetXmlNodeBool(_allowDeleteColumns, !value, true);
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 | private const string _allowDeleteRowsPath = "d:sheetProtection/@deleteRows";
|
---|
| 245 | /// <summary>
|
---|
| 246 | /// Allow users to delete rows
|
---|
| 247 | /// </summary>
|
---|
| 248 | public bool AllowDeleteRows
|
---|
| 249 | {
|
---|
| 250 | get
|
---|
| 251 | {
|
---|
| 252 | return !GetXmlNodeBool(_allowDeleteRowsPath, true);
|
---|
| 253 | }
|
---|
| 254 | set
|
---|
| 255 | {
|
---|
| 256 | SetXmlNodeBool(_allowDeleteRowsPath, !value, true);
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | private const string _allowSortPath = "d:sheetProtection/@sort";
|
---|
| 261 | /// <summary>
|
---|
| 262 | /// Allow users to sort a range
|
---|
| 263 | /// </summary>
|
---|
| 264 | public bool AllowSort
|
---|
| 265 | {
|
---|
| 266 | get
|
---|
| 267 | {
|
---|
| 268 | return !GetXmlNodeBool(_allowSortPath, true);
|
---|
| 269 | }
|
---|
| 270 | set
|
---|
| 271 | {
|
---|
| 272 | SetXmlNodeBool(_allowSortPath, !value, true);
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | private const string _allowAutoFilterPath = "d:sheetProtection/@autoFilter";
|
---|
| 277 | /// <summary>
|
---|
| 278 | /// Allow users to use autofilters
|
---|
| 279 | /// </summary>
|
---|
| 280 | public bool AllowAutoFilter
|
---|
| 281 | {
|
---|
| 282 | get
|
---|
| 283 | {
|
---|
| 284 | return !GetXmlNodeBool(_allowAutoFilterPath, true);
|
---|
| 285 | }
|
---|
| 286 | set
|
---|
| 287 | {
|
---|
| 288 | SetXmlNodeBool(_allowAutoFilterPath, !value, true);
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
| 291 | private const string _allowPivotTablesPath = "d:sheetProtection/@pivotTables";
|
---|
| 292 | /// <summary>
|
---|
| 293 | /// Allow users to use pivottables
|
---|
| 294 | /// </summary>
|
---|
| 295 | public bool AllowPivotTables
|
---|
| 296 | {
|
---|
| 297 | get
|
---|
| 298 | {
|
---|
| 299 | return !GetXmlNodeBool(_allowPivotTablesPath, true);
|
---|
| 300 | }
|
---|
| 301 | set
|
---|
| 302 | {
|
---|
| 303 | SetXmlNodeBool(_allowPivotTablesPath, !value, true);
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | private const string _passwordPath = "d:sheetProtection/@password";
|
---|
| 308 | /// <summary>
|
---|
| 309 | /// Sets a password for the sheet.
|
---|
| 310 | /// </summary>
|
---|
| 311 | /// <param name="Password"></param>
|
---|
| 312 | public void SetPassword(string Password)
|
---|
| 313 | {
|
---|
| 314 | if (IsProtected == false) IsProtected = true;
|
---|
| 315 |
|
---|
| 316 | Password = Password.Trim();
|
---|
| 317 | if (Password == "")
|
---|
| 318 | {
|
---|
| 319 | var node = TopNode.SelectSingleNode(_passwordPath, NameSpaceManager);
|
---|
| 320 | if (node != null)
|
---|
| 321 | {
|
---|
| 322 | (node as XmlAttribute).OwnerElement.Attributes.Remove(node as XmlAttribute);
|
---|
| 323 | }
|
---|
| 324 | return;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | int hash = EncryptedPackageHandler.CalculatePasswordHash(Password);
|
---|
| 328 | SetXmlNodeString(_passwordPath, ((int)hash).ToString("x"));
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | }
|
---|
| 332 | }
|
---|