|
|
|
def self.hook_before_create(item)
|
|
hook_common(item)
|
|
end
|
|
|
|
def self.hook_before_modify(item)
|
|
hook_common(item)
|
|
end
|
|
|
|
def self.hook_common(item)
|
|
pconfig_default = {
|
|
:default_group => 'users',
|
|
:min_uid => 1000,
|
|
:max_uid => 65535,
|
|
:step_uid => 100,
|
|
:default_home_base => "/home"
|
|
}
|
|
pconfig = pconfig_default.merge(self.parameters[:plugin])
|
|
|
|
unless item.attribute_present?('gidNumber')
|
|
item.primaryGroup = item.class.shadow.get_object(:group).find(:first, pconfig[:default_group])
|
|
end
|
|
|
|
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 = pconfig[:min_uid]
|
|
max_uidn = [min_uidn + pconfig[:step_uid], pconfig[:max_uid]].min
|
|
while avail_uidnumber.nil?
|
|
avail = (min_uidn..max_uidn).to_a - uidnumbers
|
|
unless avail.empty?
|
|
avail_uidnumber = avail.first
|
|
break
|
|
end
|
|
min_uidn = max_uidn
|
|
max_uidn = [min_uidn + pconfig[:step_uid], pconfig[:max_uid]].min
|
|
end
|
|
|
|
if avail_uidnumber.nil?
|
|
raise_error "Available UID range is depleted."
|
|
else
|
|
item.uidNumber = avail_uidnumber
|
|
end
|
|
end
|
|
|
|
unless item.attribute_present?('homeDirectory')
|
|
item.homeDirectory = File.join(pconfig[:default_home_base], item.uid)
|
|
end
|
|
end
|