open_directory_utils 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,680 @@
1
+ require "open_directory_utils/dscl"
2
+ require "open_directory_utils/clean_check"
3
+
4
+ module OpenDirectoryUtils
5
+
6
+ # this is a long list of pre-built dscl commands affecting users to accomplish common actions
7
+ # @note - these commands were derived from the following resrouces:
8
+ # * https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/dscl.1.html
9
+ # * https://superuser.com/questions/592921/mac-osx-users-vs-dscl-command-to-list-user/621055?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
10
+ module CommandsUser
11
+
12
+ include OpenDirectoryUtils::Dscl
13
+ include OpenDirectoryUtils::CleanCheck
14
+
15
+ def user_record_name_alternatives(attribs)
16
+ attribs[:record_name] = nil
17
+ attribs[:record_name] = attribs[:user_name]
18
+ attribs[:record_name] = attribs[:record_name] || attribs[:short_name]
19
+ attribs[:record_name] = attribs[:record_name] || attribs[:shortname]
20
+ attribs[:record_name] = attribs[:record_name] || attribs[:username]
21
+ attribs[:record_name] = attribs[:record_name] || attribs[:uid]
22
+ return attribs
23
+ end
24
+
25
+ # GET INFO
26
+ ##########
27
+ # get user record -- dscl . -read /Users/<username>
28
+ # get user value -- dscl . -read /Users/<username> <key>
29
+ # search od user -- dscl . -search /Users RealName "Andrew Garrett"
30
+ # return as xml -- dscl -plist . -search /Users RealName "Andrew Garrett"
31
+ def user_get_info(attribs, dir_info)
32
+ attribs = user_record_name_alternatives(attribs)
33
+
34
+ check_critical_attribute( attribs, :record_name )
35
+ attribs = tidy_attribs(attribs)
36
+
37
+ command = {action: 'read', scope: 'Users', attribute: nil, value: nil}
38
+ user_attrs = attribs.merge(command)
39
+
40
+ dscl( user_attrs, dir_info )
41
+ end
42
+ alias_method :user_info, :user_get_info
43
+
44
+ # get all usernames -- dscl . -list /Users
45
+ # get all user details -- dscl . -readall /Users
46
+ def user_exists?(attribs, dir_info)
47
+ user_get_info(attribs, dir_info)
48
+ end
49
+
50
+ # CHANGE OD
51
+ ###########
52
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$USER RealName "$VALUE"
53
+ def user_set_real_name(attribs, dir_info)
54
+ attribs = user_record_name_alternatives(attribs)
55
+
56
+ attribs[:value] = attribs[:value] || attribs[:cn]
57
+ attribs[:value] = attribs[:value] || attribs[:realname]
58
+ attribs[:value] = attribs[:value] || attribs[:real_name]
59
+ attribs[:value] = attribs[:value] || attribs[:fullname]
60
+ attribs[:value] = attribs[:value] || attribs[:full_name]
61
+ if attribs[:last_name]
62
+ attribs[:value] = attribs[:value] || "#{attribs[:first_name]} #{attribs[:last_name]}"
63
+ end
64
+ attribs[:value] = attribs[:value] || attribs[:record_name]
65
+
66
+ check_critical_attribute( attribs, :record_name )
67
+ check_critical_attribute( attribs, :value, :real_name )
68
+ attribs = tidy_attribs(attribs)
69
+
70
+ command = {action: 'create', scope: 'Users', attribute: 'RealName'}
71
+ user_attrs = attribs.merge(command)
72
+
73
+ dscl( user_attrs, dir_info )
74
+ end
75
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$USER cn "$NAME"
76
+ def user_set_common_name(attribs, dir_info)
77
+ attribs = user_record_name_alternatives(attribs)
78
+
79
+ attribs[:value] = attribs[:value] || attribs[:cn]
80
+ attribs[:value] = attribs[:value] || attribs[:realname]
81
+ attribs[:value] = attribs[:value] || attribs[:real_name]
82
+ attribs[:value] = attribs[:value] || attribs[:fullname]
83
+ attribs[:value] = attribs[:value] || attribs[:full_name]
84
+ attribs[:value] = attribs[:value] || "#{attribs[:first_name]} #{attribs[:last_name]}"
85
+
86
+ check_critical_attribute( attribs, :record_name )
87
+ check_critical_attribute( attribs, :value, :common_name )
88
+ attribs = tidy_attribs(attribs)
89
+
90
+ command = {action: 'create', scope: 'Users', attribute: 'cn'}
91
+ user_attrs = attribs.merge(command)
92
+
93
+ dscl( user_attrs, dir_info )
94
+ end
95
+ alias_method :user_set_cn, :user_set_common_name
96
+
97
+
98
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME FirstName "$VALUE"
99
+ def user_set_first_name(attribs, dir_info)
100
+ attribs = user_record_name_alternatives(attribs)
101
+
102
+ attribs[:value] = attribs[:value] || attribs[:given_name]
103
+ attribs[:value] = attribs[:value] || attribs[:first_name]
104
+
105
+ check_critical_attribute( attribs, :record_name )
106
+ check_critical_attribute( attribs, :value, :first_name )
107
+ attribs = tidy_attribs(attribs)
108
+
109
+ command = {action: 'create', scope: 'Users', attribute: 'FirstName'}
110
+ user_attrs = attribs.merge(command)
111
+
112
+ dscl( user_attrs, dir_info )
113
+ end
114
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME givenName "$VALUE"
115
+ def user_set_given_name(attribs, dir_info)
116
+ attribs = user_record_name_alternatives(attribs)
117
+
118
+ attribs[:value] = attribs[:value] || attribs[:given_name]
119
+ attribs[:value] = attribs[:value] || attribs[:first_name]
120
+
121
+ check_critical_attribute( attribs, :record_name )
122
+ check_critical_attribute( attribs, :value, :given_name )
123
+ attribs = tidy_attribs(attribs)
124
+
125
+ command = {action: 'create', scope: 'Users', attribute: 'givenName'}
126
+ user_attrs = attribs.merge(command)
127
+
128
+ dscl( user_attrs, dir_info )
129
+ end
130
+
131
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME LastName "$VALUE"
132
+ def user_set_last_name(attribs, dir_info)
133
+ attribs = user_record_name_alternatives(attribs)
134
+
135
+ attribs[:value] = attribs[:value] || attribs[:sn]
136
+ attribs[:value] = attribs[:value] || attribs[:surname]
137
+ attribs[:value] = attribs[:value] || attribs[:last_name]
138
+
139
+ check_critical_attribute( attribs, :record_name )
140
+ check_critical_attribute( attribs, :value, :last_name )
141
+ attribs = tidy_attribs(attribs)
142
+
143
+ command = {action: 'create', scope: 'Users', attribute: 'LastName'}
144
+ user_attrs = attribs.merge(command)
145
+
146
+ dscl( user_attrs, dir_info )
147
+ end
148
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME sn "$VALUE"
149
+ def user_set_surname(attribs, dir_info)
150
+ attribs = user_record_name_alternatives(attribs)
151
+
152
+ attribs[:value] = attribs[:value] || attribs[:sn]
153
+ attribs[:value] = attribs[:value] || attribs[:surname]
154
+ attribs[:value] = attribs[:value] || attribs[:last_name]
155
+
156
+ check_critical_attribute( attribs, :record_name )
157
+ check_critical_attribute( attribs, :value, :surname )
158
+ attribs = tidy_attribs(attribs)
159
+
160
+ command = {action: 'create', scope: 'Users', attribute: 'sn'}
161
+ user_attrs = attribs.merge(command)
162
+
163
+ dscl( user_attrs, dir_info )
164
+ end
165
+ alias_method :user_set_sn, :user_set_surname
166
+
167
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME NameSuffix "$VALUE"
168
+ def user_set_name_suffix
169
+ end
170
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME apple-namesuffix "$VALUE"
171
+ def user_set_apple_name_suffix
172
+ end
173
+
174
+
175
+
176
+ # sudo dscl . -create /Users/someuser UniqueID "1010"
177
+ def user_set_unique_id(attribs, dir_info)
178
+ attribs = user_record_name_alternatives(attribs)
179
+
180
+ attribs[:value] = attribs[:value] || attribs[:uniqueid]
181
+ attribs[:value] = attribs[:value] || attribs[:unique_id]
182
+ attribs[:value] = attribs[:value] || attribs[:uidnumber]
183
+ attribs[:value] = attribs[:value] || attribs[:usernumber]
184
+ attribs[:value] = attribs[:value] || attribs[:user_number]
185
+
186
+ check_critical_attribute( attribs, :record_name )
187
+ check_critical_attribute( attribs, :value, :unique_id )
188
+ attribs = tidy_attribs(attribs)
189
+
190
+ command = {action: 'create', scope: 'Users', attribute: 'UniqueID'}
191
+ user_attrs = attribs.merge(command)
192
+
193
+ dscl( user_attrs, dir_info )
194
+ end
195
+
196
+ # # sudo dscl . -create /Users/someuser uidnumber "1010"
197
+ def user_set_uidnumber(attribs, dir_info)
198
+ attribs = user_record_name_alternatives(attribs)
199
+
200
+ attribs[:value] = attribs[:value] || attribs[:uniqueid]
201
+ attribs[:value] = attribs[:value] || attribs[:unique_id]
202
+ attribs[:value] = attribs[:value] || attribs[:uidnumber]
203
+
204
+ check_critical_attribute( attribs, :record_name )
205
+ check_critical_attribute( attribs, :value, :unique_id )
206
+ attribs = tidy_attribs(attribs)
207
+
208
+ command = {action: 'create', scope: 'Users', attribute: 'uidnumber'}
209
+ user_attrs = attribs.merge(command)
210
+
211
+ dscl( user_attrs, dir_info )
212
+ end
213
+
214
+ # sudo dscl . -create /Users/someuser PrimaryGroupID 80
215
+ def user_set_primary_group_id(attribs, dir_info)
216
+ attribs = user_record_name_alternatives(attribs)
217
+
218
+ attribs[:value] = attribs[:value] || attribs[:group_id]
219
+ attribs[:value] = attribs[:value] || attribs[:gidnumber]
220
+ attribs[:value] = attribs[:value] || attribs[:groupnumber]
221
+ attribs[:value] = attribs[:value] || attribs[:group_number]
222
+ attribs[:value] = attribs[:value] || attribs[:primary_group_id]
223
+
224
+ check_critical_attribute( attribs, :record_name )
225
+ check_critical_attribute( attribs, :value, :group_id )
226
+ attribs = tidy_attribs(attribs)
227
+
228
+ command = {action: 'create', scope: 'Users', attribute: 'PrimaryGroupID'}
229
+ user_attrs = attribs.merge(command)
230
+
231
+ dscl( user_attrs, dir_info )
232
+ end
233
+ # sudo dscl . -create /Users/someuser PrimaryGroupID 80
234
+ def user_set_gidnumber(attribs, dir_info)
235
+ attribs = user_record_name_alternatives(attribs)
236
+
237
+ attribs[:value] = attribs[:value] || attribs[:group_id]
238
+ attribs[:value] = attribs[:value] || attribs[:gidnumber]
239
+ attribs[:value] = attribs[:value] || attribs[:group_number]
240
+ attribs[:value] = attribs[:value] || attribs[:primary_group_id]
241
+
242
+ check_critical_attribute( attribs, :record_name )
243
+ check_critical_attribute( attribs, :value, :group_id )
244
+ attribs = tidy_attribs(attribs)
245
+
246
+ command = {action: 'create', scope: 'Users', attribute: 'gidnumber'}
247
+ user_attrs = attribs.merge(command)
248
+
249
+ dscl( user_attrs, dir_info )
250
+ end
251
+
252
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/someuser NFSHomeDirectory /Users/someuser
253
+ def user_set_nfs_home_directory(attribs, dir_info)
254
+ attribs = user_record_name_alternatives(attribs)
255
+
256
+ attribs[:value] = attribs[:value] || attribs[:home_directory]
257
+ attribs[:value] = attribs[:value] || attribs[:nfs_home_directory]
258
+ attribs[:value] = attribs[:value] || '/Volumes/Macintosh HD/Users/someone'
259
+
260
+ check_critical_attribute( attribs, :record_name )
261
+ check_critical_attribute( attribs, :value, :home_directory )
262
+ attribs = tidy_attribs(attribs)
263
+
264
+ command = {action: 'create', scope: 'Users', attribute: 'NFSHomeDirectory'}
265
+ user_attrs = attribs.merge(command)
266
+
267
+ dscl( user_attrs, dir_info )
268
+ end
269
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME homedirectory "$VALUE"
270
+ def user_set_home_directory(attribs, dir_info)
271
+ attribs = user_record_name_alternatives(attribs)
272
+
273
+ attribs[:value] = attribs[:value] || attribs[:home_directory]
274
+ attribs[:value] = attribs[:value] || attribs[:nfs_home_directory]
275
+ attribs[:value] = attribs[:value] || '/Volumes/Macintosh HD/Users/someone'
276
+
277
+ command = {action: 'create', scope: 'Users', attribute: 'homedirectory'}
278
+ attribs = attribs.merge(command)
279
+
280
+ check_critical_attribute( attribs, :record_name )
281
+ check_critical_attribute( attribs, :value, :home_directory )
282
+ user_attrs = tidy_attribs(attribs)
283
+
284
+ dscl( user_attrs, dir_info )
285
+ end
286
+
287
+ # /usr/bin/pwpolicy -a diradmin -p "TopSecret" -u username -setpassword "AnotherSecret"
288
+ # /usr/bin/dscl -plist -u diradmin -P #{adminpw} /LDAPv3/127.0.0.1/ -passwd /Users/#{shortname} "#{passwd}"
289
+ def user_set_password(attribs, dir_info)
290
+ attribs = user_record_name_alternatives(attribs)
291
+
292
+ attribs[:value] = attribs[:value] || attribs[:password]
293
+ attribs[:value] = attribs[:value] || attribs[:passwd]
294
+ attribs[:value] = attribs[:value] || '*'
295
+
296
+ check_critical_attribute( attribs, :record_name )
297
+ check_critical_attribute( attribs, :value, :password )
298
+ attribs = tidy_attribs(attribs)
299
+
300
+ command = {action: 'passwd', scope: 'Users'}
301
+ user_attrs = attribs.merge(command)
302
+
303
+ dscl( user_attrs, dir_info )
304
+ end
305
+ # /usr/bin/dscl /LDAPv3/127.0.0.1 -auth #{shortname} "#{passwd}"
306
+ def user_verify_password(attribs, dir_info)
307
+ attribs = user_record_name_alternatives(attribs)
308
+
309
+ attribs[:value] = attribs[:value] || attribs[:password]
310
+ attribs[:value] = attribs[:value] || attribs[:passwd]
311
+
312
+ check_critical_attribute( attribs, :record_name )
313
+ check_critical_attribute( attribs, :value, :password )
314
+ attribs = tidy_attribs(attribs)
315
+
316
+ command = {action: 'auth', scope: 'Users'}
317
+ user_attrs = attribs.merge(command)
318
+
319
+ dscl( user_attrs, dir_info )
320
+ end
321
+
322
+ # sudo dscl . -create /Users/someuser UserShell /bin/bash
323
+ def user_set_shell(attribs, dir_info)
324
+ attribs = user_record_name_alternatives(attribs)
325
+
326
+ attribs[:value] = attribs[:value] || attribs[:user_shell]
327
+ attribs[:value] = attribs[:value] || attribs[:shell]
328
+ attribs[:value] = attribs[:value] || '/bin/bash'
329
+
330
+ check_critical_attribute( attribs, :record_name )
331
+ check_critical_attribute( attribs, :value, :shell )
332
+ attribs = tidy_attribs(attribs)
333
+
334
+ command = {action: 'create', scope: 'Users', attribute: 'UserShell'}
335
+ user_attrs = attribs.merge(command)
336
+
337
+ dscl( user_attrs, dir_info )
338
+ end
339
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME loginShell "$VALUE"
340
+ def user_set_login_shell(attribs, dir_info)
341
+ attribs = user_record_name_alternatives(attribs)
342
+
343
+ attribs[:value] = attribs[:value] || attribs[:user_shell]
344
+ attribs[:value] = attribs[:value] || attribs[:shell]
345
+ attribs[:value] = attribs[:value] || '/bin/bash'
346
+
347
+ check_critical_attribute( attribs, :record_name )
348
+ check_critical_attribute( attribs, :value, :shell )
349
+ attribs = tidy_attribs(attribs)
350
+
351
+ command = {action: 'create', scope: 'Users', attribute: 'loginShell'}
352
+ user_attrs = attribs.merge(command)
353
+
354
+ dscl( user_attrs, dir_info )
355
+ end
356
+
357
+
358
+ # OTHER FIELDS
359
+ #####################
360
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME mail "$VALUE"
361
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME email "$VALUE"
362
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME apple-user-mailattribute "$VALUE"
363
+ def user_set_first_email(attribs, dir_info)
364
+ attribs = user_record_name_alternatives(attribs)
365
+
366
+ attribs[:value] = attribs[:value] || attribs['apple-user-mailattribute']
367
+ attribs[:value] = attribs[:value] || attribs[:apple_user_mailattribute]
368
+ attribs[:value] = attribs[:value] || attribs[:email]
369
+ attribs[:value] = attribs[:value] || attribs[:mail]
370
+
371
+ check_critical_attribute( attribs, :record_name )
372
+ check_critical_attribute( attribs, :value, :email )
373
+ attribs = tidy_attribs(attribs)
374
+
375
+ answer = []
376
+
377
+ command = {action: 'create', scope: 'Users', attribute: 'mail'}
378
+ user_attrs = attribs.merge(command)
379
+ answer << dscl( user_attrs, dir_info )
380
+
381
+ command = {action: 'create', scope: 'Users', attribute: 'email'}
382
+ user_attrs = attribs.merge(command)
383
+ answer << dscl( user_attrs, dir_info )
384
+
385
+ command = {action: 'create', scope: 'Users', attribute: 'apple-user-mailattribute'}
386
+ user_attrs = attribs.merge(command)
387
+ answer << dscl( user_attrs, dir_info )
388
+
389
+ return answer
390
+ end
391
+ alias_method :user_set_email, :user_set_first_email
392
+
393
+ def user_append_email(attribs, dir_info)
394
+ attribs = user_record_name_alternatives(attribs)
395
+
396
+ attribs[:value] = attribs[:value] || attribs['apple-user-mailattribute']
397
+ attribs[:value] = attribs[:value] || attribs[:apple_user_mailattribute]
398
+ attribs[:value] = attribs[:value] || attribs[:email]
399
+ attribs[:value] = attribs[:value] || attribs[:mail]
400
+
401
+ check_critical_attribute( attribs, :record_name )
402
+ check_critical_attribute( attribs, :value, :email )
403
+ attribs = tidy_attribs(attribs)
404
+
405
+ answer = []
406
+
407
+ command = {action: 'append', scope: 'Users', attribute: 'mail'}
408
+ user_attrs = attribs.merge(command)
409
+ answer << dscl( user_attrs, dir_info )
410
+
411
+ command = {action: 'append', scope: 'Users', attribute: 'email'}
412
+ user_attrs = attribs.merge(command)
413
+ answer << dscl( user_attrs, dir_info )
414
+
415
+ return answer
416
+ end
417
+
418
+ # dscl . -delete /Users/yourUserName
419
+ # https://tutorialforlinux.com/2011/09/15/delete-users-and-groups-from-terminal/
420
+ def user_delete(attribs, dir_info)
421
+ attribs = user_record_name_alternatives(attribs)
422
+
423
+ check_critical_attribute( attribs, :record_name )
424
+ attribs = tidy_attribs(attribs)
425
+
426
+ command = {action: 'delete', scope: 'Users', value: nil, attribute: nil}
427
+ user_attrs = attribs.merge(command)
428
+
429
+ dscl( user_attrs, dir_info )
430
+ end
431
+
432
+ # https://images.apple.com/server/docs/Command_Line.pdf
433
+ # https://serverfault.com/questions/20702/how-do-i-create-user-accounts-from-the-terminal-in-mac-os-x-10-5?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
434
+ # https://superuser.com/questions/1154564/how-to-create-a-user-from-the-macos-command-line
435
+ def user_create_min(attribs, dir_info)
436
+ attribs = user_record_name_alternatives(attribs)
437
+
438
+ check_critical_attribute( attribs, :record_name )
439
+ attribs = tidy_attribs(attribs)
440
+
441
+ command = {action: 'create', scope: 'Users', value: nil, attribute: nil}
442
+ user_attrs = attribs.merge(command)
443
+
444
+ dscl( user_attrs, dir_info )
445
+
446
+ answer = []
447
+ attribs[:value] = nil
448
+ answer << dscl( user_attrs, dir_info )
449
+ attribs[:value] = nil
450
+ answer << user_set_password(attribs, dir_info)
451
+ attribs[:value] = nil
452
+ answer << user_set_real_name(attribs, dir_info)
453
+
454
+ return answer
455
+ end
456
+
457
+ # https://images.apple.com/server/docs/Command_Line.pdf
458
+ # https://serverfault.com/questions/20702/how-do-i-create-user-accounts-from-the-terminal-in-mac-os-x-10-5?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
459
+ # https://superuser.com/questions/1154564/how-to-create-a-user-from-the-macos-command-line
460
+ def user_create_full(attribs, dir_info)
461
+ attribs = user_record_name_alternatives(attribs)
462
+
463
+ check_critical_attribute( attribs, :record_name )
464
+ attribs = tidy_attribs(attribs).dup
465
+
466
+ answer = []
467
+ attribs[:value] = nil
468
+ answer << user_create_min(attribs, dir_info)
469
+ attribs[:value] = nil
470
+ answer << user_set_shell(attribs, dir_info)
471
+ attribs[:value] = nil
472
+ answer << user_set_first_name(attribs, dir_info)
473
+ attribs[:value] = nil
474
+ answer << user_set_last_name(attribs, dir_info)
475
+ attribs[:value] = nil
476
+ answer << user_set_unique_id(attribs, dir_info)
477
+ attribs[:value] = nil
478
+ answer << user_set_primary_group_id(attribs, dir_info)
479
+ attribs[:value] = nil
480
+ answer << user_set_nfs_home_directory(attribs, dir_info)
481
+ # skip email if non-sent
482
+ unless attribs[:email].nil? and attribs[:mail].nil? and attribs[:apple_user_mailattribute].nil?
483
+ attribs[:value] = nil
484
+ answer << user_set_email(attribs, dir_info)
485
+ end
486
+
487
+ return answer.flatten
488
+ end
489
+
490
+ # ADD USER TO GROUPS
491
+ #################### #
492
+ # add 1st user -- dscl . -read /Groups/ladmins
493
+ def user_in_group?(attribs, dir_info)
494
+ attribs[:record_name] = attribs[:record_name] || attribs[:group_name]
495
+ attribs[:record_name] = attribs[:record_name] || attribs[:groupname]
496
+ attribs[:record_name] = attribs[:record_name] || attribs[:gid]
497
+
498
+ check_critical_attribute( attribs, :record_name, :groupname )
499
+ attribs = tidy_attribs(attribs)
500
+
501
+ command = {action: 'read', scope: 'Groups', attribute: nil, value: nil}
502
+ user_attrs = attribs.merge(command)
503
+
504
+ dscl( user_attrs, dir_info )
505
+ end
506
+
507
+ # http://krypted.com/mac-os-x/create-groups-using-dscl/
508
+ # https://superuser.com/questions/214004/how-to-add-user-to-a-group-from-mac-os-x-command-line?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
509
+ # sudo dseditgroup -o edit -a $username_to_add -t user admin
510
+ # sudo dseditgroup -o edit -a $username_to_add -t user wheel
511
+ # http://osxdaily.com/2007/10/29/how-to-add-a-user-from-the-os-x-command-line-works-with-leopard/
512
+ #
513
+ # add 1st user -- dscl . -create /Groups/ladmins GroupMembership localadmin
514
+ # add more users -- dscl . -append /Groups/ladmins GroupMembership 2ndlocaladmin
515
+ def user_first_in_group(attribs, dir_info)
516
+ attribs[:record_name] = attribs[:record_name] || attribs[:group_name]
517
+ attribs[:record_name] = attribs[:record_name] || attribs[:groupname]
518
+ attribs[:record_name] = attribs[:record_name] || attribs[:gid]
519
+
520
+ attribs[:value] = attribs[:value] || attribs[:user_name]
521
+ attribs[:value] = attribs[:value] || attribs[:username]
522
+ attribs[:value] = attribs[:value] || attribs[:uid]
523
+
524
+ check_critical_attribute( attribs, :record_name, :groupname )
525
+ check_critical_attribute( attribs, :value, :username )
526
+ attribs = tidy_attribs(attribs)
527
+
528
+ command = {action: 'create', scope: 'Groups', attribute: 'GroupMembership'}
529
+ user_attrs = attribs.merge(command)
530
+
531
+ dscl( user_attrs, dir_info )
532
+ end
533
+ def user_append_to_group(attribs, dir_info)
534
+ attribs[:record_name] = attribs[:record_name] || attribs[:group_name]
535
+ attribs[:record_name] = attribs[:record_name] || attribs[:groupname]
536
+ attribs[:record_name] = attribs[:record_name] || attribs[:gid]
537
+
538
+ attribs[:value] = attribs[:value] || attribs[:user_name]
539
+ attribs[:value] = attribs[:value] || attribs[:username]
540
+ attribs[:value] = attribs[:value] || attribs[:uid]
541
+
542
+ check_critical_attribute( attribs, :record_name, :groupname )
543
+ check_critical_attribute( attribs, :value, :username )
544
+ attribs = tidy_attribs(attribs)
545
+
546
+ command = {action: 'append', scope: 'Groups', attribute: 'GroupMembership'}
547
+ user_attrs = attribs.merge(command)
548
+
549
+ dscl( user_attrs, dir_info )
550
+ end
551
+ alias_method :user_add_to_group, :user_append_to_group
552
+
553
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -delete /Groups/$VALUE GroupMembership $shortname_USERNAME
554
+ def user_remove_from_group(attribs, dir_info)
555
+ attribs[:record_name] = attribs[:record_name] || attribs[:group_name]
556
+ attribs[:record_name] = attribs[:record_name] || attribs[:groupname]
557
+ attribs[:record_name] = attribs[:record_name] || attribs[:gid]
558
+
559
+ attribs[:value] = attribs[:value] || attribs[:user_name]
560
+ attribs[:value] = attribs[:value] || attribs[:username]
561
+ attribs[:value] = attribs[:value] || attribs[:uid]
562
+
563
+ check_critical_attribute( attribs, :record_name, :groupname )
564
+ check_critical_attribute( attribs, :value, :username )
565
+ attribs = tidy_attribs(attribs)
566
+
567
+ command = {action: 'delete', scope: 'Groups', attribute: 'GroupMembership'}
568
+ user_attrs = attribs.merge(command)
569
+
570
+ dscl( user_attrs, dir_info )
571
+ end
572
+
573
+ # 1st keyword -- /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME apple-keyword "$VALUE"
574
+ # other keywords -- /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -append /Users/$shortname_USERNAME apple-keyword "$VALUE"
575
+ def user_set_keywords
576
+ end
577
+
578
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -append /Users/$shortname_USERNAME apple-keyword "$VALUE"
579
+ def user_add_keywords
580
+ end
581
+
582
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME mobile "$VALUE"
583
+ def user_set_mobile_phone
584
+ end
585
+
586
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME telephoneNumber "$VALUE"
587
+ def user_set_work_phone
588
+ end
589
+
590
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME homePhone "$VALUE"
591
+ def user_set_home_phone
592
+ end
593
+
594
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME apple-company "$VALUE"
595
+ def user_set_company
596
+ end
597
+ alias_method :las_program_info, :user_set_company
598
+
599
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME title "$VALUE"
600
+ def user_set_title
601
+ end
602
+
603
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME departmentNumber "$VALUE"
604
+ def user_set_department
605
+ end
606
+
607
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME street "$VALUE"
608
+ def user_set_street
609
+ end
610
+ alias_method :las_set_dorm, :user_set_street
611
+ alias_method :las_set_housing, :user_set_street
612
+
613
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname l "$VALUE"
614
+ def user_set_city
615
+ end
616
+ alias_method :las_, :user_set_city
617
+
618
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME st "$VALUE"
619
+ def user_set_state
620
+ end
621
+ alias_method :las_cultural_trip, :user_set_state
622
+
623
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME postalCode "$VALUE"
624
+ def user_set_postcode
625
+ end
626
+ alias_method :las_faculty_family, :user_set_postcode
627
+
628
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$USER c "$VALUE"
629
+ def user_set_country
630
+ end
631
+
632
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME apple-webloguri "$VALUE"
633
+ def user_set_blog
634
+ end
635
+ alias_method :user_set_weblog, :user_set_blog
636
+ alias_method :las_sync_date, :user_set_blog
637
+
638
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME apple-organizationinfo "$VALUE"
639
+ def user_set_org_info
640
+ end
641
+ alias_method :las_set_organizational_info, :user_set_org_info
642
+ alias_method :las_link_student_to_parent, :user_set_org_info
643
+
644
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME apple-relationships "$VALUE"
645
+ def user_set_relationships
646
+ end
647
+ alias_method :las_link_parent_to_student, :user_set_relationships
648
+
649
+ # first - /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$USER apple-imhandle "$VALUE"
650
+ # others - /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -append /Users/$USER apple-imhandle "$VALUE"
651
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$USER apple-imhandle "AIM:created: $CREATE"
652
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -append /Users/$USER apple-imhandle "ICQ:start: $START"
653
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -append /Users/$USER apple-imhandle "MSN:end: $END"
654
+ def user_set_chat
655
+ end
656
+ alias_method :user_set_chat_channels, :user_set_chat
657
+ alias_method :las_created_date, :user_set_chat
658
+ alias_method :las_start_date, :user_set_chat
659
+ alias_method :las_end_date, :user_set_chat
660
+
661
+
662
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$shortname_USERNAME labeledURI "$VALUE"
663
+ def user_set_homepage
664
+ end
665
+ alias_method :user_set_webpage, :user_set_homepage
666
+ alias_method :las_enrollment_date, :user_set_homepage
667
+ alias_method :las_begin_date, :user_set_homepage
668
+
669
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$USER description "$NAME"
670
+ def user_set_comments
671
+ end
672
+ alias_method :user_set_description, :user_set_comments
673
+
674
+ # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1/ -create /Users/$USER description "$NAME"
675
+ def user_comments
676
+ end
677
+ alias_method :user_description, :user_comments
678
+
679
+ end
680
+ end