summaryrefslogtreecommitdiffstats
path: root/install/static/details.js
blob: 7d0e7846f0e75056944a2e10c2407e2a16d571b9 (plain)
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*  Authors:
 *    Pavel Zuna <pzuna@redhat.com>
 *
 * Copyright (C) 2010 Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 only
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/* IPA Object Details - populating definiton lists from entry data */

/* REQUIRES: ipa.js */

var IPA_DETAILS_POPULATE = 1;
var IPA_DETAILS_UPDATE = 2;

/* name of IPA object, that we're populating the lists for */
var _ipa_obj_name = '';

/* initialize the IPA Object Details library */
function ipa_details_init(obj_name, url)
{
    ipa_init(url);
    _ipa_obj_name = obj_name;
}

var _ipa_load_on_win_callback = null;
var _ipa_load_on_fail_callback = null;

var ipa_details_cache = null;

function ipa_details_load(pkey, on_win, on_fail)
{
    if (!pkey)
	return;

    _ipa_load_on_win_callback = on_win;
    _ipa_load_on_fail_callback = on_fail;

    ipa_cmd(
	'show', [pkey], {all: true}, _ipa_load_on_win, _ipa_load_on_fail,
	_ipa_obj_name
    );
}

function _ipa_load_on_win(data, text_status, xhr)
{
    if (_ipa_load_on_win_callback)
	_ipa_load_on_win_callback(data, text_status, xhr);

    if (data['error'])
	return;

    var result = data.result.result;

    ipa_details_cache = $.extend(true, {}, result);
    ipa_details_display(result);
}

function _ipa_load_on_fail(xhr, text_status, error_thrown)
{
    if (_ipa_load_on_fail_callback)
	_ipa_load_on_fail_callback(xhr, text_status, error_thrown);
}

var _ipa_update_on_win_callback = null;
var _ipa_update_on_fail_callback = null;

function ipa_details_update(pkey, on_win, on_fail)
{
    if (!pkey)
	return;

    var modlist = {'all': true, 'setattr': [], 'addattr': []};
    var attrs_wo_option = {};

    $('.entryattrs input').each(function () {
	var jobj = $(this);

	var dt = jobj.parent().prevAll('dt').slice(0, 1);
	if (!dt)
	    return;

	var attr = dt.attr('title');
	if (!attr)
	    return;

	if (attr.indexOf('call_') == 0) {
	    var func = window[attr.substr(5)];
	    if (!func)
		return;
	    func(dt, modlist, IPA_DETAILS_UPDATE);
	    return;
	}

	var param_info = ipa_get_param_info(attr);
	if (param_info) {
	    modlist[attr] = jobj.val();
	    return;
	}

	if (!attrs_wo_option[attr])
	    attrs_wo_option[attr] = [];
	attrs_wo_option[attr].push(jobj.val());
    });

    $('.entryattrs dt').each(function () {
	var jobj = $(this);

	var attr = jobj.attr('title');
	if (!attr || attr.indexOf('call_') == 0)
	    return;

	var next = jobj.next('dd');
	if ((!next.length) || (!next.children('input').length))
	    attrs_wo_option[attr] = [''];
    });

    for (attr in attrs_wo_option) {
	var values = attrs_wo_option[attr];
	modlist['setattr'].push(attr + '=' + values[0]);
	for (var i = 1; i < values.length; ++i)
	    modlist['addattr'].push(attr + '=' + values[i]);
    }

    _ipa_update_on_win_callback = on_win;
    _ipa_update_on_fail_callback = on_fail;

    ipa_cmd(
	'mod', [pkey], modlist, _ipa_update_on_win, _ipa_update_on_fail,
	_ipa_obj_name
    );
}

function _ipa_update_on_win(data, text_status, xhr)
{
    if (_ipa_update_on_win_callback)
	_ipa_update_on_win_callback(data, text_status, xhr);

    if (data['error'])
	return;

    var result = data.result.result;
    ipa_details_cache = $.extend(true, {}, result);
    ipa_details_display(result);
}

