activesambaldap 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. data/NEWS.en +9 -0
  2. data/NEWS.ja +10 -0
  3. data/README.en +310 -0
  4. data/README.ja +307 -0
  5. data/Rakefile +95 -0
  6. data/bin/asl-groupadd +70 -0
  7. data/bin/asl-groupdel +58 -0
  8. data/bin/asl-groupmod +133 -0
  9. data/bin/asl-groupshow +31 -0
  10. data/bin/asl-passwd +99 -0
  11. data/bin/asl-populate +96 -0
  12. data/bin/asl-purge +24 -0
  13. data/bin/asl-samba-computeradd +94 -0
  14. data/bin/asl-samba-groupadd +55 -0
  15. data/bin/asl-samba-groupdel +53 -0
  16. data/bin/asl-samba-groupmod +98 -0
  17. data/bin/asl-samba-useradd +98 -0
  18. data/bin/asl-samba-userdel +47 -0
  19. data/bin/asl-samba-usermod +92 -0
  20. data/bin/asl-useradd +263 -0
  21. data/bin/asl-userdel +75 -0
  22. data/bin/asl-usermod +335 -0
  23. data/bin/asl-usershow +31 -0
  24. data/lib/active_samba_ldap/account.rb +199 -0
  25. data/lib/active_samba_ldap/base.rb +126 -0
  26. data/lib/active_samba_ldap/command.rb +94 -0
  27. data/lib/active_samba_ldap/computer.rb +13 -0
  28. data/lib/active_samba_ldap/computer_account.rb +34 -0
  29. data/lib/active_samba_ldap/configuration.rb +322 -0
  30. data/lib/active_samba_ldap/dc.rb +17 -0
  31. data/lib/active_samba_ldap/entry.rb +80 -0
  32. data/lib/active_samba_ldap/group.rb +182 -0
  33. data/lib/active_samba_ldap/idmap.rb +17 -0
  34. data/lib/active_samba_ldap/ou.rb +18 -0
  35. data/lib/active_samba_ldap/populate.rb +254 -0
  36. data/lib/active_samba_ldap/samba_account.rb +200 -0
  37. data/lib/active_samba_ldap/samba_computer.rb +20 -0
  38. data/lib/active_samba_ldap/samba_group.rb +126 -0
  39. data/lib/active_samba_ldap/samba_user.rb +39 -0
  40. data/lib/active_samba_ldap/unix_id_pool.rb +41 -0
  41. data/lib/active_samba_ldap/user.rb +14 -0
  42. data/lib/active_samba_ldap/user_account.rb +30 -0
  43. data/lib/active_samba_ldap/version.rb +3 -0
  44. data/lib/active_samba_ldap.rb +29 -0
  45. data/lib/samba/encrypt.rb +86 -0
  46. data/misc/rd2html.rb +42 -0
  47. data/rails/plugin/active_samba_ldap/README +30 -0
  48. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/scaffold_asl_generator.rb +28 -0
  49. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/computer.rb +3 -0
  50. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/dc.rb +3 -0
  51. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/group.rb +3 -0
  52. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/idmap.rb +3 -0
  53. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/ldap.yml +24 -0
  54. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/ou.rb +3 -0
  55. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/samba_controller.rb +12 -0
  56. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/samba_helper.rb +2 -0
  57. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/samba_index.rhtml +17 -0
  58. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/samba_populate.rhtml +15 -0
  59. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/samba_purge.rhtml +10 -0
  60. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/unix_id_pool.rb +3 -0
  61. data/rails/plugin/active_samba_ldap/generators/scaffold_asl/templates/user.rb +3 -0
  62. data/rails/plugin/active_samba_ldap/init.rb +6 -0
  63. data/test/asl-test-utils.rb +276 -0
  64. data/test/command.rb +64 -0
  65. data/test/config.yaml.sample +17 -0
  66. data/test/run-test.rb +18 -0
  67. data/test/test-unit-ext/always-show-result.rb +28 -0
  68. data/test/test-unit-ext/priority.rb +159 -0
  69. data/test/test-unit-ext.rb +2 -0
  70. data/test/test_asl_groupadd.rb +69 -0
  71. data/test/test_asl_groupdel.rb +88 -0
  72. data/test/test_asl_groupmod.rb +256 -0
  73. data/test/test_asl_groupshow.rb +21 -0
  74. data/test/test_asl_passwd.rb +125 -0
  75. data/test/test_asl_populate.rb +92 -0
  76. data/test/test_asl_purge.rb +21 -0
  77. data/test/test_asl_useradd.rb +710 -0
  78. data/test/test_asl_userdel.rb +73 -0
  79. data/test/test_asl_usermod.rb +541 -0
  80. data/test/test_asl_usershow.rb +27 -0
  81. data/test/test_group.rb +21 -0
  82. data/test/test_password.rb +51 -0
  83. data/test/test_samba_encrypt.rb +36 -0
  84. data/test/test_user_home_directory.rb +43 -0
  85. metadata +177 -0
