Revision 0814bfc3
Added by Marc Dequènes over 15 years ago
- ID 0814bfc328eb0569ddfe5c83442706063ee4142f
bin/shadowwalker | ||
---|---|---|
|
||
item_hdl = args.shift
|
||
begin
|
||
item = obj_klass.find(item_hdl, :attributes => ["*", "+"])
|
||
item = obj_klass.find(item_hdl, :attributes => ["*",])
|
||
rescue ActiveLdap::EntryNotFound
|
||
STDERR.puts "No such item '#{obj_hdl}/#{item_hdl}'"
|
||
exit 2
|
||
... | ... | |
puts "COIN"
|
||
p args
|
||
args.each do |mod_info|
|
||
key, val = mod_info.split("=")
|
||
raise "Syntax error in modification parameters" if val.nil?
|
||
mod_info =~ /^([a-zA-Z]+)(=|\+=|-=)(.*)$/
|
||
key = $1
|
||
op = $2
|
||
val = $3
|
||
p "key: #{key}"
|
||
p "op: #{op}"
|
||
p "val: #{val}"
|
||
|
||
# TODO: manage relations (harder)
|
||
raise "No such field '#{key}' in object '#{obj_hdl}'" unless item.has_attribute?(key)
|
||
|
||
item[key] = val
|
||
if key.nil?
|
||
STDERR.puts "Syntax error in modification parameters: invalid field name"
|
||
exit 1
|
||
end
|
||
unless item.has_attribute?(key)
|
||
STDERR.puts "No such field '#{key}' in object '#{obj_hdl}'"
|
||
exit 2
|
||
end
|
||
|
||
attr_info = ActiveLdap::Base.schema.attribute(key)
|
||
if attr_info.read_only?
|
||
STDERR.puts "The field '#{key}' cannot be modified (read only), skipping."
|
||
next
|
||
end
|
||
|
||
old_val = item.send(key)
|
||
old_val = [old_val] unless old_val.is_a? Array
|
||
|
||
# if val is nil or the latest value is removed, then the atribute is removed from the object
|
||
case op
|
||
when '='
|
||
new_val = [val]
|
||
when '+='
|
||
if attr_info.single_value?
|
||
STDERR.puts "The field '#{key}' cannot hold more than one value."
|
||
exit 3
|
||
end
|
||
new_val = old_val << val
|
||
when '-='
|
||
new_val = old_val - [val]
|
||
else
|
||
STDERR.puts "Syntax error in modification parameters: wrong operator"
|
||
exit 1
|
||
end
|
||
|
||
item.send(key + "=", new_val)
|
||
end
|
||
|
||
p item.valid?
|
||
item.save!
|
||
p item.modified_attributes
|
||
begin
|
||
item.save!
|
||
rescue ActiveLdap::OperationNotPermitted => e
|
||
STDERR.puts "You don't have enough rights to modify--at least--one of these attributes."
|
||
exit 3
|
||
rescue ActiveLdap::LdapError => e
|
||
STDERR.puts "LDAP error: " + e.to_s
|
||
exit 3
|
||
rescue ActiveLdap::EntryInvalid => e
|
||
STDERR.puts e.to_s
|
||
exit 3
|
||
end
|
||
|
||
Display.display_item(item, $program_options)
|
||
puts "Modification done"
|
||
end
|
||
cmdparser.add_command(cmd)
|
||
|
||
cmdparser.parse
|
||
|
||
# ActiveLdap bugs:
|
||
# - fetching operational attributes in an object used for modification cause
|
||
# collect_modified_attributes() to catch them by mistake and triggers an
|
||
# ActiveLdap::LdapError::ConstraintViolation exception
|
Also available in: Unified diff
[evol] finished field modifications (relations not managed yet)