Debian Codegraph @

lemonldap-ng-manager/site/js-src/manager.js

raw symbols SCIP
1 /*
2 LemonLDAP::NG Manager client
3
4 This is the main app file. Other are:
5 - struct.json and js/confTree.js that contains the full tree
6 - translate.json that contains the keywords translation
7
8 This file contains:
9 - the AngularJS controller
10 */
11 var llapp;
12
13 llapp = angular.module('llngManager', ['ui.tree', 'ui.bootstrap', 'llApp', 'ngCookies']);
14
15 /*
16 Main AngularJS controller
17 */
18 llapp.controller('TreeCtrl', [
19 '$scope',
20 '$http',
21 '$location',
22 '$q',
23 '$uibModal',
24 '$translator',
25 '$cookies',
26 '$htmlParams',
27 '$timeout',
28 function($scope,
29 $http,
30 $location,
31 $q,
32 $uibModal,
33 $translator,
34 $cookies,
35 $htmlParams,
36 $timeout) {
37 var _checkSaveResponse,
38 _download,
39 _getAll,
40 _stoggle,
41 c,
42 idinc,
43 pathEvent,
44 readError,
45 setDefault,
46 setHelp;
47 $scope.links = window.links;
48 $scope.menu = $htmlParams.menu;
49 $scope.menulinks = window.menulinks;
50 $scope.staticPrefix = window.staticPrefix;
51 $scope.formPrefix = window.formPrefix;
52 $scope.availableLanguages = window.availableLanguages;
53 $scope.waiting = true;
54 $scope.showM = false;
55 $scope.showT = false;
56 $scope.form = 'home';
57 $scope.currentCfg = {};
58 $scope.clipboardAvailable = Boolean(navigator.clipboard);
59 $scope.confPrefix = window.confPrefix;
60 $scope.message = {};
61 $scope.result = '';
62 // Import translations functions
63 $scope.translateTitle = function(node) {
64 return $translator.translateField(node,
65 'title');
66 };
67 $scope.translateP = $translator.translateP;
68 $scope.translate = $translator.translate;
69 // HELP DISPLAY
70 $scope.helpUrl = 'start.html#configuration';
71 $scope.setShowHelp = function(val) {
72 var d;
73 if (val == null) {
74 val = !$scope.showH;
75 }
76 $scope.showH = val;
77 d = new Date(Date.now());
78 d.setFullYear(d.getFullYear() + 1);
79 return $cookies.put('showhelp',
80 (val ? 'true' : 'false'), {
81 "expires": d
82 });
83 };
84 $scope.showH = $cookies.get('showhelp') === 'false' ? false : true;
85 if ($scope.showH == null) {
86 $scope.setShowHelp(true);
87 }
88 // INTERCEPT AJAX ERRORS
89 readError = function(response) {
90 var e,
91 j;
92 e = response.status;
93 j = response.statusLine;
94 $scope.waiting = false;
95 if (e === 403) {
96 $scope.message = {
97 title: 'forbidden',
98 message: '',
99 items: []
100 };
101 } else if (e === 401) {
102 console.debug('Authentication needed');
103 $scope.message = {
104 title: 'authenticationNeeded',
105 message: '__waitOrF5__',
106 items: []
107 };
108 } else if (e === 400) {
109 $scope.message = {
110 title: 'badRequest',
111 message: j,
112 items: []
113 };
114 } else if (e > 0) {
115 $scope.message = {
116 title: 'badRequest',
117 message: j,
118 items: []
119 };
120 } else {
121 $scope.message = {
122 title: 'networkProblem',
123 message: '',
124 items: []
125 };
126 }
127 return $scope.showModal('message.html');
128 };
129 // Modal launcher
130 $scope.showModal = function(tpl,
131 init) {
132 var d,
133 modalInstance;
134 modalInstance = $uibModal.open({
135 templateUrl: tpl,
136 controller: 'ModalInstanceCtrl',
137 size: 'lg',
138 resolve: {
139 elem: function() {
140 return function(s) {
141 return $scope[s];
142 };
143 },
144 set: function() {
145 return function(f,
146 s) {
147 return $scope[f] = s;
148 };
149 },
150 init: function() {
151 return init;
152 }
153 }
154 });
155 d = $q.defer();
156 modalInstance.result.then(function(msgok) {
157 $scope.message = {
158 title: '',
159 message: '',
160 items: []
161 };
162 return d.resolve(msgok);
163 },
164 function(msgnok) {
165 $scope.message = {
166 title: '',
167 message: '',
168 items: []
169 };
170 return d.reject(msgnok);
171 });
172 return d.promise;
173 };
174 // FORM DISPLAY FUNCTIONS
175
176 // Function called when a menu item is selected. It launch function stored in
177 // "action" or "title"
178 $scope.menuClick = function(button) {
179 if (button.popup) {
180 window.open(button.popup);
181 } else {
182 if (!button.action) {
183 button.action = button.title;
184 }
185 switch (typeof button.action) {
186 case 'function':
187 button.action($scope.currentNode,
188 $scope);
189 break;
190 case 'string':
191 $scope[button.action]();
192 break;
193 default:
194 console.warn('Unknown action type', typeof button.action);
195 }
196 }
197 return $scope.showM = false;
198 };
199 // Display main form
200 $scope.home = function() {
201 $scope.form = 'home';
202 return $scope.showM = false;
203 };
204 // SAVE FUNCTIONS
205
206 // Private method called by $scope.save()
207 _checkSaveResponse = function(data) {
208 var m;
209 $scope.message = {
210 title: '',
211 message: '',
212 items: [],
213 itemsE: [],
214 itemsNC: [],
215 itemsW: []
216 };
217 if (data.needConfirm) {
218 $scope.confirmNeeded = true;
219 }
220 if (data.message) {
221 $scope.message.message = data.message;
222 }
223 // Sort messages
224 if (data.details) {
225 for (m in data.details) {
226 if (m !== '__changes__') {
227 if (m === '__needConfirmation__') {
228 $scope.message.itemsNC.push({
229 message: m,
230 items: data.details[m]
231 });
232 console.debug('NeedConfirmation:', $scope.message.itemsNC);
233 } else if (m === '__warnings__') {
234 $scope.message.itemsW.push({
235 message: m,
236 items: data.details[m]
237 });
238 console.debug('Warnings:', $scope.message.itemsW);
239 } else {
240 $scope.message.itemsE.push({
241 message: m,
242 items: data.details[m]
243 });
244 console.debug('Errors:',
245 $scope.message.itemsE);
246 }
247 }
248 }
249 $scope.message.items = $scope.message.itemsE.concat($scope.message.itemsNC.concat($scope.message.itemsW));
250 }
251 $scope.waiting = false;
252 if (data.result === 1) {
253 // Force reloading page
254 $location.path('/confs/');
255 $scope.message.title = 'successfullySaved';
256 } else {
257 $scope.message.title = 'saveReport';
258 }
259 return $scope.showModal('message.html');
260 };
261 // Download raw conf
262 $scope.downloadConf = function() {
263 return window.open($scope.confPrefix + $scope.currentCfg.cfgNum + '?full=1');
264 };
265 // Download OIDC metadata
266 $scope.downloadOidcMetadata = function() {
267 return window.open($scope.confPrefix + $scope.currentCfg.cfgNum + '?oidcMetadata=1');
268 };
269 // Download SAML metadata
270 $scope.downloadSamlMetadata = function() {
271 return window.open($scope.confPrefix + $scope.currentCfg.cfgNum + '?samlMetadata=1');
272 };
273 // Main save function
274 $scope.save = function() {
275 $scope.showModal('save.html').then(function() {
276 $scope.waiting = true;
277 $scope.data.push({
278 id: "cfgLog",
279 title: "cfgLog",
280 data: $scope.result ? $scope.result : ''
281 });
282 return $http.post(`${window.confPrefix}?cfgNum=${$scope.currentCfg.cfgNum}&cfgDate=${$scope.currentCfg.cfgDate}${$scope.forceSave ? "&force=1" : ''}`,
283 $scope.data).then(function(response) {
284 $scope.data.pop();
285 return _checkSaveResponse(response.data);
286 },
287 function(response) {
288 readError(response);
289 return $scope.data.pop();
290 });
291 },
292 function() {
293 console.debug('Saving canceled');
294 });
295 return $scope.showM = false;
296 };
297 // Raw save function
298 $scope.saveRawConf = function($fileContent) {
299 $scope.waiting = true;
300 return $http.post(`${window.confPrefix}/raw`,
301 $fileContent).then(function(response) {
302 return _checkSaveResponse(response.data);
303 },
304 readError);
305 };
306 // Restore raw conffunction
307 $scope.restore = function() {
308 $scope.currentNode = null;
309 return $scope.form = 'restore';
310 };
311 // Cancel save function
312 $scope.cancel = function() {
313 $scope.currentNode.data = null;
314 return $scope.getKey($scope.currentNode);
315 };
316 // NODES MANAGEMENT
317 idinc = 1;
318 $scope._findContainer = function() {
319 return $scope._findScopeContainer().$modelValue;
320 };
321 $scope._findScopeContainer = function() {
322 var cs;
323 cs = $scope.currentScope;
324 while (!cs.$modelValue.type.match(/Container$/)) {
325 cs = cs.$parentNodeScope;
326 }
327 return cs;
328 };
329 $scope._findScopeByKey = function(k) {
330 var cs;
331 cs = $scope.currentScope;
332 while (!(cs.$modelValue.title === k)) {
333 cs = cs.$parentNodeScope;
334 }
335 return cs;
336 };
337 // Add grant rule entry
338 $scope.newGrantRule = function() {
339 var node;
340 node = $scope._findContainer();
341 node.nodes.length;
342 return node.nodes.push({
343 id: `${node.id}/n${idinc++}`,
344 title: 'New rule',
345 re: '"Message"',
346 comment: 'New rule',
347 data: '1',
348 type: "grant"
349 });
350 };
351 // Add rules entry
352 $scope.newRule = function() {
353 var l,
354 n,
355 node;
356 node = $scope._findContainer();
357 l = node.nodes.length;
358 n = l > 0 ? l - 1 : 0;
359 return node.nodes.splice(n,
360 0, {
361 id: `${node.id}/n${idinc++}`,
362 title: 'New rule',
363 re: '^/new',
364 comment: 'New rule',
365 data: 'accept',
366 type: "rule"
367 });
368 };
369 // Add form replay
370 $scope.newPost = function() {
371 var node;
372 node = $scope._findContainer();
373 return node.nodes.push({
374 id: `${node.id}/n${idinc++}`,
375 title: "/absolute/path/to/form",
376 data: {},
377 type: "post"
378 });
379 };
380 $scope.newPostVar = function() {
381 if ($scope.currentNode.data.vars == null) {
382 $scope.currentNode.data.vars = [];
383 }
384 return $scope.currentNode.data.vars.push(['var1',
385 '$uid'
386 ]);
387 };
388 // Add auth chain entry to authChoice
389 $scope.newAuthChoice = function() {
390 var node;
391 node = $scope._findContainer();
392 node.nodes.push({
393 id: `${node.id}/n${idinc++}`,
394 title: "1_Key",
395 data: ['Null',
396 'Null',
397 'Null'
398 ],
399 type: "authChoice"
400 });
401 return $scope.execFilters($scope._findScopeByKey('authParams'));
402 };
403 // Add hash entry
404 $scope.newHashEntry = function() {
405 var node;
406 node = $scope._findContainer();
407 return node.nodes.push({
408 id: `${node.id}/n${idinc++}`,
409 title: 'new',
410 data: '',
411 type: "keyText"
412 });
413 };
414 // Menu cat entry
415 $scope.newCat = function() {
416 var cs;
417 cs = $scope.currentScope;
418 if (cs.$modelValue.type === 'menuApp') {
419 cs = cs.$parentNodeScope;
420 }
421 return cs.$modelValue.nodes.push({
422 id: `${cs.$modelValue.id}/n${idinc++}`,
423 title: "New category",
424 type: "menuCat",
425 nodes: []
426 });
427 };
428 // Menu app entry
429 $scope.newApp = function() {
430 var cs;
431 cs = $scope.currentScope;
432 if (cs.$modelValue.type === 'menuApp') {
433 cs = cs.$parentNodeScope;
434 }
435 return cs.$modelValue.nodes.push({
436 id: `${cs.$modelValue.id}/n${idinc++}`,
437 title: "New application",
438 type: "menuApp",
439 data: {
440 description: "New app description",
441 uri: "https://test.example.com/",
442 tooltip: "New app tooltip",
443 logo: "network.png",
444 display: "auto"
445 }
446 });
447 };
448 // Combination module
449 $scope.newCmbMod = function() {
450 var node;
451 node = $scope._findContainer();
452 node.nodes.push({
453 id: `${node.id}/n${idinc++}`,
454 title: 'new',
455 type: 'cmbModule',
456 data: {
457 type: 'LDAP',
458 for: '0',
459 over: []
460 }
461 });
462 return $scope.execFilters($scope._findScopeByKey('authParams'));
463 };
464 $scope.newSfExtra = function() {
465 var node;
466 node = $scope._findContainer();
467 return node.nodes.push({
468 id: `${node.id}/n${idinc++}`,
469 title: 'new',
470 type: 'sfExtra',
471 data: {
472 type: '',
473 rule: '',
474 logo: '',
475 level: '',
476 label: '',
477 over: []
478 }
479 });
480 };
481 $scope.newSfOver = function() {
482 var d;
483 d = $scope.currentNode.data;
484 if (!d.over) {
485 d.over = [];
486 }
487 return d.over.push([`new${idinc++}`,
488 ''
489 ]);
490 };
491 $scope.newCmbOver = function() {
492 var d;
493 d = $scope.currentNode.data;
494 if (!d.over) {
495 d.over = [];
496 }
497 return d.over.push([`new${idinc++}`,
498 ''
499 ]);
500 };
501 $scope.newChoiceOver = function() {
502 var d;
503 d = $scope.currentNode.data;
504 console.debug("data", d);
505 if (!d[5]) {
506 d[5] = [];
507 }
508 return d[5].push([`new${idinc++}`,
509 ''
510 ]);
511 };
512 // Add host
513 $scope.addHost = function() {
514 var cn;
515 cn = $scope.currentNode;
516 if (!cn.data) {
517 cn.data = [];
518 }
519 return cn.data.push({
520 k: "newHost",
521 h: [{
522 "k": "key",
523 "v": "uid"
524 }]
525 });
526 };
527 // SAML attribute entry
528 $scope.addSamlAttribute = function() {
529 var node;
530 node = $scope._findContainer();
531 return node.nodes.push({
532 id: `${node.id}/n${idinc++}`,
533 title: 'new',
534 type: 'samlAttribute',
535 data: ['0',
536 'New',
537 '',
538 ''
539 ]
540 });
541 };
542 // OIDC attribute entry
543 $scope.addOidcAttribute = function() {
544 var node;
545 node = $scope._findContainer();
546 return node.nodes.push({
547 id: `${node.id}/n${idinc++}`,
548 title: 'new',
549 type: 'oidcAttribute',
550 data: ['',
551 'string',
552 'auto'
553 ]
554 });
555 };
556 // Nodes with template
557 $scope.addVhost = function() {
558 var name;
559 name = $scope.domain ? `.${$scope.domain.data}` : '.example.com';
560 $scope.message = {
561 title: 'virtualHostName',
562 field: 'hostname'
563 };
564 return $scope.showModal('prompt.html',
565 name).then(function() {
566 var n;
567 n = $scope.result;
568 if (n) {
569 return $scope.addTemplateNode(n,
570 'virtualHost');
571 }
572 });
573 };
574 $scope.duplicateVhost = function() {
575 var name;
576 name = $scope.domain ? `.${$scope.domain.data}` : '.example.com';
577 $scope.message = {
578 title: 'virtualHostName',
579 field: 'hostname'
580 };
581 return $scope.showModal('prompt.html',
582 name).then(function() {
583 var n;
584 n = $scope.result;
585 return $scope.duplicateNode(n,
586 'virtualHost',
587 $scope.currentNode.title);
588 });
589 };
590 $scope.addSamlIDP = function() {
591 return $scope.newTemplateNode('samlIDPMetaDataNode',
592 'samlPartnerName',
593 'idp-example');
594 };
595 $scope.addSamlSP = function() {
596 return $scope.newTemplateNode('samlSPMetaDataNode',
597 'samlPartnerName',
598 'sp-example');
599 };
600 $scope.duplicateSamlSp = function() {
601 $scope.message = {
602 title: 'samlPartnerName',
603 field: 'name'
604 };
605 return $scope.showModal('prompt.html',
606 'sp-copy').then(function() {
607 var n;
608 n = $scope.result;
609 return $scope.duplicateNode(n,
610 'samlSPMetaDataNode',
611 $scope.currentNode.title);
612 });
613 };
614 $scope.addOidcOp = function() {
615 return $scope.newTemplateNode('oidcOPMetaDataNode',
616 'oidcOPName',
617 'op-example');
618 };
619 $scope.addOidcRp = function() {
620 return $scope.newTemplateNode('oidcRPMetaDataNode',
621 'oidcRPName',
622 'rp-example');
623 };
624 $scope.duplicateOidcRp = function() {
625 $scope.message = {
626 title: 'oidcRPName',
627 field: 'name'
628 };
629 return $scope.showModal('prompt.html',
630 'rp-copy').then(function() {
631 var n;
632 n = $scope.result;
633 return $scope.duplicateNode(n,
634 'oidcRPMetaDataNode',
635 $scope.currentNode.title);
636 });
637 };
638 $scope.addCasSrv = function() {
639 return $scope.newTemplateNode('casSrvMetaDataNode',
640 'casPartnerName',
641 'srv-example');
642 };
643 $scope.addCasApp = function() {
644 return $scope.newTemplateNode('casAppMetaDataNode',
645 'casPartnerName',
646 'app-example');
647 };
648 $scope.duplicateCasApp = function() {
649 $scope.message = {
650 title: 'casPartnerName',
651 field: 'name'
652 };
653 return $scope.showModal('prompt.html',
654 'app-copy').then(function() {
655 var n;
656 n = $scope.result;
657 return $scope.duplicateNode(n,
658 'casAppMetaDataNode',
659 $scope.currentNode.title);
660 });
661 };
662 $scope.addKey = function() {
663 return $scope.newTemplateNode('keyNode',
664 'keyName',
665 'key-example');
666 };
667 $scope.newTemplateNode = function(type,
668 title,
669 init) {
670 $scope.message = {
671 title: title,
672 field: 'name'
673 };
674 return $scope.showModal('prompt.html',
675 init).then(function() {
676 var name;
677 name = $scope.result;
678 if (name) {
679 return $scope.addTemplateNode(name,
680 type);
681 }
682 });
683 };
684 $scope.addTemplateNode = function(name,
685 type) {
686 var cs,
687 t;
688 cs = $scope.currentScope;
689 while (cs.$modelValue.title !== `${type}s`) {
690 cs = cs.$parentNodeScope;
691 }
692 t = {
693 id: `${type}s/new__${name}`,
694 title: name,
695 type: type,
696 nodes: templates(type,
697 `new__${name}`)
698 };
699 setDefault(t.nodes);
700 cs.$modelValue.nodes.push(t);
701 cs.expand();
702 return t;
703 };
704 setDefault = function(node) {
705 var len,
706 n,
707 o;
708 for (o = 0, len = node.length; o < len; o++) {
709 n = node[o];
710 if (n.cnodes && n.default) {
711 delete n.cnodes;
712 n._nodes = n.default;
713 }
714 if (n._nodes) {
715 setDefault(n._nodes);
716 } else if (n.default || n.default === 0) {
717 n.data = n.default;
718 }
719 }
720 return node;
721 };
722 _getAll = function(node) {
723 var d,
724 d2;
725 d = $q.defer();
726 d2 = $q.defer();
727 if (node._nodes) {
728 _stoggle(node);
729 d.resolve();
730 } else if (node.cnodes) {
731 _download(node).then(function() {
732 return d.resolve();
733 });
734 } else if (node.nodes || node.data) {
735 d.resolve();
736 } else {
737 $scope.getKey(node).then(function() {
738 return d.resolve();
739 });
740 }
741 d.promise.then(function() {
742 var len,
743 n,
744 o,
745 ref,
746 t;
747 t = [];
748 if (node.nodes) {
749 ref = node.nodes;
750 for (o = 0, len = ref.length; o < len; o++) {
751 n = ref[o];
752 t.push(_getAll(n));
753 }
754 }
755 return $q.all(t).then(function() {
756 return d2.resolve();
757 });
758 });
759 return d2.promise;
760 };
761 $scope.duplicateNode = function(name,
762 type,
763 idkey) {
764 var cs;
765 cs = $scope.currentScope;
766 return _getAll($scope.currentNode).then(function() {
767 var t;
768 while (cs.$modelValue.title !== `${type}s`) {
769 cs = cs.$parentNodeScope;
770 }
771 t = JSON.parse(JSON.stringify($scope.currentNode).replace(/[*]/g,
772 '').replace(new RegExp(idkey,
773 'g'),
774 'new__' + name));
775 t.id = `${type}s/new__${name}`;
776 t.title = name;
777 cs.$modelValue.nodes.push(t);
778 return t;
779 });
780 };
781 $scope.del = function(a,
782 i) {
783 return a.splice(i,
784 1);
785 };
786 $scope.deleteEntry = function() {
787 var p;
788 p = $scope.currentScope.$parentNodeScope;
789 $scope.currentScope.remove();
790 return $scope.displayForm(p);
791 };
792 $scope.down = function() {
793 var i,
794 id,
795 ind,
796 len,
797 n,
798 o,
799 p,
800 ref,
801 tmp;
802 id = $scope.currentNode.id;
803 p = $scope.currentScope.$parentNodeScope.$modelValue;
804 ind = p.nodes.length;
805 ref = p.nodes;
806 for (i = o = 0, len = ref.length; o < len; i = ++o) {
807 n = ref[i];
808 if (n.id === id) {
809 ind = i;
810 }
811 }
812 if (ind < p.nodes.length - 1) {
813 tmp = p.nodes[ind];
814 p.nodes[ind] = p.nodes[ind + 1];
815 p.nodes[ind + 1] = tmp;
816 }
817 return ind;
818 };
819 $scope.up = function() {
820 var i,
821 id,
822 ind,
823 len,
824 n,
825 o,
826 p,
827 ref,
828 tmp;
829 id = $scope.currentNode.id;
830 p = $scope.currentScope.$parentNodeScope.$modelValue;
831 ind = -1;
832 ref = p.nodes;
833 for (i = o = 0, len = ref.length; o < len; i = ++o) {
834 n = ref[i];
835 if (n.id === id) {
836 ind = i;
837 }
838 }
839 if (ind > 0) {
840 tmp = p.nodes[ind];
841 p.nodes[ind] = p.nodes[ind - 1];
842 p.nodes[ind - 1] = tmp;
843 }
844 return ind;
845 };
846 // test if value is in select
847 $scope.inSelect = function(value) {
848 var len,
849 n,
850 o,
851 ref;
852 ref = $scope.currentNode.select;
853 for (o = 0, len = ref.length; o < len; o++) {
854 n = ref[o];
855 if (n.k === value) {
856 return true;
857 }
858 }
859 return false;
860 };
861 // This is for rule form: title = comment if defined, else title = re
862 $scope.changeRuleTitle = function(node) {
863 return node.title = node.comment.length > 0 ? node.comment : node.re;
864 };
865 // Node opening
866
867 // authParams mechanism: show used auth modules only (launched by stoggle)
868 $scope.filters = {};
869 $scope.execFilters = function(scope) {
870 var filter,
871 func,
872 ref;
873 scope = scope ? scope : $scope;
874 ref = $scope.filters;
875 for (filter in ref) {
876 func = ref[filter];
877 if ($scope.filters.hasOwnProperty(filter)) {
878 return window.filterFunctions[filter](scope,
879 $q,
880 func);
881 }
882 }
883 return false;
884 };
885 // To avoid binding all the tree, nodes are pushed in DOM only when opened
886 $scope.stoggle = function(scope) {
887 var node;
888 node = scope.$modelValue;
889 _stoggle(node);
890 return scope.toggle();
891 };
892 _stoggle = function(node) {
893 var a,
894 len,
895 len1,
896 len2,
897 n,
898 o,
899 q,
900 r,
901 ref,
902 ref1,
903 ref2;
904 ref = ['nodes', 'nodes_cond'];
905 for (o = 0, len = ref.length; o < len; o++) {
906 n = ref[o];
907 if (node[`_${n}`]) {
908 node[n] = [];
909 ref1 = node[`_${n}`];
910 for (q = 0, len1 = ref1.length; q < len1; q++) {
911 a = ref1[q];
912 node[n].push(a);
913 }
914 delete node[`_${n}`];
915 }
916 }
917 // Call execFilter for authParams
918 if (node._nodes_filter) {
919 if (node.nodes) {
920 ref2 = node.nodes;
921 for (r = 0, len2 = ref2.length; r < len2; r++) {
922 n = ref2[r];
923 n.onChange = $scope.execFilters;
924 }
925 }
926 $scope.filters[node._nodes_filter] = node;
927 return $scope.execFilters();
928 }
929 };
930 // Simple toggle management
931 $scope.toggle = function(scope) {
932 return scope.toggle();
933 };
934 // cnodes management: hash keys/values are loaded when parent node is opened
935 $scope.download = function(scope) {
936 var node;
937 node = scope.$modelValue;
938 return _download(node);
939 };
940 _download = function(node) {
941 var d,
942 uri;
943 d = $q.defer();
944 d.notify('Trying to get datas');
945 $scope.waiting = true;
946 console.debug(`Trying to get key ${node.cnodes}`);
947 uri = encodeURI(node.cnodes);
948 $http.get(`${window.confPrefix}${$scope.currentCfg.cfgNum}/${uri}`).then(function(response) {
949 var a,
950 data,
951 len,
952 o;
953 data = response.data;
954 // Manage datas errors
955 if (!data) {
956 d.reject('Empty response from server');
957 } else if (data.error) {
958 if (data.error.match(/setDefault$/)) {
959 if (node['default']) {
960 node.nodes = node['default'].slice(0);
961 } else {
962 node.nodes = [];
963 }
964 delete node.cnodes;
965 d.resolve('Set data to default value');
966 } else {
967 d.reject(`Server return an error: ${data.error}`);
968 }
969 } else {
970 // Store datas
971 delete node.cnodes;
972 if (!node.type) {
973 node.type = 'keyTextContainer';
974 }
975 node.nodes = [];
976 // TODO: try/catch
977 for (o = 0, len = data.length; o < len; o++) {
978 a = data[o];
979 if (a.template) {
980 a._nodes = templates(a.template,
981 a.title);
982 }
983 node.nodes.push(a);
984 if (a.type.match(/^rule$/)) {
985 console.debug("Parse rule AuthnLevel as integer");
986 if (a.level && typeof a.level === 'string') {
987 a.level = parseInt(a.level,
988 10);
989 }
990 }
991 }
992 d.resolve('OK');
993 }
994 return $scope.waiting = false;
995 },
996 function(response) {
997 readError(response);
998 return d.reject('');
999 });
1000 return d.promise;
1001 };
1002 $scope.openCnode = function(scope) {
1003 return $scope.download(scope).then(function() {
1004 return scope.toggle();
1005 });
1006 };
1007 $scope.copyPath = function() {
1008 var text = $scope.breadCrumb.join(" » ");
1009 navigator.clipboard.writeText(text).then(function () {
1010 $scope.$apply(function () {
1011 $scope.copySuccess = true;
1012 $timeout(function() {
1013 $scope.copySuccess = false;
1014 }, 400);
1015 });
1016 });
1017 };
1018 setHelp = function(scope) {
1019 while (!scope.$modelValue.help && scope.$parentNodeScope) {
1020 scope = scope.$parentNodeScope;
1021 }
1022 return $scope.helpUrl = scope.$modelValue.help || 'start.html#configuration';
1023 };
1024 // Form management
1025
1026 // `currentNode` contains the last select node
1027
1028 // method `diplayForm()`:
1029 // - set the `form` property to the name of the form to download
1030 // (`text` by default or `home` for node without `type` property)
1031 // - launch getKeys to set `node.data`
1032 // - hide tree when in XS size
1033
1034 $scope.getTrPath = function(scope) {
1035 var path = [];
1036 var trpath = [];
1037 var current = scope;
1038 var safetycount = 0;
1039 while (current && current.$modelValue && safetycount < 100) {
1040 safetycount = safetycount + 1;
1041 if (current.$modelValue.title && current.$modelValue.title != path[0]) {
1042 trpath.unshift(scope.translate(current.$modelValue.title));
1043 path.unshift(current.$modelValue.title);
1044 }
1045 current = current.$parent;
1046 }
1047 return trpath;
1048 };
1049
1050 $scope.displayForm = function(scope) {
1051 var f,
1052 len,
1053 n,
1054 node,
1055 o,
1056 ref;
1057
1058 node = scope.$modelValue;
1059 if (node.cnodes) {
1060 $scope.download(scope);
1061 }
1062 if (node._nodes) {
1063 $scope.stoggle(scope);
1064 }
1065 $scope.currentNode = node;
1066 $scope.currentScope = scope;
1067 f = node.type ? node.type : 'text';
1068 if (node.nodes || node._nodes || node.cnodes) {
1069 $scope.form = f !== 'text' ? f : 'mini';
1070 } else {
1071 $scope.form = f;
1072 // Get datas
1073 $scope.getKey(node);
1074 }
1075 if (node.type && node.type === 'simpleInputContainer') {
1076 ref = node.nodes;
1077 for (o = 0, len = ref.length; o < len; o++) {
1078 n = ref[o];
1079 $scope.getKey(n);
1080 }
1081 }
1082 $scope.showT = false;
1083 $scope.breadCrumb = $scope.getTrPath(scope);
1084 return setHelp(scope);
1085 };
1086 $scope.keyWritable = function(scope) {
1087 var node;
1088 node = scope.$modelValue;
1089 // regexp-assemble of:
1090 // authChoice
1091 // cmbModule
1092 // keyText
1093 // menuApp
1094 // menuCat
1095 // rule
1096 // oidcAttribute
1097 // samlAttribute
1098 // samlIDPMetaDataNode
1099 // samlSPMetaDataNode
1100 // sfExtra
1101 // virtualHost
1102 if (node.type && node.type.match(/^(?:s(?:aml(?:(?:ID|S)PMetaDataNod|Attribut)e|fExtra)|oidcAttribute|(?:(?:cmbMod|r)ul|authChoic)e|(?:virtualHos|keyTex)t|menu(?:App|Cat))$/)) {
1103 return true;
1104 } else {
1105 return false;
1106 }
1107 };
1108 // Send test Email
1109 $scope.sendTestMail = function() {
1110 $scope.message = {
1111 title: 'sendTestMail',
1112 field: 'dest'
1113 };
1114 return $scope.showModal('prompt.html').then(function() {
1115 var dest;
1116 $scope.result;
1117 $scope.waiting = true;
1118 dest = $scope.result;
1119 return $http.post(`${window.confPrefix}/sendTestMail`, {
1120 "dest": dest
1121 }).then(function(response) {
1122 var error,
1123 success;
1124 success = response.data.success;
1125 error = response.data.error;
1126 $scope.waiting = false;
1127 if (success) {
1128 $scope.message = {
1129 title: 'ok',
1130 message: '__sendTestMailSuccess__',
1131 items: []
1132 };
1133 } else {
1134 $scope.message = {
1135 title: 'error',
1136 message: error,
1137 items: []
1138 };
1139 }
1140 return $scope.showModal('message.html');
1141 },
1142 readError);
1143 },
1144 function() {
1145 console.error('Error sending test email');
1146 });
1147 };
1148 // RSA keys generation
1149 $scope.newCertificate = function() {
1150 return $scope.showModal('password.html').then(function() {
1151 var currentNode,
1152 password;
1153 $scope.waiting = true;
1154 currentNode = $scope.currentNode;
1155 password = $scope.result;
1156 return $http.post(`${window.confPrefix}/newCertificate`, {
1157 "password": password
1158 }).then(function(response) {
1159 currentNode.data[0].data = response.data.private;
1160 currentNode.data[1].data = password;
1161 currentNode.data[2].data = response.data.public;
1162 return $scope.waiting = false;
1163 },
1164 readError);
1165 },
1166 function() {
1167 console.debug('New key cancelled');
1168 });
1169 };
1170 $scope.newEcCertificate = function() {
1171 return $scope.showModal('password.html').then(function() {
1172 var currentNode,
1173 password;
1174 $scope.waiting = true;
1175 currentNode = $scope.currentNode;
1176 password = $scope.result;
1177 return $http.post(`${window.confPrefix}/newEcCertificate`, {
1178 "password": password
1179 }).then(function(response) {
1180 currentNode.data[0].data = response.data.private;
1181 currentNode.data[1].data = password;
1182 currentNode.data[2].data = response.data.public;
1183 return $scope.waiting = false;
1184 },
1185 readError);
1186 },
1187 function() {
1188 console.debug('New key cancelled');
1189 });
1190 };
1191 $scope.newEcKeys = function() {
1192 var currentNode;
1193 $scope.waiting = true;
1194 currentNode = $scope.currentNode;
1195 return $http.post(`${window.confPrefix}/newEcKeys`, {
1196 "password": ''
1197 }).then(function(response) {
1198 var i,
1199 o;
1200 for (i = o = 0; o <= 3; i = ++o) {
1201 currentNode.data[i + 4].data = currentNode.data[i].data;
1202 }
1203 currentNode.data[0].data = response.data.private;
1204 currentNode.data[1].data = response.data.public;
1205 currentNode.data[2].data = response.data.hash;
1206 currentNode.data[3].data = 'EC';
1207 return $scope.waiting = false;
1208 },
1209 readError);
1210 };
1211 $scope.newCertificateNoPassword = function() {
1212 var currentNode;
1213 $scope.waiting = true;
1214 currentNode = $scope.currentNode;
1215 return $http.post(`${window.confPrefix}/newCertificate`, {
1216 "password": ''
1217 }).then(function(response) {
1218 var i,
1219 o;
1220 for (i = o = 0; o <= 3; i = ++o) {
1221 currentNode.data[i + 4].data = currentNode.data[i].data;
1222 }
1223 currentNode.data[0].data = response.data.private;
1224 currentNode.data[1].data = response.data.public;
1225 currentNode.data[2].data = response.data.hash;
1226 currentNode.data[3].data = 'RSA';
1227 return $scope.waiting = false;
1228 },
1229 readError);
1230 };
1231 $scope.newRSAKey = function() {
1232 return $scope.showModal('password.html').then(function() {
1233 var currentNode,
1234 password;
1235 $scope.waiting = true;
1236 currentNode = $scope.currentNode;
1237 password = $scope.result;
1238 return $http.post(`${window.confPrefix}/newRSAKey`, {
1239 "password": password
1240 }).then(function(response) {
1241 currentNode.data[0].data = response.data.private;
1242 currentNode.data[1].data = password;
1243 currentNode.data[2].data = response.data.public;
1244 return $scope.waiting = false;
1245 },
1246 readError);
1247 },
1248 function() {
1249 console.debug('New key cancelled');
1250 });
1251 };
1252 // - return a promise with the data:
1253 // - from node when set
1254 // - after downloading else
1255
1256 $scope.getKey = function(node) {
1257 var d,
1258 i,
1259 len,
1260 n,
1261 o,
1262 ref,
1263 tmp,
1264 uri;
1265 d = $q.defer();
1266 if (!node.data) {
1267 $scope.waiting = true;
1268 if (node.get && typeof node.get === 'object') {
1269 node.data = [];
1270 tmp = [];
1271 ref = node.get;
1272 for (i = o = 0, len = ref.length; o < len; i = ++o) {
1273 n = ref[i];
1274 node.data[i] = {
1275 title: n.split("/").pop(),
1276 get: n,
1277 id: n
1278 };
1279 tmp.push($scope.getKey(node.data[i]));
1280 }
1281 $q.all(tmp).then(function() {
1282 return d.resolve(node.data);
1283 },
1284 function(response) {
1285 d.reject(response.statusLine);
1286 return $scope.waiting = false;
1287 });
1288 } else {
1289 uri = '';
1290 if (node.get) {
1291 console.debug(`Trying to get key ${node.get}`);
1292 uri = encodeURI(node.get);
1293 } else {
1294 console.debug(`Trying to get title ${node.title}`);
1295 }
1296 $http.get(`${window.confPrefix}${$scope.currentCfg.cfgNum}/${node.get ? uri : node.title}`).then(function(response) {
1297 var data;
1298 // Set default value if response is null or if asked by server
1299 data = response.data;
1300 if ((data.value === null || (data.error && data.error.match(/setDefault$/))) && node['default'] !== null) {
1301 node.data = node['default'];
1302 } else {
1303 node.data = data.value;
1304 }
1305 // Cast int as int (remember that booleans are int for Perl)
1306 if (node.type && node.type.match(/^(bool|trool|boolOrExpr)$/)) {
1307 if (typeof node.data === 'string' && node.data.match(/^(?:-1|0|1)$/)) {
1308 node.data = parseInt(node.data,
1309 10);
1310 }
1311 }
1312 if (node.type && node.type.match(/^int$/)) {
1313 node.data = parseInt(node.data,
1314 10);
1315 }
1316 if (node.type && node.type.match(/^select$/)) {
1317 node.data = node.data ? node.data.toString() : '';
1318 // Split SAML types
1319 } else if (node.type && node.type.match(/^(saml(Service|Assertion)|blackWhiteList)$/) && !(node.data && typeof node.data === 'object')) {
1320 node.data = node.data ? node.data.split(';') : [];
1321 }
1322 $scope.waiting = false;
1323 return d.resolve(node.data);
1324 },
1325 function(response) {
1326 readError(response);
1327 return d.reject(response.status);
1328 });
1329 }
1330 } else {
1331 d.resolve(node.data);
1332 }
1333 return d.promise;
1334 };
1335 // function `pathEvent(event, next; current)`:
1336 // Called when $location.path() change, launch getCfg() with the new
1337 // configuration number
1338 pathEvent = function(event,
1339 next,
1340 current) {
1341 var n;
1342 n = next.match(new RegExp('#!?/confs/(latest|[0-9]+)'));
1343 if (n === null) {
1344 return $location.path('/confs/latest');
1345 } else {
1346 console.debug(`Trying to get cfg number ${n[1]}`);
1347 return $scope.getCfg(n[1]);
1348 }
1349 };
1350 $scope.$on('$locationChangeSuccess',
1351 pathEvent);
1352 // function `getCfg(n)`:
1353 // Download configuration metadatas
1354 $scope.getCfg = function(n) {
1355 if ($scope.currentCfg.cfgNum !== n) {
1356 return $http.get(`${window.confPrefix}${n}`).then(function(response) {
1357 var d;
1358 $scope.currentCfg = response.data;
1359 d = new Date($scope.currentCfg.cfgDate * 1000);
1360 $scope.currentCfg.date = d.toLocaleString();
1361 console.debug(`Metadatas of cfg ${n} loaded`);
1362 $location.path(`/confs/${n}`);
1363 return $scope.init();
1364 },
1365 function(response) {
1366 return readError(response).then(function() {
1367 $scope.currentCfg.cfgNum = 0;
1368 return $scope.init();
1369 });
1370 });
1371 } else {
1372 return $scope.waiting = false;
1373 }
1374 };
1375 // method `getLanguage(lang)`
1376 // Launch init() after setting current language
1377 $scope.getLanguage = function(lang) {
1378 $scope.lang = lang;
1379 // Force reload home
1380 $scope.form = 'white';
1381 $scope.init();
1382 return $scope.showM = false;
1383 };
1384 // Initialization
1385
1386 // Load JSON files:
1387 // - struct.json: the main tree
1388 // - languages/<lang>.json: the chosen language datas
1389 $scope.init = function() {
1390 var tmp;
1391 tmp = null;
1392 $scope.waiting = true;
1393 $scope.breadCrumb = null;
1394 $scope.data = [];
1395 $scope.confirmNeeded = false;
1396 $scope.forceSave = false;
1397 $q.all([
1398 $translator.init($scope.lang),
1399 $http.get(`${window.staticPrefix}struct.json`).then(function(response) {
1400 tmp = response.data;
1401 console.debug("Structure loaded");
1402 })
1403 ]).then(function() {
1404 console.debug("Starting structure binding");
1405 $scope.data = tmp;
1406 tmp = null;
1407 if ($scope.currentCfg.cfgNum !== 0) {
1408 setScopeVars($scope);
1409 } else {
1410 $scope.message = {
1411 title: 'emptyConf',
1412 message: '__zeroConfExplanations__'
1413 };
1414 $scope.showModal('message.html');
1415 }
1416 $scope.form = 'home';
1417 return $scope.waiting = false;
1418 },
1419 readError);
1420 // Colorized link
1421 $scope.activeModule = "conf";
1422 return $scope.myStyle = {
1423 color: '#ffb84d'
1424 };
1425 };
1426 c = $location.path().match(new RegExp('^/confs/(latest|[0-9]+)'));
1427 if (!c) {
1428 console.debug("Redirecting to /confs/latest");
1429 $location.path('/confs/latest');
1430 }
1431 // File form function
1432 $scope.replaceContentByUrl = function(node,
1433 url) {
1434 $scope.waiting = true;
1435 return $http.post(window.scriptname + "prx", {
1436 url: url
1437 }).then(function(response) {
1438 node.data = response.data.content;
1439 return $scope.waiting = false;
1440 },
1441 readError);
1442 };
1443 $scope.replaceContent = function(node,
1444 $fileContent) {
1445 return node.data = $fileContent;
1446 };
1447 // Import Filesaver.js saveAs()
1448 $scope.saveAs = function(content,
1449 type,
1450 filename) {
1451 return saveAs(new Blob([content], {
1452 "type": type
1453 }),
1454 filename);
1455 };
1456 // Save as pem, text,...
1457 $scope.saveAsPem = function(cs,
1458 scope) {
1459 return scope.saveAs(`${cs.data[0].data}\n${cs.data[2].data}`,
1460 'application/x-pem-file',
1461 `${cs.title}.pem`);
1462 };
1463 return $scope.saveAsText = function(cs,
1464 scope) {
1465 return scope.saveAs(cs.data,
1466 'text/plain',
1467 `${cs.title}.txt`);
1468 };
1469 }
1470 ]);