Project

General

Profile

« Previous | Next » 

Revision f706cc1d

Added by Marc Dequènes about 15 years ago

  • ID f706cc1dcc9263550db2ca8219d705c9015ee535

[evol] fix and improve before hooks for aspects

View differences:

TODO
a common code to be able to relocate an item
- find a way to restrict parent locations for new objects (regex for
<obj>/<item> string ? regex for DN ? OR/AND with both ? ???)
- search by family relationship ?
- after-create/modify and before/after-delete hooks for aspects
- whole hooks panoply for objects
- handle language subtypes ? (would need the RFC2798 preferredLanguage
parser out of the CyborgHood project)
- support X-ORDERED attributetypes ?
- search by family relationship ?
bin/shadowwalker
rescue ActiveLdap::Error => e
STDERR.puts _("LDAP error: %s") % e.to_s
exit 3
rescue ProcessingError => e
STDERR.puts _("Processing error: %s") % e.to_s
exit 3
rescue WeirdError =>e
STDERR.puts _("Weird error: %s") % e.to_s
exit 4
conf/ldap_shadows/shadows/MilkyPond/hooks/aspects/fs.rb
STEP_UID = 100
DEFAULT_GROUP = 'dc-users'
def self.hook_modify(mapper, item)
def self.hook_before_create(mapper, item)
hook_common(mapper, item)
end
def self.hook_before_modify(mapper, item)
hook_common(mapper, item)
end
def self.hook_common(mapper, item)
unless item.attribute_present?('gidNumber')
item.primaryGroup = mapper.find_klass(:group).find(:first, DEFAULT_GROUP)
end
unless item.attribute_present?('uidNumber')
groups = ActiveLdap::Base.search(:scope => :sub, :filter => "(uidNumber=*)", :attributes => ['uidNumber'])
uidnumbers = groups.collect {|group| group[1]['uidNumber'].first.to_i }
if item.attribute_present?('uidNumber')
user = ActiveLdap::Base.find(:first, :scope => :sub, :filter => "(&(objectClass=posixAccount)(uidNumber=#{item.uidNumber}))", :attributes => ['uidNumber'])
unless user.nil? or user.dn == item.dn
raise_error "UID already used by another item"
end
else
users = ActiveLdap::Base.find(:all, :scope => :sub, :filter => "(&(objectClass=posixAccount)(uidNumber=*))", :attributes => ['uidNumber'])
uidnumbers = users.collect {|user| user.uidNumber }
avail_uidnumber = nil
min_uidn = MIN_UID
......
end
if avail_uidnumber.nil?
STDERR.puts "Available UID range is depleted."
exit 4
raise_error "Available UID range is depleted."
else
item.uidNumber = avail_uidnumber
end
lib/ldap_shadows/controller.rb
aspect_def.recursive_symbolize_keys!
@aspects[aspect_name] = aspect_def
filename = File.join(Config::CFG_DIR, "hooks", "aspects", aspect_name.to_s.downcase + ".rb")
return if @shadow_config_path.nil?
filename = File.join(@shadow_config_path, "hooks", "aspects", aspect_name.to_s.downcase + ".rb")
if File.exists?(filename)
klass_name = "LdapAspect" + aspect_name.to_s.capitalize
klass_content = IO.read(filename)
......
end
class Aspect
def hook_create
def self.hook_before_create
end
def hook_modify
def self.hook_before_modify
end
protected
def self.raise_error(msg)
raise ProcessingError, msg
end
end
lib/ldap_shadows/object.rb
protected
def before_save_jobs
check_hooks
check_hooks_before
check_missing_attributes
end
def check_hooks
def check_hooks_before
self.aspects.each do |aspect|
aklass = self.class.mapper.get_aspect_klass(aspect)
next if aklass.nil?
if self.new_entry?
aklass.hook_create(self.class.mapper, self)
aklass.hook_before_create(self.class.mapper, self)
else
aklass.hook_modify(self.class.mapper, self)
aklass.hook_before_modify(self.class.mapper, self)
end
end
end

Also available in: Unified diff