Class HTAuth::PasswdFile
In: lib/htauth/passwd_file.rb
lib/htauth/passwd_file.rb
Parent: HTAuth::File

PasswdFile provides API style access to an htpasswd produced file

Methods

Constants

ENTRY_KLASS = HTAuth::PasswdEntry
ENTRY_KLASS = HTAuth::PasswdEntry

Public Instance methods

add an new record. raises an error if the entry exists.

[Source]

    # 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

add an new record. raises an error if the entry exists.

[Source]

    # 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

add or update an entry as appropriate

[Source]

    # File lib/htauth/passwd_file.rb, line 34
34:         def add_or_update(username, password, algorithm = Algorithm::DEFAULT)
35:             if has_entry?(username) then
36:                 update(username, password, algorithm)
37:             else
38:                 add(username, password, algorithm)
39:             end
40:         end

add or update an entry as appropriate

[Source]

    # File lib/htauth/passwd_file.rb, line 34
34:         def add_or_update(username, password, algorithm = Algorithm::DEFAULT)
35:             if has_entry?(username) then
36:                 update(username, password, algorithm)
37:             else
38:                 add(username, password, algorithm)
39:             end
40:         end

remove an entry from the file

[Source]

    # 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

remove an entry from the file

[Source]

    # 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

[Source]

    # File lib/htauth/passwd_file.rb, line 72
72:         def entry_klass
73:             ENTRY_KLASS
74:         end

[Source]

    # File lib/htauth/passwd_file.rb, line 72
72:         def entry_klass
73:             ENTRY_KLASS
74:         end

fetches a copy of an entry from the file. Updateing the entry returned from fetch will NOT propogate back to the file.

[Source]

    # 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

fetches a copy of an entry from the file. Updateing the entry returned from fetch will NOT propogate back to the file.

[Source]

    # 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

[Source]

    # 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

does the entry the the specified username and realm exist in the file

[Source]

    # 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

[Source]

    # 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

update an already existing entry with a new password. raises an error if the entry does not exist

[Source]

    # 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

[Validate]