Class: ActiveSambaLdap::Populate::ClassMethods::Private

Inherits:
Object
  • Object
show all
Defined in:
lib/active_samba_ldap/populate.rb

Instance Method Summary collapse

Constructor Details

#initialize(base, options) ⇒ Private

Returns a new instance of Private.



17
18
19
20
# File 'lib/active_samba_ldap/populate.rb', line 17

def initialize(base, options)
  @base = base
  @options = options.dup
end

Instance Method Details

#computer_classObject



71
72
73
# File 'lib/active_samba_ldap/populate.rb', line 71

def computer_class
  @options[:computer_class]
end

#ensure_baseObject



125
126
127
128
129
# File 'lib/active_samba_ldap/populate.rb', line 125

def ensure_base
  ensure_container_base(@base.base, "dc", Dc, true) do |dc|
    dc.o = dc.dc
  end
end

#ensure_computer_baseObject



143
144
145
# File 'lib/active_samba_ldap/populate.rb', line 143

def ensure_computer_base
  ensure_ou_base(computer_class.prefix)
end

#ensure_container_base(dn, target_name, klass, ignore_base = false) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_samba_ldap/populate.rb', line 97

def ensure_container_base(dn, target_name, klass, ignore_base=false)
  dn = dn.to_s if dn.is_a?(ActiveLdap::DN)
  entries = []
  suffixes = []
  dn.split(/,/).reverse_each do |suffix|
    name, value = suffix.split(/=/, 2)
    next unless name == target_name
    container_class = Class.new(klass)
    prefix = suffixes.reverse.join(",")
    suffixes << suffix
    if ignore_base
      container_class.ldap_mapping :prefix => "", :scope => :base
      container_class.base = prefix
    else
      container_class.ldap_mapping :prefix => prefix, :scope => :base
    end
    next if container_class.exists?(value, :prefix => suffix)
    container = container_class.new(value)
    yield(container) if block_given?
    begin
      container.save!
    rescue ActiveLdap::OperationNotPermitted
    end
    entries << container
  end
  entries
end

#ensure_group_baseObject



139
140
141
# File 'lib/active_samba_ldap/populate.rb', line 139

def ensure_group_base
  ensure_ou_base(group_class.prefix)
end

#ensure_idmap_baseObject



147
148
149
# File 'lib/active_samba_ldap/populate.rb', line 147

def ensure_idmap_base
  ensure_ou_base(idmap_class.prefix)
end

#ensure_ou_base(dn) ⇒ Object



131
132
133
# File 'lib/active_samba_ldap/populate.rb', line 131

def ensure_ou_base(dn)
  ensure_container_base(dn, "ou", Ou)
end

#ensure_user_baseObject



135
136
137
# File 'lib/active_samba_ldap/populate.rb', line 135

def ensure_user_base
  ensure_ou_base(user_class.prefix)
end

#group_classObject



67
68
69
# File 'lib/active_samba_ldap/populate.rb', line 67

def group_class
  @options[:group_class]
end

#idmap_classObject



75
76
77
# File 'lib/active_samba_ldap/populate.rb', line 75

def idmap_class
  @options[:idmap_class]
end

#init_classesObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_samba_ldap/populate.rb', line 39

def init_classes
  @options[:user_class] = user_class = Class.new(User)
  @options[:group_class] = group_class = Class.new(Group)
  @options[:computer_class] = computer_class = Class.new(Computer)
  @options[:idmap_class] = idmap_class = Class.new(Idmap)
  @options[:unix_id_pool_class] = id_pool_class = Class.new(UnixIdPool)

  user_class.ldap_mapping
  group_class.ldap_mapping
  computer_class.ldap_mapping
  idmap_class.ldap_mapping
  id_pool_class.ldap_mapping

  user_class.set_associated_class(:primary_group, group_class)
  computer_class.set_associated_class(:primary_group, group_class)
  user_class.set_associated_class(:groups, group_class)
  computer_class.set_associated_class(:groups, group_class)

  group_class.set_associated_class(:users, user_class)
  group_class.set_associated_class(:computers, computer_class)
  group_class.set_associated_class(:primary_users, user_class)
  group_class.set_associated_class(:primary_computers, computer_class)
end

#init_optionsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/active_samba_ldap/populate.rb', line 79

