google-code-export / ruby-activeldap

Automatically exported from code.google.com/p/ruby-activeldap
Other
1 stars 1 forks source link

Strangeness with DN #33

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create model role.rb:
class Role < ActiveLdap::Base
  ldap_mapping :dn_attribute => 'i2Canonic', :prefix => 
'ou=domains', :classes => ['Role'], :scope => :one, :sort_by => 'i2Canonic'

  attr_accessor :i2Domain

private

  def validate_on_create
    unless self.id.blank?
      unless self.i2Domain.blank?
        roles = OrgUnit.find(:attribute => "ou", :value => 
"roles", :prefix => "dc=#{self.i2Domain},ou=domains")
        self.dn = "#{self.dn_attribute}=#{self.id},#{roles.dn}"
      end
    end
  end

end
2. Run script/console
3. Create new Role:
?> role = Role.create(:i2Canonic => "USER3", :i2Name => "User", :i2Domain 
=> "test3.com")
=> #<Role objectClass:<Role>, must:<i2Canonic, i2Name>, may:<description, 
i2Right>, description: [], i2Canonic: ["USER2"], i2Name: ["User"], 
i2Right: [], objectClass: ["Role"]>

Checking value of DN:

>> old_rdn = role.id
=> "USER3"

>> role.base
=> #<ActiveLdap::DistinguishedName:0xa02c4a8 @rdns=[{"ou"=>"roles"}, 
{"dc"=>"test3.com"}, {"ou"=>"domains"}, {"dc"=>"..."}, {"dc"=>""}, 
{"dc"=>""}, {"dc"=>"com"}]>

>> old_dn = role.dn
=> #<ActiveLdap::DistinguishedName:0xa017844 @rdns=
[{"i2Canonic"=>"USER3"}, {"ou"=>"roles"}, {"dc"=>"test3.com"}, 
{"ou"=>"domains"}, {"dc"=>"..."}, {"dc"=>""}, {"dc"=>""}, {"dc"=>"com"}]>

I trying change id (The same happens when I trying change rdn)

>> role.id = "USER2"
=> "USER2"

>> role.base
=> #<ActiveLdap::DistinguishedName:0xa02c4a8 @rdns=[{"ou"=>"roles"}, 
{"dc"=>"test3.com"}, {"ou"=>"domains"}, {"dc"=>"..."}, {"dc"=>""}, 
{"dc"=>""}, {"dc"=>"com"}]>

Here is strange behavior:

>> role.dn
=> #<ActiveLdap::DistinguishedName:0x9ee6474 @rdns=
[{"i2Canonic"=>"USER2"}, {"ou"=>"domains"}, {"dc"=>"..."}, {"dc"=>""}, 
{"dc"=>""}, {"dc"=>"com"}]>

>> role.base
=> #<ActiveLdap::DistinguishedName:0xa342a7c @rdns=[{"ou"=>"domains"}, 
{"dc"=>"..."}, {"dc"=>""}, {"dc"=>""}, {"dc"=>"com"}]>

So, when I tried save record, I got error:

?> role.save
ActiveLdap::NotImplemented: not implemented: modify RDN with new superior
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
adapter/ldap.rb:162:in `block in modify_rdn'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
adapter/base.rb:238:in `block in modify_rdn'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
adapter/base.rb:270:in `block in operation'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
timeout.rb:15:in `call'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
timeout.rb:15:in `alarm'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
adapter/base.rb:316:in `with_timeout'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
adapter/base.rb:269:in `operation'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
adapter/base.rb:237:in `modify_rdn'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
adapter/ldap.rb:160:in `modify_rdn'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
operations.rb:559:in `modify_rdn_entry'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
base.rb:1562:in `block in update'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
base.rb:1526:in `prepare_data_for_saving'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
base.rb:1551:in `update'
 from /usr/lib/ruby/gems/1.9.1/gems/activerecord-2.3.4/lib/active_record/
callbacks.rb:282:in `update_with_callbacks'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
base.rb:1511:in `create_or_update'
 from /usr/lib/ruby/gems/1.9.1/gems/activerecord-2.3.4/lib/active_record/
callbacks.rb:250:in `create_or_update_with_callbacks'
 from /usr/lib/ruby/gems/1.9.1/gems/activeldap-1.2.0/lib/active_ldap/
base.rb:816:in `save'
 from /usr/lib/ruby/gems/1.9.1/gems/activerecord-2.3.4/lib/active_record/
validations.rb:1078:in `save_with_validation'
 from (irb):182
 from /usr/bin/irb:12:in `<main>'>>
?>

I would suggest solution: Generate new DN from old DN and replace old rdn 
when not present new_superior (only for created records)

?> role.dn = old_dn.to_s.gsub("USER3", "USER2")
=> 
"i2Canonic=USER2,ou=roles,dc=test3.com,ou=domains,dc=...,dc=...,dc=...,dc=com"
>>
?> role.save
=> true

LDAP: modify: RDN (4.1ms): 
{:dn=>"i2Canonic=USER3,ou=roles,dc=test3.com,ou=domains,dc=...,dc=...,dc=...,dc=
com", :new_rdn=>"i2Canonic=USER2", :new_superior=>nil, :delete_old_rdn=>true}

Original issue reported on code.google.com by Alexey.Chebotar@gmail.com on 10 Oct 2009 at 7:24

GoogleCodeExporter commented 9 years ago
It seems that different behavior on create/save causes confusion.
What about creating #rdn= method?

P.S. It seems that you should use before_save or before_validation not
validate_on_create.

Original comment by kou...@gmail.com on 12 Oct 2009 at 2:34

GoogleCodeExporter commented 9 years ago
What this method will do? What will be with method #id= ?

I will try check it with before_save or before_validation little bit later

P.S. Will be great if you can make works all callbacks (like before_create).

Original comment by Alexey.Chebotar@gmail.com on 14 Oct 2009 at 11:07

GoogleCodeExporter commented 9 years ago
#rdn= will work like you want but #id= will not be changed.

{before,after}_create are invoked just for new entries. {before,after}_save are
invoked for all entries.

Original comment by kou...@gmail.com on 16 Oct 2009 at 12:21

GoogleCodeExporter commented 9 years ago
> #id= will not be changed.

It means #id= behavior will not be changed.

Original comment by kou...@gmail.com on 16 Oct 2009 at 12:23