Class: HTAuth::PasswdFile
Overview
PasswdFile provides API style access to an htpasswd
produced file
Constant Summary collapse
- ENTRY_KLASS =
HTAuth::PasswdEntry
Constants inherited from File
File::ALTER, File::CREATE, File::STDOUT_FLAG
Instance Attribute Summary
Attributes inherited from File
Instance Method Summary collapse
-
#add(username, password, algorithm = Algorithm::DEFAULT) ⇒ Object
add an new record.
-
#add_or_update(username, password, algorithm = Algorithm::DEFAULT) ⇒ Object
add or update an entry as appropriate.
-
#delete(username) ⇒ Object
remove an entry from the file.
- #entry_klass ⇒ Object
-
#fetch(username) ⇒ Object
fetches a copy of an entry from the file.
-
#has_entry?(username) ⇒ Boolean
does the entry the the specified username and realm exist in the file.
-
#update(username, password, algorithm = Algorithm::EXISTING) ⇒ Object
update an already existing entry with a new password.
Methods inherited from File
#contents, #dirty!, #dirty?, #initialize, #load_entries, open, #save!
Constructor Details
This class inherits a constructor from HTAuth::File
Instance Method Details
#add(username, password, algorithm = Algorithm::DEFAULT) ⇒ Object
add an new record. raises an error if the entry exists.
44 45 46 47 48 49 50 51 52 |
# File 'lib/htauth/passwd_file.rb', line 44 def add(username, password, algorithm = Algorithm::DEFAULT) raise PasswdFileError, "Unable to add already existing user #{username}" if has_entry?(username) new_entry = PasswdEntry.new(username, password, algorithm) new_index = @lines.size @lines << new_entry.to_s @entries[new_entry.key] = { 'entry' => new_entry, 'line_index' => new_index } dirty! return nil end |
#add_or_update(username, password, algorithm = Algorithm::DEFAULT) ⇒ Object
add or update an entry as appropriate
35 36 37 38 39 40 41 |
# File 'lib/htauth/passwd_file.rb', line 35 def add_or_update(username, password, algorithm = Algorithm::DEFAULT) if has_entry?(username) then update(username, password, algorithm) else add(username, password, algorithm) end end |
#delete(username) ⇒ Object
remove an entry from the file
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/htauth/passwd_file.rb', line 23 def delete(username) if has_entry?(username) then ir = internal_record(username) line_index = ir['line_index'] @entries.delete(ir['entry'].key) @lines[line_index] = nil dirty! end nil end |
#entry_klass ⇒ Object
73 74 75 |
# File 'lib/htauth/passwd_file.rb', line 73 def entry_klass ENTRY_KLASS end |
#fetch(username) ⇒ Object
fetches a copy of an entry from the file. Updateing the entry returned from fetch will NOT propogate back to the file.
67 68 69 70 71 |
# File 'lib/htauth/passwd_file.rb', line 67 def fetch(username) return nil unless has_entry?(username) ir = internal_record(username) return ir['entry'].dup end |
#has_entry?(username) ⇒ Boolean
does the entry the the specified username and realm exist in the file
17 18 19 20 |
# File 'lib/htauth/passwd_file.rb', line 17 def has_entry?(username) test_entry = PasswdEntry.new(username) @entries.has_key?(test_entry.key) end |
#update(username, password, algorithm = Algorithm::EXISTING) ⇒ Object
update an already existing entry with a new password. raises an error if the entry does not exist
55 56 57 58 59 60 61 62 63 |
# File 'lib/htauth/passwd_file.rb', line 55 def update(username, password, algorithm = Algorithm::EXISTING) raise PasswdFileError, "Unable to update non-existent user #{username}" unless has_entry?(username) ir = internal_record(username) ir['entry'].algorithm = algorithm ir['entry'].password = password @lines[ir['line_index']] = ir['entry'].to_s dirty! return nil end |