Line | |
---|
1 | #
|
---|
2 | # httpauth/htgroup.rb -- Apache compatible htgroup file
|
---|
3 | #
|
---|
4 | # Author: IPR -- Internet Programming with Ruby -- writers
|
---|
5 | # Copyright (c) 2003 Internet Programming with Ruby writers. All rights
|
---|
6 | # reserved.
|
---|
7 | #
|
---|
8 | # $IPR: htgroup.rb,v 1.1 2003/02/16 22:22:56 gotoyuzo Exp $
|
---|
9 |
|
---|
10 | require 'tempfile'
|
---|
11 |
|
---|
12 | module WEBrick
|
---|
13 | module HTTPAuth
|
---|
14 | class Htgroup
|
---|
15 | def initialize(path)
|
---|
16 | @path = path
|
---|
17 | @mtime = Time.at(0)
|
---|
18 | @group = Hash.new
|
---|
19 | open(@path,"a").close unless File::exist?(@path)
|
---|
20 | reload
|
---|
21 | end
|
---|
22 |
|
---|
23 | def reload
|
---|
24 | if (mtime = File::mtime(@path)) > @mtime
|
---|
25 | @group.clear
|
---|
26 | open(@path){|io|
|
---|
27 | while line = io.gets
|
---|
28 | line.chomp!
|
---|
29 | group, members = line.split(/:\s*/)
|
---|
30 | @group[group] = members.split(/\s+/)
|
---|
31 | end
|
---|
32 | }
|
---|
33 | @mtime = mtime
|
---|
34 | end
|
---|
35 | end
|
---|
36 |
|
---|
37 | def flush(output=nil)
|
---|
38 | output ||= @path
|
---|
39 | tmp = Tempfile.new("htgroup", File::dirname(output))
|
---|
40 | begin
|
---|
41 | @group.keys.sort.each{|group|
|
---|
42 | tmp.puts(format("%s: %s", group, self.members(group).join(" ")))
|
---|
43 | }
|
---|
44 | tmp.close
|
---|
45 | File::rename(tmp.path, output)
|
---|
46 | rescue
|
---|
47 | tmp.close(true)
|
---|
48 | end
|
---|
49 | end
|
---|
50 |
|
---|
51 | def members(group)
|
---|
52 | reload
|
---|
53 | @group[group] || []
|
---|
54 | end
|
---|
55 |
|
---|
56 | def add(group, members)
|
---|
57 | @group[group] = members(group) | members
|
---|
58 | end
|
---|
59 | end
|
---|
60 | end
|
---|
61 | end
|
---|
Note:
See
TracBrowser
for help on using the repository browser.