Guest User

Untitled

a guest
May 29th, 2025
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.51 KB | None | 0 0
  1. <script>
  2. $(function() {
  3. // Get current username directly from Joget hash variable
  4. var currentUsername = "#currentUser.username#";
  5.  
  6. // Log the username
  7. console.log("Current username: " + currentUsername);
  8.  
  9. // Add click handlers only to approve and reject buttons
  10. $("#process_approve, #process_reject").on("click", function(e) {
  11. // Prevent default submission
  12. e.preventDefault();
  13.  
  14. // Store which button was clicked for later use
  15. var clickedButtonId = $(this).attr('id');
  16. console.log(clickedButtonId + " button clicked, showing authentication popup");
  17.  
  18. // Create and show authentication popup
  19. createAuthPopup(clickedButtonId);
  20.  
  21. return false;
  22. });
  23.  
  24. function createAuthPopup(buttonId) {
  25. // Create overlay div
  26. var overlay = $("<div>").attr("id", "authOverlay").css({
  27. "position": "fixed",
  28. "top": "0",
  29. "left": "0",
  30. "width": "100%",
  31. "height": "100%",
  32. "background-color": "rgba(0,0,0,0.5)",
  33. "z-index": "99999",
  34. "display": "flex",
  35. "justify-content": "center",
  36. "align-items": "center"
  37. });
  38.  
  39. // Create dialog box
  40. var dialog = $("<div>").attr("id", "authDialog").css({
  41. "background": "white",
  42. "width": "320px",
  43. "padding": "20px",
  44. "border-radius": "5px",
  45. "box-shadow": "0 2px 10px rgba(0,0,0,0.3)"
  46. });
  47.  
  48. // Create action title based on button clicked
  49. var actionTitle = buttonId === "process_approve" ? "Approve Process" : "Reject Process";
  50.  
  51. // Add form inside dialog
  52. var authContent = $("<div>").attr({
  53. "id": "authContent"
  54. });
  55.  
  56. // Add title
  57. authContent.append($("<h3>").text("Authentication Required").css({
  58. "margin-top": "0",
  59. "margin-bottom": "5px"
  60. }));
  61.  
  62. authContent.append($("<p>").text("Please enter your password to " + actionTitle.toLowerCase()).css({
  63. "margin-top": "0",
  64. "margin-bottom": "15px",
  65. "color": "#666"
  66. }));
  67.  
  68. // Username field - readonly and prefilled
  69. var usernameGroup = $("<div>").css("margin-bottom", "15px");
  70. usernameGroup.append($("<label>").attr("for", "auth_username").text("Username:").css({
  71. "display": "block",
  72. "margin-bottom": "5px",
  73. "font-weight": "bold"
  74. }));
  75. usernameGroup.append($("<input>").attr({
  76. "type": "text",
  77. "id": "auth_username",
  78. "name": "auth_username",
  79. "value": currentUsername,
  80. "readonly": "readonly",
  81. "disabled": "disabled"
  82. }).css({
  83. "width": "100%",
  84. "padding": "8px",
  85. "box-sizing": "border-box",
  86. "border": "1px solid #ccc",
  87. "border-radius": "3px",
  88. "background-color": "#f5f5f5",
  89. "color": "#666"
  90. }));
  91. authContent.append(usernameGroup);
  92.  
  93. // Password field
  94. var passwordGroup = $("<div>").css("margin-bottom", "15px");
  95. passwordGroup.append($("<label>").attr("for", "auth_password").text("Password:").css({
  96. "display": "block",
  97. "margin-bottom": "5px",
  98. "font-weight": "bold"
  99. }));
  100.  
  101. var passwordInput = $("<input>").attr({
  102. "type": "password",
  103. "id": "auth_password",
  104. "name": "auth_password"
  105. }).css({
  106. "width": "100%",
  107. "padding": "8px",
  108. "box-sizing": "border-box",
  109. "border": "1px solid #ccc",
  110. "border-radius": "3px"
  111. });
  112.  
  113. passwordGroup.append(passwordInput);
  114. authContent.append(passwordGroup);
  115.  
  116. // Error message area
  117. var errorMsg = $("<div>").attr("id", "auth_error").css({
  118. "color": "red",
  119. "margin-bottom": "15px",
  120. "min-height": "20px"
  121. });
  122. authContent.append(errorMsg);
  123.  
  124. // Buttons
  125. var buttons = $("<div>").css({
  126. "display": "flex",
  127. "justify-content": "space-between"
  128. });
  129.  
  130. var cancelBtn = $("<button>").attr({
  131. "type": "button",
  132. "id": "auth_cancel"
  133. }).text("Cancel").css({
  134. "padding": "8px 15px",
  135. "background": "#f1f1f1",
  136. "border": "1px solid #ddd",
  137. "border-radius": "3px",
  138. "cursor": "pointer"
  139. });
  140.  
  141. var actionColor = buttonId === "process_approve" ? "#2196F3" : "#f44336"; // Blue for approve, Red for reject
  142.  
  143. var loginBtn = $("<button>").attr({
  144. "type": "button",
  145. "id": "auth_login"
  146. }).text("Continue").css({
  147. "padding": "8px 15px",
  148. "background": actionColor,
  149. "color": "white",
  150. "border": "none",
  151. "border-radius": "3px",
  152. "cursor": "pointer"
  153. });
  154.  
  155. buttons.append(cancelBtn, loginBtn);
  156. authContent.append(buttons);
  157.  
  158. // Add content to dialog
  159. dialog.append(authContent);
  160. overlay.append(dialog);
  161.  
  162. // Add to page
  163. $("body").append(overlay);
  164.  
  165. // Focus on password field
  166. setTimeout(function() {
  167. $("#auth_password").focus();
  168. }, 100);
  169.  
  170. // Handle button clicks
  171. $("#auth_cancel").on("click", function() {
  172. $("#authOverlay").remove();
  173. });
  174.  
  175. $("#auth_login").on("click", function() {
  176. authenticateUser(buttonId);
  177. });
  178.  
  179. // Handle Enter key in password field
  180. $("#auth_password").on("keypress", function(e) {
  181. if (e.which === 13) {
  182. authenticateUser(buttonId);
  183. }
  184. });
  185. }
  186.  
  187. function authenticateUser(buttonId) {
  188. var username = currentUsername;
  189. var password = $("#auth_password").val();
  190.  
  191. if (!password) {
  192. $("#auth_error").text("Please enter your password");
  193. return;
  194. }
  195.  
  196. // Show loading state
  197. $("#auth_login").text("Processing...").prop("disabled", true);
  198.  
  199. // Use fetch WITHOUT credentials to create a new session
  200. fetch('/jw/api/sso', {
  201. method: 'POST',
  202. headers: {
  203. 'Content-Type': 'application/x-www-form-urlencoded',
  204. 'accept': 'application/json',
  205. 'api_id': 'API-2325f1c7-df58-4c52-a555-926d696a593c',
  206. 'api_key': ''
  207. },
  208. credentials: 'omit', // Don't send cookies
  209. body: 'j_username=' + encodeURIComponent(username) + '&j_password=' + encodeURIComponent(password)
  210. })
  211. .then(response => {
  212. if (!response.ok) {
  213. throw new Error('Authentication failed');
  214. }
  215. return response.json();
  216. })
  217. .then(data => {
  218. console.log("Authentication successful", data);
  219.  
  220. // Close the popup
  221. $("#authOverlay").remove();
  222.  
  223. // Now trigger the original button click
  224. var originalButton = $("#" + buttonId);
  225.  
  226. // Remove our click handler temporarily
  227. originalButton.off("click");
  228.  
  229. // Submit the form with the correct button
  230. var form = originalButton.closest("form");
  231.  
  232. // Create a hidden input to simulate the button click if needed
  233. if (form.length) {
  234. // Add a hidden input with the button's name and value
  235. $("<input>").attr({
  236. type: "hidden",
  237. name: originalButton.attr("name"),
  238. value: originalButton.attr("value")
  239. }).appendTo(form);
  240.  
  241. // Submit the form
  242. form.submit();
  243. } else {
  244. // If no form found, just trigger a click on the button
  245. originalButton.click();
  246. }
  247.  
  248. // Re-attach our handler after a short delay
  249. setTimeout(function() {
  250. attachButtonHandlers();
  251. }, 500);
  252. })
  253. .catch(error => {
  254. console.error("Authentication error:", error);
  255. $("#auth_error").text("Authentication failed. Please check your password and try again.");
  256. $("#auth_login").text("Continue").prop("disabled", false);
  257. });
  258. }
  259.  
  260. // Function to re-attach button handlers if needed
  261. function attachButtonHandlers() {
  262. $("#process_approve, #process_reject").each(function() {
  263. var btn = $(this);
  264. if (!btn.data("has-auth-handler")) {
  265. btn.on("click", function(e) {
  266. e.preventDefault();
  267. var clickedButtonId = $(this).attr('id');
  268. console.log(clickedButtonId + " button clicked, showing authentication popup");
  269. createAuthPopup(clickedButtonId);
  270. return false;
  271. }).data("has-auth-handler", true);
  272. }
  273. });
  274. }
  275. });
  276. </script>
Advertisement
Add Comment
Please, Sign In to add comment