Debian Codegraph @

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

raw symbols SCIP
1 /*
2 LemonLDAP::NG Viewer 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('llngViewer', ['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 _download,
38 _stoggle,
39 c,
40 id,
41 pathEvent,
42 readError,
43 setHelp;
44 $scope.links = window.links;
45 $scope.menu = $htmlParams.menu;
46 $scope.menulinks = window.menulinks;
47 $scope.staticPrefix = window.staticPrefix;
48 $scope.formPrefix = window.formPrefix;
49 $scope.availableLanguages = window.availableLanguages;
50 $scope.waiting = true;
51 $scope.showM = false;
52 $scope.showT = false;
53 $scope.form = 'homeViewer';
54 $scope.currentCfg = {};
55 $scope.clipboardAvailable = Boolean(navigator.clipboard);
56 $scope.viewPrefix = window.viewPrefix;
57 $scope.allowDiff = window.allowDiff;
58 $scope.message = {};
59 $scope.result = '';
60 // Import translations functions
61 $scope.translateTitle = function(node) {
62 return $translator.translateField(node,
63 'title');
64 };
65 $scope.translateP = $translator.translateP;
66 $scope.translate = $translator.translate;
67 // HELP DISPLAY
68 $scope.helpUrl = 'start.html#configuration';
69 $scope.setShowHelp = function(val) {
70 var d;
71 if (val == null) {
72 val = !$scope.showH;
73 }
74 $scope.showH = val;
75 d = new Date(Date.now());
76 d.setFullYear(d.getFullYear() + 1);
77 return $cookies.put('showhelp',
78 (val ? 'true' : 'false'), {
79 "expires": d
80 });
81 };
82 $scope.showH = $cookies.get('showhelp') === 'false' ? false : true;
83 if ($scope.showH == null) {
84 $scope.setShowHelp(true);
85 }
86 // INTERCEPT AJAX ERRORS
87 readError = function(response) {
88 var e,
89 j;
90 e = response.status;
91 j = response.statusLine;
92 $scope.waiting = false;
93 if (e === 403) {
94 $scope.message = {
95 title: 'forbidden',
96 message: '',
97 items: []
98 };
99 } else if (e === 401) {
100 console.debug('Authentication needed');
101 $scope.message = {
102 title: 'authenticationNeeded',
103 message: '__waitOrF5__',
104 items: []
105 };
106 } else if (e === 400) {
107 $scope.message = {
108 title: 'badRequest',
109 message: j,
110 items: []
111 };
112 } else if (e > 0) {
113 $scope.message = {
114 title: 'badRequest',
115 message: j,
116 items: []
117 };
118 } else {
119 $scope.message = {
120 title: 'networkProblem',
121 message: '',
122 items: []
123 };
124 }
125 return $scope.showModal('message.html');
126 };
127 // Modal launcher
128 $scope.showModal = function(tpl,
129 init) {
130 var d,
131 modalInstance;
132 modalInstance = $uibModal.open({
133 templateUrl: tpl,
134 controller: 'ModalInstanceCtrl',
135 size: 'lg',
136 resolve: {
137 elem: function() {
138 return function(s) {
139 return $scope[s];
140 };
141 },
142 set: function() {
143 return function(f,
144 s) {
145 return $scope[f] = s;
146 };
147 },
148 init: function() {
149 return init;
150 }
151 }
152 });
153 d = $q.defer();
154 modalInstance.result.then(function(msgok) {
155 $scope.message = {
156 title: '',
157 message: '',
158 items: []
159 };
160 return d.resolve(msgok);
161 },
162 function(msgnok) {
163 $scope.message = {
164 title: '',
165 message: '',
166 items: []
167 };
168 return d.reject(msgnok);
169 });
170 return d.promise;
171 };
172 // FORM DISPLAY FUNCTIONS
173
174 // Function called when a menu item is selected. It launch function stored in
175 // "action" or "title"
176 $scope.menuClick = function(button) {
177 if (button.popup) {
178 window.open(button.popup);
179 } else {
180 if (!button.action) {
181 button.action = button.title;
182 }
183 switch (typeof button.action) {
184 case 'function':
185 button.action($scope.currentNode,
186 $scope);
187 break;
188 case 'string':
189 $scope[button.action]();
190 break;
191 default:
192 console.warn('Unknown action type', typeof button.action);
193 }
194 }
195 return $scope.showM = false;
196 };
197 // Display main form
198 $scope.home = function() {
199 $scope.form = 'homeViewer';
200 return $scope.showM = false;
201 };
202 // Download raw conf
203 $scope.downloadConf = function() {
204 return window.open($scope.viewPrefix + $scope.currentCfg.cfgNum + '?full=1');
205 };
206 // NODES MANAGEMENT
207 id = 1;
208 $scope._findContainer = function() {
209 return $scope._findScopeContainer().$modelValue;
210 };
211 $scope._findScopeContainer = function() {
212 var cs;
213 cs = $scope.currentScope;
214 while (!cs.$modelValue.type.match(/Container$/)) {
215 cs = cs.$parentNodeScope;
216 }
217 return cs;
218 };
219 $scope._findScopeByKey = function(k) {
220 var cs;
221 cs = $scope.currentScope;
222 while (!(cs.$modelValue.title === k)) {
223 cs = cs.$parentNodeScope;
224 }
225 return cs;
226 };
227 $scope.down = function() {
228 var i,
229 ind,
230 l,
231 len,
232 n,
233 p,
234 ref,
235 tmp;
236 id = $scope.currentNode.id;
237 p = $scope.currentScope.$parentNodeScope.$modelValue;
238 ind = p.nodes.length;
239 ref = p.nodes;
240 for (i = l = 0, len = ref.length; l < len; i = ++l) {
241 n = ref[i];
242 if (n.id === id) {
243 ind = i;
244 }
245 }
246 if (ind < p.nodes.length - 1) {
247 tmp = p.nodes[ind];
248 p.nodes[ind] = p.nodes[ind + 1];
249 p.nodes[ind + 1] = tmp;
250 }
251 return ind;
252 };
253 $scope.up = function() {
254 var i,
255 ind,
256 l,
257 len,
258 n,
259 p,
260 ref,
261 tmp;
262 id = $scope.currentNode.id;
263 p = $scope.currentScope.$parentNodeScope.$modelValue;
264 ind = -1;
265 ref = p.nodes;
266 for (i = l = 0, len = ref.length; l < len; i = ++l) {
267 n = ref[i];
268 if (n.id === id) {
269 ind = i;
270 }
271 }
272 if (ind > 0) {
273 tmp = p.nodes[ind];
274 p.nodes[ind] = p.nodes[ind - 1];
275 p.nodes[ind - 1] = tmp;
276 }
277 return ind;
278 };
279 // test if value is in select
280 $scope.inSelect = function(value) {
281 var l,
282 len,
283 n,
284 ref;
285 ref = $scope.currentNode.select;
286 for (l = 0, len = ref.length; l < len; l++) {
287 n = ref[l];
288 if (n.k === value) {
289 return true;
290 }
291 }
292 return false;
293 };
294 // This is for rule form: title = comment if defined, else title = re
295 $scope.changeRuleTitle = function(node) {
296 return node.title = node.comment.length > 0 ? node.comment : node.re;
297 };
298 // Node opening
299
300 // authParams mechanism: show used auth modules only (launched by stoggle)
301 $scope.filters = {};
302 $scope.execFilters = function(scope) {
303 var filter,
304 func,
305 ref;
306 scope = scope ? scope : $scope;
307 ref = $scope.filters;
308 for (filter in ref) {
309 func = ref[filter];
310 if ($scope.filters.hasOwnProperty(filter)) {
311 return window.filterFunctions[filter](scope,
312 $q,
313 func);
314 }
315 }
316 return false;
317 };
318 // To avoid binding all the tree, nodes are pushed in DOM only when opened
319 $scope.stoggle = function(scope) {
320 var node;
321 node = scope.$modelValue;
322 _stoggle(node);
323 return scope.toggle();
324 };
325 _stoggle = function(node) {
326 var a,
327 l,
328 len,
329 len1,
330 len2,
331 m,
332 n,
333 o,
334 ref,
335 ref1,
336 ref2;
337 ref = ['nodes', 'nodes_cond'];
338 for (l = 0, len = ref.length; l < len; l++) {
339 n = ref[l];
340 if (node[`_${n}`]) {
341 node[n] = [];
342 ref1 = node[`_${n}`];
343 for (m = 0, len1 = ref1.length; m < len1; m++) {
344 a = ref1[m];
345 node[n].push(a);
346 }
347 delete node[`_${n}`];
348 }
349 }
350 // Call execFilter for authParams
351 if (node._nodes_filter) {
352 if (node.nodes) {
353 ref2 = node.nodes;
354 for (o = 0, len2 = ref2.length; o < len2; o++) {
355 n = ref2[o];
356 n.onChange = $scope.execFilters;
357 }
358 }
359 $scope.filters[node._nodes_filter] = node;
360 return $scope.execFilters();
361 }
362 };
363 // Simple toggle management
364 $scope.toggle = function(scope) {
365 return scope.toggle();
366 };
367 // cnodes management: hash keys/values are loaded when parent node is opened
368 $scope.download = function(scope) {
369 var node;
370 node = scope.$modelValue;
371 return _download(node);
372 };
373 _download = function(node) {
374 var d,
375 uri;
376 d = $q.defer();
377 d.notify('Trying to get datas');
378 $scope.waiting = true;
379 console.debug(`Trying to get key ${node.cnodes}`);
380 uri = encodeURI(node.cnodes);
381 $http.get(`${window.viewPrefix}${$scope.currentCfg.cfgNum}/${uri}`).then(function(response) {
382 var a,
383 data,
384 l,
385 len;
386 data = response.data;
387 // Manage datas errors
388 if (!data) {
389 d.reject('Empty response from server');
390 } else if (data.error) {
391 if (data.error.match(/setDefault$/)) {
392 if (node['default']) {
393 node.nodes = node['default'].slice(0);
394 } else {
395 node.nodes = [];
396 }
397 delete node.cnodes;
398 d.resolve('Set data to default value');
399 } else {
400 d.reject(`Server return an error: ${data.error}`);
401 }
402 } else {
403 // Store datas
404 delete node.cnodes;
405 if (!node.type) {
406 node.type = 'keyTextContainer';
407 }
408 node.nodes = [];
409 // TODO: try/catch
410 for (l = 0, len = data.length; l < len; l++) {
411 a = data[l];
412 if (a.template) {
413 a._nodes = templates(a.template,
414 a.title);
415 }
416 node.nodes.push(a);
417 }
418 d.resolve('OK');
419 }
420 return $scope.waiting = false;
421 },
422 function(response) {
423 readError(response);
424 return d.reject('');
425 });
426 return d.promise;
427 };
428 $scope.openCnode = function(scope) {
429 return $scope.download(scope).then(function() {
430 return scope.toggle();
431 });
432 };
433 $scope.copyPath = function() {
434 var text = $scope.breadCrumb.join(" ยป ");
435 navigator.clipboard.writeText(text).then(function () {
436 $scope.$apply(function () {
437 $scope.copySuccess = true;
438 $timeout(function() {
439 $scope.copySuccess = false;
440 }, 400);
441 });
442 });
443 };
444 setHelp = function(scope) {
445 while (!scope.$modelValue.help && scope.$parentNodeScope) {
446 scope = scope.$parentNodeScope;
447 }
448 return $scope.helpUrl = scope.$modelValue.help || 'start.html#configuration';
449 };
450 // Form management
451
452 // `currentNode` contains the last select node
453
454 // method `diplayForm()`:
455 // - set the `form` property to the name of the form to download
456 // (`text` by default or `home` for node without `type` property)
457 // - launch getKeys to set `node.data`
458 // - hide tree when in XS size
459
460 $scope.getTrPath = function(scope) {
461 var path = [];
462 var trpath = [];
463 var current = scope;
464 var safetycount = 0;
465 while (current && current.$modelValue && safetycount < 100) {
466 safetycount = safetycount + 1;
467 if (current.$modelValue.title && current.$modelValue.title != path[0]) {
468 trpath.unshift(scope.translate(current.$modelValue.title));
469 path.unshift(current.$modelValue.title);
470 }
471 current = current.$parent;
472 }
473 return trpath;
474 };
475
476 $scope.displayForm = function(scope) {
477 var f,
478 l,
479 len,
480 n,
481 node,
482 ref;
483 node = scope.$modelValue;
484 if (node.cnodes) {
485 $scope.download(scope);
486 }
487 if (node._nodes) {
488 $scope.stoggle(scope);
489 }
490 $scope.currentNode = node;
491 $scope.currentScope = scope;
492 f = node.type ? node.type : 'text';
493 if (node.nodes || node._nodes || node.cnodes) {
494 $scope.form = f !== 'text' ? f : 'mini';
495 } else {
496 $scope.form = f;
497 // Get datas
498 $scope.getKey(node);
499 }
500 if (node.type && node.type === 'simpleInputContainer') {
501 ref = node.nodes;
502 for (l = 0, len = ref.length; l < len; l++) {
503 n = ref[l];
504 $scope.getKey(n);
505 }
506 }
507 $scope.showT = false;
508 $scope.breadCrumb = $scope.getTrPath(scope);
509 return setHelp(scope);
510 };
511 $scope.keyTranslatable = function(scope) {
512 var node;
513 node = scope.$modelValue;
514 // Regexp::Assemble->new->add(qw(
515 // authChoice
516 // casAppMetaDataNode
517 // casSrvMetaDataNode
518 // cmbModule
519 // keyNode
520 // keyText
521 // menuApp
522 // menuCat
523 // rule
524 // oidcAttribute
525 // oidcOPMetaDataNode
526 // oidcRPMetaDataNode
527 // samlAttribute
528 // samlIDPMetaDataNode
529 // samlSPMetaDataNode
530 // sfExtra
531 // virtualHost
532 // ))->anchor_line->as_string
533 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)$/)) {
534 return true;
535 } else {
536 return false;
537 }
538 };
539 // method `getKey()`:
540 // - return a promise with the data:
541 // - from node when set
542 // - after downloading else
543
544 $scope.getKey = function(node) {
545 var d,
546 i,
547 l,
548 len,
549 n,
550 ref,
551 tmp,
552 uri;
553 d = $q.defer();
554 if (!node.data) {
555 $scope.waiting = true;
556 if (node.get && typeof node.get === 'object') {
557 node.data = [];
558 tmp = [];
559 ref = node.get;
560 for (i = l = 0, len = ref.length; l < len; i = ++l) {
561 n = ref[i];
562 node.data[i] = {
563 title: n.split("/").pop(),
564 get: n,
565 id: n
566 };
567 tmp.push($scope.getKey(node.data[i]));
568 }
569 $q.all(tmp).then(function() {
570 return d.resolve(node.data);
571 },
572 function(response) {
573 d.reject(response.statusLine);
574 return $scope.waiting = false;
575 });
576 } else {
577 uri = '';
578 if (node.get) {
579 console.debug(`Trying to get key ${node.get}`);
580 uri = encodeURI(node.get);
581 } else {
582 console.debug(`Trying to get title ${node.title}`);
583 }
584 $http.get(`${window.viewPrefix}${$scope.currentCfg.cfgNum}/${node.get ? uri : node.title}`).then(function(response) {
585 var data;
586 // Set default value if response is null or if asked by server
587 data = response.data;
588 if ((data.value === null || (data.error && data.error.match(/setDefault$/))) && node['default'] !== null) {
589 node.data = node['default'];
590 } else {
591 node.data = data.value;
592 }
593 if (node.data && node.data.toString().match(/_Hidden_$/)) {
594 node.type = 'text';
595 node.data = '######';
596 }
597 // Cast int as int (remember that booleans are int for Perl)
598 if (node.type && node.type.match(/^(bool|trool|boolOrExpr)$/)) {
599 if (typeof node.data === 'string' && node.data.match(/^(?:-1|0|1)$/)) {
600 node.data = parseInt(node.data,
601 10);
602 }
603 }
604 if (node.type && node.type.match(/^int$/)) {
605 node.data = parseInt(node.data,
606 10);
607 // Split SAML types
608 } else if (node.type && node.type.match(/^(saml(Service|Assertion)|blackWhiteList)$/) && !(typeof node.data === 'object')) {
609 node.data = node.data.split(';');
610 }
611 $scope.waiting = false;
612 return d.resolve(node.data);
613 },
614 function(response) {
615 readError(response);
616 return d.reject(response.status);
617 });
618 }
619 } else {
620 if (node.data.toString().match(/_Hidden_$/)) {
621 node.type = 'text';
622 node.data = '######';
623 }
624 d.resolve(node.data);
625 }
626 return d.promise;
627 };
628 // function `pathEvent(event, next; current)`:
629 // Called when $location.path() change, launch getCfg() with the new
630 // configuration number
631 pathEvent = function(event,
632 next,
633 current) {
634 var n;
635 n = next.match(new RegExp('#!?/view/(latest|[0-9]+)'));
636 if (n === null) {
637 return $location.path('/view/latest');
638 } else {
639 console.debug(`Trying to get cfg number ${n[1]}`);
640 return $scope.getCfg(n[1]);
641 }
642 };
643 $scope.$on('$locationChangeSuccess',
644 pathEvent);
645 // function `getCfg(n)`:
646 // Download configuration metadatas
647 $scope.getCfg = function(n) {
648 if ($scope.currentCfg.cfgNum !== n) {
649 return $http.get(`${window.viewPrefix}${n}`).then(function(response) {
650 var d;
651 $scope.currentCfg = response.data;
652 d = new Date($scope.currentCfg.cfgDate * 1000);
653 $scope.currentCfg.date = d.toLocaleString();
654 console.debug(`Metadatas of cfg ${n} loaded`);
655 $location.path(`/view/${n}`);
656 return $scope.init();
657 },
658 function(response) {
659 return readError(response).then(function() {
660 $scope.currentCfg.cfgNum = 0;
661 return $scope.init();
662 });
663 });
664 } else {
665 return $scope.waiting = false;
666 }
667 };
668 // method `getLanguage(lang)`
669 // Launch init() after setting current language
670 $scope.getLanguage = function(lang) {
671 $scope.lang = lang;
672 // Force reload home
673 $scope.form = 'white';
674 $scope.init();
675 return $scope.showM = false;
676 };
677 // Initialization
678
679 // Load JSON files:
680 // - struct.json: the main tree
681 // - languages/<lang>.json: the chosen language datas
682 $scope.init = function() {
683 var tmp;
684 tmp = null;
685 $scope.waiting = true;
686 $scope.breadCrumb = null;
687 $scope.data = [];
688 $scope.confirmNeeded = false;
689 $scope.forceSave = false;
690 $q.all([
691 $translator.init($scope.lang),
692 $http.get(`${window.staticPrefix}struct.json`).then(function(response) {
693 tmp = response.data;
694 console.debug("Structure loaded");
695 })
696 ]).then(function() {
697 console.debug("Starting structure binding");
698 $scope.data = tmp;
699 tmp = null;
700 if ($scope.currentCfg.cfgNum !== 0) {
701 setScopeVars($scope);
702 } else {
703 $scope.message = {
704 title: 'emptyConf',
705 message: '__zeroConfExplanations__'
706 };
707 $scope.showModal('message.html');
708 }
709 $scope.form = 'homeViewer';
710 return $scope.waiting = false;
711 },
712 readError);
713 // Colorized link
714 $scope.activeModule = "viewer";
715 return $scope.myStyle = {
716 color: '#ffb84d'
717 };
718 };
719 c = $location.path().match(new RegExp('^/view/(latest|[0-9]+)'));
720 if (!c) {
721 console.debug("Redirecting to /view/latest");
722 return $location.path('/view/latest');
723 }
724 }
725 ]);