summaryrefslogtreecommitdiffstats
path: root/install/ui/src/freeipa/rule.js
blob: f573b56d469ac8c6652f8e49c66fd6fd1259db90 (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
/*  Authors:
 *    Endi Sukma Dewata <edewata@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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

define([
    './ipa',
    './jquery',
    './phases',
    './reg',
    './details',
    './search',
    './association',
    './entity'],
        function(IPA, $, phases, reg) {

IPA.rule_details_widget = function(spec) {

    spec = spec || {};

    var that = IPA.composite_widget(spec);

    that.radio_name = spec.radio_name;
    that.options = spec.options || [];
    that.tables = spec.tables || [];
    that.columns = spec.columns;

    that.init = function() {

        that.enable_radio = IPA.radio_widget({
            name: that.radio_name,
            options: that.options
        });

        that.widgets.add_widget(that.enable_radio);
        that.enable_radio.value_changed.attach(that.on_enable_radio_changed);
        that.enable_radio.updated.attach(that.on_enable_radio_changed);
    };

    that.on_enable_radio_changed = function() {
        var value = that.enable_radio.save();
        if(value.length > 0) {
            var enabled = ('' === value[0]);
            for (var i=0; i<that.tables.length; i++) {
                var table = that.tables[i];

                var table_widget = that.widgets.get_widget(table.name);
                table_widget.set_enabled(enabled);
            }
        }
    };

    that.create = function(container) {

        that.container = container;

        //enable radios
        var param_info = IPA.get_entity_param(that.entity.name, that.radio_name);
        var title = param_info ? param_info.doc : that.radio_name;
        var enable_radio_container = $('<div/>', {
            name: that.radio_name,
            title: title,
            'class': 'field'
        }).appendTo(container);

        enable_radio_container.append(title+': ');
        that.enable_radio.create(enable_radio_container);

        //tables
        for (var j=0; j<that.tables.length; j++) {
            var table = that.tables[j];

            var metadata = IPA.get_entity_param(that.entity.name, table.name);

            var table_container = $('<div/>', {
                name: table.name,
                title: metadata ? metadata.doc : table.name,
                'class': 'field'
            }).appendTo(container);

            var widget = that.widgets.get_widget(table.name);
            widget.create(table_container);
        }
    };

    that.init();

    return that;
};


IPA.rule_association_table_widget = function(spec) {

    spec = spec || {};

    var that = IPA.association_table_widget(spec);

    that.external = spec.external;

    that.enabled = spec.enabled !== undefined ? spec.enabled : true;

    that.setup_column = function(column, div, record) {
        var suppress_link = false;
        if (that.external) {
            suppress_link = record[that.external] === 'true';
        }
        column.setup(div, record, suppress_link);
    };

    that.create_columns = function() {

        if (!that.columns.length) {
            that.association_table_widget_create_columns();
            if (that.external) {
                that.create_column({
                    name: that.external,
                    label: '@i18n:objects.sudorule.external',
                    entity: that.other_entity,
                    formatter: 'boolean',
                    width: '200px'
                });
            }
        }
    };

    that.create_add_dialog = function() {

        var entity_label = that.entity.metadata.label_singular;
        var pkey = that.facet.get_pkey();
        var other_entity_label = that.other_entity.metadata.label;

        var title = that.add_title;
        title = title.replace('${entity}', entity_label);
        title = title.replace('${primary_key}', pkey);
        title = title.replace('${other_entity}', other_entity_label);

        var exclude = that.values;
        if (that.external) {
            exclude = [];
            for (var i=0; i<that.values.length; i++) {
                exclude.push(that.values[i][that.name]);
            }
        }

        return IPA.rule_association_adder_dialog({
            title: title,
            pkey: pkey,
            other_entity: that.other_entity,
            attribute_member: that.attribute_member,
            entity: that.entity,
            external: that.external,
            exclude: exclude
        });
    };

    return that;
};

IPA.rule_association_table_field = function(spec) {

    spec = spec || {};

    var that = IPA.association_table_field(spec);

    that.external = spec.external;

    that.set_values_external = function(values, external) {

        for (var i=0; i<values.length; i++) {

            var record = values[i];

            if (typeof record !== 'object') {
                record = {};
                record[that.param] = values[i];
            }

            record[that.external] = external;

            values[i] = record;
        }
    };

    that.load = function(result) {
        that.values = result[that.param] || [];

        if (that.external) {
            that.set_values_external(that.values, '');
            var external_values = result[that.external] || [];
            that.set_values_external(external_values, 'true');
            $.merge(that.values, external_values);
        }

        that.widget.update(that.values);
        that.widget.unselect_all();
    };

    that.get_update_info = function() {

        var update_info = IPA.update_info_builder.new_update_info();

        //association_table_widget performs basic add and remove operation
        //immediately. Rule association field test if its enabled and if not it
        //performs delete operation.

        if (!that.widget.enabled) {
            var values = that.save();

            if (values.length > 0) { //no need to delete if has no values

                var command = IPA.command({
                    entity: that.entity.name,
                    method: that.widget.remove_method,
                    args: that.facet.get_pkeys()
                });

                command.set_option(that.widget.other_entity.name, values);
                update_info.append_command(command, that.priority);
            }
        }

        return update_info;
    };

    return that;
};

IPA.rule_association_adder_dialog = function(spec) {

    spec = spec || {};

    var that = IPA.association_adder_dialog(spec);

    that.external = spec.external;

    that.add = function() {
        var rows = that.available_table.remove_selected_rows();
        that.selected_table.add_rows(rows);

        if (that.external) {
            var pkey_name = that.other_entity.metadata.primary_key;
            var value = that.external_field.val();
            if (!value) return;

            var record = {};
            record[pkey_name] = value;
            that.selected_table.add_record(record);
            that.external_field.val('');
        }
    };

    return that;
};

phases.on('registration', function() {
    var w = reg.widget;
    var f = reg.field;

    w.register('rule_association_table', IPA.rule_association_table_widget);
    f.register('rule_association_table', IPA.rule_association_table_field);
});

return {};
});