def init_options
  config = @base.configuration
  @options[:start_uid] ||= Integer(config[:start_uid])
  @options[:start_gid] ||= Integer(config[:start_gid])
  @options[:administrator] ||= user_class::DOMAIN_ADMIN_NAME
  @options[:administrator_uid] ||=
    user_class.rid2uid(user_class::DOMAIN_ADMIN_RID)
  @options[:administrator_gid] ||=
    group_class.rid2gid(group_class::DOMAIN_ADMINS_RID)
  @options[:guest] ||= user_class::DOMAIN_GUEST_NAME
  @options[:guest_uid] ||=
    user_class.rid2uid(user_class::DOMAIN_GUEST_RID)
  @options[:guest_gid] ||=
    group_class.rid2gid(group_class::DOMAIN_GUESTS_RID)
  @options[:default_user_gid] ||= config[:default_user_gid]
  @options[:default_computer_gid] ||= config[:default_computer_gid]
end

#make_group(group_class, name, gid, description = nil, type = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/active_samba_ldap/populate.rb', line 191

def make_group(group_class, name, gid, description=nil, type=nil)
  if group_class.exists?(name)
    group = group_class.find(name)
  else
    group = group_class.new(name)
    group.change_type(type || "domain")
    group.display_name = name
    group.description = name || description
    group.change_gid_number(gid)

    group.save!
  end
  group
end

#make_groupsObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/active_samba_ldap/populate.rb', line 206

def make_groups
  entries = []
  [
   ["Domain Admins", @options[:administrator_gid],
    "Netbios Domain Administrators"],
   ["Domain Users", @options[:default_user_gid],
    "Netbios Domain Users"],
   ["Domain Guests", @options[:guest_gid],
    "Netbios Domain Guest Users"],
   ["Domain Computers", @options[:default_computer_gid],
    "Netbios Domain Computers"],
   ["Administrators", nil, nil, "builtin",
    group_class::LOCAL_ADMINS_RID],
   ["Users", nil, nil, "builtin", group_class::LOCAL_USERS_RID],
   ["Guests", nil, nil, "builtin", group_class::LOCAL_GUESTS_RID],
   ["Power Users", nil, nil, "builtin",
    group_class::LOCAL_POWER_USERS_RID],
   ["Account Operators", nil, nil, "builtin",
    group_class::LOCAL_ACCOUNT_OPERATORS_RID],
   ["System Operators", nil, nil, "builtin",
    group_class::LOCAL_SYSTEM_OPERATORS_RID],
   ["Print Operators", nil, nil, "builtin",
    group_class::LOCAL_PRINT_OPERATORS_RID],
   ["Backup Operators", nil, nil, "builtin",
    group_class::LOCAL_BACKUP_OPERATORS_RID],
   ["Replicators", nil, nil, "builtin",
    group_class::LOCAL_REPLICATORS_RID],
  ].each do |name, gid, description, type, rid|
    gid ||= group_class.rid2gid(rid)
    entries << make_group(group_class, name, gid, description, type)
  end
  entries
end

#make_poolObject



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/active_samba_ldap/populate.rb', line 240

def make_pool
  config = @base.configuration
  klass = @options[:unix_id_pool_class]
  name = config[:samba_domain]
  if klass.exists?(name)
    pool = klass.find(name)
  else
    pool = klass.new(name)
    pool.samba_sid = config[:sid]
    pool.uid_number = @options[:start_uid]
    pool.gid_number = @options[:start_gid]
    pool.save!
  end
  [pool]
end

#make_user(user_class, name, uid, group) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/active_samba_ldap/populate.rb', line 151

def make_user(user_class, name, uid, group)
  if user_class.exists?(name)
    user = user_class.find(name)
    group = nil
  else
    user = user_class.new(name)
    user.fill_default_values("uid_number" => uid, "group" => group)
    user.save!
    group.users << user
  end
  [user, group]
end

#make_usersObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/active_samba_ldap/populate.rb', line 164

def make_users
  user_class = @options[:user_class]
  group_class = @options[:group_class]
  entries = []
  [
   [@options[:administrator], @options[:administrator_uid],
    @options[:administrator_gid]],
   [@options[:guest], @options[:guest_uid], @options[:guest_gid]],
  ].each do |name, uid, gid|
    user, group = make_user(user_class, name, uid,
                            group_class.find_by_gid_number(gid))
    entries << user
    if group
      old_group = entries.find do |entry|
        entry.is_a?(group_class) and entry.cn == group.cn
      end
      index = entries.index(old_group)
      if index
        entries[index] = group
      else
        entries << group
      end
    end
  end
  entries
end

#populateObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_samba_ldap/populate.rb', line 22

def populate
  init_classes
  init_options

  entries = []
  entries.concat(ensure_base)
  entries.concat(ensure_group_base)
  entries.concat(ensure_user_base)
  entries.concat(ensure_computer_base)
  entries.concat(ensure_idmap_base)
  entries.concat(make_groups)
  entries.concat(make_users)
  entries.concat(make_pool)

  [entries, @options]
end

#user_classObject



63
64
65
# File 'lib/active_samba_ldap/populate.rb', line 63

def user_class
  @options[:user_class]
end