atlasLogin.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. //Define indexOf for IE
  19. if (!Array.prototype.indexOf) {
  20. Array.prototype.indexOf = function(obj, start) {
  21. for (var i = start || 0; i < this.length; i++) {
  22. if (this[i] == obj) {
  23. return i;
  24. }
  25. }
  26. return -1;
  27. };
  28. }
  29. if (!String.prototype.startsWith) {
  30. String.prototype.startsWith = function(str, matchStr) {
  31. return str.lastIndexOf(matchStr, 0) === 0;
  32. };
  33. }
  34. function doLogin() {
  35. var userName = "admin";
  36. var passwd = "admin";
  37. if (userName === "" || passwd === "") {
  38. $("#errorBox").show();
  39. $("#signInLoading").hide();
  40. $("#signIn").removeAttr("disabled");
  41. $("#errorBox .errorMsg").text("The username or password you entered is blank..");
  42. return false;
  43. }
  44. var baseUrl = getBaseUrl();
  45. $.ajax({
  46. data: {
  47. j_username: userName,
  48. j_password: passwd
  49. },
  50. url: baseUrl + "/j_spring_security_check",
  51. type: "POST",
  52. headers: {
  53. "cache-control": "no-cache"
  54. },
  55. success: function() {
  56. redirect(baseUrl);
  57. },
  58. error: function(jqXHR, textStatus, err) {
  59. $("#signIn").removeAttr("disabled");
  60. $("#signInLoading").css("visibility", "hidden");
  61. if (jqXHR.status && jqXHR.status == 412) {
  62. $("#errorBox").hide();
  63. $("#errorBoxUnsynced").show();
  64. } else {
  65. try {
  66. var resp = JSON.parse(jqXHR.responseText);
  67. if (resp.msgDesc.startsWith("Username not found") || resp.msgDesc.startsWith("Wrong password")) {
  68. $("#errorBox .errorMsg").text("Invalid User credentials. Please try again.");
  69. } else if (resp.msgDesc.startsWith("User role credentials is not set properly")) {
  70. $("#errorBox .errorMsg").text("User role or credentials is not set properly");
  71. } else {
  72. $("#errorBox .errorMsg").text("Error while authenticating");
  73. }
  74. } catch (err) {
  75. $("#errorBox .errorMsg").text("Something went wrong");
  76. }
  77. $("#errorBox").show();
  78. $("#errorBoxUnsynced").hide();
  79. }
  80. }
  81. });
  82. }
  83. function redirect(baseUrl) {
  84. $.ajax({
  85. url: baseUrl + "api/atlas/admin/session",
  86. success: function(data) {
  87. var PRIMARY_UI = "v2",
  88. indexpath = "/n/index.html";
  89. if (data && data["atlas.ui.default.version"]) {
  90. PRIMARY_UI = data["atlas.ui.default.version"];
  91. }
  92. if (PRIMARY_UI !== "v2") {
  93. indexpath = "/index.html";
  94. }
  95. if (window.localStorage.last_ui_load === "v1") {
  96. indexpath = "/index.html";
  97. } else if (window.localStorage.last_ui_load === "v2") {
  98. indexpath = "/n/index.html";
  99. }
  100. indexpath = baseUrl + indexpath;
  101. if (location.hash.length > 2) {
  102. indexpath += location.hash;
  103. }
  104. window.location.replace(indexpath);
  105. },
  106. error: function() {
  107. window.location.replace("index.html");
  108. }
  109. });
  110. }
  111. function getBaseUrl() {
  112. return window.location.pathname.replace(/\/[\w-]+.(jsp|html)|\/+$/ig, '')
  113. }
  114. $(function() {
  115. // register handlers
  116. if (!("placeholder" in HTMLInputElement.prototype)) {
  117. $("#username , #password").placeholder();
  118. }
  119. $("#signIn").on("click", function() {
  120. $("#signIn").attr("disabled", true);
  121. $("#signInLoading").css("visibility", "visible");
  122. doLogin();
  123. return false;
  124. });
  125. $("#loginForm").each(function() {
  126. $("input").keypress(function(e) {
  127. // Enter pressed?
  128. if (e.which == 10 || e.which == 13) {
  129. doLogin();
  130. }
  131. });
  132. });
  133. $("#loginForm li[class^=control-group] > input").on("change", function(e) {
  134. if (e.target.value === "") {
  135. $(e.target)
  136. .parent()
  137. .addClass("error");
  138. } else {
  139. $(e.target)
  140. .parent()
  141. .removeClass("error");
  142. }
  143. });
  144. $("#password").on("keyup", function() {
  145. if (this.value.trim() === "") {
  146. $(".show-password ").hide();
  147. } else {
  148. $(".show-password ").show();
  149. }
  150. });
  151. var showPassword = false;
  152. $(".show-password").on("click", function() {
  153. showPassword = !showPassword;
  154. $("#password").attr("type", showPassword ? "text" : "password");
  155. $(".show-password").toggleClass("fa-eye-slash fa-eye");
  156. });
  157. });
  158. doLogin();