forked from ManageIQ/manageiq-ui-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap.rb
475 lines (434 loc) · 18.5 KB
/
ldap.rb
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
module OpsController::Settings::Ldap
extend ActiveSupport::Concern
# Show the main LdapRegions list view
def ldap_regions_list
ldap_region_build_list
update_gtl_div("ldap_regions_list") if !params[:button] && pagination_or_gtl_request?
end
def ldap_region_show
@display = "main"
return if record_no_longer_exists?(@selected_lr)
end
def ldap_domain_show
@display = "main"
return if record_no_longer_exists?(@selected_ld)
end
def ldap_region_add
@_params[:typ] = "new"
ldap_region_edit
end
def ldap_region_edit
case params[:button]
when "cancel"
if !session[:edit][:ldap_region_id]
add_flash(_("Add of new LDAP Region was cancelled by the user"))
else
add_flash(_("Edit of LDAP Region \"%{name}\" was cancelled by the user") % {:name => session[:edit][:new][:name]})
end
get_node_info(x_node)
@ldap_region = nil
@edit = session[:edit] = nil # clean out the saved info
replace_right_cell(:nodetype => @nodetype)
when "save", "add"
id = params[:id] ? params[:id] : "new"
return unless load_edit("ldap_region_edit__#{id}", "replace_cell__explorer")
ldap_region_get_form_vars
if @edit[:new][:name].blank?
add_flash(_("Name is required"), :error)
end
if @flash_array
javascript_flash
return
end
ldap_region_set_record_vars(@ldap_region)
if @ldap_region.valid? && !flash_errors? && @ldap_region.save
AuditEvent.success(build_saved_audit(@ldap_region, params[:button] == "add"))
add_flash(_("LDAP Region \"%{name}\" was saved") % {:name => @ldap_region.name})
@edit = session[:edit] = nil # clean out the saved info
if params[:button] == "add"
self.x_node = "xx-l" # reset node to show list
ldap_regions_list
settings_get_info("st")
else # set selected ldap_region
self.x_node = "lr-#{@ldap_region.id}"
get_node_info(x_node)
end
replace_right_cell(:nodetype => "root", :replace_trees => [:settings])
else
@ldap_region.errors.each do |field, msg|
add_flash("#{field.to_s.capitalize} #{msg}", :error)
end
@changed = session[:changed] = (@edit[:new] != @edit[:current])
javascript_flash
end
when "reset", nil # Reset or first time in
@ldap_region = find_or_new(LdapRegion, checked_or_params_id)
ldap_region_set_form_vars
@in_a_form = true
session[:changed] = false
if params[:button] == "reset"
add_flash(_("All changes have been reset"), :warning)
end
replace_right_cell(:nodetype => "lre")
end
end
# AJAX driven routine to check for changes in ANY field on the form
def ldap_region_form_field_changed
return unless load_edit("ldap_region_edit__#{params[:id]}", "replace_cell__explorer")
ldap_region_get_form_vars
render :update do |page|
page << javascript_prologue
@changed = (@edit[:new] != @edit[:current])
page << javascript_for_miq_button_visibility(@changed)
page << "miqSparkle(false);"
end
end
# Delete all selected or single displayed action(s)
def ldap_region_delete
ldap_regions = []
if !params[:id] # showing a list
ldap_regions = find_checked_ids_with_rbac(LdapRegion)
if ldap_regions.empty?
add_flash(_("No LDAP Regions were selected for deletion"), :error)
javascript_flash
end
process_ldap_regions(ldap_regions, "destroy") unless ldap_regions.empty?
else # showing 1 ldap_region, delete it
if params[:id].nil? || LdapRegion.find_by_id(params[:id]).nil?
add_flash(_("LDAP Region no longer exists"), :error)
javascript_flash
else
ldap_regions.push(params[:id])
end
process_ldap_regions(ldap_regions, "destroy") unless ldap_regions.empty?
end
self.x_node = "xx-l"
get_node_info(x_node)
replace_right_cell(:nodetype => x_node, :replace_trees => [:settings])
end
def ldap_domain_add
@_params[:typ] = "new"
@_params[:region_id] = x_node.split('-').last
ldap_domain_edit
end
def ldap_domain_edit
if params[:button] == "verify"
return unless load_edit("ldap_domain_edit__#{params[:domain_id]}", "replace_cell__explorer")
ldap_domain_get_form_vars
ldap_domain = params[:domain_id] == "new" ? LdapDomain.new : LdapDomain.find_by_id(params[:domain_id])
ldap_domain_set_record_vars(ldap_domain, :validate)
ldap_server = ldap_domain.ldap_servers[params[:id].to_i]
@in_a_form = true
begin
ldap_server.verify_credentials
rescue => bang
add_flash(bang.to_s, :error)
else
add_flash(_("Credential validation was successful"))
end
javascript_flash
elsif params[:button] == "cancel"
@ldap_domain = session[:edit][:ldap_domain] if session[:edit] && session[:edit][:ldap_domain]
if !@ldap_domain || @ldap_domain.id.blank?
add_flash(_("Add of new LDAP Domain was cancelled by the user"))
else
add_flash(_("Edit of LDAP Domain \"%{name}\" was cancelled by the user") % {:name => @ldap_domain.name})
end
get_node_info(x_node)
@ldap_domain = nil
@edit = session[:edit] = nil # clean out the saved info
replace_right_cell(:nodetype => @nodetype)
elsif params[:button] == "save" || params[:button] == "add"
id = params[:id] ? params[:id] : "new"
return unless load_edit("ldap_domain_edit__#{id}", "replace_cell__explorer")
ldap_domain_get_form_vars
if @edit[:new][:name].blank?
add_flash(_("Name is required"), :error)
end
if !@edit[:new][:bind_pwd].blank? && @edit[:new][:bind_dn].blank?
add_flash(_("Username must be entered if Password is entered"), :error)
end
if @flash_array
javascript_flash
return
end
ldap_domain_set_record_vars(@ldap_domain)
if @ldap_domain.valid? && !flash_errors? && @ldap_domain.save
AuditEvent.success(build_saved_audit(@ldap_domain, params[:button] == "add"))
add_flash(_("LDAP Domain \"%{name}\" was saved") % {:name => @ldap_domain.name})
@in_a_form = @edit = session[:edit] = nil # clean out the saved info
if params[:button] == "add"
self.x_node = "lr-#{@ldap_domain.ldap_region_id}" # reset node to show list
else # set selected ldap_domain
self.x_node = "ld-#{@ldap_domain.id}"
end
get_node_info(x_node)
replace_right_cell(:nodetype => x_node, :replace_trees => [:settings])
else
@ldap_domain.errors.each do |field, msg|
add_flash("#{field.to_s.capitalize} #{msg}", :error)
end
@changed = session[:changed] = (@edit[:new] != @edit[:current])
javascript_flash
end
elsif params[:accept]
id = params[:id] ? params[:id] : "new"
return unless load_edit("ldap_domain_edit__#{id}", "replace_cell__explorer")
ldap_domain_get_form_vars
if params[:entry]
server = {}
server[:hostname] = params[:entry][:hostname]
if params[:entry][:hostname] == ""
render_flash(_("Hostname is required"), :error)
return
else
server[:mode] = params[:entry_mode]
server[:port] = params[:entry][:port] == "" ? (server[:mode] == "ldap" ? "389" : "636") : params[:entry][:port]
server[:bind_dn] = params[:entry][:bind_dn]
server[:bind_pwd] = params[:entry][:bind_pwd]
if params[:entry][:idx]
# update existing one
@edit[:new][:ldap_servers][params[:entry][:idx].to_i] = server
else
# add new entry
@edit[:new][:ldap_servers].push(server)
end
end
@in_a_form = true
@changed = true
render :update do |page|
page << javascript_prologue
page.replace("ldap_server_entries_div", :partial => "ldap_server_entries", :locals => {:entry => nil, :edit => false, :domain_id => params[:id]})
page << javascript_for_miq_button_visibility(@changed)
end
end
else # Reset or first time in
@ldap_domain = find_or_new(LdapDomain, checked_or_params_id)
ldap_domain_set_form_vars
@in_a_form = true
session[:changed] = false
if params[:button] == "reset"
add_flash(_("All changes have been reset"), :warning)
end
replace_right_cell(:nodetype => "lde")
end
end
# AJAX driven routine to check for changes in ANY field on the form
def ldap_domain_form_field_changed
return unless load_edit("ldap_domain_edit__#{params[:id]}", "replace_cell__explorer")
ldap_domain_get_form_vars
render :update do |page|
page << javascript_prologue
if @authusertype_changed
if @edit[:new][:user_type] == "dn-cn"
page << javascript_hide("upn-mail_prefix")
page << javascript_hide("dn-uid_prefix")
page << javascript_show("dn-cn_prefix")
elsif @edit[:new][:user_type] == "dn-uid"
page << javascript_hide("upn-mail_prefix")
page << javascript_hide("dn-cn_prefix")
page << javascript_show("dn-uid_prefix")
else
page << javascript_hide("dn-cn_prefix")
page << javascript_hide("dn-uid_prefix")
page << javascript_show("upn-mail_prefix")
end
end
@changed = (@edit[:new] != @edit[:current])
page << javascript_for_miq_button_visibility(@changed)
page << "miqSparkle(false);"
end
end
# AJAX driven routine to check for changes in ANY field on the form
def ldap_entry_changed
return unless load_edit("ldap_domain_edit__#{params[:id]}", "replace_cell__explorer")
ldap_domain_get_form_vars
render :update do |page|
page << javascript_prologue
page << "$('#entry_port').val('#{params[:entry_mode] == "ldaps" ? '636' : '389'}');"
page << "miqSparkle(false);"
end
end
# Delete all selected or single displayed action(s)
def ldap_domain_delete
ldap_domains = []
if params[:id].nil? || LdapDomain.find_by_id(params[:id]).nil?
add_flash(_("LDAP Domain no longer exists"), :error)
javascript_flash
else
ldap_domains.push(params[:id])
end
ld = LdapDomain.find_by_id(params[:id])
self.x_node = "lr-#{ld.ldap_region_id}"
process_ldap_domains(ldap_domains, "destroy") unless ldap_domains.empty?
get_node_info(x_node)
replace_right_cell(:nodetype => x_node, :replace_trees => [:settings])
end
# AJAX driven routine to select a classification entry
def ls_select
return unless load_edit("ldap_domain_edit__#{params[:domain_id]}", "replace_cell__explorer")
ldap_domain_get_form_vars
if params[:id] == "new"
render :update do |page|
page << javascript_prologue
page.replace("flash_msg_div", :partial => "layouts/flash_msg")
page.replace("ldap_server_entries_div", :partial => "ldap_server_entries", :locals => {:entry => "new", :edit => true, :domain_id => params[:domain_id]})
page << javascript_focus('entry_name')
page << "$('#entry_name').select();"
end
session[:entry] = "new"
else
entry = params[:id]
render :update do |page|
page << javascript_prologue
page.replace("flash_msg_div", :partial => "layouts/flash_msg")
page.replace("ldap_server_entries_div", :partial => "ldap_server_entries", :locals => {:entry => entry, :edit => true, :domain_id => params[:domain_id]})
page << javascript_focus("entry_#{j_str(params[:field])}")
page << "$('#entry_#{j_str(params[:field])}').select();"
end
session[:entry] = entry
end
end
# AJAX driven routine to delete a classification entry
def ls_delete
return unless load_edit("ldap_domain_edit__#{params[:domain_id]}", "replace_cell__explorer")
ldap_domain_get_form_vars
@edit[:new][:ldap_servers].delete_at(params[:id].to_i)
@changed = true
render :update do |page|
page << javascript_prologue
page.replace("ldap_server_entries_div", :partial => "ldap_server_entries", :locals => {:entry => nil, :edit => false, :domain_id => params[:domain_id]})
page << javascript_for_miq_button_visibility(@changed)
end
end
private
# Create the view and associated vars for the ldap_regions list
def ldap_region_build_list
@lastaction = "ldap_regions_list"
@force_no_grid_xml = true
@gtl_type = "list"
if params[:ppsetting] # User selected new per page value
@items_per_page = params[:ppsetting].to_i # Set the new per page value
@settings.store_path(:perpage, @gtl_type.to_sym, @items_per_page) # Set the per page setting for this gtl type
end
@sortcol = session[:ldap_region_sortcol].nil? ? 0 : session[:ldap_region_sortcol].to_i
@sortdir = session[:ldap_region_sortdir].nil? ? "ASC" : session[:ldap_region_sortdir]
@view, @pages = get_view(LdapRegion) # Get the records (into a view) and the paginator
@current_page = @pages[:current] unless @pages.nil? # save the current page number
session[:ldap_region_sortcol] = @sortcol
session[:ldap_region_sortdir] = @sortdir
end
# Set form variables for edit
def ldap_region_set_form_vars
@edit = {}
@edit[:ldap_region_id] = @ldap_region.id
@edit[:new] = {}
@edit[:current] = {}
@edit[:key] = "ldap_region_edit__#{@ldap_region.id || "new"}"
@edit[:new][:name] = @ldap_region.name
@edit[:new][:description] = @ldap_region.description
@edit[:new][:zone_id] = @ldap_region.zone.try(:id)
@edit[:zones] = Zone.all.sort_by { |z| z.name.to_s }.collect { |z| [z.name, z.id] }
@edit[:current] = copy_hash(@edit[:new])
session[:edit] = @edit
end
# Get variables from edit form
def ldap_region_get_form_vars
@ldap_region = @edit[:ldap_region_id] ? LdapRegion.find_by_id(@edit[:ldap_region_id]) : LdapRegion.new
@edit[:new][:name] = params[:name] if params[:name]
@edit[:new][:description] = params[:description] if params[:description]
@edit[:new][:zone_id] = params[:zone_id] if params[:zone_id]
end
# Set record variables to new values
def ldap_region_set_record_vars(ldap_region)
ldap_region.name = @edit[:new][:name]
ldap_region.description = @edit[:new][:description]
ldap_region.zone = Zone.find_by_id(@edit[:new][:zone_id])
end
# Common ldap_region button handler routines follow
def process_ldap_regions(ldap_regions, task)
process_elements(ldap_regions, LdapRegion, task)
end
# Set form variables for edit
def ldap_domain_set_form_vars
@edit = {}
@edit[:ldap_domain_id] = @ldap_domain.id
@edit[:ldap_region_id] = @ldap_domain.id ? @ldap_domain.ldap_region_id : params[:region_id]
@edit[:new] = {}
@edit[:current] = {}
@edit[:key] = "ldap_domain_edit__#{@ldap_domain.id || "new"}"
@edit[:new][:name] = @ldap_domain.name
@edit[:new][:user_type] = @ldap_domain.user_type ? @ldap_domain.user_type : "userprincipalname"
@edit[:new][:user_suffix] = @ldap_domain.user_suffix
@edit[:new][:domain_prefix] = @ldap_domain.domain_prefix
@edit[:new][:get_user_groups] = @ldap_domain.get_user_groups ? @ldap_domain.get_user_groups : false
@edit[:new][:get_direct_groups] = @ldap_domain.get_direct_groups
@edit[:new][:follow_referrals] = @ldap_domain.follow_referrals ? @ldap_domain.follow_referrals : false
@edit[:new][:base_dn] = @ldap_domain.base_dn
@edit[:new][:bind_dn] = @ldap_domain.authentication_userid
@edit[:new][:bind_pwd] = @ldap_domain.authentication_password
@edit[:new][:ldap_servers] = []
@ldap_domain.ldap_servers.sort_by { |svr| svr.hostname.to_s }.each do |svr|
server = {}
server[:id] = svr.id
server[:hostname] = svr.hostname
server[:mode] = svr.mode
server[:port] = svr.port
@edit[:new][:ldap_servers].push(server)
end
@edit[:current] = copy_hash(@edit[:new])
session[:edit] = @edit
end
# Get variables from edit form
def ldap_domain_get_form_vars
@ldap_domain = @edit[:ldap_domain_id] ? LdapDomain.find_by_id(@edit[:ldap_domain_id]) : LdapDomain.new
@edit[:new][:name] = params[:name] if params[:name]
@sb[:get_user_groups] = (params[:get_user_groups].to_s == "1") if params[:get_user_groups]
if params[:user_type] && params[:user_type] != @edit[:new][:user_type]
@authusertype_changed = true
end
@edit[:new][:user_suffix] = params[:user_suffix] if params[:user_suffix]
@edit[:new][:domain_prefix] = params[:domain_prefix] if params[:domain_prefix]
if @sb[:get_user_groups] != @edit[:new][:get_user_groups]
@edit[:new][:get_user_groups] = @sb[:get_user_groups]
@authldaprole_changed = true
end
@edit[:new][:port] = params[:port] if params[:port]
@edit[:new][:user_type] = params[:user_type] if params[:user_type]
@edit[:new][:base_dn] = params[:base_dn] if params[:base_dn]
@edit[:new][:bind_dn] = params[:bind_dn] if params[:bind_dn]
@edit[:new][:bind_pwd] = params[:bind_pwd] if params[:bind_pwd]
@edit[:new][:follow_referrals] = (params[:follow_referrals].to_s == "1") if params[:follow_referrals]
@edit[:new][:get_direct_groups] = (params[:get_direct_groups].to_s == "1") if params[:get_direct_groups]
end
# Set record variables to new values
def ldap_domain_set_record_vars(ldap_domain, mode = nil)
ldap_domain.name = @edit[:new][:name]
ldap_domain.user_type = @edit[:new][:user_type]
ldap_domain.user_suffix = @edit[:new][:user_suffix]
ldap_domain.domain_prefix = @edit[:new][:domain_prefix]
ldap_domain.get_user_groups = @edit[:new][:get_user_groups]
ldap_domain.get_direct_groups = @edit[:new][:get_direct_groups]
ldap_domain.follow_referrals = @edit[:new][:follow_referrals]
ldap_domain.base_dn = @edit[:new][:base_dn]
ldap_domain.ldap_region = LdapRegion.find_by_id(@edit[:ldap_region_id]) if ldap_domain.ldap_region.nil?
creds = {}
creds[:default] = {:userid => @edit[:new][:bind_dn], :password => @edit[:new][:bind_pwd]} unless @edit[:new][:bind_dn].blank?
ldap_domain.update_authentication(creds, :save => (mode != :validate))
ldap_servers = []
unless @edit[:new][:ldap_servers].blank?
@edit[:new][:ldap_servers].each do |svr|
ldap_server = LdapServer.new
ldap_server.hostname = svr[:hostname]
ldap_server.mode = svr[:mode]
ldap_server.port = svr[:port]
ldap_servers.push(ldap_server)
end
end
ldap_domain.ldap_servers = ldap_servers
end
# Common ldap_domain button handler routines follow
def process_ldap_domains(ldap_domains, task)
process_elements(ldap_domains, LdapDomain, task)
end
end