Project

General

Profile

« Previous | Next » 

Revision 4b58d4fd

Added by Marc Dequènes almost 16 years ago

  • ID 4b58d4fd19ee646b78356e72908d4b5a6b4fda28

[evol] move new classes into CyborgHood module and add new exception handling

View differences:

lib/cyborghood/base.rb
PRODUCT = "CyborgHood"
VERSION = "0.0.1"
class Logger < Log4r::Logger
# severities: # :dangerous :unrecoverable :ignorable
# categories:
# - db
# + ldap
# + sql
# - protocol
# + imap
# - service
# + dns
class Error < StandardError
attr_accessor :severity, :category
#include GetTextSupport
def initialize(severity, category, message)
@severity = severity
@category = category
super(message)
end
end
class Logger < Log4r::Logger
include Singleton
LOG_FORMAT = "[%5l - %d] #PREFIX#%m"
lib/cyborghood/objects.rb
require 'cyborghood/objects/ldap'
class DnsDomain < Delegator
attr_reader :ldap, :name
module CyborgHood
class DnsDomain < Delegator
attr_reader :ldap, :name
def initialize(name)
@name = name
raise "invalid zone name" unless self.is_valid?
def initialize(name)
@name = name
raise "invalid zone name" unless self.is_valid?
# may not exist (if creating a new one)
begin
@ldap = LdapDnsDomain.find(name)
rescue
@ldap = nil
# may not exist (if creating a new one)
begin
@ldap = LdapDnsDomain.find(name)
rescue
@ldap = nil
end
end
end
def self.is_valid?(name)
name =~ /^[a-z0-9.-]+\.[a-z]{2,4}$/
end
def self.is_valid?(name)
name =~ /^[a-z0-9.-]+\.[a-z]{2,4}$/
end
def is_valid?
self.class.is_valid?(@name)
end
def is_valid?
self.class.is_valid?(@name)
end
def hosted?
not @ldap.nil?
end
def hosted?
not @ldap.nil?
end
def managed_by?(user)
@ldap.managers.include? user.ldap.dn
end
def managed_by?(user)
@ldap.managers.include? user.ldap.dn
end
def __getobj__
@ldap
end
def __getobj__
@ldap
end
def self.find_by_manager(user)
list = LdapDnsDomain.find(:all, :attribute => 'manager', :value => user.ldap.dn)
list.collect do |l_dom|
domain = allocate
domain.instance_variable_set("@ldap", l_dom)
def self.find_by_manager(user)
list = LdapDnsDomain.find(:all, :attribute => 'manager', :value => user.ldap.dn)
list.collect do |l_dom|
domain = allocate
domain.instance_variable_set("@ldap", l_dom)
end
rescue ActiveLdap::Error
raise Error.new(:unrecoverable, "db/ldap", e.message)
end
end
end
class Person < Delegator
attr_reader :ldap
class Person < Delegator
attr_reader :ldap
def self.find_by_fingerprint(fingerprint)
list = LdapPerson.find(:all, :attribute => 'keyFingerPrint', :value => fingerprint)
case list.size
when 0
nil
when 1
person = allocate
person.instance_variable_set("@ldap", list.first)
person
else
logger.warn "Multiple users match in database, so i guess there is a mistake. It is safer to skip..."
nil
def self.find_by_fingerprint(fingerprint)
list = LdapPerson.find(:all, :attribute => 'keyFingerPrint', :value => fingerprint)
case list.size
when 0
nil
when 1
person = allocate
person.instance_variable_set("@ldap", list.first)
person
else
logger.warn "Multiple users match in database, so i guess there is a mistake. It is safer to skip..."
nil
end
rescue ActiveLdap::Error => e
raise Error.new(:unrecoverable, "db/ldap", e.message)
end
end
def __getobj__
@ldap
def __getobj__
@ldap
end
end
end
lib/cyborghood/objects/ldap.rb
module CyborgHood
class LdapDnsDomain < ActiveLdap::Base
ldap_mapping :dn_attribute => 'cn', :prefix => '', :classes => ['genericDomain']
class LdapDnsDomain < ActiveLdap::Base
ldap_mapping :dn_attribute => 'cn', :prefix => '', :classes => ['genericDomain']
def managers
list = self.manager
return [] if list.nil?
return list.collect{|dn| dn.to_s } if list.is_a? Array
return [list.to_s]
def managers
list = self.manager
return [] if list.nil?
return list.collect{|dn| dn.to_s } if list.is_a? Array
return [list.to_s]
end
end
end
class LdapPerson < ActiveLdap::Base
ldap_mapping :dn_attribute => 'uid', :prefix => '', :classes => ['person', 'extInetOrgPerson']
class LdapPerson < ActiveLdap::Base
ldap_mapping :dn_attribute => 'uid', :prefix => '', :classes => ['person', 'extInetOrgPerson']
end
end

Also available in: Unified diff