diff options
| author | Randy Morgan <[email protected]> | 2012-10-14 13:20:29 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-10-14 13:20:29 +0900 |
| commit | 25ebea9143c136999bdaeb372fb26d2c070ca730 (patch) | |
| tree | 0916e207715d821d3f4f8de45c9690ca65272633 /lib/axlsx/workbook/worksheet/sheet_protection.rb | |
| parent | 93b70a39999ac4d06e43e495f3fd283e9630d9d2 (diff) | |
| download | caxlsx-25ebea9143c136999bdaeb372fb26d2c070ca730.tar.gz caxlsx-25ebea9143c136999bdaeb372fb26d2c070ca730.zip | |
Refactored to use options parser, accessors and serialization attributes
Diffstat (limited to 'lib/axlsx/workbook/worksheet/sheet_protection.rb')
| -rw-r--r-- | lib/axlsx/workbook/worksheet/sheet_protection.rb | 212 |
1 files changed, 57 insertions, 155 deletions
diff --git a/lib/axlsx/workbook/worksheet/sheet_protection.rb b/lib/axlsx/workbook/worksheet/sheet_protection.rb index 104be53b..a472ec55 100644 --- a/lib/axlsx/workbook/worksheet/sheet_protection.rb +++ b/lib/axlsx/workbook/worksheet/sheet_protection.rb @@ -4,110 +4,9 @@ module Axlsx # The SheetProtection object manages worksheet protection options per sheet. class SheetProtection - # If 1 or true then AutoFilters should not be allowed to operate when the sheet is protected. - # If 0 or false then AutoFilters should be allowed to operate when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :auto_filter - - # If 1 or true then deleting columns should not be allowed when the sheet is protected. - # If 0 or false then deleting columns should be allowed when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :delete_columns - - # If 1 or true then deleting rows should not be allowed when the sheet is protected. - # If 0 or false then deleting rows should be allowed when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :delete_rows - - # If 1 or true then formatting cells should not be allowed when the sheet is protected. - # If 0 or false then formatting cells should be allowed when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :format_cells - - # If 1 or true then formatting columns should not be allowed when the sheet is protected. - # If 0 or false then formatting columns should be allowed when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :format_columns - - # If 1 or true then formatting rows should not be allowed when the sheet is protected. - # If 0 or false then formatting rows should be allowed when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :format_rows - - # If 1 or true then inserting columns should not be allowed when the sheet is protected. - # If 0 or false then inserting columns should be allowed when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :insert_columns - - # If 1 or true then inserting hyperlinks should not be allowed when the sheet is protected. - # If 0 or false then inserting hyperlinks should be allowed when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :insert_hyperlinks - - # If 1 or true then inserting rows should not be allowed when the sheet is protected. - # If 0 or false then inserting rows should be allowed when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :insert_rows - - # If 1 or true then editing of objects should not be allowed when the sheet is protected. - # If 0 or false then objects are allowed to be edited when the sheet is protected. - # @return [Boolean] - # default false - attr_reader :objects - - # If 1 or true then PivotTables should not be allowed to operate when the sheet is protected. - # If 0 or false then PivotTables should be allowed to operate when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :pivot_tables - - # Specifies the salt which was prepended to the user-supplied password before it was hashed using the hashing algorithm - # @return [String] - attr_reader :salt_value - - # If 1 or true then Scenarios should not be edited when the sheet is protected. - # If 0 or false then Scenarios are allowed to be edited when the sheet is protected. - # @return [Boolean] - # default false - attr_reader :scenarios - - # If 1 or true then selection of locked cells should not be allowed when the sheet is protected. - # If 0 or false then selection of locked cells should be allowed when the sheet is protected. - # @return [Boolean] - # default false - attr_reader :select_locked_cells - - # If 1 or true then selection of unlocked cells should not be allowed when the sheet is protected. - # If 0 or false then selection of unlocked cells should be allowed when the sheet is protected. - # @return [Boolean] - # default false - attr_reader :select_unlocked_cells - - # If 1 or true then the sheet is protected. - # If 0 or false then the sheet is not protected. - # @return [Boolean] - # default true - attr_reader :sheet - - # If 1 or true then sorting should not be allowed when the sheet is protected. - # If 0 or false then sorting should be allowed when the sheet is protected. - # @return [Boolean] - # default true - attr_reader :sort - - # Password hash - # @return [String] - # default nil - attr_reader :password + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes + include Axlsx::Accessors # Creates a new SheetProtection instance # @option options [Boolean] sheet @see SheetProtection#sheet @@ -127,64 +26,68 @@ module Axlsx # @option options [Boolean] pivot_tables @see SheetProtection#pivot_tables # @option options [Boolean] select_unlocked_cells @see SheetProtection#select_unlocked_cells # @option options [String] password. The password required for unlocking. @see SheetProtection#password= - # @option options [Boolean] objects @see SheetProtection#objects def initialize(options={}) @objects = @scenarios = @select_locked_cells = @select_unlocked_cells = false @sheet = @format_cells = @format_rows = @format_columns = @insert_columns = @insert_rows = @insert_hyperlinks = @delete_columns = @delete_rows = @sort = @auto_filter = @pivot_tables = true @password = nil + parse_options options + end - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + boolean_attr_accessor :sheet, :objects, :scenarios, :format_cells, :format_columns, :format_rows, + :insert_columns, :insert_rows, :insert_hyperlinks, :delete_columns, :delete_rows, + :select_locked_cells, :sort, :auto_filter, :pivot_tables, :select_unlocked_cells + + serializable_attributes :sheet, :objects, :scenarios, :format_cells, :format_columns, :format_rows, + :insert_columns, :insert_rows, :insert_hyperlinks, :delete_columns, :delete_rows, + :select_locked_cells, :sort, :auto_filter, :pivot_tables, :select_unlocked_cells, :salt, :password + + # Specifies the salt which was prepended to the user-supplied password before it was hashed using the hashing algorithm + # @return [String] + attr_reader :salt_value + + # If 1 or true then sorting should not be allowed when the sheet is protected. + # If 0 or false then sorting should be allowed when the sheet is protected. + # @return [Boolean] + # default true + attr_reader :sort + + # Password hash + # @return [String] + # default nil + attr_reader :password + + # This block is intended to implement the salt_value, hash_value and spin count as per the ECMA-376 standard. + # However, it does not seem to actually work in EXCEL - instead they are using their old retro algorithm shown below + # defined in the transitional portion of the speck. I am leaving this code in in the hope that someday Ill be able to + # figure out why it does not work, and if Excel even supports it. + # def propper_password=(v) + # @algorithm_name = v == nil ? nil : 'SHA-1' + # @salt_value = @spin_count = @hash_value = v if v == nil + # return if v == nil + # require 'digest/sha1' + # @spin_count = 10000 + # @salt_value = Digest::SHA1.hexdigest(rand(36**8).to_s(36)) + # @spin_count.times do |count| + # @hash_value = Digest::SHA1.hexdigest((@hash_value ||= (@salt_value + v.to_s)) + Array(count).pack('V')) + # end + # end + + # encodes password for protection locking + def password=(v) + return if v == nil + @password = create_password_hash(v) end + # Serialize the object + # @param [String] str + # @return [String] + def to_xml_string(str = '') + str << '<sheetProtection ' + serialized_attributes str + str << '/>' + end - # create validating setters for boolean values - # @return [Boolean] - [:sheet, :objects, :scenarios, :select_locked_cells, :sort, - :select_unlocked_cells, :format_cells, :format_rows, :format_columns, - :insert_columns, :insert_rows, :insert_hyperlinks, :delete_columns, - :delete_rows, :auto_filter, :pivot_tables].each do |f_name| - define_method "#{f_name.to_s}=".to_sym do |v| - Axlsx::validate_boolean(v) - instance_variable_set "@#{f_name.to_s}".to_sym, v - end - end - - # This block is intended to implement the salt_value, hash_value and spin count as per the ECMA-376 standard. - # However, it does not seem to actually work in EXCEL - instead they are using their old retro algorithm shown below - # defined in the transitional portion of the speck. I am leaving this code in in the hope that someday Ill be able to - # figure out why it does not work, and if Excel even supports it. -# def propper_password=(v) -# @algorithm_name = v == nil ? nil : 'SHA-1' -# @salt_value = @spin_count = @hash_value = v if v == nil -# return if v == nil -# require 'digest/sha1' -# @spin_count = 10000 -# @salt_value = Digest::SHA1.hexdigest(rand(36**8).to_s(36)) -# @spin_count.times do |count| -# @hash_value = Digest::SHA1.hexdigest((@hash_value ||= (@salt_value + v.to_s)) + Array(count).pack('V')) -# end -# end - - - - # encodes password for protection locking - def password=(v) - return if v == nil - @password = create_password_hash(v) - end - - # Serialize the object - # @param [String] str - # @return [String] - def to_xml_string(str = '') - str << '<sheetProtection ' - str << instance_values.map{ |k,v| k.gsub(/_(.)/){ $1.upcase } << %{="#{v.to_s}"} }.join(' ') - str << '/>' - end - - private + private # Creates a password hash for a given password # @return [String] def create_password_hash(password) @@ -196,7 +99,6 @@ module Axlsx password_as_string[2..3] + password_as_string[0..1] end - # Encodes a given password # Based on the algorithm provided by Daniel Rentz of OpenOffice. # http://www.openoffice.org/sc/excelfileformat.pdf, Revision 1.42, page 115 (21.05.2012) |