function _ipa_update_on_fail(xhr, text_status, error_thrown)
{
    if (_ipa_update_on_fail_callback)
	_ipa_update_on_fail_callback(xhr, text_status, error_thrown);
}

function ipa_details_create(dls)
{
    for (var i = 0; i < def_lists.length; ++i) {
	var d = dls[i];
	ipa_generate_dl($('hr').last(), d[0], d[1], d[2]);
    }
}

var _ipa_h2_template = '<h2 onclick="_h2_on_click(this)">&#8722; I</h2>';
var _ipa_dl_template = '<dl id="I" class="entryattrs"></dl>';
var _ipa_dt_template = '<dt title="T">N:</dt>';

function ipa_generate_dl(jobj, id, name, dts)
{
    if (!dts)
	return;

    jobj.after(_ipa_h2_template.replace('I', name));
    jobj = jobj.next();
    jobj.after(_ipa_dl_template.replace('I', id));
    jobj = jobj.next();
    jobj.after('<hr />');

    for (var i = 0; i < dts.length; ++i) {
	var label = '';
	if (dts[i][0].indexOf('call_') != 0) {
	    var param_info = ipa_get_param_info(dts[i][0]);
	    if (param_info)
		label = param_info['label'];
	}
	if ((!label) && (dts[i].length > 1))
	    label = dts[i][1];
	jobj.append(
	    _ipa_dt_template.replace('T', dts[i][0]).replace('N', label)
	);
    }
}

/* HTML templates for ipa_details_display() */
var _ipa_a_add_template =
    '<a href="jslink" onclick="return (_ipa_add_on_click(this))" title="A">Add</a>';

/* populate definition lists with the class 'entryattrs' with entry attributes
 *
 * The list has to be specially crafted for this function to work properly:
 * <dt> tags should have the 'title' attribute set to an LDAP attribute name
 * OR to a javascript function name prefixed with 'call_', which will be given
 * the <dt> object and entry_attrs as arguments.
 * Example:
 *   <dl class="entryattrs">
 *     <dt title="givenname">First Name:</dt>
 *     <dt title="call_some_callback">Some Attribute:</dt>
 *   </dl>
 *
 * arguments:
 *   entry_attrs - 'result' field as returned by ipa *-show commnads
 *                 (basically an associative array with attr:value pairs) */
function ipa_details_display(entry_attrs)
{
    /* remove all <dd> tags i.e. all attribute values */
    $('.entryattrs dd').remove();

    /* go through all <dt> tags and pair them with newly created <dd>s */
    $('.entryattrs dt').each(function () {
	var jobj = $(this);

	var attr = jobj.attr('title');
	if (attr.indexOf('call_') == 0) {
	    /* title contains callback instead of attribute name */
	    var func = window[attr.substr(5)];
	    if (func)
		func(jobj, entry_attrs, IPA_DETAILS_POPULATE);
	    else
		jobj.after(_ipa_dd_first_template.replace('I', '-'));
	} else {
	    /* title contains attribute name - default behaviour */
	    var value = entry_attrs[attr];
	    if (value) {
		ipa_insert_first_dd(jobj, ipa_create_input(attr, value[0]));
		for (var i = 1; i < value.length; ++i) {
		    jobj = jobj.next();
		    ipa_insert_other_dd(jobj, ipa_create_input(attr, value[i]));
		}
	    } else {
		ipa_insert_first_dd(jobj, _ipa_a_add_template.replace('A', attr));
	    }
	}
    });
}

var _ipa_dd_first_template = '<dd class="first">I</dd>';

function ipa_insert_first_dd(jobj, content)
{
    jobj.after(_ipa_dd_first_template.replace('I', content));
}

var _ipa_dd_other_template = '<dd class="other">I</dd>';

function ipa_insert_other_dd(jobj, content)
{
    jobj.after(_ipa_dd_other_template.replace('I', content));
}


