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.keyTranslatable = function(scope) {
1087 var node;
1088 node = scope.$modelValue;
1089 // Regexp::Assemble->new->add(qw(
1090 // authChoice
1091 // casAppMetaDataNode
1092 // casSrvMetaDataNode
1093 // cmbModule
1094 // keyNode
1095 // keyText
1096 // menuApp
1097 // menuCat
1098 // rule
1099 // oidcAttribute
1100 // oidcOPMetaDataNode
1101 // oidcRPMetaDataNode
1102 // samlAttribute
1103 // samlIDPMetaDataNode
1104 // samlSPMetaDataNode
1105 // sfExtra
1106 // virtualHost
1107 // ))->anchor_line->as_string
1108 if (node.type && node.type.match(/^(?:(?:c(?:as(?:App|Srv)MetaDataNod|mbModul)|oidc(?:[OR]PMetaDataNod|Attribut)|authChoic|rul)e|s(?:aml(?:(?:ID|S)PMetaDataNod|Attribut)e|fExtra)|key(?:Node|Text)|menu(?:App|Cat)|virtualHost)$/)) {
1109 return true;
1110 } else {
1111 return false;
1112 }
1113 };
1114 // Send test Email
1115 $scope.sendTestMail = function() {
1116 $scope.message = {
1117 title: 'sendTestMail',
1118 field: 'dest'
1119 };
1120 return $scope.showModal('prompt.html').then(function() {
1121 var dest;
1122 $scope.result;
1123 $scope.waiting = true;
1124 dest = $scope.result;
1125 return $http.post(`${window.confPrefix}/sendTestMail`, {
1126 "dest": dest
1127 }).then(function(response) {
1128 var error,
1129 success;
1130 success = response.data.success;
1131 error = response.data.error;
1132 $scope.waiting = false;
1133 if (success) {
1134 $scope.message = {
1135 title: 'ok',
1136 message: '__sendTestMailSuccess__',
1137 items: []
1138 };
1139 } else {
1140 $scope.message = {
1141 title: 'error',
1142 message: error,
1143 items: []
1144 };
1145 }
1146 return $scope.showModal('message.html');
1147 },
1148 readError);
1149 },
1150 function() {
1151 console.error('Error sending test email');
1152 });
1153 };
1154 // RSA keys generation
1155 $scope.newCertificate = function() {
1156 return $scope.showModal('password.html').then(function() {
1157 var currentNode,
1158 password;
1159 $scope.waiting = true;
1160 currentNode = $scope.currentNode;
1161 password = $scope.result;
1162 return $http.post(`${window.confPrefix}/newCertificate`, {
1163 "password": password
1164 }).then(function(response) {
1165 currentNode.data[0].data = response.data.private;
1166 currentNode.data[1].data = password;
1167 currentNode.data[2].data = response.data.public;
1168 return $scope.waiting = false;
1169 },
1170 readError);
1171 },
1172 function() {
1173 console.debug('New key cancelled');
1174 });
1175 };
1176 $scope.newEcCertificate = function() {
1177 return $scope.showModal('password.html').then(function() {
1178 var currentNode,
1179 password;
1180 $scope.waiting = true;
1181 currentNode = $scope.currentNode;
1182 password = $scope.result;
1183 return $http.post(`${window.confPrefix}/newEcCertificate`, {
1184 "password": password
1185 }).then(function(response) {
1186 currentNode.data[0].data = response.data.private;
1187 currentNode.data[1].data = password;
1188 currentNode.data[2].data = response.data.public;
1189 return $scope.waiting = false;
1190 },
1191 readError);
1192 },
1193 function() {
1194 console.debug('New key cancelled');
1195 });
1196 };
1197 $scope.newEcKeys = function() {
1198 var currentNode;
1199 $scope.waiting = true;
1200 currentNode = $scope.currentNode;
1201 return $http.post(`${window.confPrefix}/newEcKeys`, {
1202 "password": ''
1203 }).then(function(response) {
1204 var i,
1205 o;
1206 for (i = o = 0; o <= 3; i = ++o) {
1207 currentNode.data[i + 4].data = currentNode.data[i].data;
1208 }
1209 currentNode.data[0].data = response.data.private;
1210 currentNode.data[1].data = response.data.public;
1211 currentNode.data[2].data = response.data.hash;
1212 currentNode.data[3].data = 'EC';
1213 return $scope.waiting = false;
1214 },
1215 readError);
1216 };
1217 $scope.newCertificateNoPassword = function() {
1218 var currentNode;
1219 $scope.waiting = true;
1220 currentNode = $scope.currentNode;
1221 return $http.post(`${window.confPrefix}/newCertificate`, {
1222 "password": ''
1223 }).then(function(response) {
1224 var i,
1225 o;
1226 for (i = o = 0; o <= 3; i = ++o) {
1227 currentNode.data[i + 4].data = currentNode.data[i].data;
1228 }
1229 currentNode.data[0].data = response.data.private;
1230 currentNode.data[1].data = response.data.public;
1231 currentNode.data[2].data = response.data.hash;
1232 currentNode.data[3].data = 'RSA';
1233 return $scope.waiting = false;
1234 },
1235 readError);
1236 };
1237 $scope.newRSAKey = function() {
1238 return $scope.showModal('password.html').then(function() {
1239 var currentNode,
1240 password;
1241 $scope.waiting = true;
1242 currentNode = $scope.currentNode;
1243 password = $scope.result;
1244 return $http.post(`${window.confPrefix}/newRSAKey`, {
1245 "password": password
1246 }).then(function(response) {
1247 currentNode.data[0].data = response.data.private;
1248 currentNode.data[1].data = password;
1249 currentNode.data[2].data = response.data.public;
1250 return $scope.waiting = false;
1251 },
1252 readError);
1253 },
1254 function() {
1255 console.debug('New key cancelled');
1256 });
1257 };
1258 // - return a promise with the data:
1259 // - from node when set
1260 // - after downloading else
1261
1262 $scope.getKey = function(node) {
1263 var d,
1264 i,
1265 len,
1266 n,
1267 o,
1268 ref,
1269 tmp,
1270 uri;
1271 d = $q.defer();
1272 if (!node.data) {
1273 $scope.waiting = true;
1274 if (node.get && typeof node.get === 'object') {
1275 node.data = [];
1276 tmp = [];
1277 ref = node.get;
1278 for (i = o = 0, len = ref.length; o < len; i = ++o) {
1279 n = ref[i];
1280 node.data[i] = {
1281 title: n.split("/").pop(),
1282 get: n,
1283 id: n
1284 };
1285 tmp.push($scope.getKey(node.data[i]));
1286 }
1287 $q.all(tmp).then(function() {
1288 return d.resolve(node.data);
1289 },
1290 function(response) {
1291 d.reject(response.statusLine);
1292 return $scope.waiting = false;
1293 });
1294 } else {
1295 uri = '';
1296 if (node.get) {
1297 console.debug(`Trying to get key ${node.get}`);
1298 uri = encodeURI(node.get);
1299 } else {
1300 console.debug(`Trying to get title ${node.title}`);
1301 }
1302 $http.get(`${window.confPrefix}${$scope.currentCfg.cfgNum}/${node.get ? uri : node.title}`).then(function(response) {
1303 var data;
1304 // Set default value if response is null or if asked by server
1305 data = response.data;
1306 if ((data.value === null || (data.error && data.error.match(/setDefault$/))) && node['default'] !== null) {
1307 node.data = node['default'];
1308 } else {
1309 node.data = data.value;
1310 }
1311 // Cast int as int (remember that booleans are int for Perl)
1312 if (node.type && node.type.match(/^(bool|trool|boolOrExpr)$/)) {
1313 if (typeof node.data === 'string' && node.data.match(/^(?:-1|0|1)$/)) {
1314 node.data = parseInt(node.data,
1315 10);
1316 }
1317 }
1318 if (node.type && node.type.match(/^int$/)) {
1319 node.data = parseInt(node.data,
1320 10);
1321 }
1322 if (node.type && node.type.match(/^select$/)) {
1323 node.data = node.data ? node.data.toString() : '';
1324 // Split SAML types
1325 } else if (node.type && node.type.match(/^(saml(Service|Assertion)|blackWhiteList)$/) && !(node.data && typeof node.data === 'object')) {
1326 node.data = node.data ? node.data.split(';') : [];
1327 }
1328 $scope.waiting = false;
1329 return d.resolve(node.data);
1330 },
1331 function(response) {
1332 readError(response);
1333 return d.reject(response.status);
1334 });
1335 }
1336 } else {
1337 d.resolve(node.data);
1338 }
1339 return d.promise;
1340 };
1341 // function `pathEvent(event, next; current)`:
1342 // Called when $location.path() change, launch getCfg() with the new
1343 // configuration number
1344 pathEvent = function(event,
1345 next,
1346 current) {
1347 var n;
1348 n = next.match(new RegExp('#!?/confs/(latest|[0-9]+)'));
1349 if (n === null) {
1350 return $location.path('/confs/latest');
1351 } else {
1352 console.debug(`Trying to get cfg number ${n[1]}`);
1353 return $scope.getCfg(n[1]);
1354 }
1355 };
1356 $scope.$on('$locationChangeSuccess',
1357 pathEvent);
1358 // function `getCfg(n)`:
1359 // Download configuration metadatas
1360 $scope.getCfg = function(n) {
1361 if ($scope.currentCfg.cfgNum !== n) {
1362 return $http.get(`${window.confPrefix}${n}`).then(function(response) {
1363 var d;
1364 $scope.currentCfg = response.data;
1365 d = new Date($scope.currentCfg.cfgDate * 1000);
1366 $scope.currentCfg.date = d.toLocaleString();
1367 console.debug(`Metadatas of cfg ${n} loaded`);
1368 $location.path(`/confs/${n}`);
1369 return $scope.init();
1370 },
1371 function(response) {
1372 return readError(response).then(function() {
1373 $scope.currentCfg.cfgNum = 0;
1374 return $scope.init();
1375 });
1376 });
1377 } else {
1378 return $scope.waiting = false;
1379 }
1380 };
1381 // method `getLanguage(lang)`
1382 // Launch init() after setting current language
1383 $scope.getLanguage = function(lang) {
1384 $scope.lang = lang;
1385 // Force reload home
1386 $scope.form = 'white';
1387 $scope.init();
1388 return $scope.showM = false;
1389 };
1390 // Initialization
1391
1392 // Load JSON files:
1393 // - struct.json: the main tree
1394 // - languages/<lang>.json: the chosen language datas
1395 $scope.init = function() {
1396 var tmp;
1397 tmp = null;
1398 $scope.waiting = true;
1399 $scope.breadCrumb = null;
1400 $scope.data = [];
1401 $scope.confirmNeeded = false;
1402 $scope.forceSave = false;
1403 $q.all([
1404 $translator.init($scope.lang),
1405 $http.get(`${window.staticPrefix}struct.json`).then(function(response) {
1406 tmp = response.data;
1407 console.debug("Structure loaded");
1408 })
1409 ]).then(function() {
1410 console.debug("Starting structure binding");
1411 $scope.data = tmp;
1412 tmp = null;
1413 if ($scope.currentCfg.cfgNum !== 0) {
1414 setScopeVars($scope);
1415 } else {
1416 $scope.message = {
1417 title: 'emptyConf',
1418 message: '__zeroConfExplanations__'
1419 };
1420 $scope.showModal('message.html');
1421 }
1422 $scope.form = 'home';
1423 return $scope.waiting = false;
1424 },
1425 readError);
1426 // Colorized link
1427 $scope.activeModule = "conf";
1428 return $scope.myStyle = {
1429 color: '#ffb84d'
1430 };
1431 };
1432 c = $location.path().match(new RegExp('^/confs/(latest|[0-9]+)'));
1433 if (!c) {
1434 console.debug("Redirecting to /confs/latest");
1435 $location.path('/confs/latest');
1436 }
1437 // File form function
1438 $scope.replaceContentByUrl = function(node,
1439 url) {
1440 $scope.waiting = true;
1441 return $http.post(window.scriptname + "prx", {
1442 url: url
1443 }).then(function(response) {
1444 node.data = response.data.content;
1445 return $scope.waiting = false;
1446 },
1447 readError);
1448 };
1449 $scope.replaceContent = function(node,
1450 $fileContent) {
1451 return node.data = $fileContent;
1452 };
1453 // Import Filesaver.js saveAs()
1454 $scope.saveAs = function(content,
1455 type,
1456 filename) {
1457 return saveAs(new Blob([content], {
1458 "type": type
1459 }),
1460 filename);
1461 };
1462 // Save as pem, text,...
1463 $scope.saveAsPem = function(cs,
1464 scope) {
1465 return scope.saveAs(`${cs.data[0].data}\n${cs.data[2].data}`,
1466 'application/x-pem-file',
1467 `${cs.title}.pem`);
1468 };
1469 return $scope.saveAsText = function(cs,
1470 scope) {
1471 return scope.saveAs(cs.data,
1472 'text/plain',
1473 `${cs.title}.txt`);
1474 };
1475 }
1476 ]);