Class: HTAuth::PasswdFile

Inherits:
File
  • Object
show all
Defined in:
lib/htauth/passwd_file.rb

Overview

PasswdFile provides API style access to an htpasswd produced file

Constant Summary

ENTRY_KLASS =
HTAuth::PasswdEntry

Constants inherited from File

File::ALTER, File::CREATE, File::STDOUT_FLAG

Instance Attribute Summary

Attributes inherited from File

#file, #filename

Instance Method Summary (collapse)

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

- (Object) add(username, password, algorithm = Algorithm::DEFAULT)

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

Raises:



43
44
45
46
47
48
49
50
51
# File 'lib/htauth/passwd_file.rb', line 43

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

- (Object) add_or_update(username, password, algorithm = Algorithm::DEFAULT)

add or update an entry as appropriate



34
35
36
37
38
39
40
# File 'lib/htauth/passwd_file.rb', line 34

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

- (Object) delete(username)

remove an entry from the file



22
23
24
25
26
27
28
29
30
31
# File 'lib/htauth/passwd_file.rb', line 22

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

- (Object) entry_klass



72
73
74
# File 'lib/htauth/passwd_file.rb', line 72

def entry_klass
    ENTRY_KLASS
end

- (Object) fetch(username)

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



66
67
68
69
70
# File 'lib/htauth/passwd_file.rb', line 66

def fetch(username)
    return nil unless has_entry?(username)
    ir = internal_record(username)
    return ir['entry'].dup
end

- (Boolean) has_entry?(username)

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

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/htauth/passwd_file.rb', line 16

def has_entry?(username)
    test_entry = PasswdEntry.new(username)
    @entries.has_key?(test_entry.key)
end

- (Object) update(username, password, algorithm = Algorithm::EXISTING)

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

Raises:



54
55
56
57
58
59
60
61
62
# File 'lib/htauth/passwd_file.rb', line 54

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