/* mapping of parameter types to handlers used to create inputs */
var _ipa_param_type_2_handler_map = {
    'Str': _ipa_create_text_input,
    'Int': _ipa_create_text_input,
    'Bool': _ipa_create_text_input,
};

/* create an HTML element for displaying/editing an attribute
 * arguments:
 *   attr - LDAP attribute name
 *   value - the attributes value */
function ipa_create_input(attr, value)
{
    var param_info = ipa_get_param_info(attr);
    if (!param_info) {
	/* no information about the param is available, default to text input */
	return (
	    _ipa_create_text_input(attr, value, null) +
	    _ipa_create_remove_link(attr, null)
	);
    }

    /* check if the param value can be modified */
    if (param_info['primary_key'] || ('no_update' in param_info['flags']))
	return (value.toString());

    /* call handler by param class */
    var handler = _ipa_param_type_2_handler_map[param_info['class']];
    if (handler) {
	return (
	    handler(attr, value, param_info) +
	    _ipa_create_remove_link(attr, param_info)
	);
    }

    /* no handler for this type? don't allow modification */
    return (value.toString());
}

/* HTML template for _ipa_create_remove_link() */
var _ipa_a_remove_template =
    '<a href="jslink" onclick="return (_ipa_remove_on_click(this))" title="A">Remove</a>';

/* creates a Remove link for deleting attribute values */
function _ipa_create_remove_link(attr, param_info)
{
    if (!param_info)
	return (_ipa_a_remove_template.replace('A', attr));

    /* check if the param is required or of the Password type
     * if it is, then we don't want people to be able to remove it */
    if ((param_info['required']) || (param_info['class'] == 'Password'))
	return ('');

    return (_ipa_a_remove_template.replace('A', attr));
}

/* HTML template for _ipa_create_text_input() */
var _ipa_input_text_template =
    '<input type="text" name="A" value="V" />';

/* creates a input box for editing a string attribute */
function _ipa_create_text_input(attr, value, param_info)
{
    return (
	_ipa_input_text_template.replace('A', attr).replace(
	    'V', value.toString()
	)
    );
}

function ipa_details_reset()
{
    if (ipa_details_cache)
	ipa_details_display(ipa_details_cache);
}

/* Event handlers */

function _ipa_add_on_click(obj)
{
    var jobj = $(obj);
    var par = jobj.parent();
    par.append(ipa_create_input(jobj.attr('title'), ''));
    jobj.next('input').focus();
    jobj.remove();
    return (false);
}

function _ipa_remove_on_click(obj)
{
    var jobj = $(obj);
    var attr = jobj.attr('title');
    var par = jobj.parent();

    var next = par.next('dd');
    if (next.length) {
	if (par.hasClass('first')) {
	    next.addClass('first');
	    next.removeClass('other');
	}
	par.remove();
    } else {
	par.empty();
	par.append(_ipa_a_add_template.replace('A', attr));
    }

    return (false);
}

var qs = ipa_parse_qs();

/* "Top-level" code */

function load_object(body,obj)
{
    if (!qs['pkey'])
	return;
    ipa_details_init(obj);
    $('#butreset').click(reset_on_click);
    $('#butupdate').click(update_on_click);
    ipa_details_load(qs['pkey'], on_win);
    $('h1').text('Managing user: ' + qs['pkey']);

}

function on_win(data, textStatus, xhr)
{
    if (data['error'])
	alert(data['error']['message']);
}

function reset_on_click()
{
    if (ipa_details_cache)
	ipa_details_display(ipa_details_cache);
    return (false);
}

function update_on_click()
{
    ipa_details_update(qs['pkey'], on_win);
    return (false);
}

/* H2 expand/collapse */

function _h2_on_click(obj)
{
    var jobj = $(obj);
    var txt = jobj.text().replace(/^\s*/, '');
    if (txt.charCodeAt(0) == 8722) {
	obj.dl = jobj.next().detach();
	jobj.text('+' + txt.substr(1));
    } else {
	if (obj.dl)
	    obj.dl.insertAfter(obj);
	jobj.text(
	    String.fromCharCode(8722) + txt.substr(1)
	);
    }
}

