PageSourceSearch

https://weatherfile.com/js/wf/user_admin.js

js weatherfile.com collected 2026-10-02 11:22:18 UTC 13,091 bytes, 465 lines download raw bytes

1/* User administration */
2
3function _t(key, vars) {
4  return (window.WF && WF.i18n && typeof WF.i18n.t === "function") ? WF.i18n.t(key, vars) : key;
5}
6
7
8function showResetPwd() {
9  $("#resetpwd_error_message").html("");
10  showDiv("pwd_reset_container");
11}
12
13function doResetPwd() {
14  var uname = ($("#reset_pwd_user_name").val() || "").trim();
15  $("#resetpwd_error_message").html("");
16
17  if (!uname) {
18    $("#resetpwd_error_message").html("User name must be provided.");
19    return;
20  }
21
22  var formData = {};
23  formData['captcha_result'] = grecaptcha.getResponse(resetPwdWidget);
24  var targeturl = server_api_url + "/" + server_api_version + "/admin/user/" + encodeURIComponent(uname) + "/resetpwd";
25
26  $.ajax({
27    type: 'POST',
28    url: targeturl,
29    data: formData,
30    headers: {
31      "wf-tkn": wf_user_token
32    }
33  }).done(function (data) {
34    var jsonpayload;
35    try {
36      jsonpayload = typeof data === "string" ? JSON.parse(data) : data;
37    } catch (e) {
38      $("#resetpwd_error_message").html("Invalid response from server.");
39      return;
40    }
41
42    if (jsonpayload.status === "ok") {
43      $("#uid").val("");
44      $("#pwd").val("");
45      showUserAlert("Please check your email.\nYou will receive an email containing a temporary password, to reactivate your log in");
46      showDiv("login_container");
47      return;
48    }
49
50    var msg = jsonpayload.message || "Password reset failed.";
51    $("#resetpwd_error_message").html(msg);
52  }).fail(function (xhr) {
53    var msg =
54      (xhr.responseJSON && xhr.responseJSON.message) ||
55      xhr.statusText ||
56      _t('common.error');
57    $("#resetpwd_error_message").html(msg);
58  });
59}
60
61function showChangePwd() {
62  showChangePassword();
63}
64
65function cancelLogin() {
66  renderCanvas("/");
67}
68
69function doRegister() {
70  var msg = _t("auth.register_invite");
71  showUserAlert(String(msg).replace(/\n/g, "<br>"));
72}
73
74function logInOut() {
75  if (wf_user_token == "PUBLIC") {
76    $("#uid").val("");
77    $("#pwd").val("");
78    showDiv("login_container");
79  } else {
80    // If we have called this from history do not log us out
81    //doLogout();
82  }
83}
84
85function doLogout() {
86  clearLogin()
87  // clear details, then load with defaults
88  loadLogin()
89  checkLogin();
90  deleteMarkers(true);
91  getLatestData();
92}
93
94
95function doChangePwd(cb) {
96  // This calls the /VTEST/admin/changeuserpwd api
97  // This api call expect the following headers:
98  // wf-pwd = the current pwd of the user (or the temp password they were given)
99  // wf-uid = the user's user name
100  // wf-new-pwd = the users new pwd
101  // wf-conf-pwd = the users new pwd repeated (we should check that the pwds match before sending this
102  //                call, but we should also be sure on the server side.
103  //
104  $("#changepwd_error_message").html("");
105  $("#changepwd_message").html("");
106  var tmp_pwd = $("#c_pwd").val();
107  var wf_user_id = $("#change_pwd_user_name").val();
108  var wf_new_pwd = $("#new_pwd").val();
109  var wf_conf_pwd = $("#new_pwd_conf").val();
110  if (wf_new_pwd != wf_conf_pwd) {
111    $("#changepwd_error_message").html(_t("auth.passwords_mismatch"));
112    return
113  }
114
115  $("#loginform_error_message").html("");
116  $("#loginform_message").html("");
117  $.ajax({
118    type: 'POST',
119    url: server_api_url + "/" + server_api_version + "/admin/user/" + wf_user_id + "/changepwd",
120    headers: {
121      "wf_pwd": tmp_pwd,
122      "wf_new_pwd": wf_new_pwd
123    }
124
125  }).done(function (data) {
126
127    if (data.substring(1, 7) === "Error!:") {
128      return;
129    }
130
131    jsonpayload = JSON.parse(data);
132    if (typeof (jsonpayload['status']) != "undefined") {
133
134      if (jsonpayload['status'] === "ok") {
135        saveLogin(jsonpayload)
136        loadLogin();
137        checkLogin();
138        deleteMarkers(true);
139        getLatestData();
140
141        if (cb) {
142          cb()
143        }
144
145        $("#changepwd_message").html(_t("auth.password_saved"));
146        $("#changepwd_message").show().delay(5000).fadeOut();
147      }
148      else if (jsonpayload['status'] === "accepttandc") {
149        var accept = function () {
150          // send to server that we have accepted
151          // send the login request again
152          doConsent(jsonpayload['token']);
153        }
154        showUserTermsAndConditions("You must accept the following Terms & Conditions to use WeatherFile", "tc_content", accept);
155      } else {
156
157        $("#changepwd_error_message").html(jsonpayload['message'])
158
159      }
160
161
162    } else {
163      alert(data)
164    };
165  });
166}
167
168function loadEditProfile() {
169  console.log("Sending request");
170  $("#user_admin_container").load("/dinc/user_admin_container.html", function (response, status, xhr) {
171    if (status == "error") {
172      var msg = "Sorry but there was an error: ";
173      $("#error").html(msg + xhr.status + " " + xhr.statusText);
174      errorHandler(msg + xhr.status + " " + xhr.statusText);
175    } else {
176      console.log("Html Received");
177      getUserInfo(wf_user_uid, "profile_edit");
178    }
179  });
180}
181
182function showEditProfile() {
183
184  // profile_edit_user_email
185  // profile_edit_user_phone
186  // profile_edit_user_name
187  if ($("#user_admin_container").html() === "") {
188    console.log("Loading html for profile container");
189    loadEditProfile();
190    return;
191  }
192  getUserInfo(wf_user_uid, "profile_edit");
193}
194
195function doLogin() {
196  var tmp_pwd = $("#pwd").val();
197  var user_id = $("#uid").val();
198  var url = server_api_url + "/" + server_api_version + "/admin/login";
199
200  $("#loginform_error_message").html("");
201  $.ajax({
202    type: 'POST',
203    url: url,
204    headers: {
205      "wf_pwd": tmp_pwd,
206      "wf_user_id": user_id
207    }
208
209  }).done(function (data) {
210    if (data.substring(1, 7) === "Error!:") {
211      return;
212    }
213    jsonpayload = JSON.parse(data);
214    if (typeof (jsonpayload['status']) !== "undefined") {
215      if (jsonpayload['status'] === "ok") {
216        saveLogin(jsonpayload)
217        loadLogin();
218        checkLogin();
219        deleteMarkers(true);
220        getLatestData();
221
222        onLogin();
223      } else {
224        if (jsonpayload['status'] === "changepassword") {
225
226          $("#c_pwd").val($("#pwd").val());
227          wf_user_name = jsonpayload['user_name'];
228
229          showChangePassword("Please update your password");
230
231        } else {
232
233          if (jsonpayload['status'] === "accepttandc") {
234            var accept = function () {
235              // send to server that we have accepted
236              // send the login request again
237              doConsent(jsonpayload['token']);
238            }
239            showUserTermsAndConditions("You must accept the following Terms & Conditions to use WeatherFile", "tc_content", accept);
240          } else {
241            // If there's an error we clear the user token
242            $("#loginform_error_message").html(jsonpayload['message']);
243            // alert("RRROR:"+data);
244            // $("#loginform_error_message").html(data);
245            wf_user_token = "PUBLIC";
246            wf_user_name = "PUBLIC";
247            wf_user_data = [];
248          }
249        }
250
251      }
252    } else {
253      alert(data)
254    };
255  });
256}
257
258function onLogin() {
259  /*[HISTORYURL]if a user was trying to access something they needed to be logged in for,
260        redirect them after log in*/
261  if (loginRedirectURL) {
262    renderCanvas(loginRedirectURL);
263    loginRedirectURL = "";
264  } else {
265    renderCanvas("/");
266  }
267}
268
269
270var LOGIN_EXPIRY = (3600 * 1000 * 24); // 1 day
271
272function saveLogin(data) {
273  localStorage.setItem('wf_user_token', data['token']);
274  localStorage.setItem('wf_user_level', data['user_level']);
275  localStorage.setItem('wf_partner_id', data['partner_id']);
276  localStorage.setItem('wf_user_name', data['user_name']);
277  localStorage.setItem('wf_user_uid', data['user_id']);
278  localStorage.setItem('wf_expiry', new Date(Date.now() + LOGIN_EXPIRY));
279}
280
281function loadLogin() {
282  wf_user_token = localStorage.getItem('wf_user_token') || "PUBLIC";
283  wf_user_name = localStorage.getItem('wf_user_name') || "PUBLIC";
284  wf_user_level = localStorage.getItem('wf_user_level') || "";
285  wf_partner_id = localStorage.getItem('wf_partner_id') || "";
286  wf_user_uid = localStorage.getItem('wf_user_uid') || "";
287
288  if (wf_user_uid != "") {
289    var exp = localStorage.getItem("wf_expiry");
290    if (exp === null) { // we can get here if a user clears there local storage, handle it.
291      clearLogin();
292      loadLogin();
293    }
294    var now = new Date();
295    if (now > Date.parse(exp)) {
296      console.log("Login expired, logging out user.")
297      clearLogin();
298      loadLogin();
299    }
300  }
301}
302
303function clearLogin() {
304  localStorage.removeItem('wf_user_token');
305  localStorage.removeItem('wf_user_level');
306  localStorage.removeItem('wf_partner_id');
307  localStorage.removeItem('wf_user_name');
308  localStorage.removeItem('wf_user_uid');
309  localStorage.removeItem('wf_expiry');
310}
311
312function doConsent(token) {
313  // /admin/accepttermsandconditions
314  $.ajax({
315    type: 'POST',
316    url: server_api_url + "/" + server_api_version + "/admin/user/accepttermsandconditions",
317    headers: {
318      "wf-tkn": token
319    }
320  }).done(function (data) {
321    showUserAlert("Thank you for accepting our terms & conditions, you may now proceed to weatherfile.")
322    doLogin();
323  }).fail(function (data) {
324    console.log(data)
325    showUserAlert(_t('common.error'))
326  });
327}
328
329function cancelChangePwd() {
330  returnToPreviousDiv()
331}
332
333function showChangePassword() {
334  $("#changepwd_error_message").html("");
335  if (wf_user_name === "PUBLIC") {
336    $("#change_pwd_user_name").val("");
337  } else {
338    $("#change_pwd_user_name").val(wf_user_name);
339  }
340  //    showDiv("pwd_change_container");
341  // console.log("Last panel = " + lastPanel);
342  // console.log("show #pwd_change_container");
343  // $("#pwd_change_container").show();
344  // $("#login_container").hide();
345  // $("#settings_container").hide();
346  showDiv("pwd_change_container");
347}
348
349function showUserEdit(user_id) {
350  // This stub is here because when more adv functionality is impleented it will be needed
351  getUserInfo(user_id, "edit");
352}
353
354function editUserSave() {
355
356  var userSaveFailed = false;
357  edit_uid = $("#uf_user_id").val();
358  $("#userform_emsg").html("");
359  edit_user_name = $("#uf_user_name").val();
360  edit_user_phone = $("#uf_user_phone").val();
361  edit_user_email = $("#uf_user_email").val();
362  edit_user_level = $("#uf_user_level").val();
363  edit_user_partner_id = $("#uf_partner_id").val();
364  edit_user_tmp_pwd = $("#uf_temp_pwd").val();
365
366  var data = {
367    user_name: edit_user_name,
368    user_phone: edit_user_phone,
369    user_email: edit_user_email,
370    user_level: edit_user_level,
371    partner_id: edit_user_partner_id,
372    user_tmp_pwd: edit_user_tmp_pwd,
373  };
374  var url = server_api_url + "/" + server_api_version + "/admin/";
375  var method = ""
376
377  if (edit_uid == "newuser") {
378    url += "users"
379    method = "POST"
380  } else {
381    url += "user/" + edit_uid
382    method += "PUT"
383  }
384
385  $.ajax({
386    type: method,
387    url: url,
388    data: data,
389    headers: {
390      "wf-tkn": wf_user_token
391    }
392  }).then(function (data) {
393
394    jsonpayload = JSON.parse(data);
395
396    if (typeof (jsonpayload['status']) != "undefined") {
397      //DO stuff here
398      if (jsonpayload['status'] === "ok") {
399        $("#admin_show_user_msg").html("User saved successfully");
400        $("#admin_show_user_msg").show().delay(5000).fadeOut();
401
402        if (wf_user_level === "superadmin" && edit_uid !== "newuser") {
403          var gid = $('#uf_subscription_level').val()
404
405          var method = "DELETE";
406          if (gid !== "NONE") {
407            method = "PUT"
408          }
409
410          return $.ajax({
411            type: method,
412            url: server_api_url + "/" + server_api_version + "/admin/user/" + edit_uid + "/WF_PREMIUM",
413            contentType: "application/json",
414            headers: {
415              "wf-tkn": wf_user_token
416            }
417          });
418        }
419
420      }
421      if (jsonpayload['status'] === "Error") {
422        userSaveFailed = true;
423        $("#userform_emsg").html(jsonpayload['message']);
424      }
425    }
426  }).then(function (data) {
427    jsonpayload = JSON.parse(data);
428    if (jsonpayload['status'] !== "ok") {
429      showUserAlert(jsonpayload['message'])
430    }
431
432    if (method == "PUT" && $('#uf_subscription_level').val() !== "NONE") {
433      var notifs = []
434      for (var i = 0; i < userNotifications.length; i++) {
435        notifs.push(userNotifications[i].toJson())
436      }
437      console.log("Setting user notifications: ", notifs)
438
439      return $.ajax({
440        type: 'POST',
441        url: server_api_url + "/" + server_api_version + "/admin/user/" + edit_uid + "/notifications",
442        contentType: "application/json",
443        data: JSON.stringify(notifs),
444        headers: {
445          "wf-tkn": wf_user_token
446        }
447      });
448    }
449  }).then(function (data) {
450    if (isValid(data)) {
451      jsonpayload = JSON.parse(data);
452      if (jsonpayload['status'] !== "ok") {
453        showUserAlert(jsonpayload['message'])
454      }
455    }
456  }).fail(function (err) {
457    userSaveFailed = true;
458    jsonpayload = JSON.parse(err.responseText);
459    showUserAlert(jsonpayload['message']);
460  }).always(function () {
461    if (!userSaveFailed) {
462      showUserEdit(getQueryVariable("uid", currentURL))
463    }
464  });
465}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.