protection.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright (c) 2010-2024 openpyxl
  2. from openpyxl.descriptors import (
  3. Bool,
  4. String,
  5. Alias,
  6. Integer,
  7. )
  8. from openpyxl.descriptors.serialisable import Serialisable
  9. from openpyxl.descriptors.excel import (
  10. Base64Binary,
  11. )
  12. from openpyxl.utils.protection import hash_password
  13. class _Protected:
  14. _password = None
  15. def set_password(self, value='', already_hashed=False):
  16. """Set a password on this sheet."""
  17. if not already_hashed:
  18. value = hash_password(value)
  19. self._password = value
  20. @property
  21. def password(self):
  22. """Return the password value, regardless of hash."""
  23. return self._password
  24. @password.setter
  25. def password(self, value):
  26. """Set a password directly, forcing a hash step."""
  27. self.set_password(value)
  28. class SheetProtection(Serialisable, _Protected):
  29. """
  30. Information about protection of various aspects of a sheet. True values
  31. mean that protection for the object or action is active This is the
  32. **default** when protection is active, ie. users cannot do something
  33. """
  34. tagname = "sheetProtection"
  35. sheet = Bool()
  36. enabled = Alias('sheet')
  37. objects = Bool()
  38. scenarios = Bool()
  39. formatCells = Bool()
  40. formatColumns = Bool()
  41. formatRows = Bool()
  42. insertColumns = Bool()
  43. insertRows = Bool()
  44. insertHyperlinks = Bool()
  45. deleteColumns = Bool()
  46. deleteRows = Bool()
  47. selectLockedCells = Bool()
  48. selectUnlockedCells = Bool()
  49. sort = Bool()
  50. autoFilter = Bool()
  51. pivotTables = Bool()
  52. saltValue = Base64Binary(allow_none=True)
  53. spinCount = Integer(allow_none=True)
  54. algorithmName = String(allow_none=True)
  55. hashValue = Base64Binary(allow_none=True)
  56. __attrs__ = ('selectLockedCells', 'selectUnlockedCells', 'algorithmName',
  57. 'sheet', 'objects', 'insertRows', 'insertHyperlinks', 'autoFilter',
  58. 'scenarios', 'formatColumns', 'deleteColumns', 'insertColumns',
  59. 'pivotTables', 'deleteRows', 'formatCells', 'saltValue', 'formatRows',
  60. 'sort', 'spinCount', 'password', 'hashValue')
  61. def __init__(self, sheet=False, objects=False, scenarios=False,
  62. formatCells=True, formatRows=True, formatColumns=True,
  63. insertColumns=True, insertRows=True, insertHyperlinks=True,
  64. deleteColumns=True, deleteRows=True, selectLockedCells=False,
  65. selectUnlockedCells=False, sort=True, autoFilter=True, pivotTables=True,
  66. password=None, algorithmName=None, saltValue=None, spinCount=None, hashValue=None):
  67. self.sheet = sheet
  68. self.objects = objects
  69. self.scenarios = scenarios
  70. self.formatCells = formatCells
  71. self.formatColumns = formatColumns
  72. self.formatRows = formatRows
  73. self.insertColumns = insertColumns
  74. self.insertRows = insertRows
  75. self.insertHyperlinks = insertHyperlinks
  76. self.deleteColumns = deleteColumns
  77. self.deleteRows = deleteRows
  78. self.selectLockedCells = selectLockedCells
  79. self.selectUnlockedCells = selectUnlockedCells
  80. self.sort = sort
  81. self.autoFilter = autoFilter
  82. self.pivotTables = pivotTables
  83. if password is not None:
  84. self.password = password
  85. self.algorithmName = algorithmName
  86. self.saltValue = saltValue
  87. self.spinCount = spinCount
  88. self.hashValue = hashValue
  89. def set_password(self, value='', already_hashed=False):
  90. super().set_password(value, already_hashed)
  91. self.enable()
  92. def enable(self):
  93. self.sheet = True
  94. def disable(self):
  95. self.sheet = False
  96. def __bool__(self):
  97. return self.sheet