/* Account status Toggle button */

function toggle_on_click(obj)
{
    var jobj = $(obj);
    var val = jobj.attr('title');
    if (val == 'Active') {
	ipa_cmd(
	    'lock', [qs['pkey']], {}, on_lock_win, on_fail,
	    PluginData['name']
	);
    } else {
	ipa_cmd(
	    'unlock', [qs['pkey']], {}, on_lock_win, on_fail,
	    PluginData['name']
	);
    }
    return (false);
}

function on_lock_win(data, textStatus, xhr)
{
    if (data['error']) {
	alert(data['error']['message']);
	return;
    }

    var jobj = $('a[title=Active]');
    if (jobj.length) {
	if (ipa_details_cache) {
	    var memberof = ipa_details_cache['memberof'];
	    if (memberof) {
		memberof.push(
		    'cn=inactivated,cn=account inactivation'
		);
	    } else {
		memberof = ['cn=inactivated,cn=account inactivation'];
	    }
	    ipa_details_cache['memberof'] = memberof;
	    a_status(jobj.parent().prev(), ipa_details_cache);
	    jobj.parent().remove()
	}
	return;
    }

    var jobj = $('a[title=Inactive]');
    if (jobj.length) {
	if (ipa_details_cache) {
	    var memberof = ipa_details_cache['memberof'];
	    if (memberof) {
		for (var i = 0; i < memberof.length; ++i) {
		    if (memberof[i].indexOf('cn=inactivated,cn=account inactivation') != -1) {
			memberof.splice(i, 1);
			break;
		    }
		}
	    } else {
		memberof = [];
	    }
	    ipa_details_cache['memberof'] = memberof;
	    a_status(jobj.parent().prev(), ipa_details_cache);
	    jobj.parent().remove();
	}
	return;
    }
}

/* ATTRIBUTE CALLBACKS */

var toggle_temp = 'S <a href="jslink" onclick="return (toggle_on_click(this))" title="S">Toggle</a>';
function a_status(jobj, result, mode)
{
    if (mode != IPA_DETAILS_POPULATE)
	return;

    var memberof = result['memberof'];
    if (memberof) {
	for (var i = 0; i < memberof.length; ++i) {
	    if (memberof[i].indexOf('cn=inactivated,cn=account inactivation') != -1) {
		var t = toggle_temp.replace(/S/g, 'Inactive');
		ipa_insert_first_dd(jobj, t);
		return;
	    }
	}
    }
    ipa_insert_first_dd(jobj, toggle_temp.replace(/S/g, 'Inactive'));
}

var pwd_temp = '<a href="jslink" onclick="return (resetpwd_on_click(this))" title="A">Reset Password</a>';
function a_password(jobj, result, mode)
{
    if (mode == IPA_DETAILS_POPULATE)
	ipa_insert_first_dd(jobj, pwd_temp.replace('A', 'userpassword'));
}

var select_temp = '<select title="st"></select>';
var option_temp = '<option value="V">V</option>';
var states = [
    'AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FM',
    'FL', 'GA', 'GU', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA',
    'ME', 'MH', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV',
    'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'MP', 'OH', 'OK', 'OR', 'PW',
    'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VI', 'VA',
    'WA', 'WV', 'WI', 'WY', '',
];
function a_st(jobj, result, mode)
{
    if (mode != IPA_DETAILS_POPULATE)
	return;

    var next = jobj.next();
    next.css('clear', 'none');
    next.css('width', '70px');

    ipa_insert_first_dd(jobj, select_temp);

    var sel = jobj.next().children().first();
    for (var i = 0; i < states.length; ++i)
	sel.append(option_temp.replace(/V/g, states[i]));

    var st = result['st'];
    if (st)
	sel.val(st);
    else
	sel.val('');
}