Class | HTAuth::PasswdFile |
In: |
lib/htauth/passwd_file.rb
|
Parent: | HTAuth::File |
PasswdFile provides API style access to an htpasswd produced file
ENTRY_KLASS | = | HTAuth::PasswdEntry |
add an new record. raises an error if the entry exists.
# File lib/htauth/passwd_file.rb, line 43 43: def add(username, password, algorithm = Algorithm::DEFAULT) 44: raise PasswdFileError, "Unable to add already existing user #{username}" if has_entry?(username) 45: new_entry = PasswdEntry.new(username, password, algorithm) 46: new_index = @lines.size 47: @lines << new_entry.to_s 48: @entries[new_entry.key] = { 'entry' => new_entry, 'line_index' => new_index } 49: dirty! 50: return nil 51: end
remove an entry from the file
# File lib/htauth/passwd_file.rb, line 22 22: def delete(username) 23: if has_entry?(username) then 24: ir = internal_record(username) 25: line_index = ir['line_index'] 26: @entries.delete(ir['entry'].key) 27: @lines[line_index] = nil 28: dirty! 29: end 30: nil 31: end
fetches a copy of an entry from the file. Updateing the entry returned from fetch will NOT propogate back to the file.
# File lib/htauth/passwd_file.rb, line 66 66: def fetch(username) 67: return nil unless has_entry?(username) 68: ir = internal_record(username) 69: return ir['entry'].dup 70: end
does the entry the the specified username and realm exist in the file
# File lib/htauth/passwd_file.rb, line 16 16: def has_entry?(username) 17: test_entry = PasswdEntry.new(username) 18: @entries.has_key?(test_entry.key) 19: end
update an already existing entry with a new password. raises an error if the entry does not exist
# File lib/htauth/passwd_file.rb, line 54 54: def update(username, password, algorithm = Algorithm::EXISTING) 55: raise PasswdFileError, "Unable to update non-existent user #{username}" unless has_entry?(username) 56: ir = internal_record(username) 57: ir['entry'].algorithm = algorithm 58: ir['entry'].password = password 59: @lines[ir['line_index']] = ir['entry'].to_s 60: dirty! 61: return nil 62: end