File: persistence.rb

package info (click to toggle)
ruby-activeldap 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,588 kB
  • sloc: ruby: 18,143; sh: 12; makefile: 5
file content (102 lines) | stat: -rw-r--r-- 2,486 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module ActiveLdap
  module Persistence
    # new_entry?
    #
    # Return whether the entry is new entry in LDAP or not
    def new_entry?
      @new_entry
    end

    # Return whether the entry is saved entry or not.
    def persisted?
      not new_entry?
    end

    # destroy
    #
    # Delete this entry from LDAP
    def destroy
      # TODO: support deleting relations
      delete
    end

    def delete(options={})
      if persisted?
        default_options = {
          :connection => connection,
        }
        self.class.delete_entry(dn, default_options.merge(options))
      end
      @new_entry = true
      freeze
    end

    # save
    #
    # Save and validate this object into LDAP
    # either adding or replacing attributes
    # TODO: Relative DN support
    def save(*)
      create_or_update
    end

    def save!(*)
      unless create_or_update
        raise EntryNotSaved, _("entry %s can't be saved") % dn
      end
      true
    end

    def create_or_update
      new_entry? ? create : update
    end

    def create
      prepare_data_for_saving do |data, ldap_data|
        attributes = collect_all_attributes(data)
        add_entry(dn, attributes)
        @new_entry = false
        true
      end
    end

    def update
      prepare_data_for_saving do |data, ldap_data|
        new_dn_value, attributes = collect_modified_attributes(ldap_data, data)
        modify_entry(@original_dn, attributes)
        if new_dn_value
          old_dn_base = DN.parse(@original_dn).parent
          new_dn_base = dn.clone.parent
          if old_dn_base == new_dn_base
            new_superior = nil
          else
            new_superior = new_dn_base.to_s
          end
          modify_rdn_entry(@original_dn,
                           "#{dn_attribute}=#{DN.escape_value(new_dn_value)}",
                           true,
                           new_superior)
        end
        true
      end
    end

    def reload(options={})
      clear_association_cache
      search_options = options.merge(value: id)
      _, attributes = search(search_options).find do |_dn, _attributes|
        dn == _dn
      end
      if attributes.nil?
        raise EntryNotFound, _("Can't find DN '%s' to reload") % dn
      end

      @ldap_data.update(attributes)
      classes = extract_object_class!(attributes)
      self.classes = classes
      self.attributes = attributes
      @new_entry = false
      self
    end
  end # Persistence
end # ActiveLdap