]>
Commit | Line | Data |
---|---|---|
03ab9fba DM |
1 | Ext.define('PVE.IPSetList', { |
2 | extend: 'Ext.grid.Panel', | |
3 | alias: 'widget.pveIPSetList', | |
4 | ||
123e1c80 DC |
5 | stateful: true, |
6 | stateId: 'grid-firewall-ipsetlist', | |
7 | ||
03ab9fba DM |
8 | ipset_panel: undefined, |
9 | ||
10 | base_url: undefined, | |
11 | ||
12 | addBtn: undefined, | |
13 | removeBtn: undefined, | |
14 | editBtn: undefined, | |
15 | ||
16 | initComponent: function() { | |
d5e771ce | 17 | |
03ab9fba DM |
18 | var me = this; |
19 | ||
20 | if (me.ipset_panel == undefined) { | |
21 | throw "no rule panel specified"; | |
22 | } | |
23 | ||
24 | if (me.base_url == undefined) { | |
25 | throw "no base_url specified"; | |
26 | } | |
27 | ||
28 | var store = new Ext.data.Store({ | |
29 | fields: [ 'name', 'comment', 'digest' ], | |
30 | proxy: { | |
31 | type: 'pve', | |
32 | url: "/api2/json" + me.base_url | |
33 | }, | |
34 | idProperty: 'name', | |
35 | sorters: { | |
36 | property: 'name', | |
37 | order: 'DESC' | |
38 | } | |
39 | }); | |
40 | ||
41 | var sm = Ext.create('Ext.selection.RowModel', {}); | |
42 | ||
43 | var reload = function() { | |
44 | var oldrec = sm.getSelection()[0]; | |
45 | store.load(function(records, operation, success) { | |
46 | if (oldrec) { | |
47 | var rec = store.findRecord('name', oldrec.data.name); | |
48 | if (rec) { | |
49 | sm.select(rec); | |
50 | } | |
51 | } | |
52 | }); | |
53 | }; | |
54 | ||
55 | var run_editor = function() { | |
56 | var rec = sm.getSelection()[0]; | |
57 | if (!rec) { | |
58 | return; | |
59 | } | |
60 | var win = Ext.create('PVE.window.Edit', { | |
61 | subject: "IPSet '" + rec.data.name + "'", | |
62 | url: me.base_url, | |
63 | method: 'POST', | |
64 | digest: rec.data.digest, | |
65 | items: [ | |
66 | { | |
67 | xtype: 'hiddenfield', | |
68 | name: 'rename', | |
69 | value: rec.data.name | |
70 | }, | |
71 | { | |
72 | xtype: 'textfield', | |
73 | name: 'name', | |
74 | value: rec.data.name, | |
75 | fieldLabel: gettext('Name'), | |
76 | allowBlank: false | |
77 | }, | |
78 | { | |
79 | xtype: 'textfield', | |
80 | name: 'comment', | |
81 | value: rec.data.comment, | |
82 | fieldLabel: gettext('Comment') | |
83 | } | |
84 | ] | |
85 | }); | |
86 | win.show(); | |
87 | win.on('destroy', reload); | |
88 | }; | |
89 | ||
90 | me.editBtn = new PVE.button.Button({ | |
91 | text: gettext('Edit'), | |
92 | disabled: true, | |
93 | selModel: sm, | |
94 | handler: run_editor | |
95 | }); | |
96 | ||
97 | me.addBtn = new PVE.button.Button({ | |
98 | text: gettext('Create'), | |
99 | handler: function() { | |
100 | sm.deselectAll(); | |
101 | var win = Ext.create('PVE.window.Edit', { | |
102 | subject: 'IPSet', | |
103 | url: me.base_url, | |
104 | method: 'POST', | |
105 | items: [ | |
106 | { | |
107 | xtype: 'textfield', | |
108 | name: 'name', | |
109 | value: '', | |
110 | fieldLabel: gettext('Name'), | |
111 | allowBlank: false | |
112 | }, | |
113 | { | |
114 | xtype: 'textfield', | |
115 | name: 'comment', | |
116 | value: '', | |
117 | fieldLabel: gettext('Comment') | |
118 | } | |
119 | ] | |
120 | }); | |
121 | win.show(); | |
122 | win.on('destroy', reload); | |
123 | ||
124 | } | |
125 | }); | |
126 | ||
127 | me.removeBtn = new PVE.button.Button({ | |
128 | text: gettext('Remove'), | |
129 | selModel: sm, | |
130 | disabled: true, | |
131 | handler: function() { | |
132 | var rec = sm.getSelection()[0]; | |
133 | if (!rec || !me.base_url) { | |
134 | return; | |
135 | } | |
136 | PVE.Utils.API2Request({ | |
137 | url: me.base_url + '/' + rec.data.name, | |
138 | method: 'DELETE', | |
139 | waitMsgTarget: me, | |
140 | failure: function(response, options) { | |
141 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
142 | }, | |
143 | callback: reload | |
144 | }); | |
145 | } | |
146 | }); | |
147 | ||
148 | Ext.apply(me, { | |
149 | store: store, | |
150 | tbar: [ '<b>IPSet:</b>', me.addBtn, me.removeBtn, me.editBtn ], | |
151 | selModel: sm, | |
152 | columns: [ | |
123e1c80 | 153 | { header: 'IPSet', dataIndex: 'name', width: '100' }, |
2eb0767b | 154 | { header: gettext('Comment'), dataIndex: 'comment', renderer: Ext.String.htmlEncode, flex: 1 } |
03ab9fba DM |
155 | ], |
156 | listeners: { | |
157 | itemdblclick: run_editor, | |
158 | select: function(sm, rec) { | |
159 | var url = me.base_url + '/' + rec.data.name; | |
160 | me.ipset_panel.setBaseUrl(url); | |
161 | }, | |
162 | deselect: function() { | |
163 | me.ipset_panel.setBaseUrl(undefined); | |
164 | }, | |
165 | show: reload | |
166 | } | |
167 | }); | |
168 | ||
169 | me.callParent(); | |
170 | ||
171 | store.load(); | |
172 | } | |
173 | }); | |
174 | ||
175 | Ext.define('PVE.IPSetCidrEdit', { | |
176 | extend: 'PVE.window.Edit', | |
177 | ||
178 | cidr: undefined, | |
179 | ||
180 | initComponent : function() { | |
d5e771ce | 181 | |
03ab9fba DM |
182 | var me = this; |
183 | ||
d5e771ce | 184 | me.isCreate = (me.cidr === undefined); |
03ab9fba DM |
185 | |
186 | ||
d5e771ce | 187 | if (me.isCreate) { |
03ab9fba DM |
188 | me.url = '/api2/extjs' + me.base_url; |
189 | me.method = 'POST'; | |
190 | } else { | |
191 | me.url = '/api2/extjs' + me.base_url + '/' + me.cidr; | |
192 | me.method = 'PUT'; | |
193 | } | |
194 | ||
195 | var column1 = []; | |
196 | ||
d5e771ce | 197 | if (me.isCreate) { |
03ab9fba DM |
198 | if (!me.list_refs_url) { |
199 | throw "no alias_base_url specified"; | |
200 | } | |
201 | ||
202 | column1.push({ | |
203 | xtype: 'pveIPRefSelector', | |
204 | name: 'cidr', | |
205 | ref_type: 'alias', | |
206 | autoSelect: false, | |
207 | editable: true, | |
208 | base_url: me.list_refs_url, | |
209 | value: '', | |
210 | fieldLabel: gettext('IP/CIDR') | |
211 | }); | |
212 | } else { | |
213 | column1.push({ | |
214 | xtype: 'displayfield', | |
215 | name: 'cidr', | |
03ab9fba DM |
216 | value: '', |
217 | fieldLabel: gettext('IP/CIDR') | |
218 | }); | |
219 | } | |
220 | ||
221 | var ipanel = Ext.create('PVE.panel.InputPanel', { | |
d5e771ce | 222 | isCreate: me.isCreate, |
03ab9fba DM |
223 | column1: column1, |
224 | column2: [ | |
225 | { | |
226 | xtype: 'pvecheckbox', | |
227 | name: 'nomatch', | |
228 | checked: false, | |
03ab9fba | 229 | uncheckedValue: 0, |
185a77e5 | 230 | fieldLabel: 'nomatch' |
03ab9fba DM |
231 | } |
232 | ], | |
233 | columnB: [ | |
234 | { | |
235 | xtype: 'textfield', | |
236 | name: 'comment', | |
237 | value: '', | |
238 | fieldLabel: gettext('Comment') | |
239 | } | |
240 | ] | |
241 | }); | |
242 | ||
243 | Ext.apply(me, { | |
244 | subject: gettext('IP/CIDR'), | |
245 | items: [ ipanel ] | |
246 | }); | |
247 | ||
248 | me.callParent(); | |
249 | ||
d5e771ce | 250 | if (!me.isCreate) { |
03ab9fba DM |
251 | me.load({ |
252 | success: function(response, options) { | |
253 | var values = response.result.data; | |
254 | ipanel.setValues(values); | |
255 | } | |
256 | }); | |
257 | } | |
258 | } | |
259 | }); | |
260 | ||
261 | Ext.define('PVE.IPSetGrid', { | |
262 | extend: 'Ext.grid.Panel', | |
263 | alias: 'widget.pveIPSetGrid', | |
264 | ||
123e1c80 DC |
265 | stateful: true, |
266 | stateId: 'grid-firewall-ipsets', | |
267 | ||
03ab9fba DM |
268 | base_url: undefined, |
269 | list_refs_url: undefined, | |
270 | ||
271 | addBtn: undefined, | |
272 | removeBtn: undefined, | |
273 | editBtn: undefined, | |
274 | ||
275 | setBaseUrl: function(url) { | |
276 | var me = this; | |
277 | ||
278 | me.base_url = url; | |
279 | ||
280 | if (url === undefined) { | |
281 | me.addBtn.setDisabled(true); | |
282 | me.store.removeAll(); | |
283 | } else { | |
284 | me.addBtn.setDisabled(false); | |
285 | me.store.setProxy({ | |
286 | type: 'pve', | |
287 | url: '/api2/json' + url | |
288 | }); | |
289 | ||
290 | me.store.load(); | |
291 | } | |
292 | }, | |
293 | ||
294 | initComponent: function() { | |
295 | /*jslint confusion: true */ | |
296 | var me = this; | |
297 | ||
298 | if (!me.list_refs_url) { | |
299 | throw "no1 list_refs_url specified"; | |
300 | } | |
301 | ||
302 | var store = new Ext.data.Store({ | |
303 | model: 'pve-ipset' | |
304 | }); | |
305 | ||
306 | var reload = function() { | |
307 | store.load(); | |
308 | }; | |
309 | ||
310 | var sm = Ext.create('Ext.selection.RowModel', {}); | |
311 | ||
312 | var run_editor = function() { | |
313 | var rec = sm.getSelection()[0]; | |
314 | if (!rec) { | |
315 | return; | |
316 | } | |
317 | var win = Ext.create('PVE.IPSetCidrEdit', { | |
318 | base_url: me.base_url, | |
319 | cidr: rec.data.cidr | |
320 | }); | |
321 | win.show(); | |
322 | win.on('destroy', reload); | |
323 | }; | |
324 | ||
325 | me.editBtn = new PVE.button.Button({ | |
326 | text: gettext('Edit'), | |
327 | disabled: true, | |
328 | selModel: sm, | |
329 | handler: run_editor | |
330 | }); | |
331 | ||
332 | me.addBtn = new PVE.button.Button({ | |
333 | text: gettext('Add'), | |
334 | disabled: true, | |
335 | handler: function() { | |
336 | if (!me.base_url) { | |
337 | return; | |
338 | } | |
339 | var win = Ext.create('PVE.IPSetCidrEdit', { | |
340 | base_url: me.base_url, | |
341 | list_refs_url: me.list_refs_url | |
342 | }); | |
343 | win.show(); | |
344 | win.on('destroy', reload); | |
345 | } | |
346 | }); | |
347 | ||
348 | me.removeBtn = new PVE.button.Button({ | |
349 | text: gettext('Remove'), | |
350 | selModel: sm, | |
351 | disabled: true, | |
352 | handler: function() { | |
353 | var rec = sm.getSelection()[0]; | |
354 | if (!rec || !me.base_url) { | |
355 | return; | |
356 | } | |
357 | ||
358 | PVE.Utils.API2Request({ | |
359 | url: me.base_url + '/' + rec.data.cidr, | |
360 | method: 'DELETE', | |
361 | waitMsgTarget: me, | |
362 | failure: function(response, options) { | |
363 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
364 | }, | |
365 | callback: reload | |
366 | }); | |
367 | } | |
368 | }); | |
369 | ||
370 | var render_errors = function(value, metaData, record) { | |
371 | var errors = record.data.errors; | |
372 | if (errors) { | |
373 | var msg = errors.cidr || errors.nomatch; | |
374 | if (msg) { | |
c0f75e23 | 375 | metaData.tdCls = 'pve-invalid-row'; |
03ab9fba DM |
376 | var html = '<p>' + Ext.htmlEncode(msg) + '</p>'; |
377 | metaData.tdAttr = 'data-qwidth=600 data-qtitle="ERROR" data-qtip="' + | |
378 | html.replace(/\"/g,'"') + '"'; | |
379 | } | |
380 | } | |
381 | return value; | |
382 | }; | |
383 | ||
384 | Ext.apply(me, { | |
385 | tbar: [ '<b>IP/CIDR:</b>', me.addBtn, me.removeBtn, me.editBtn ], | |
386 | store: store, | |
387 | selModel: sm, | |
388 | listeners: { | |
389 | itemdblclick: run_editor | |
390 | }, | |
391 | columns: [ | |
392 | { | |
393 | xtype: 'rownumberer' | |
394 | }, | |
395 | { | |
396 | header: gettext('IP/CIDR'), | |
397 | dataIndex: 'cidr', | |
398 | width: 150, | |
399 | renderer: function(value, metaData, record) { | |
400 | value = render_errors(value, metaData, record); | |
401 | if (record.data.nomatch) { | |
402 | return '<b>! </b>' + value; | |
403 | } | |
404 | return value; | |
405 | } | |
406 | }, | |
407 | { | |
408 | header: gettext('Comment'), | |
409 | dataIndex: 'comment', | |
410 | flex: 1, | |
411 | renderer: function(value) { | |
412 | return Ext.util.Format.htmlEncode(value); | |
413 | } | |
414 | } | |
415 | ] | |
416 | }); | |
417 | ||
418 | me.callParent(); | |
419 | ||
420 | if (me.base_url) { | |
421 | me.setBaseUrl(me.base_url); // load | |
422 | } | |
423 | } | |
424 | }, function() { | |
425 | ||
426 | Ext.define('pve-ipset', { | |
427 | extend: 'Ext.data.Model', | |
428 | fields: [ { name: 'nomatch', type: 'boolean' }, | |
429 | 'cidr', 'comment', 'errors' ], | |
430 | idProperty: 'cidr' | |
431 | }); | |
432 | ||
433 | }); | |
434 | ||
435 | Ext.define('PVE.IPSet', { | |
436 | extend: 'Ext.panel.Panel', | |
437 | alias: 'widget.pveIPSet', | |
438 | ||
439 | title: 'IPSet', | |
440 | ||
ba93a9c6 DC |
441 | onlineHelp: 'pve_firewall_ip_sets', |
442 | ||
03ab9fba DM |
443 | list_refs_url: undefined, |
444 | ||
445 | initComponent: function() { | |
446 | var me = this; | |
447 | ||
448 | if (!me.list_refs_url) { | |
449 | throw "no list_refs_url specified"; | |
450 | } | |
451 | ||
452 | var ipset_panel = Ext.createWidget('pveIPSetGrid', { | |
453 | region: 'center', | |
454 | list_refs_url: me.list_refs_url, | |
03ab9fba DM |
455 | border: false |
456 | }); | |
457 | ||
458 | var ipset_list = Ext.createWidget('pveIPSetList', { | |
459 | region: 'west', | |
460 | ipset_panel: ipset_panel, | |
461 | base_url: me.base_url, | |
123e1c80 | 462 | width: '50%', |
03ab9fba DM |
463 | border: false, |
464 | split: true | |
465 | }); | |
466 | ||
467 | Ext.apply(me, { | |
468 | layout: 'border', | |
469 | items: [ ipset_list, ipset_panel ], | |
470 | listeners: { | |
471 | show: function() { | |
472 | ipset_list.fireEvent('show', ipset_list); | |
473 | } | |
474 | } | |
475 | }); | |
476 | ||
477 | me.callParent(); | |
478 | } | |
479 | }); |