root/lib/cyborghood/objects.rb @ d5a0d3bc
d32ee48a | Marc Dequenes | #--
|
|
# CyborgHood, a distributed system management software.
|
|||
e7315259 | Marc Dequènes (Duck) | # Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|
d32ee48a | Marc Dequenes | #
|
|
# This program 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 3 of the License, or
|
|||
# (at your option) any later version.
|
|||
#
|
|||
# This program is distributed in the hope that it will be useful,
|
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||
# GNU General Public License for more details.
|
|||
#
|
|||
# You should have received a copy of the GNU General Public License
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#++
|
|||
bc4894ce | Marc Dequenes | require 'delegate'
|
|
78a20655 | Marc Dequenes | require 'cyborghood/objects/ldap'
|
|
83a04d84 | Marc Dequènes (Duck) | require 'http_headers'
|
|
78a20655 | Marc Dequenes | ||
4b58d4fd | Marc Dequenes | module CyborgHood
|
|
class DnsDomain < Delegator
|
|||
attr_reader :ldap, :name
|
|||
78a20655 | Marc Dequenes | ||
4b58d4fd | Marc Dequenes | def initialize(name)
|
|
@name = name
|
|||
323a6bb6 | Marc Dequenes | raise CyberError.new(:unrecoverable, "objects/dns", "invalid zone name") unless self.is_valid?
|
|
78a20655 | Marc Dequenes | ||
4b58d4fd | Marc Dequenes | # may not exist (if creating a new one)
|
|
begin
|
|||
@ldap = LdapDnsDomain.find(name)
|
|||
rescue
|
|||
@ldap = nil
|
|||
end
|
|||
78a20655 | Marc Dequenes | end
|
|
4b58d4fd | Marc Dequenes | def self.is_valid?(name)
|
|
name =~ /^[a-z0-9.-]+\.[a-z]{2,4}$/
|
|||
end
|
|||
78a20655 | Marc Dequenes | ||
4b58d4fd | Marc Dequenes | def is_valid?
|
|
self.class.is_valid?(@name)
|
|||
end
|
|||
78a20655 | Marc Dequenes | ||
4b58d4fd | Marc Dequenes | def hosted?
|
|
not @ldap.nil?
|
|||
end
|
|||
78a20655 | Marc Dequenes | ||
4b58d4fd | Marc Dequenes | def managed_by?(user)
|
|
@ldap.managers.include? user.ldap.dn
|
|||
end
|
|||
78a20655 | Marc Dequenes | ||
4b58d4fd | Marc Dequenes | def __getobj__
|
|
@ldap
|
|||
end
|
|||
78a20655 | Marc Dequenes | ||
4b58d4fd | Marc Dequenes | 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
|
|||
ec4a99c3 | Marc Dequenes | rescue ActiveLdap::Error => e
|
|
323a6bb6 | Marc Dequenes | raise CyberError.new(:unrecoverable, "db/ldap", e.message)
|
|
78a20655 | Marc Dequenes | end
|
|
end
|
|||
4b58d4fd | Marc Dequenes | class Person < Delegator
|
|
attr_reader :ldap
|
|||
78a20655 | Marc Dequenes | ||
4b58d4fd | Marc Dequenes | 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
|
|||
323a6bb6 | Marc Dequenes | raise CyberError.new(:unrecoverable, "db/ldap", e.message)
|
|
78a20655 | Marc Dequenes | end
|
|
83a04d84 | Marc Dequènes (Duck) | def prefered_language(available_languages)
|
|
lang_chooser = HTTPHeaders::AcceptLanguage.parse(self.preferredLanguage)
|
|||
b8b8b4eb | Marc Dequènes (Duck) | return nil if lang_chooser.nil?
|
|
83a04d84 | Marc Dequènes (Duck) | ordered_list = lang_chooser.reduce(available_languages)
|
|
ordered_list.empty? ? nil : ordered_list.first.range
|
|||
end
|
|||
4b58d4fd | Marc Dequenes | def __getobj__
|
|
@ldap
|
|||
end
|
|||
78a20655 | Marc Dequenes | end
|
|
end
|