~ubuntu-branches/ubuntu/trusty/freeipa/trusty

« back to all changes in this revision

Viewing changes to install/ui/automember.js

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2013-03-07 14:10:03 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130307141003-kz4lq9vj4x692mqq
Tags: 3.1.2-0ubuntu1
* Merge from unreleased debian git.
  - new upstream release
  - doesn't use chkconfig anymore (LP: #1025018, #1124093)
  - drop -U from the ntpdate options (LP: #1149468)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*jsl:import ipa.js */
 
2
 
 
3
/*  Authors:
 
4
 *    Petr Vobornik <pvoborni@redhat.com>
 
5
 *
 
6
 * Copyright (C) 2012 Red Hat
 
7
 * see file 'COPYING' for use and warranty information
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation, either version 3 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 */
 
22
 
 
23
/* REQUIRES: ipa.js, details.js, search.js, add.js, facet.js, entity.js */
 
24
 
 
25
IPA.automember = {};
 
26
 
 
27
IPA.automember.entity = function(spec) {
 
28
 
 
29
     //HACK: Automember takes_params is missing a cn attribute. This hack
 
30
     //copies cn from mod command. Also it is set as pkey.
 
31
    var pkey_attr = IPA.metadata.commands.automember_mod.takes_args[0];
 
32
    pkey_attr.primary_key = true;
 
33
    IPA.metadata.objects.automember.takes_params.push(pkey_attr);
 
34
    IPA.metadata.objects.automember.primary_key = pkey_attr.name;
 
35
 
 
36
    spec = spec || {};
 
37
 
 
38
    spec.policies = spec.policies || [
 
39
        IPA.facet_update_policy({
 
40
            source_facet: 'usergrouprule',
 
41
            dest_facet: 'searchgroup'
 
42
        }),
 
43
        IPA.facet_update_policy({
 
44
            source_facet: 'hostgrouprule',
 
45
            dest_facet: 'searchhostgroup'
 
46
        })
 
47
    ];
 
48
 
 
49
    var that = IPA.entity(spec);
 
50
 
 
51
    that.init = function() {
 
52
 
 
53
        that.entity_init();
 
54
 
 
55
        that.builder.search_facet({
 
56
            factory: IPA.automember.rule_search_facet,
 
57
            name: 'searchgroup',
 
58
            group_type: 'group',
 
59
            label: IPA.messages.objects.automember.usergrouprules,
 
60
            details_facet: 'usergrouprule',
 
61
            pagination: false,
 
62
            columns: [
 
63
                'cn',
 
64
                'description'
 
65
            ]
 
66
        }).
 
67
        search_facet({
 
68
            factory: IPA.automember.rule_search_facet,
 
69
            name: 'searchhostgroup',
 
70
            group_type: 'hostgroup',
 
71
            label: IPA.messages.objects.automember.hostgrouprules,
 
72
            details_facet: 'hostgrouprule',
 
73
            pagination: false,
 
74
            columns: [
 
75
                'cn',
 
76
                'description'
 
77
            ]
 
78
        }).
 
79
        details_facet({
 
80
            factory: IPA.automember.rule_details_facet,
 
81
            name: 'usergrouprule',
 
82
            group_type: 'group',
 
83
            label: IPA.messages.objects.automember.usergrouprule,
 
84
            disable_facet_tabs: true,
 
85
            check_rights: false,
 
86
            redirect_info: { tab: 'amgroup' }
 
87
        }).
 
88
        details_facet({
 
89
            factory: IPA.automember.rule_details_facet,
 
90
            name: 'hostgrouprule',
 
91
            group_type: 'hostgroup',
 
92
            label: IPA.messages.objects.automember.hostgrouprule,
 
93
            disable_facet_tabs: true,
 
94
            check_rights: false,
 
95
            redirect_info: { tab: 'amhostgroup' }
 
96
        }).
 
97
        adder_dialog({
 
98
            factory: IPA.automember.rule_adder_dialog,
 
99
            title: IPA.messages.objects.automember.add_rule,
 
100
            fields: [
 
101
                {
 
102
                    type: 'entity_select',
 
103
                    name: 'cn',
 
104
                    other_entity: 'group',
 
105
                    other_field: 'cn'
 
106
                }
 
107
            ],
 
108
            height: '300'
 
109
        }).
 
110
        deleter_dialog({
 
111
            factory: IPA.automember.rule_deleter_dialog
 
112
        });
 
113
    };
 
114
 
 
115
    return that;
 
116
};
 
117
 
 
118
 
 
119
IPA.automember.rule_search_facet = function(spec) {
 
120
 
 
121
    spec = spec || {};
 
122
 
 
123
    var that = IPA.search_facet(spec);
 
124
 
 
125
    that.group_type = spec.group_type;
 
126
 
 
127
    var init = function() {
 
128
 
 
129
        that.default_group_widget = IPA.automember.default_group_widget({
 
130
            entity: that.entity,
 
131
            group_type: that.group_type
 
132
        });
 
133
    };
 
134
 
 
135
    that.refresh = function() {
 
136
 
 
137
        that.search_facet_refresh();
 
138
        that.default_group_widget.refresh();
 
139
    };
 
140
 
 
141
 
 
142
    that.get_records_command_name = function() {
 
143
        return that.managed_entity.name + that.group_type+'_get_records';
 
144
    };
 
145
 
 
146
    that.get_search_command_name = function() {
 
147
        var name = that.managed_entity.name + that.group_type + '_find';
 
148
        if (that.pagination && !that.search_all_entries) {
 
149
            name += '_pkeys';
 
150
        }
 
151
        return name;
 
152
    };
 
153
 
 
154
    that.create_get_records_command = function(pkeys, on_success, on_error) {
 
155
 
 
156
        var batch = that.table_facet_create_get_records_command(pkeys, on_success, on_error);
 
157
 
 
158
        for (var i=0; i<batch.commands.length; i++) {
 
159
            var command = batch.commands[i];
 
160
            command.set_option('type', that.group_type);
 
161
        }
 
162
 
 
163
        return batch;
 
164
    };
 
165
 
 
166
    that.create_refresh_command = function() {
 
167
 
 
168
        var command = that.search_facet_create_refresh_command();
 
169
 
 
170
        command.set_option('type', that.group_type);
 
171
 
 
172
        return command;
 
173
    };
 
174
 
 
175
    that.create_content = function(container) {
 
176
 
 
177
        var header = $('<div/>', {
 
178
            'class': 'automember-header'
 
179
        }).appendTo(container);
 
180
 
 
181
        var content = $('<div/>', {
 
182
            'class': 'automember-content'
 
183
        }).appendTo(container);
 
184
 
 
185
        that.default_group_widget.create(header);
 
186
        that.table.create(content);
 
187
 
 
188
    };
 
189
 
 
190
    init();
 
191
 
 
192
    return that;
 
193
};
 
194
 
 
195
IPA.automember.rule_details_facet = function(spec) {
 
196
 
 
197
    spec = spec || {};
 
198
 
 
199
    spec.fields = [
 
200
        {
 
201
            name: 'cn',
 
202
            widget: 'general.cn'
 
203
        },
 
204
        {
 
205
            type: 'textarea',
 
206
            name: 'description',
 
207
            widget: 'general.description'
 
208
        },
 
209
        {
 
210
            type: 'automember_condition',
 
211
            name: 'automemberinclusiveregex',
 
212
            widget: 'inclusive.automemberinclusiveregex'
 
213
        },
 
214
        {
 
215
            type: 'automember_condition',
 
216
            name: 'automemberexclusiveregex',
 
217
            widget: 'exclusive.automemberexclusiveregex'
 
218
        }
 
219
    ];
 
220
 
 
221
    spec.widgets = [
 
222
            {
 
223
            type: 'details_table_section',
 
224
            name: 'general',
 
225
            label: IPA.messages.details.general,
 
226
            widgets: [
 
227
                {
 
228
                    name: 'cn'
 
229
                },
 
230
                {
 
231
                    type: 'textarea',
 
232
                    name: 'description'
 
233
                }
 
234
            ]
 
235
        },
 
236
        {
 
237
            factory: IPA.collapsible_section,
 
238
            name: 'inclusive',
 
239
            label: IPA.messages.objects.automember.inclusive,
 
240
            widgets: [
 
241
                {
 
242
                    type: 'automember_condition',
 
243
                    name: 'automemberinclusiveregex',
 
244
                    group_type: spec.group_type,
 
245
                    add_command: 'add_condition',
 
246
                    remove_command: 'remove_condition',
 
247
                    adder_dialog: {
 
248
                        title: IPA.messages.objects.automember.add_condition,
 
249
                        fields: [
 
250
                            {
 
251
                                name: 'key',
 
252
                                type: 'select',
 
253
                                options: IPA.automember.get_condition_attributes(spec.group_type),
 
254
                                label: IPA.messages.objects.automember.attribute
 
255
                            },
 
256
                            {
 
257
                                name: 'automemberinclusiveregex',
 
258
                                label: IPA.messages.objects.automember.expression
 
259
                            }
 
260
                        ]
 
261
                    }
 
262
                }
 
263
            ]
 
264
        },
 
265
        {
 
266
            factory: IPA.collapsible_section,
 
267
            name: 'exclusive',
 
268
            label: IPA.messages.objects.automember.exclusive,
 
269
            widgets: [
 
270
                {
 
271
                    type: 'automember_condition',
 
272
                    name: 'automemberexclusiveregex',
 
273
                    group_type: spec.group_type,
 
274
                    add_command: 'add_condition',
 
275
                    remove_command: 'remove_condition',
 
276
                    adder_dialog: {
 
277
                        title: IPA.messages.objects.automember.add_condition,
 
278
                        fields:  [
 
279
                            {
 
280
                                name: 'key',
 
281
                                type: 'select',
 
282
                                options: IPA.automember.get_condition_attributes(spec.group_type),
 
283
                                label: IPA.messages.objects.automember.attribute
 
284
                            },
 
285
                            {
 
286
                                name: 'automemberexclusiveregex',
 
287
                                label: IPA.messages.objects.automember.expression
 
288
                            }
 
289
                        ]
 
290
                    }
 
291
                }
 
292
            ]
 
293
        }
 
294
    ];
 
295
 
 
296
    var that = IPA.details_facet(spec);
 
297
 
 
298
    that.group_type = spec.group_type;
 
299
 
 
300
    that.get_refresh_command_name = function() {
 
301
        return that.entity.name+that.group_type+'_show';
 
302
    };
 
303
 
 
304
    that.create_refresh_command = function() {
 
305
 
 
306
        var command = that.details_facet_create_refresh_command();
 
307
        command.set_option('type', that.group_type);
 
308
 
 
309
        return command;
 
310
    };
 
311
 
 
312
    that.create_update_command = function() {
 
313
 
 
314
        var command = that.details_facet_create_update_command();
 
315
        command.set_option('type', that.group_type);
 
316
 
 
317
        return command;
 
318
    };
 
319
 
 
320
    return that;
 
321
};
 
322
 
 
323
IPA.automember.rule_adder_dialog = function(spec) {
 
324
 
 
325
    spec = spec || {};
 
326
 
 
327
    var that = IPA.entity_adder_dialog(spec);
 
328
 
 
329
    that.show_edit_page = function (entity,result) {
 
330
        var pkey_name = entity.metadata.primary_key;
 
331
        var pkey = result[pkey_name];
 
332
        if (pkey instanceof Array) {
 
333
            pkey = pkey[0];
 
334
        }
 
335
        var facet = IPA.current_entity.get_facet();
 
336
        var facetname = facet.group_type === 'group' ? 'usergrouprule' :
 
337
                            'hostgrouprule';
 
338
 
 
339
        IPA.nav.show_entity_page(that.entity, facetname, pkey);
 
340
    };
 
341
 
 
342
    that.reset = function() {
 
343
 
 
344
        var field = that.fields.get_field('cn');
 
345
        var facet = IPA.current_entity.get_facet();
 
346
 
 
347
        field.widget.other_entity = IPA.get_entity(facet.group_type);
 
348
 
 
349
        that.dialog_reset();
 
350
    };
 
351
 
 
352
    that.create_add_command = function(record) {
 
353
 
 
354
        var facet = IPA.current_entity.get_facet();
 
355
        var command = that.entity_adder_dialog_create_add_command(record);
 
356
        command.name = that.entity.name+facet.group_type+'_show';
 
357
        command.set_option('type', facet.group_type);
 
358
 
 
359
        return command;
 
360
    };
 
361
 
 
362
 
 
363
    return that;
 
364
};
 
365
 
 
366
IPA.automember.rule_deleter_dialog = function(spec) {
 
367
 
 
368
    spec = spec || {};
 
369
 
 
370
    var that = IPA.search_deleter_dialog(spec);
 
371
 
 
372
    that.create_command = function() {
 
373
 
 
374
        var facet = IPA.current_entity.get_facet();
 
375
 
 
376
        var batch = that.search_deleter_dialog_create_command();
 
377
 
 
378
        for (var i=0; i<batch.commands.length; i++) {
 
379
            var command = batch.commands[i];
 
380
            command.set_option('type', facet.group_type);
 
381
        }
 
382
 
 
383
        return batch;
 
384
    };
 
385
 
 
386
    return that;
 
387
};
 
388
 
 
389
IPA.automember.get_condition_attributes = function(type) {
 
390
    var options = [];
 
391
 
 
392
    if (type === 'group') {
 
393
        options = IPA.metadata.objects.user.aciattrs;
 
394
    } else if (type === 'hostgroup') {
 
395
        options = IPA.metadata.objects.host.aciattrs;
 
396
    }
 
397
 
 
398
    var list_options = IPA.create_options(options);
 
399
    return list_options;
 
400
};
 
401
 
 
402
IPA.automember.parse_condition_regex = function(regex) {
 
403
 
 
404
    var delimeter_index = regex.indexOf('=');
 
405
    var condition = {
 
406
        condition: regex,
 
407
        attribute: regex.substring(0, delimeter_index),
 
408
        expression: regex.substring(delimeter_index+1)
 
409
    };
 
410
 
 
411
    return condition;
 
412
};
 
413
 
 
414
IPA.automember.condition_field = function(spec) {
 
415
 
 
416
    spec = spec || {};
 
417
    var that = IPA.field(spec);
 
418
 
 
419
    that.attr_name = spec.attribute || that.name;
 
420
 
 
421
    that.load = function(record) {
 
422
 
 
423
        var regexes = record[that.attr_name];
 
424
        that.values = [];
 
425
 
 
426
        if (regexes) {
 
427
            for (var i=0, j=0; i<regexes.length; i++) {
 
428
                var condition = IPA.automember.parse_condition_regex(regexes[i]);
 
429
                that.values.push(condition);
 
430
            }
 
431
        }
 
432
 
 
433
        that.load_writable(record);
 
434
        that.reset();
 
435
    };
 
436
 
 
437
    return that;
 
438
};
 
439
 
 
440
IPA.field_factories['automember_condition'] = IPA.automember.condition_field;
 
441
 
 
442
IPA.automember.condition_widget = function(spec) {
 
443
 
 
444
    spec = spec || {};
 
445
 
 
446
    spec.columns = $.merge(spec.columns || [], [
 
447
        {
 
448
            name: 'attribute',
 
449
            label: IPA.messages.objects.automember.attribute
 
450
        },
 
451
        {
 
452
            name: 'expression',
 
453
            label: IPA.messages.objects.automember.expression
 
454
        }
 
455
    ]);
 
456
 
 
457
    spec.value_attribute = 'condition';
 
458
 
 
459
    var that = IPA.attribute_table_widget(spec);
 
460
 
 
461
    that.group_type = spec.group_type;
 
462
 
 
463
    that.get_additional_options = function() {
 
464
        return [
 
465
            {
 
466
                name: 'type',
 
467
                value: that.group_type
 
468
            }
 
469
        ];
 
470
    };
 
471
 
 
472
    that.on_add = function(data) {
 
473
 
 
474
        if (data.result.completed === 0) {
 
475
            that.refresh_facet();
 
476
        } else {
 
477
            that.reload_facet(data);
 
478
        }
 
479
    };
 
480
 
 
481
    that.on_remove = function(data) {
 
482
 
 
483
        var results = data.result.results;
 
484
 
 
485
        var i = results.length - 1;
 
486
        while (i >= 0) {
 
487
            if (results[i].completed === 1){
 
488
                that.reload_facet({ result: results[i] });
 
489
                return;
 
490
            }
 
491
            i--;
 
492
        }
 
493
 
 
494
        that.refresh_facet();
 
495
    };
 
496
 
 
497
    that.create_remove_command = function(values, on_success, on_error) {
 
498
 
 
499
        var batch = IPA.batch_command({
 
500
            name: 'automember_remove_condition',
 
501
            on_success: on_success,
 
502
            on_error: on_error
 
503
        });
 
504
 
 
505
        var pkeys = that.get_pkeys();
 
506
 
 
507
        for (var i=0; i<values.length; i++) {
 
508
            var condition = IPA.automember.parse_condition_regex(values[i]);
 
509
 
 
510
            var command = that.attribute_table_create_remove_command([]);
 
511
            command.set_option('key', condition.attribute);
 
512
            command.set_option(that.attribute_name, condition.expression);
 
513
 
 
514
            batch.add_command(command);
 
515
        }
 
516
 
 
517
        return batch;
 
518
    };
 
519
 
 
520
    return that;
 
521
};
 
522
 
 
523
IPA.widget_factories['automember_condition'] = IPA.automember.condition_widget;
 
524
 
 
525
IPA.automember.default_group_widget = function(spec) {
 
526
 
 
527
    spec = spec || {};
 
528
 
 
529
    var that = IPA.widget(spec);
 
530
    that.group_type = spec.group_type;
 
531
    that.group = '';
 
532
 
 
533
    var init = function() {
 
534
 
 
535
        that.group_select = IPA.entity_select_widget({
 
536
            name: 'automemberdefaultgroup',
 
537
            other_entity: that.group_type,
 
538
            other_field: 'cn',
 
539
            show_undo: false
 
540
        });
 
541
 
 
542
        that.group_select.value_changed.attach(that.group_changed);
 
543
    };
 
544
 
 
545
    that.get_group = function() {
 
546
 
 
547
        var group = that.group_select.save();
 
548
        group = group.length === 0 ? '' : group[0];
 
549
        return group;
 
550
    };
 
551
 
 
552
    that.set_group = function(group) {
 
553
 
 
554
        if (group === that.group) return;
 
555
 
 
556
        that.group = group;
 
557
        that.group_select.update([group]);
 
558
    };
 
559
 
 
560
    that.group_changed = function() {
 
561
 
 
562
        var group = that.get_group();
 
563
 
 
564
        if (group === that.group) return;
 
565
 
 
566
        if (group === '') {
 
567
            that.remove_default_group();
 
568
        } else {
 
569
            that.set_default_group(group);
 
570
        }
 
571
    };
 
572
 
 
573
    that.load = function(data) {
 
574
 
 
575
        var group = data.result.result.automemberdefaultgroup;
 
576
 
 
577
        if (group) group = group[0];
 
578
 
 
579
        if (!group || group.indexOf('cn=') === -1) {
 
580
            group = '';
 
581
        } else {
 
582
            //extract from dn
 
583
            var i1 = group.indexOf('=');
 
584
            var i2 = group.indexOf(',');
 
585
            if (i1 > -1 && i2 > -1) {
 
586
                group = group.substring(i1 + 1,i2);
 
587
            }
 
588
        }
 
589
 
 
590
        that.update(group);
 
591
    };
 
592
 
 
593
    that.update = function(group) {
 
594
 
 
595
        group = group || '';
 
596
 
 
597
        that.set_group(group);
 
598
    };
 
599
 
 
600
    that.create_command = function(method) {
 
601
 
 
602
        method = 'default_group_' + method;
 
603
        var command_name = that.entity.name + that.group_type + '_' + method;
 
604
 
 
605
        var command  = IPA.command({
 
606
            name: command_name,
 
607
            entity: that.entity.name,
 
608
            method: method,
 
609
            options: {
 
610
                type: that.group_type
 
611
            }
 
612
        });
 
613
 
 
614
        return command;
 
615
    };
 
616
 
 
617
    that.refresh = function() {
 
618
 
 
619
        var command = that.create_command('show');
 
620
        command.on_success = that.load;
 
621
 
 
622
        command.execute();
 
623
    };
 
624
 
 
625
    that.remove_default_group = function() {
 
626
 
 
627
        var command = that.create_command('remove');
 
628
 
 
629
        command.on_success = function() {
 
630
            that.update('');
 
631
        };
 
632
        command.on_error = that.refresh;
 
633
 
 
634
        command.execute();
 
635
    };
 
636
 
 
637
    that.set_default_group = function(group) {
 
638
 
 
639
        var command = that.create_command('set');
 
640
        command.on_success = that.load;
 
641
        command.on_error = that.refresh;
 
642
        command.set_option('automemberdefaultgroup', group);
 
643
 
 
644
        command.execute();
 
645
    };
 
646
 
 
647
 
 
648
    that.create = function(container) {
 
649
 
 
650
        var title = that.get_title();
 
651
 
 
652
        var default_group = $('<div />', {
 
653
            'class': 'default_group'
 
654
        }).appendTo(container);
 
655
 
 
656
        that.header = $('<h2/>', {
 
657
            name: 'header',
 
658
            text: title,
 
659
            title: title
 
660
        }).appendTo(default_group);
 
661
 
 
662
        that.group_select.create(default_group);
 
663
    };
 
664
 
 
665
    that.get_title = function() {
 
666
        if (that.group_type === 'group') {
 
667
            return IPA.messages.objects.automember.default_user_group;
 
668
        } else {
 
669
            return IPA.messages.objects.automember.default_host_group;
 
670
        }
 
671
    };
 
672
 
 
673
    init();
 
674
 
 
675
    return that;
 
676
};
 
677
 
 
678
 
 
679
IPA.register('automember', IPA.automember.entity);
 
 
b'\\ No newline at end of file'