data/bin/asl-userdel ADDED
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'active_samba_ldap'
4
+ require 'active_samba_ldap/command'
5
+
6
+ argv, opts, options = ActiveSambaLdap::Command.parse_options do |opts, options|
7
+ options.remove_home_directory = false
8
+ options.interactive = false
9
+ options.computer_account = false
10
+
11
+ opts.banner += " USER_NAME"
12
+
13
+ opts.on("-c", "--[no-]computer-account",
14
+ "is a Windows Workstation",
15
+ "(otherwise, Windows user)",
16
+ "(#{options.computer_account})") {|options.computer_account|}
17
+
18
+ opts.on("-r", "--[no-]remove-home-directory",
19
+ "remove home directory (#{options.remove_home_directory})") do |bool|
20
+ options.remove_home_directory = bool
21
+ end
22
+
23
+ opts.on("-i", "--[no-]interactive",
24
+ "do interactively (#{options.interactive})") do |bool|
25
+ options.interactive = bool
26
+ end
27
+ end
28
+
29
+ name = nil
30
+ if argv.size == 1
31
+ name = argv.first
32
+ else
33
+ $stderr.puts opts
34
+ exit 1
35
+ end
36
+
37
+ unless Process.uid.zero?
38
+ $stderr.puts "need root authority."
39
+ exit 1
40
+ end
41
+
42
+ ActiveSambaLdap::Base.establish_connection("update")
43
+
44
+ class User < ActiveSambaLdap::SambaUser
45
+ ldap_mapping
46
+ end
47
+
48
+ class Computer < ActiveSambaLdap::SambaComputer
49
+ ldap_mapping
50
+ end
51
+
52
+ class Group < ActiveSambaLdap::SambaGroup
53
+ ldap_mapping
54
+ end
55
+
56
+ member_class = options.computer_account ? Computer : User
57
+ member_type = member_class.name.downcase
58
+
59
+ if options.computer_account
60
+ name = name.chomp("$") + "$"
61
+ end
62
+
63
+ unless member_class.exists?(name)
64
+ $stderr.puts "#{member_type} '#{name}' doesn't exist."
65
+ exit 1
66
+ end
67
+ member = member_class.find(name)
68
+
69
+ member.destroy(:removed_from_group => true,
70
+ :remove_home_directory => options.remove_home_directory,
71
+ :remove_home_directory_interactive => options.interactive)
72
+
73
+ ActiveSambaLdap::Base.restart_nscd
74
+
75
+ ActiveSambaLdap::Base.clear_active_connections!
data/bin/asl-usermod ADDED
@@ -0,0 +1,335 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'time'
4
+ require 'fileutils'
5
+
6
+ require 'active_samba_ldap'
7
+ require 'active_samba_ldap/command'
8
+
9
+ argv, opts, options = ActiveSambaLdap::Command.parse_options do |opts, options|
10
+ options.computer_account = false
11
+ options.gecos = nil
12
+ options.home_directory = nil
13
+ options.move_home_directory = false
14
+ # options.inactive_days = nil
15
+ options.new_user_name = nil
16
+ options.uid = nil
17
+ options.allow_non_unique_uid_number = false
18
+ options.gid = nil
19
+ options.merge_groups = true
20
+ options.supplementary_groups = nil
21
+ options.shell = nil
22
+ options.given_name = nil
23
+ options.canonical_name = nil
24
+ options.surname = nil
25
+
26
+ options.expire_date = nil
27
+ options.can_change_password = nil
28
+ options.must_change_password = nil
29
+ options.samba_home_path = nil
30
+ options.samba_home_drive = nil
31
+ options.samba_logon_script = nil
32
+ options.samba_profile_path = nil
33
+ options.samba_account_flags = nil
34
+ options.enable = nil
35
+ options.mail_addresses = nil
36
+ options.mail_to_addresses = nil
37
+
38
+
39
+ opts.banner += " USER_NAME"
40
+
41
+ opts.on("-c", "--[no-]computer-account",
42
+ "is a Windows Workstation",
43
+ "(otherwise, Windows user)",
44
+ "(#{options.computer_account})") {|options.computer_account|}
45
+
46
+ opts.on("--gecos=GECOS", "gecos") {|options.gecos|}
47
+ opts.on("-d", "--home-directory=HOME_DIR",
48
+ "home directory") {|options.home_directory|}
49
+ opts.on("-m", "--[no-]move-home-directory",
50
+ "move home directory (#{options.move_home_directory})") do |bool|
51
+ options.move_home_directory = bool
52
+ end
53
+ opts.on("-r", "--rename=NEW_NAME",
54
+ "new user name (cn and dn are updated)") do |name|
55
+ options.new_user_name = name
56
+ end
57
+ opts.on("-u", "--uid=UID", Integer, "uid") {|options.uid|}
58
+ opts.on("--[no-]allow-non-unique-uid",
59
+ "uid can be non unique " +
60
+ "(#{options.allow_non_unique_uid_number})") do |bool|
61
+ options.allow_non_unique_uid_number = bool
62
+ end
63
+ opts.on("-g", "--gid=GID", "gid") {|options.gid|}
64
+ opts.on("-G", "--groups=GID1,GID2,GID3", Array,
65
+ "supplementary groups (comma separated)") do |groups|
66
+ options.supplementary_groups = groups
67
+ end
68
+ opts.on("--[no-]merge-groups",
69
+ "replace supplementary groups " +
70
+ "(#{!options.merge_groups})") {|options.merge_groups|}
71
+ opts.on("-s", "--shell=SHELL", "shell") {|options.shell|}
72
+ opts.on("--given-name=NAME", "given name") {|options.given_name|}
73
+ opts.on("-N", "--canonical-name=NAME",
74
+ "canonical name") {|options.canonical_name|}
75
+ opts.on("-S", "--surname=NAME", "surname") {|options.surname|}
76
+
77
+ opts.separator("")
78
+ opts.separator("For samba users:")
79
+
80
+ opts.on("-e", "--expire-date=DATE", "expire date") do |date|
81
+ options.expire_date = Time.parse(date)
82
+ end
83
+ opts.on("-C", "--[no-]can-change-password",
84
+ "can change password") do |bool|
85
+ options.can_change_password = bool
86
+ end
87
+ opts.on("-M", "--[no-]must-change-password",
88
+ "must change password") do |bool|
89
+ options.must_change_password = bool
90
+ end
91
+ opts.on("--samba-home-path=PATH",
92
+ "sambaHomePath",
93
+ "(SMB home share, like '\\\\PDC\\user'") do |path|
94
+ options.samba_home_path = path
95
+ end
96
+ opts.on("--samba-home-drive=DRIVE",
97
+ "sambaHomeDrive",
98
+ "(letter associated with home share,",
99
+ " like 'H:')") do |drive|
100
+ options.samba_home_drive = drive
101
+ end
102
+ opts.on("--samba-logon-script=SCRIPT",
103
+ "sambaLogonScript",
104
+ "(DOS script to execute on login)") do |script|
105
+ options.samba_logon_script = script
106
+ end
107
+ opts.on("--samba-profile-path=PATH",
108
+ "sambaProfilePath",
109
+ "(profile directory,",
110
+ " like '\\\\PDC\\profiles\\user')") do |path|
111
+ options.samba_profile_path = path
112
+ end
113
+ opts.on("--samba-account-flags=FLAGS",
114
+ "sambaAcctFlags",
115
+ "(samba account control bits,",
116
+ " like '[NDHTUMWSLXI]')") {|options.samba_account_flags|}
117
+ opts.on("-D", "--[no-]disable-user", "disable an user") do |bool|
118
+ options.enable = !bool
119
+ end
120
+ opts.on("-E", "--[no-]enable-user", "enable an user") do |bool|
121
+ options.enable = bool
122
+ end
123
+ # opts.on("--mail-addresses=ADDRESS1,ADDRESS2,ADDRESS3",
124
+ # Array,
125
+ # "mailAddresses (comma separated)") {|options.mail_addresses|}
126
+ # opts.on("--mail-to-addresses=ADDRESS1,ADDRESS2,ADDRESS3",
127
+ # Array,
128
+ # "mailToAddresses (forward address)",
129
+ # "(comma separated)") do |addresses|
130
+ # options.mail_to_addresses = addresses
131
+ # end
132
+ end
133
+
134
+ name = nil
135
+ if argv.size == 1
136
+ name = argv.first
137
+ else
138
+ $stderr.puts opts
139
+ exit 1
140
+ end
141
+
142
+ ActiveSambaLdap::Base.establish_connection("update")
143
+
144
+ class User < ActiveSambaLdap::SambaUser
145
+ ldap_mapping
146
+ end
147
+
148
+ class Computer < ActiveSambaLdap::SambaComputer
149
+ ldap_mapping
150
+ end
151
+
152
+ class Group < ActiveSambaLdap::SambaGroup
153
+ ldap_mapping
154
+ end
155
+
156
+ options.computer_account = true if /\$$/ =~ name
157
+
158
+ member_class = options.computer_account ? Computer : User
159
+ member_type = member_class.name.downcase
160
+
161
+ if options.computer_account
162
+ name = name.chomp("$") + "$"
163
+ end
164
+
165
+ unless member_class.exists?(name)
166
+ $stderr.puts "#{member_type} '#{name}' doesn't exist."
167
+ exit 1
168
+ end
169
+ member = member_class.find(name)
170
+
171
+ unless Process.uid.zero?
172
+ password = ActiveSambaLdap::Command.read_password("Enter your password: ")
173
+
174
+ auth_class = Class.new(ActiveSambaLdap::Base)
175
+ config = ActiveSambaLdap::Base.configurations["reference"]
176
+ begin
177
+ auth_class.establish_connection(config.merge(:bind_dn => member.dn,
178
+ :password => password,
179
+ :allow_anonymous => false))
180
+ rescue ActiveLdap::AuthenticationError
181
+ $stderr.puts "password isn't match"
182
+ exit 1
183
+ ensure
184
+ auth_class.remove_connection
185
+ end
186
+ end
187
+
188
+ if options.uid
189
+ begin
190
+ member.change_uid_number(options.uid, options.allow_non_unique_uid_number)
191
+ rescue ActiveSambaLdap::UidNumberAlreadyExists
192
+ $stderr.puts $!.message
193
+ exit 1
194
+ end
195
+ end
196
+
197
+ if options.gid
198
+ begin
199
+ member.primary_group = Group.find_by_name_or_gid_number(options.gid)
200
+ rescue ActiveSambaLdap::Error
201
+ $stderr.puts $!.message
202
+ exit 1
203
+ end
204
+ end
205
+
206
+ if options.shell
207
+ member.login_shell = options.shell
208
+ end
209
+
210
+ if options.gecos
211
+ member.gecos = options.gecos
212
+ member.description = options.gecos
213
+ member.display_name = options.gecos
214
+ end
215
+
216
+ if options.home_directory
217
+ if options.move_home_directory and !File.exist?(options.home_directory)
218
+ FileUtils.mv(member.home_directory, options.home_directory)
219
+ end
220
+ member.home_directory = options.home_directory
221
+ end
222
+
223
+ if options.canonical_name
224
+ member.cn = options.canonical_name
225
+ end
226
+
227
+ if options.surname
228
+ member.sn = options.surname
229
+ end
230
+
231
+ if options.given_name
232
+ member.given_name = options.given_name
233
+ end
234
+
235
+ if options.mail_addresses
236
+ raise "not implemented"
237
+ end
238
+
239
+ if options.mail_to_addresses
240
+ raise "not implemented"
241
+ end
242
+
243
+ if options.supplementary_groups
244
+ member.groups = [] unless options.merge_groups
245
+ member.groups = options.supplementary_groups.collect do |group|
246
+ begin
247
+ Group.find_by_name_or_gid_number(group)
248
+ rescue ActiveSambaLdap::GidNumberDoesNotExist
249
+ $stderr.puts $!
250
+ exit 1
251
+ end
252
+ end
253
+ end
254
+
255
+ if options.expire_date
256
+ member.samba_kickoff_time = options.expire_date.to_i.to_s
257
+ end
258
+
259
+ if options.samba_account_flags
260
+ member.samba_acct_flags = options.samba_account_flags
261
+ end
262
+
263
+ unless options.can_change_password.nil?
264
+ if options.can_change_password
265
+ member.enable_password_change
266
+ else
267
+ member.disable_password_change
268
+ end
269
+ end
270
+
271
+ unless options.must_change_password.nil?
272
+ if options.must_change_password
273
+ member.enable_forcing_password_change
274
+ else
275
+ member.disable_forcing_password_change
276
+ end
277
+ end
278
+
279
+ if options.samba_home_path
280
+ member.samba_home_path = options.samba_home_path
281
+ end
282
+
283
+ if options.samba_home_drive
284
+ member.samba_home_drive = options.samba_home_drive.sub(/([^:])$/, "\\1:")
285
+ end
286
+
287
+ if options.samba_logon_script
288
+ member.samba_logon_script = options.samba_logon_script
289
+ end
290
+
291
+ if options.samba_profile_path
292
+ member.samba_profile_path = options.samba_profile_path
293
+ end
294
+
295
+ unless options.enable.nil?
296
+ if options.enable
297
+ member.enable
298
+ else
299
+ member.disable
300
+ end
301
+ end
302
+
303
+ member.save!
304
+
305
+ if options.new_user_name
306
+ if options.computer_account
307
+ options.new_user_name = options.new_user_name.chomp("$") + "$"
308
+ end
309
+ if member_class.exists?(options.new_user_name)
310
+ $stderr.puts "#{member_type} '#{options.new_user_name}' always exists."
311
+ exit 1
312
+ end
313
+ new_member = member_class.new(options.new_user_name)
314
+
315
+ new_member.cn = options.new_user_name
316
+ new_member.attributes = member.attributes.reject do |key, value|
317
+ %w(dn cn uid).include?(key)
318
+ end
319
+ new_member.save!
320
+ member.groups.each do |group|
321
+ if options.computer_account
322
+ group.computers -= [member]
323
+ group.computers << new_member
324
+ else
325
+ group.users -= [member]
326
+ group.users << new_member
327
+ end
328
+ end
329
+
330
+ member.destroy
331
+ end
332
+
333
+ ActiveSambaLdap::Base.restart_nscd
334
+
335
+ ActiveSambaLdap::Base.clear_active_connections!
data/bin/asl-usershow ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'active_samba_ldap'
4
+ require 'active_samba_ldap/command'
5
+
6
+ argv, opts, options = ActiveSambaLdap::Command.parse_options do |opts, options|
7
+ opts.banner += " USER_NAME"
8
+ end
9
+
10
+ name = nil
11
+ if argv.size == 1
12
+ name = argv.first
13
+ else
14
+ $stderr.puts opts
15
+ exit 1
16
+ end
17
+
18
+ ActiveSambaLdap::Base.establish_connection("reference")
19
+
20
+ class User < ActiveSambaLdap::SambaUser
21
+ ldap_mapping
22
+ end
23
+
24
+ unless User.exists?(name)
25
+ $stderr.puts "user '#{name}' doesn't exist."
26
+ exit 1
27
+ end
28
+ user = User.find(name)
29
+ puts user.to_ldif
30
+
31
+ ActiveSambaLdap::Base.clear_active_connections!
@@ -0,0 +1,199 @@
1
+ require 'time'
2
+ require 'fileutils'
3
+ require 'English'
4
+
5
+ module ActiveSambaLdap
6
+ module Account
7
+ NAME_RE_SRC = "(?!\\d)[\\w @_\\-\\.]+"
8
+
9
+ def self.included(base)
10
+ super
11
+ base.extend(ClassMethods)
12
+ end
13
+
14
+ module ClassMethods
15
+ def ldap_mapping(options={})
16
+ options = default_options.merge(options)
17
+ super(extract_ldap_mapping_options(options))
18
+ belongs_to :primary_group, primary_group_options(options)
19
+ belongs_to :groups, groups_options(options)
20
+ end
21
+
22
+ def find_by_uid_number(number)
23
+ options = {:objects => true}
24
+ attribute = "uidNumber"
25
+ value = Integer(number)
26
+ find(:first, :filter => "(#{attribute}=#{value})")
27
+ end
28
+
29
+ private
30
+ def default_options
31
+ {
32
+ :dn_attribute => "uid",
33
+ :ldap_scope => :sub,
34
+ :primary_group_class => default_group_class,
35
+ :primary_group_foreign_key => "gidNumber",
36
+ :primary_group_primary_key => "gidNumber",
37
+ :groups_class => default_group_class,
38
+ :groups_many => "memberUid",
39
+ :prefix => default_prefix,
40
+ :classes => default_classes,
41
+ }
42
+ end
43
+
44
+ def default_group_class
45
+ "Group"
46
+ end
47
+
48
+ def default_classes
49
+ ["top", "inetOrgPerson", "posixAccount"]
50
+ end
51
+
52
+ def primary_group_options(options)
53
+ {
54
+ :class => options[:primary_group_class],
55
+ :foreign_key => options[:primary_group_foreign_key],
56
+ :primary_key => options[:primary_group_primary_key],
57
+ }
58
+ end
59
+
60
+ def groups_options(options)
61
+ {
62
+ :class => options[:groups_class],
63
+ :many => options[:groups_many],
64
+ }
65
+ end
66
+
67
+ def prepare_create_options(account, options)
68
+ prepare_create_options_for_number(:uid_number, account, options)
69
+ end
70
+ end
71
+
72
+ def fill_default_values(options={})
73
+ self.cn ||= uid
74
+ self.sn ||= uid
75
+ self.given_name ||= uid
76
+ self.display_name ||= cn
77
+ self.gecos ||= substituted_value(:user_gecos) {cn}
78
+ self.home_directory ||= substituted_value(:user_home_directory)
79
+ self.login_shell ||= self.class.configuration[:user_login_shell]
80
+
81
+ password = options[:password]
82
+ change_password(password) if password
83
+ self.user_password ||= "{crypt}x"
84
+
85
+ uid_number = options[:uid_number]
86
+ self.change_uid_number(uid_number) if uid_number
87
+
88
+ group = options[:group] || retrieve_default_group(options)
89
+ self.primary_group = group if group
90
+
91
+ self
92
+ end
93
+
94
+ def destroy(options={})
95
+ if options[:removed_from_group]
96
+ groups.each do |group|
97
+ remove_from_group(group)
98
+ end
99
+ end
100
+ dir = home_directory
101
+ need_remove_home_directory =
102
+ options[:remove_home_directory] && !new_entry?
103
+ super()
104
+ if need_remove_home_directory and File.directory?(dir)
105
+ if options[:remove_home_directory_interactive]
106
+ system("rm", "-r", "-i", dir)
107
+ else
108
+ FileUtils.rm_r(dir)
109
+ end
110
+ end
111
+ new_entry?
112
+ end
113
+
114
+ def change_uid_number(uid, allow_non_unique=false)
115
+ check_unique_uid_number(uid) unless allow_non_unique
116
+ self.uid_number = Integer(uid).to_s
117
+ end
118
+
119
+ def change_password(password)
120
+ hash_type = self.class.configuration[:password_hash_type]
121
+ hashed_password = ActiveLdap::UserPassword.__send__(hash_type, password)
122
+ self.user_password = hashed_password
123
+ end
124
+
125
+ def setup_home_directory(options={})
126
+ dest = home_directory
127
+ return unless dest
128
+
129
+ FileUtils.mkdir_p(dest)
130
+ mode = options[:mode]
131
+ mode ||= self.class.configuration[:user_home_directory_mode]
132
+ FileUtils.chmod(Integer(mode), dest)
133
+ skel = options[:skeleton_directory]
134
+ skel ||= self.class.configuration[:skeleton_directory]
135
+ FileUtils.cp_r(Dir.glob(File.join(skel, ".*")) +
136
+ Dir.glob(File.join(skel, "*")) -
137
+ [File.join(skel, "."), File.join(skel, "..")],
138
+ dest)
139
+ FileUtils.chown_R(uid_number, gid_number, dest)
140
+ end
141
+
142
+ private
143
+ def check_unique_uid_number(uid_number)
144
+ ActiveSambaLdap::Base.restart_nscd do
145
+ if self.class.find_by_uid_number(uid_number)
146
+ raise UidNumberAlreadyExists.new(uid_number)
147
+ end
148
+ end
149
+ end
150
+
151
+ def substitute_template(template)
152
+ template.gsub(/%U/, uid)
153
+ end
154
+
155
+ def substituted_value(key)
156
+ config = self.class.configuration
157
+ if block_given?
158
+ value = config[key.to_sym]
159
+ if value
160
+ substitute_template(value)
161
+ else
162
+ yield
163
+ end
164
+ else
165
+ substitute_template(config[key.to_sym])
166
+ end
167
+ end
168
+
169
+ def created_group_name
170
+ uid
171
+ end
172
+
173
+ def retrieve_default_group(options={})
174
+ group = nil
175
+
176
+ gid_number = options[:gid_number]
177
+ group_class = options[:group_class]
178
+ unless gid_number
179
+ if options[:create_group]
180
+ group_name = created_group_name
181
+ if group_class.exists?(group_name)
182
+ group = group_class.find(group_name)
183
+ else
184
+ group = group_class.create(:cn => group_name,
185
+ :pool => options[:pool],
186
+ :pool_class => options[:pool_class])
187
+ end
188
+ else
189
+ gid_number = default_gid_number
190
+ end
191
+ end
192
+ if gid_number
193
+ group = group_class.find_by_gid_number(gid_number)
194
+ end
195
+
196
+ group
197
+ end
198
+ end
199
+ end