!function(){"use strict";angular.module("llngManager",["ui.tree","ui.bootstrap","llApp","ngCookies"]).controller("TreeCtrl",["$scope","$http","$location","$q","$uibModal","$translator","$cookies","$htmlParams","$timeout",function(f,s,a,l,r,n,o,e,t){var i,d,u,c,p,m,g,w;return f.links=window.links,f.menu=e.menu,f.menulinks=window.menulinks,f.staticPrefix=window.staticPrefix,f.formPrefix=window.formPrefix,f.availableLanguages=window.availableLanguages,f.waiting=!0,f.showM=!1,f.showT=!1,f.form="home",f.currentCfg={},f.clipboardAvailable=Boolean(navigator.clipboard),f.confPrefix=window.confPrefix,f.message={},f.result="",f.translateTitle=function(e){return n.translateField(e,"title")},f.translateP=n.translateP,f.translate=n.translate,f.helpUrl="start.html#configuration",f.setShowHelp=function(e){var t;return null==e&&(e=!f.showH),f.showH=e,(t=new Date(Date.now())).setFullYear(t.getFullYear()+1),o.put("showhelp",e?"true":"false",{expires:t})},f.showH="false"!==o.get("showhelp"),null==f.showH&&f.setShowHelp(!0),m=function(e){var t=e.status,e=e.statusLine;return f.waiting=!1,403===t?f.message={title:"forbidden",message:"",items:[]}:401===t?(console.debug("Authentication needed"),f.message={title:"authenticationNeeded",message:"__waitOrF5__",items:[]}):f.message=400===t||0<t?{title:"badRequest",message:e,items:[]}:{title:"networkProblem",message:"",items:[]},f.showModal("message.html")},f.showModal=function(e,t){var e=r.open({templateUrl:e,controller:"ModalInstanceCtrl",size:"lg",resolve:{elem:function(){return function(e){return f[e]}},set:function(){return function(e,t){return f[e]=t}},init:function(){return t}}}),n=l.defer();return e.result.then(function(e){return f.message={title:"",message:"",items:[]},n.resolve(e)},function(e){return f.message={title:"",message:"",items:[]},n.reject(e)}),n.promise},f.menuClick=function(e){if(e.popup)window.open(e.popup);else switch(e.action||(e.action=e.title),typeof e.action){case"function":e.action(f.currentNode,f);break;case"string":f[e.action]();break;default:console.warn("Unknown action type",typeof e.action)}return f.showM=!1},f.home=function(){return f.form="home",f.showM=!1},i=function(e){if(f.message={title:"",message:"",items:[],itemsE:[],itemsNC:[],itemsW:[]},e.needConfirm&&(f.confirmNeeded=!0),e.message&&(f.message.message=e.message),e.details){for(var t in e.details)"__changes__"!==t&&("__needConfirmation__"===t?(f.message.itemsNC.push({message:t,items:e.details[t]}),console.debug("NeedConfirmation:",f.message.itemsNC)):"__warnings__"===t?(f.message.itemsW.push({message:t,items:e.details[t]}),console.debug("Warnings:",f.message.itemsW)):(f.message.itemsE.push({message:t,items:e.details[t]}),console.debug("Errors:",f.message.itemsE)));f.message.items=f.message.itemsE.concat(f.message.itemsNC.concat(f.message.itemsW))}return f.waiting=!1,1===e.result?(a.path("/confs/"),f.message.title="successfullySaved"):f.message.title="saveReport",f.showModal("message.html")},f.downloadConf=function(){return window.open(f.confPrefix+f.currentCfg.cfgNum+"?full=1")},f.downloadOidcMetadata=function(){return window.open(f.confPrefix+f.currentCfg.cfgNum+"?oidcMetadata=1")},f.downloadSamlMetadata=function(){return window.open(f.confPrefix+f.currentCfg.cfgNum+"?samlMetadata=1")},f.save=function(){return f.showModal("save.html").then(function(){return f.waiting=!0,f.data.push({id:"cfgLog",title:"cfgLog",data:f.result||""}),s.post(`${window.confPrefix}?cfgNum=${f.currentCfg.cfgNum}&cfgDate=`+f.currentCfg.cfgDate+(f.forceSave?"&force=1":""),f.data).then(function(e){return f.data.pop(),i(e.data)},function(e){return m(e),f.data.pop()})},function(){console.debug("Saving canceled")}),f.showM=!1},f.saveRawConf=function(e){return f.waiting=!0,s.post(window.confPrefix+"/raw",e).then(function(e){return i(e.data)},m)},f.restore=function(){return f.currentNode=null,f.form="restore"},f.cancel=function(){return f.currentNode.data=null,f.getKey(f.currentNode)},p=1,f._findContainer=function(){return f._findScopeContainer().$modelValue},f._findScopeContainer=function(){for(var e=f.currentScope;!e.$modelValue.type.match(/Container$/);)e=e.$parentNodeScope;return e},f._findScopeByKey=function(e){for(var t=f.currentScope;t.$modelValue.title!==e;)t=t.$parentNodeScope;return t},f.newGrantRule=function(){var e=f._findContainer();return e.nodes.length,e.nodes.push({id:e.id+"/n"+p++,title:"New rule",re:"Message",comment:"New rule",data:"1",type:"grant"})},f.newRule=function(){var e=f._findContainer(),t=e.nodes.length;return e.nodes.splice(0<t?t-1:0,0,{id:e.id+"/n"+p++,title:"New rule",re:"^/new",comment:"New rule",data:"accept",type:"rule"})},f.newPost=function(){var e=f._findContainer();return e.nodes.push({id:e.id+"/n"+p++,title:"/absolute/path/to/form",data:{},type:"post"})},f.newPostVar=function(){return null==f.currentNode.data.vars&&(f.currentNode.data.vars=[]),f.currentNode.data.vars.push(["var1","$uid"])},f.newAuthChoice=function(){var e=f._findContainer();return e.nodes.push({id:e.id+"/n"+p++,title:"1_Key",data:["Null","Null","Null"],type:"authChoice"}),f.execFilters(f._findScopeByKey("authParams"))},f.newHashEntry=function(){var e=f._findContainer();return e.nodes.push({id:e.id+"/n"+p++,title:"new",data:"",type:"keyText"})},f.newCat=function(){var e=f.currentScope;return(e="menuApp"===e.$modelValue.type?e.$parentNodeScope:e).$modelValue.nodes.push({id:e.$modelValue.id+"/n"+p++,title:"New category",type:"menuCat",nodes:[]})},f.newApp=function(){var e=f.currentScope;return(e="menuApp"===e.$modelValue.type?e.$parentNodeScope:e).$modelValue.nodes.push({id:e.$modelValue.id+"/n"+p++,title:"New application",type:"menuApp",data:{description:"New app description",uri:"https://test.example.com/",tooltip:"New app tooltip",logo:"network.png",display:"auto"}})},f.newCmbMod=function(){var e=f._findContainer();return e.nodes.push({id:e.id+"/n"+p++,title:"new",type:"cmbModule",data:{type:"LDAP",for:"0",over:[]}}),f.execFilters(f._findScopeByKey("authParams"))},f.newSfExtra=function(){var e=f._findContainer();return e.nodes.push({id:e.id+"/n"+p++,title:"new",type:"sfExtra",data:{type:"",rule:"",logo:"",level:"",label:"",over:[]}})},f.newSfOver=function(){var e=f.currentNode.data;return e.over||(e.over=[]),e.over.push(["new"+p++,""])},f.newCmbOver=function(){var e=f.currentNode.data;return e.over||(e.over=[]),e.over.push(["new"+p++,""])},f.newChoiceOver=function(){var e=f.currentNode.data;return console.debug("data",e),e[5]||(e[5]=[]),e[5].push(["new"+p++,""])},f.addHost=function(){var e=f.currentNode;return e.data||(e.data=[]),e.data.push({k:"newHost",h:[{k:"key",v:"uid"}]})},f.addSamlAttribute=function(){var e=f._findContainer();return e.nodes.push({id:e.id+"/n"+p++,title:"new",type:"samlAttribute",data:["0","New","",""]})},f.addOidcAttribute=function(){var e=f._findContainer();return e.nodes.push({id:e.id+"/n"+p++,title:"new",type:"oidcAttribute",data:["","string","auto"]})},f.addVhost=function(){var e=f.domain?"."+f.domain.data:".example.com";return f.message={title:"virtualHostName",field:"hostname"},f.showModal("prompt.html",e).then(function(){var e=f.result;if(e)return f.addTemplateNode(e,"virtualHost")})},f.duplicateVhost=function(){var e=f.domain?"."+f.domain.data:".example.com";return f.message={title:"virtualHostName",field:"hostname"},f.showModal("prompt.html",e).then(function(){var e=f.result;return f.duplicateNode(e,"virtualHost",f.currentNode.title)})},f.addSamlIDP=function(){return f.newTemplateNode("samlIDPMetaDataNode","samlPartnerName","idp-example")},f.addSamlSP=function(){return f.newTemplateNode("samlSPMetaDataNode","samlPartnerName","sp-example")},f.addOidcOp=function(){return f.newTemplateNode("oidcOPMetaDataNode","oidcOPName","op-example")},f.addOidcRp=function(){return f.newTemplateNode("oidcRPMetaDataNode","oidcRPName","rp-example")},f.addCasSrv=function(){return f.newTemplateNode("casSrvMetaDataNode","casPartnerName","srv-example")},f.addCasApp=function(){return f.newTemplateNode("casAppMetaDataNode","casPartnerName","app-example")},f.addKey=function(){return f.newTemplateNode("keyNode","keyName","key-example")},f.newTemplateNode=function(t,e,n){return f.message={title:e,field:"name"},f.showModal("prompt.html",n).then(function(){var e=f.result;if(e)return f.addTemplateNode(e,t)})},f.addTemplateNode=function(e,t){for(var n=f.currentScope;n.$modelValue.title!==t+"s";)n=n.$parentNodeScope;return e={id:t+"s/new__"+e,title:e,type:t,nodes:templates(t,"new__"+e)},g(e.nodes),n.$modelValue.nodes.push(e),n.expand(),e},g=function(e){for(var t,n=0,a=e.length;n<a;n++)(t=e[n]).cnodes&&t.default&&(delete t.cnodes,t._nodes=t.default),t._nodes?g(t._nodes):!t.default&&0!==t.default||(t.data=t.default);return e},u=function(o){var e=l.defer(),i=l.defer();return o._nodes?(c(o),e.resolve()):o.cnodes?d(o).then(function(){return e.resolve()}):o.nodes||o.data?e.resolve():f.getKey(o).then(function(){return e.resolve()}),e.promise.then(function(){var e,t,n,a,r=[];if(o.nodes)for(n=0,e=(a=o.nodes).length;n<e;n++)t=a[n],r.push(u(t));return l.all(r).then(function(){return i.resolve()})}),i.promise},f.duplicateNode=function(t,n,a){var r=f.currentScope;return u(f.currentNode).then(function(){for(var e;r.$modelValue.title!==n+"s";)r=r.$parentNodeScope;return(e=JSON.parse(JSON.stringify(f.currentNode).replace(/[*]/g,"").replace(new RegExp(a,"g"),"new__"+t))).id=n+"s/new__"+t,e.title=t,r.$modelValue.nodes.push(e),e})},f.del=function(e,t){return e.splice(t,1)},f.deleteEntry=function(){var e=f.currentScope.$parentNodeScope;return f.currentScope.remove(),f.displayForm(e)},f.down=function(){for(var e,t,n=f.currentNode.id,a=f.currentScope.$parentNodeScope.$modelValue,r=a.nodes.length,o=a.nodes,i=e=0,d=o.length;e<d;i=++e)o[i].id===n&&(r=i);return r<a.nodes.length-1&&(t=a.nodes[r],a.nodes[r]=a.nodes[r+1],a.nodes[r+1]=t),r},f.up=function(){for(var e,t,n=f.currentNode.id,a=f.currentScope.$parentNodeScope.$modelValue,r=-1,o=a.nodes,i=e=0,d=o.length;e<d;i=++e)o[i].id===n&&(r=i);return 0<r&&(t=a.nodes[r],a.nodes[r]=a.nodes[r-1],a.nodes[r-1]=t),r},f.inSelect=function(e){for(var t=f.currentNode.select,n=0,a=t.length;n<a;n++)if(t[n].k===e)return!0;return!1},f.changeRuleTitle=function(e){return e.title=0<e.comment.length?e.comment:e.re},f.filters={},f.execFilters=function(e){var t,n,a;for(t in e=e||f,a=f.filters)if(n=a[t],f.filters.hasOwnProperty(t))return window.filterFunctions[t](e,l,n);return!1},f.stoggle=function(e){var t=e.$modelValue;return c(t),e.toggle()},c=function(e){for(var t,n,a,r,o,i,d,u,s=["nodes","nodes_cond"],l=0,c=s.length;l<c;l++)if(e["_"+(r=s[l])]){for(e[r]=[],o=0,n=(d=e["_"+r]).length;o<n;o++)t=d[o],e[r].push(t);delete e["_"+r]}if(e._nodes_filter){if(e.nodes)for(i=0,a=(u=e.nodes).length;i<a;i++)(r=u[i]).onChange=f.execFilters;return f.filters[e._nodes_filter]=e,f.execFilters()}},f.toggle=function(e){return e.toggle()},f.download=function(e){e=e.$modelValue;return d(e)},d=function(o){var e,i=l.defer();return i.notify("Trying to get datas"),f.waiting=!0,console.debug("Trying to get key "+o.cnodes),e=encodeURI(o.cnodes),s.get(""+window.confPrefix+f.currentCfg.cfgNum+"/"+e).then(function(e){var t,n,a,r=e.data;if(r)if(r.error)r.error.match(/setDefault$/)?(o.default?o.nodes=o.default.slice(0):o.nodes=[],delete o.cnodes,i.resolve("Set data to default value")):i.reject("Server return an error: "+r.error);else{for(delete o.cnodes,o.type||(o.type="keyTextContainer"),o.nodes=[],a=0,n=r.length;a<n;a++)(t=r[a]).template&&(t._nodes=templates(t.template,t.title)),o.nodes.push(t),t.type.match(/^rule$/)&&(console.debug("Parse rule AuthnLevel as integer"),t.level)&&"string"==typeof t.level&&(t.level=parseInt(t.level,10));i.resolve("OK")}else i.reject("Empty response from server");return f.waiting=!1},function(e){return m(e),i.reject("")}),i.promise},f.openCnode=function(e){return f.download(e).then(function(){return e.toggle()})},f.copyPath=function(){var e=f.breadCrumb.join(" ยป ");navigator.clipboard.writeText(e).then(function(){f.$apply(function(){f.copySuccess=!0,t(function(){f.copySuccess=!1},400)})})},w=function(e){for(;!e.$modelValue.help&&e.$parentNodeScope;)e=e.$parentNodeScope;return f.helpUrl=e.$modelValue.help||"start.html#configuration"},f.getTrPath=function(e){for(var t=[],n=[],a=e,r=0;a&&a.$modelValue&&r<100;)r+=1,a.$modelValue.title&&a.$modelValue.title!=t[0]&&(n.unshift(e.translate(a.$modelValue.title)),t.unshift(a.$modelValue.title)),a=a.$parent;return n},f.displayForm=function(e){var t,n,a,r,o,i=e.$modelValue;if(i.cnodes&&f.download(e),i._nodes&&f.stoggle(e),f.currentNode=i,f.currentScope=e,t=i.type||"text",i.nodes||i._nodes||i.cnodes?f.form="text"!==t?t:"mini":(f.form=t,f.getKey(i)),i.type&&"simpleInputContainer"===i.type)for(r=0,n=(o=i.nodes).length;r<n;r++)a=o[r],f.getKey(a);return f.showT=!1,f.breadCrumb=f.getTrPath(e),w(e)},f.keyWritable=function(e){e=e.$modelValue;return!(!e.type||!e.type.match(/^(?:s(?:aml(?:(?:ID|S)PMetaDataNod|Attribut)e|fExtra)|oidcAttribute|(?:(?:cmbMod|r)ul|authChoic)e|(?:virtualHos|keyTex)t|menu(?:App|Cat))$/))},f.sendTestMail=function(){return f.message={title:"sendTestMail",field:"dest"},f.showModal("prompt.html").then(function(){var e;return f.result,f.waiting=!0,e=f.result,s.post(window.confPrefix+"/sendTestMail",{dest:e}).then(function(e){var t=e.data.success,e=e.data.error;return f.waiting=!1,f.message=t?{title:"ok",message:"__sendTestMailSuccess__",items:[]}:{title:"error",message:e,items:[]},f.showModal("message.html")},m)},function(){console.error("Error sending test email")})},f.newCertificate=function(){return f.showModal("password.html").then(function(){var t,n;return f.waiting=!0,t=f.currentNode,n=f.result,s.post(window.confPrefix+"/newCertificate",{password:n}).then(function(e){return t.data[0].data=e.data.private,t.data[1].data=n,t.data[2].data=e.data.public,f.waiting=!1},m)},function(){console.debug("New key cancelled")})},f.newEcCertificate=function(){return f.showModal("password.html").then(function(){var t,n;return f.waiting=!0,t=f.currentNode,n=f.result,s.post(window.confPrefix+"/newEcCertificate",{password:n}).then(function(e){return t.data[0].data=e.data.private,t.data[1].data=n,t.data[2].data=e.data.public,f.waiting=!1},m)},function(){console.debug("New key cancelled")})},f.newEcKeys=function(){var a;return f.waiting=!0,a=f.currentNode,s.post(window.confPrefix+"/newEcKeys",{password:""}).then(function(e){for(var t,n=t=0;t<=3;n=++t)a.data[n+4].data=a.data[n].data;return a.data[0].data=e.data.private,a.data[1].data=e.data.public,a.data[2].data=e.data.hash,a.data[3].data="EC",f.waiting=!1},m)},f.newCertificateNoPassword=function(){var a;return f.waiting=!0,a=f.currentNode,s.post(window.confPrefix+"/newCertificate",{password:""}).then(function(e){for(var t,n=t=0;t<=3;n=++t)a.data[n+4].data=a.data[n].data;return a.data[0].data=e.data.private,a.data[1].data=e.data.public,a.data[2].data=e.data.hash,a.data[3].data="RSA",f.waiting=!1},m)},f.newRSAKey=function(){return f.showModal("password.html").then(function(){var t,n;return f.waiting=!0,t=f.currentNode,n=f.result,s.post(window.confPrefix+"/newRSAKey",{password:n}).then(function(e){return t.data[0].data=e.data.private,t.data[1].data=n,t.data[2].data=e.data.public,f.waiting=!1},m)},function(){console.debug("New key cancelled")})},f.getKey=function(t){var e,n,a,r,o,i,d,u=l.defer();if(t.data)u.resolve(t.data);else if(f.waiting=!0,t.get&&"object"==typeof t.get){for(t.data=[],i=[],e=r=0,n=(o=t.get).length;r<n;e=++r)a=o[e],t.data[e]={title:a.split("/").pop(),get:a,id:a},i.push(f.getKey(t.data[e]));l.all(i).then(function(){return u.resolve(t.data)},function(e){return u.reject(e.statusLine),f.waiting=!1})}else d="",t.get?(console.debug("Trying to get key "+t.get),d=encodeURI(t.get)):console.debug("Trying to get title "+t.title),s.get(""+window.confPrefix+f.currentCfg.cfgNum+"/"+(t.get?d:t.title)).then(function(e){e=e.data;return(null===e.value||e.error&&e.error.match(/setDefault$/))&&null!==t.default?t.data=t.default:t.data=e.value,t.type&&t.type.match(/^(bool|trool|boolOrExpr)$/)&&"string"==typeof t.data&&t.data.match(/^(?:-1|0|1)$/)&&(t.data=parseInt(t.data,10)),t.type&&t.type.match(/^int$/)&&(t.data=parseInt(t.data,10)),t.type&&t.type.match(/^select$/)?t.data=t.data?t.data.toString():"":!t.type||!t.type.match(/^(saml(Service|Assertion)|blackWhiteList)$/)||t.data&&"object"==typeof t.data||(t.data=t.data?t.data.split(";"):[]),f.waiting=!1,u.resolve(t.data)},function(e){return m(e),u.reject(e.status)});return u.promise},f.$on("$locationChangeSuccess",function(e,t,n){t=t.match(new RegExp("#!?/confs/(latest|[0-9]+)"));return null===t?a.path("/confs/latest"):(console.debug("Trying to get cfg number "+t[1]),f.getCfg(t[1]))}),f.getCfg=function(t){return f.currentCfg.cfgNum!==t?s.get(""+window.confPrefix+t).then(function(e){return f.currentCfg=e.data,e=new Date(1e3*f.currentCfg.cfgDate),f.currentCfg.date=e.toLocaleString(),console.debug(`Metadatas of cfg ${t} loaded`),a.path("/confs/"+t),f.init()},function(e){return m(e).then(function(){return f.currentCfg.cfgNum=0,f.init()})}):f.waiting=!1},f.getLanguage=function(e){return f.lang=e,f.form="white",f.init(),f.showM=!1},f.init=function(){var t=null;return f.waiting=!0,f.breadCrumb=null,f.data=[],f.confirmNeeded=!1,f.forceSave=!1,l.all([n.init(f.lang),s.get(window.staticPrefix+"struct.json").then(function(e){t=e.data,console.debug("Structure loaded")})]).then(function(){return console.debug("Starting structure binding"),f.data=t,t=null,0!==f.currentCfg.cfgNum?setScopeVars(f):(f.message={title:"emptyConf",message:"__zeroConfExplanations__"},f.showModal("message.html")),f.form="home",f.waiting=!1},m),f.activeModule="conf",f.myStyle={color:"#ffb84d"}},a.path().match(new RegExp("^/confs/(latest|[0-9]+)"))||(console.debug("Redirecting to /confs/latest"),a.path("/confs/latest")),f.replaceContentByUrl=function(t,e){return f.waiting=!0,s.post(window.scriptname+"prx",{url:e}).then(function(e){return t.data=e.data.content,f.waiting=!1},m)},f.replaceContent=function(e,t){return e.data=t},f.saveAs=function(e,t,n){return saveAs(new Blob([e],{type:t}),n)},f.saveAsPem=function(e,t){return t.saveAs(e.data[0].data+`
`+e.data[2].data,"application/x-pem-file",e.title+".pem")},f.saveAsText=function(e,t){return t.saveAs(e.data,"text/plain",e.title+".txt")}}])}();