1(function (Drupal, drupalSettings, once) { 2 Drupal.behaviors.mysurreyautologin = { 3 attach: function (context, settings) { 4 once("mysurreyautologin", "#my-surrey-navigation", context).forEach( 5 6 // If the user is not logged in and we see an LR token, 7 // and the token is not expired, redirect to the login. 8 function () { 9 // Get the cookie named lr_token_expires_in. 10 const lrUserSessionToken = getCookie("lr-session-token"); 11 12 // First we update the login link to include the destination. 13 const loginLink = document.querySelector( 14 'a[data-drupal-link-system-path="my-surrey/login"]' 15 ); 16 // Update the logout link to include the destination. 17 const logoutLink = document.querySelector( 18 'a[data-drupal-link-system-path="my-surrey/logout"]' 19 ); 20 21 const logoutMenuLink = document.querySelector('a.icon-logout'); 22 23 // Make sure the current page is not the login page nor the home page. 24 if ( 25 loginLink && 26 (window.location.pathname === "/my-surrey/login" || 27 window.location.pathname === "/") 28 ) { 29 loginLink.href = loginLink.href + "?destination=<front>"; 30 } else if (loginLink) { 31 loginLink.href = 32 loginLink.href + "?destination=" + window.location.pathname; 33 } else if ( 34 logoutLink && 35 (window.location.pathname === "/my-surrey/logout" || 36 window.location.pathname === "/") 37 ) { 38 logoutLink.href = logoutLink.href + "?destination=<front>"; 39 } else if (logoutLink) { 40 logoutLink.href = 41 logoutLink.href + "?destination=" + window.location.pathname; 42 } 43 44 if ( 45 logoutMenuLink && 46 (window.location.pathname === "/my-surrey/login" || 47 window.location.pathname === "/") 48 ) { 49 logoutMenuLink.href = logoutMenuLink.href + "?destination=/"; 50 } else if (logoutMenuLink) { 51 logoutMenuLink.href = 52 logoutMenuLink.href + "?destination=" + window.location.pathname; 53 } 54 55 // Don't do any redirection when the SSO is off. 56 if ( 57 drupalSettings.mySurreyMenuSettings.disable_sso == true || 58 drupalSettings.mySurreyMenuSettings.auto_login != true 59 ) { 60 return; 61 } 62 63 // Track that we're attempting a login. 64 if ( 65 (lrUserSessionToken && 66 drupalSettings.mySurreyMenuSettings.isAuthenticated === false && 67 sessionStorage.getItem("mysurreyAutologin") !== "true") || 68 (sessionStorage.getItem("mysurreyAutoLoginDebug") == "true" && 69 drupalSettings.mySurreyMenuSettings.isAuthenticated === false) 70 ) { 71 // Avoid a login loop. Try once, if it doesn't work give up for this 72 // session. 73 sessionStorage.setItem("mysurreyAutologin", "true"); 74 // Always clear the debug flag. 75 sessionStorage.removeItem("mysurreyAutoLoginDebug"); 76 // Redirect to the login page. 77 window.location.href = loginLink.href; 78 } 79 } 80 ); 81 }, 82 }; 83 84 Drupal.behaviors.mysurreymenu = { 85 attach: function (context, settings) { 86 once("mySurreyUserInitials", "#my-surrey-button", context).forEach( 87 (mysurreymenu) => { 88 let initials = ""; 89 // Get the initials from the api endpoint. 90 try { 91 const url = drupalSettings.mySurreyMenuSettings.profileUrl; 92 const basicAuth = drupalSettings.mySurreyMenuSettings.basicAuth; 93 const headers = { 94 "Content-Type": "application/json", 95 }; 96 if (basicAuth) { 97 headers.Authorization = `Basic ${basicAuth}`; 98 } 99 if (url) { 100 fetch(url, { headers }) 101 .then((response) => response.json()) 102 .then((data) => { 103 mysurreymenu 104 .querySelector("span") 105 .setAttribute("data-initials", data.initials); 106 }) 107 } 108 } catch (error) { 109 // Fallback to no initials. 110 } 111 112 } 113 ); 114 }, 115 }; 116})(Drupal, drupalSettings, once); 117 118/** 119 * Gets the value of a cookie. 120 * 121 * @param {string} name 122 * @returns {string} 123 */ 124function getCookie(name) { 125 var value = "; " + document.cookie; 126 var parts = value.split("; " + name + "="); 127 if (parts.length == 2) return parts.pop().split(";").shift(); 128}
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.