configmanager.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Password Management Servlets (PWM)
  3. * http://code.google.com/p/pwm/
  4. *
  5. * Copyright (c) 2006-2009 Novell, Inc.
  6. * Copyright (c) 2009-2015 The PWM Project
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. "use strict";
  23. var PWM_CONFIG = PWM_CONFIG || {};
  24. var PWM_GLOBAL = PWM_GLOBAL || {};
  25. PWM_CONFIG.lockConfiguration=function() {
  26. PWM_MAIN.showConfirmDialog({text:PWM_CONFIG.showString('Confirm_LockConfig'),okAction:function(){
  27. PWM_MAIN.showWaitDialog({loadFunction:function() {
  28. var url = 'ConfigManager?processAction=lockConfiguration';
  29. var loadFunction = function(data) {
  30. if (data['error'] == true) {
  31. PWM_MAIN.closeWaitDialog();
  32. PWM_MAIN.showDialog({
  33. title: PWM_MAIN.showString('Title_Error'),
  34. text: data['errorDetail']
  35. });
  36. } else {
  37. PWM_CONFIG.waitForRestart();
  38. }
  39. };
  40. PWM_MAIN.ajaxRequest(url,loadFunction);
  41. }});
  42. }});
  43. };
  44. PWM_CONFIG.waitForRestart=function(options) {
  45. PWM_VAR['cancelHeartbeatCheck'] = true;
  46. options = options === undefined ? {} : options;
  47. console.log("beginning request to determine application status: ");
  48. var loadFunction = function(data) {
  49. try {
  50. var serverStartTime = data['data']['PWM_GLOBAL']['startupTime'];
  51. if (serverStartTime != PWM_GLOBAL['startupTime']) {
  52. console.log("application appears to be restarted, redirecting to context url: ");
  53. var redirectUrl = 'location' in options ? options['location'] : '/';
  54. PWM_MAIN.goto(redirectUrl);
  55. return;
  56. }
  57. } catch (e) {
  58. console.log("can't read current server startupTime, will retry detection (current error: " + e + ")");
  59. }
  60. setTimeout(function() {
  61. PWM_CONFIG.waitForRestart(options)
  62. }, Math.random() * 3000);
  63. };
  64. var errorFunction = function(error) {
  65. setTimeout(function() {
  66. PWM_CONFIG.waitForRestart(options)
  67. }, 3000);
  68. console.log('Waiting for server restart, unable to contact server: ' + error);
  69. };
  70. var url = PWM_GLOBAL['url-restservice'] + "/app-data/client?checkForRestart=true";
  71. PWM_MAIN.ajaxRequest(url,loadFunction,{errorFunction:errorFunction,method:'GET'});
  72. };
  73. PWM_CONFIG.startNewConfigurationEditor=function(template) {
  74. PWM_MAIN.showWaitDialog({title:'Loading...',loadFunction:function(){
  75. require(["dojo"],function(dojo){
  76. dojo.xhrGet({
  77. url:"ConfigManager?processAction=setOption&pwmFormID=" + PWM_GLOBAL['pwmFormID'] + "&getTemplate=" + template,
  78. preventCache: true,
  79. error: function(errorObj) {
  80. PWM_MAIN.showError("error starting configuration editor: " + errorObj);
  81. },
  82. load: function() {
  83. window.location = "ConfigManager?processAction=editMode&pwmFormID=" + PWM_GLOBAL['pwmFormID'] + '&mode=SETTINGS';
  84. }
  85. });
  86. });
  87. }});
  88. };
  89. PWM_CONFIG.startConfigurationEditor=function() {
  90. require(["dojo"],function(dojo){
  91. if(dojo.isIE <= 8){ // only IE8 and below
  92. alert('Internet Explorer 8 and below is not able to edit the configuration. Please use a newer version of Internet Explorer or a different browser.');
  93. document.forms['cancelEditing'].submit();
  94. } else {
  95. PWM_MAIN.goto('/private/config/ConfigEditor');
  96. }
  97. });
  98. };
  99. PWM_CONFIG.uploadConfigDialog=function() {
  100. var uploadOptions = {};
  101. uploadOptions['url'] = window.location.pathname + '?processAction=uploadConfig';
  102. uploadOptions['title'] = 'Upload Configuration';
  103. uploadOptions['nextFunction'] = function() {
  104. PWM_MAIN.showWaitDialog({title:'Save complete, restarting application...',loadFunction:function(){
  105. PWM_CONFIG.waitForRestart({location:'/'});
  106. }});
  107. };
  108. PWM_CONFIG.uploadFile(uploadOptions);
  109. };
  110. PWM_CONFIG.uploadLocalDB=function() {
  111. PWM_MAIN.showConfirmDialog({
  112. text:PWM_CONFIG.showString('Confirm_UploadLocalDB'),
  113. okAction:function(){
  114. var uploadOptions = {};
  115. uploadOptions['url'] = 'ConfigManager?processAction=importLocalDB';
  116. uploadOptions['title'] = 'Upload and Import LocalDB Archive';
  117. uploadOptions['nextFunction'] = function() {
  118. PWM_MAIN.showWaitDialog({title:'Save complete, restarting application...',loadFunction:function(){
  119. PWM_CONFIG.waitForRestart({location:'/'});
  120. }});
  121. };
  122. PWM_CONFIG.uploadFile(uploadOptions);
  123. }
  124. });
  125. };
  126. PWM_CONFIG.showString=function (key, options) {
  127. options = options === undefined ? {} : options;
  128. options['bundle'] = 'Config';
  129. return PWM_MAIN.showString(key,options);
  130. };
  131. PWM_CONFIG.openLogViewer=function(level) {
  132. var windowUrl = PWM_GLOBAL['url-context'] + '/private/admin/Administration?processAction=viewLogWindow' + ((level) ? '&level=' + level : '');
  133. var windowName = 'logViewer';
  134. PWM_MAIN.newWindowOpen(windowUrl,windowName);
  135. };
  136. PWM_CONFIG.showHeaderHealth = function() {
  137. var refreshUrl = PWM_GLOBAL['url-restservice'] + "/health";
  138. var parentDiv = PWM_MAIN.getObject('panel-header-healthData');
  139. if (!parentDiv) {
  140. return;
  141. }
  142. var headerDiv = PWM_MAIN.getObject('header-warning');
  143. if (parentDiv && headerDiv) {
  144. var loadFunction = function(data) {
  145. if (data['data'] && data['data']['records']) {
  146. var healthRecords = data['data']['records'];
  147. var hasWarnTopics = false;
  148. for (var i = 0; i < healthRecords.length; i++) {
  149. var healthData = healthRecords[i];
  150. if (healthData['status'] == 'WARN') {
  151. hasWarnTopics = true;
  152. }
  153. }
  154. if (hasWarnTopics) {
  155. PWM_MAIN.openHeaderWarningPanel();
  156. parentDiv.innerHTML = '<div id="panel-healthHeaderErrors" class="header-error"><span class="fa fa-warning"></span> ' + PWM_ADMIN.showString('Header_ConfigWarningsPresent') + '</div>';
  157. var tooltipBody = PWM_ADMIN.makeHealthHtml(data['data'],true,false);
  158. PWM_MAIN.showTooltip({
  159. position:'below',
  160. id:'panel-healthHeaderErrors',
  161. text:tooltipBody
  162. });
  163. }
  164. setTimeout(function () {
  165. PWM_CONFIG.showHeaderHealth()
  166. }, 60 * 1000);
  167. }
  168. };
  169. var errorFunction = function(error) {
  170. console.log('unable to read header health status: ' + error);
  171. };
  172. PWM_MAIN.ajaxRequest(refreshUrl, loadFunction,{errorFunction:errorFunction,method:'GET'});
  173. }
  174. };
  175. PWM_CONFIG.downloadLocalDB = function () {
  176. PWM_MAIN.showConfirmDialog({
  177. text:PWM_CONFIG.showString("Warning_DownloadLocal"),
  178. okAction:function(){
  179. PWM_MAIN.showWaitDialog({
  180. loadFunction:function(){
  181. PWM_MAIN.goto('ConfigManager?processAction=exportLocalDB',{addFormID:true,hideDialog:true});
  182. setTimeout(function(){PWM_MAIN.closeWaitDialog()},5000);
  183. }
  184. });
  185. }
  186. });
  187. };
  188. PWM_CONFIG.downloadConfig = function () {
  189. PWM_MAIN.showConfirmDialog({
  190. text:PWM_CONFIG.showString("Warning_DownloadConfiguration"),
  191. okAction:function(){
  192. PWM_MAIN.showWaitDialog({
  193. loadFunction:function(){
  194. PWM_MAIN.goto('ConfigManager?processAction=downloadConfig',{addFormID:true,hideDialog:true});
  195. setTimeout(function(){PWM_MAIN.closeWaitDialog()},5000);
  196. }
  197. });
  198. }
  199. });
  200. };
  201. PWM_CONFIG.downloadSupportBundle = function() {
  202. if (PWM_VAR['config_localDBLogLevel'] != 'TRACE') {
  203. PWM_MAIN.showDialog({title: PWM_MAIN.showString('Title_Error'), text: PWM_CONFIG.showString("Warning_MakeSupportZipNoTrace")});
  204. } else {
  205. PWM_MAIN.showConfirmDialog({
  206. text:PWM_CONFIG.showString("Warning_DownloadSupportZip"),
  207. okAction:function(){
  208. PWM_MAIN.showWaitDialog({
  209. loadFunction:function(){
  210. PWM_MAIN.goto('ConfigManager?processAction=generateSupportZip',{addFormID:true,hideDialog:true});
  211. setTimeout(function(){PWM_MAIN.closeWaitDialog()},5000);
  212. }
  213. });
  214. }
  215. });
  216. }
  217. };
  218. PWM_CONFIG.uploadFile = function(options) {
  219. options = options === undefined ? {} : options;
  220. var body = '<div id="uploadFormWrapper"><form action="UploadForm" enctype="multipart/form-data">';
  221. body += '<div id="fileList"></div>';
  222. body += '<input name="uploadFile" type="file" label="Select File" id="uploadFile"/>';
  223. body += '<input type="submit" id="uploadButton" name="Upload"/>';
  224. body += '<br/></form></div><br/><br/>';
  225. var currentUrl = window.location.pathname;
  226. var uploadUrl = 'url' in options ? options['url'] : currentUrl;
  227. var title = 'title' in options ? options['title'] : 'Upload File';
  228. uploadUrl = PWM_MAIN.addPwmFormIDtoURL(uploadUrl);
  229. var nextFunction = 'nextFunction' in options ? options['nextFunction'] : function(data){
  230. PWM_MAIN.showDialog({title: PWM_MAIN.showString("Title_Success"), text: data['successMessage'],okAction:function(){
  231. PWM_MAIN.goto(currentUrl)
  232. }});
  233. };
  234. var completeFunction = function(data){
  235. if (data['error'] == true) {
  236. var errorText = 'The file upload has failed. Please try again or check the server logs for error information.';
  237. PWM_MAIN.showErrorDialog(data,{text:errorText,okAction:function(){
  238. location.reload();
  239. }});
  240. } else {
  241. nextFunction(data);
  242. }
  243. };
  244. var errorFunction = function(data) {
  245. var errorText = 'The file upload has failed. Please try again or check the server logs for error information.';
  246. PWM_MAIN.showErrorDialog(data,{text:errorText});
  247. };
  248. completeFunction = 'completeFunction' in options ? options['completeFunction'] : completeFunction;
  249. require(["dojo","dijit/Dialog","dojox/form/Uploader","dojox/form/uploader/FileList","dijit/form/Button"],function(
  250. dojo,Dialog,Uploader,FileList,Button){
  251. if(dojo.isIE <= 9){ // IE9 and below no workie
  252. PWM_MAIN.showDialog({title:PWM_MAIN.showString("Title_Error"),text:PWM_CONFIG.showString("Warning_UploadIE9")});
  253. return;
  254. }
  255. PWM_MAIN.showWaitDialog({loadFunction:function() {
  256. console.log('uploading file to url ' + uploadUrl);
  257. PWM_MAIN.closeWaitDialog();
  258. var idName = 'dialogPopup';
  259. PWM_MAIN.clearDijitWidget(idName);
  260. var theDialog = new Dialog({
  261. id: idName,
  262. title: title,
  263. style: "width: 500px",
  264. content: body
  265. });
  266. theDialog.show();
  267. var uploader = new dojox.form.Uploader({
  268. multiple: false,
  269. name: "uploadFile",
  270. label: 'Select File',
  271. required: true,
  272. url: uploadUrl,
  273. isDebug: true,
  274. devMode: true,
  275. preventCache: true
  276. }, 'uploadFile');
  277. uploader.startup();
  278. var uploadButton = new Button({
  279. label: 'Upload',
  280. type: 'submit'
  281. }, "uploadButton");
  282. uploadButton.startup();
  283. new FileList({
  284. uploaderId: 'uploadFile'
  285. }, "fileList");
  286. dojo.connect(uploader, "onComplete", completeFunction);
  287. dojo.connect(uploader, "onError", errorFunction);
  288. dojo.connect(uploader, "onBegin", function () {
  289. PWM_MAIN.showWaitDialog({title:"Uploading..."});
  290. });
  291. dojo.connect(uploader, "onProgress", function (data) {
  292. var decimal = data['decimal'];
  293. require(["dijit/registry"],function(registry){
  294. var progressBar = registry.byId('progressBar');
  295. progressBar.set("maximum",100);
  296. progressBar.set("indeterminate",false);
  297. progressBar.set("value", decimal * 100);
  298. });
  299. });
  300. }});
  301. });
  302. };
  303. PWM_CONFIG.heartbeatCheck = function() {
  304. var heartbeatFrequency = 10 * 1000;
  305. if (PWM_VAR['cancelHeartbeatCheck']) {
  306. console.log('heartbeat check cancelled');
  307. return;
  308. }
  309. if (typeof document['hidden'] !== "undefined" && document['hidden']) {
  310. console.log('skipping heartbeat check because page is not currently visible');
  311. setTimeout(PWM_CONFIG.heartbeatCheck,heartbeatFrequency);
  312. return;
  313. }
  314. require(["dojo","dijit/Dialog"],function() {
  315. /* make sure dialog js is loaded, server may not be available to load lazy */
  316. });
  317. console.log('beginning config-editor heartbeat check');
  318. var handleErrorFunction = function(message) {
  319. console.log('config-editor heartbeat failed');
  320. PWM_MAIN.showErrorDialog('There has been a problem communicating with the application server, please refresh your browser to continue.<br/><br/>' + message,{
  321. showOk:false
  322. });
  323. };
  324. var loadFunction = function(data) {
  325. try {
  326. var serverStartTime = data['data']['PWM_GLOBAL']['startupTime'];
  327. if (serverStartTime != PWM_GLOBAL['startupTime']) {
  328. var message = "Application appears to have be restarted.";
  329. handleErrorFunction(message);
  330. } else {
  331. setTimeout(PWM_CONFIG.heartbeatCheck,heartbeatFrequency);
  332. }
  333. } catch (e) {
  334. handleErrorFunction('Error reading server status.');
  335. }
  336. };
  337. var errorFunction = function(e) {
  338. handleErrorFunction('I/O error communicating with server.');
  339. };
  340. var url = PWM_GLOBAL['url-restservice'] + "/app-data/client?heartbeat=true";
  341. PWM_MAIN.ajaxRequest(url,loadFunction,{errorFunction:errorFunction,method:'GET'});
  342. };
  343. PWM_CONFIG.initConfigHeader = function() {
  344. // header initialization
  345. if (PWM_MAIN.getObject('header_configManagerButton')) {
  346. PWM_MAIN.addEventHandler('header_configManagerButton', 'click', function () {
  347. PWM_MAIN.goto('/private/config/ConfigManager')
  348. });
  349. }
  350. if (PWM_MAIN.getObject('header_configEditorButton')) {
  351. PWM_MAIN.addEventHandler('header_configEditorButton', 'click', function () {
  352. PWM_CONFIG.startConfigurationEditor()
  353. });
  354. }
  355. PWM_MAIN.addEventHandler('header_openLogViewerButton', 'click', function () {
  356. PWM_CONFIG.openLogViewer(null)
  357. });
  358. PWM_MAIN.addEventHandler('panel-header-healthData','click',function(){
  359. PWM_MAIN.goto('/private/config/ConfigManager');
  360. });
  361. PWM_MAIN.addEventHandler('button-closeHeader','click',function(){
  362. PWM_MAIN.closeHeaderWarningPanel();
  363. });
  364. PWM_MAIN.addEventHandler('button-openHeader','click',function(){
  365. PWM_MAIN.openHeaderWarningPanel();
  366. });
  367. PWM_CONFIG.showHeaderHealth();
  368. console.log('initConfigHeader completed');
  369. };