/tools/Ruby/lib/ruby/1.8/webrick/httpauth/htgroup.rb

http://github.com/agross/netopenspace · Ruby · 61 lines · 47 code · 6 blank · 8 comment · 3 complexity · ae7e9bb032404f2938a6f5b2e8e9b16d MD5 · raw file

  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. require 'tempfile'
  10. module WEBrick
  11. module HTTPAuth
  12. class Htgroup
  13. def initialize(path)
  14. @path = path
  15. @mtime = Time.at(0)
  16. @group = Hash.new
  17. open(@path,"a").close unless File::exist?(@path)
  18. reload
  19. end
  20. def reload
  21. if (mtime = File::mtime(@path)) > @mtime
  22. @group.clear
  23. open(@path){|io|
  24. while line = io.gets
  25. line.chomp!
  26. group, members = line.split(/:\s*/)
  27. @group[group] = members.split(/\s+/)
  28. end
  29. }
  30. @mtime = mtime
  31. end
  32. end
  33. def flush(output=nil)
  34. output ||= @path
  35. tmp = Tempfile.new("htgroup", File::dirname(output))
  36. begin
  37. @group.keys.sort.each{|group|
  38. tmp.puts(format("%s: %s", group, self.members(group).join(" ")))
  39. }
  40. tmp.close
  41. File::rename(tmp.path, output)
  42. rescue
  43. tmp.close(true)
  44. end
  45. end
  46. def members(group)
  47. reload
  48. @group[group] || []
  49. end
  50. def add(group, members)
  51. @group[group] = members(group) | members
  52. end
  53. end
  54. end
  55. end