67 lines
1.7 KiB
Ruby
67 lines
1.7 KiB
Ruby
# = ildus/server/account_backends/yaml - account backend library for YAML files
|
|
#
|
|
# Copyright (C) 2005 Paul van Tilburg <paul@luon.net>
|
|
#
|
|
# Ildus is free software; you can redistribute it and/or modify it under
|
|
# the terms of the GNU General Public License as published by the Free
|
|
# Software Foundation; either version 2 of the License, or (at your option)
|
|
# any later version.
|
|
|
|
module Ildus::Server::AccountBackend
|
|
|
|
# = Account backend class for Apache's htaccess files.
|
|
class Yaml < Basic
|
|
|
|
def initialize(*args)
|
|
super
|
|
@db = nil
|
|
load_db
|
|
end
|
|
|
|
def authenticate
|
|
case config['crypt'].to_sym
|
|
when :unix
|
|
regged_pass = @db[user]
|
|
return false if regged_pass.nil?
|
|
self.auth = (pass.crypt(regged_pass) == regged_pass)
|
|
else
|
|
raise "unsupported crypt type: #{config['crypt']}"
|
|
end
|
|
end
|
|
|
|
def register_account(user, pass)
|
|
raise "user already exists" if @db.has_key? user
|
|
raise "password to short" if pass.length < 5
|
|
salt = pass[-2..-1].succ.succ
|
|
@db[user] = pass.crypt(salt)
|
|
save_db
|
|
end
|
|
|
|
def unregister_account(user)
|
|
raise "user does not exist" unless @db.has_k
|
|
@db.delete user
|
|
save_db
|
|
end
|
|
|
|
########
|
|
private
|
|
########
|
|
|
|
def load_db
|
|
db = nil
|
|
File.open(config['file'], 'r') { |f| db = YAML.load(f) || Hash.new }
|
|
raise "wrong account database format" unless db.is_a? Hash
|
|
@db = db
|
|
end
|
|
|
|
def save_db
|
|
unless FileTest.exists? config['file']
|
|
raise "account database file does not exist: " + config['file']
|
|
end
|
|
File.open(config['file'], 'w') { |f| f.puts @db.to_yaml }
|
|
end
|
|
|
|
end # class Yaml
|
|
|
|
end # module Ildus::Server::AccountBackend
|