Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add password encryption, storage, and checking to the settings class

  • Loading branch information...
commit 0f0f4ab6cdc2c6e249c5ded4d0fac4c12ddf6cb5 1 parent 23b6a1c
@timriley authored
Showing with 23 additions and 17 deletions.
  1. +23 −17 app/models/settings.rb
View
40 app/models/settings.rb
@@ -1,6 +1,7 @@
class Settings
include Singleton
+ # this allows us to read and write settings of any name
def method_missing(method_id, *arguments)
if /=$/ =~ method_id.to_s
write_setting(method_id.to_s.gsub(/=$/,''), arguments.first)
@@ -8,32 +9,37 @@ def method_missing(method_id, *arguments)
read_setting(method_id.to_s)
end
end
-
- private
- def load_tree
- @raw_tree = YAML.load_file("#{Rails.root}/config/config.yml")
+ # the password setting is special, we need to encrypt its contents
+ def password=(value)
+ salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--")
+ write_setting('password_salt', salt)
+ write_setting('password', self.class.encrypt(value, salt))
end
- def save_tree
- # merge the trees
- @raw_tree[Rails.env] = @new_tree[Rails.env]
+ def password?(password)
+ self.password == self.class.encrypt(password, self.password_salt)
+ end
+
+ def save
File.open("#{Rails.root}/config/config.yml", 'w') { |f| YAML.dump(@raw_tree, f) }
end
+ def self.encrypt(string, salt)
+ Digest::SHA1.hexdigest("--#{salt}--#{string}--")
+ end
+
+ private
+
+ def tree
+ @raw_tree ||= YAML.load_file("#{Rails.root}/config/config.yml")
+ end
+
def read_setting(key)
- load_tree
- @raw_tree[Rails.env][key]
+ tree[Rails.env][key]
end
def write_setting(key, val)
- load_tree
- @new_tree ||= @raw_tree.dup
- @new_tree[Rails.env][key] = val
- save_tree
+ tree[Rails.env][key] = val
end
-end
-
-def settings
- Settings.instance
end
Please sign in to comment.
Something went wrong with that request. Please try again.