luci-app-libreswan: prevent proposals and tunnels overwriting each other
authorPaul Donald <newtwen+github@gmail.com>
Fri, 22 Nov 2024 22:47:19 +0000 (23:47 +0100)
committerPaul Donald <newtwen+github@gmail.com>
Fri, 22 Nov 2024 22:47:19 +0000 (23:47 +0100)
Signed-off-by: Paul Donald <newtwen+github@gmail.com>
applications/luci-app-libreswan/htdocs/luci-static/resources/view/libreswan/proposals.js
applications/luci-app-libreswan/htdocs/luci-static/resources/view/libreswan/tunnels.js

index 8569e05caa45d46f8dd3b88ee10ab9d33cdee414..e1b14a8e324eb14c87bfea9d3c5e2d8582af5ca2 100644 (file)
@@ -2,6 +2,7 @@
 'require view';
 'require ui';
 'require form';
+'require uci';
 
 return view.extend({
        render: function() {
@@ -15,6 +16,25 @@ return view.extend({
                s.nodescriptions = true;
                s.addbtntitle = _('Add Proposal');
 
+               s.renderSectionAdd = function(extra_class) {
+                       var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments),
+                               nameEl = el.querySelector('.cbi-section-create-name');
+                       ui.addValidator(nameEl, 'uciname', true, function(v) {
+                               let sections = [
+                                       ...uci.sections('libreswan', 'crypto_proposal'),
+                                       ...uci.sections('libreswan', 'tunnel'),
+                               ];
+                               if (sections.find(function(s) {
+                                       return s['.name'] == v;
+                               })) {
+                                       return _('This may not share the same name as other proposals or configured tunnels.');
+                               }
+                               if (v.length > 15) return _('Name length shall not exceed 15 characters');
+                               return true;
+                       }, 'blur', 'keyup');
+                       return el;
+               };
+
                o = s.tab('general', _('General'));
 
                o = s.taboption('general', form.MultiValue, 'hash_algorithm', _('Hash Algorithm'), ('* = %s').format(_('Unsafe')));
index f7cb4394584828a3cbca688617e96db0d3881afc..762e17cee748c1a47699ca6cb07ca1ef849f4a4f 100644 (file)
@@ -54,6 +54,26 @@ return view.extend({
                s.nodedescription = false;
                s.addbtntitle = _('Add Tunnel');
 
+               s.renderSectionAdd = function(extra_class) {
+                       var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments),
+                               nameEl = el.querySelector('.cbi-section-create-name');
+                       ui.addValidator(nameEl, 'uciname', true, function(v) {
+                               let sections = [
+                                       ...uci.sections('libreswan', 'crypto_proposal'),
+                                       ...uci.sections('libreswan', 'tunnel'),
+                               ];
+                               
+                               if (sections.find(function(s) {
+                                       return s['.name'] == v;
+                               })) {
+                                       return _('This may not share the same name as other proposals or configured tunnels.');
+                               }
+                               if (v.length > 15) return _('Name length shall not exceed 15 characters');
+                               return true;
+                       }, 'blur', 'keyup');
+                       return el;
+               };
+
                o = s.tab('general', _('General'));
                o = s.tab('authentication', _('Authentication'));
                o = s.tab('interface', _('Interface'));