login.js

来自「Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是」· JavaScript 代码 · 共 190 行

JS
190
字号
/*
* Copyright 2001-2007 Hippo (www.hippo.nl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

importClass(Packages.org.apache.commons.httpclient.UsernamePasswordCredentials);
importClass(Packages.org.apache.commons.httpclient.HttpState);
importClass(Packages.org.apache.cocoon.environment.Cookie);
importClass(Packages.nl.hippo.cocoon.webdav.WebDAVHelper);
importClass(Packages.nl.hippo.cocoon.site.SiteManager);
importClass(Packages.nl.hippo.cocoon.repository.RepositoryManager);

cocoon.load("site:/config/config.js");

var authentication = {}
var message = "";

authentication.getHttpState = function () {
  if (cocoon.session.httpstate == undefined) {
    login();
  }
  return cocoon.session.httpstate;
}

function login(browserWarning, browserError) {

  var url = "/login/login.html";
  var authenticated = false;
  
  // Set session cookie for loadbalancing + sticky sessions with mod_proxy_balancer
  var userCookie = cocoon.response.createCookie("sessionhost","sess." + cocoon.request.getServerName());
  userCookie.path = "/";
  cocoon.response.addCookie(userCookie);
  
  while (!authenticated) {
    cocoon.sendPageAndWait(url, {username:cocoon.request.username, message:message, 
        browserWarning:browserWarning, browserError:browserError});
        
    authenticated = do_login(cocoon.request.username, cocoon.request.password, cocoon.request.rememberme, cocoon.request.locale);
  }
  return authenticated;
}

function do_login(username, password, rememberme, locale) {

    var credentials = new UsernamePasswordCredentials();

    var rc = config.getCurrentRepository();
    var host = rc.getHost();
    var users = rc.getUsers();
      
    var httpState = new HttpState();

    httpState.setCredentials(null, null, credentials);
    httpState.setAuthenticationPreemptive(true);

    credentials.setUserName(username);
    credentials.setPassword(password);

    var re = /^([_a-zA-Z0-9@.\-])+$/;
    var isUsernameValid = username != null && String(username).length > 0 && username.search(re) == 0;
    
    if (isUsernameValid && WebDAVHelper.login(users + "/" + username, httpState)) {

      var namespace="http://hippo.nl/cms/1.0";
      var name="fullname";          
      var useruri = users + "/" + username;
      var fullname = WebDAVHelper.propfindAsString(useruri, namespace, name, httpState);

      cocoon.session.setAttribute("httpstate", httpState);
      cocoon.session.setAttribute("repositoryroot", rc.getRoot());
      cocoon.session.setAttribute("username", httpState.getCredentials(null, host).getUserName());
      cocoon.session.setAttribute("fullname", fullname);
      cocoon.session.setAttribute("rememberme", rememberme);
      cocoon.session.setAttribute("locale", locale);
      cocoon.session.setMaxInactiveInterval("14400"); // 4 hours
	  
	  if(rememberme != undefined && rememberme == "on") {
	  
	      var userCookie = cocoon.response.createCookie("username", httpState.getCredentials(null, host).getUserName());
	      userCookie.maxAge = 60*60*24*30;  //30 days
	      userCookie.path = "/";
	      cocoon.response.addCookie(userCookie);
	
	      var fullnameCookie = cocoon.response.createCookie("fullname", fullname);
	      fullnameCookie.maxAge = 60*60*24*30;  //30 days
	      fullnameCookie.path = "/";
	      cocoon.response.addCookie(fullnameCookie);
	      
	      var pwdCookie = cocoon.response.createCookie("pwd", httpState.getCredentials(null, host).getPassword());
	      pwdCookie.maxAge = 60*60*24*30;  //30 days
	      pwdCookie.path = "/";
	      cocoon.response.addCookie(pwdCookie);
	
	      var userCookie = cocoon.response.createCookie("rememberme", rememberme);
	      userCookie.maxAge = 60*60*24*30;  //30 days
	      userCookie.path = "/";
	      cocoon.response.addCookie(userCookie);
	      
	      var userCookie = cocoon.response.createCookie("locale", locale);
	      userCookie.maxAge = 60*60*24*30;  //30 days
	      userCookie.path = "/";
	      cocoon.response.addCookie(userCookie);
      }else{
          var aCookie = cocoon.response.createCookie("username", "");
	      aCookie.maxAge = 1;
	      aCookie.path = "/";
	      cocoon.response.addCookie(aCookie);
	
	      aCookie = cocoon.response.createCookie("fullname", "");
	      aCookie.maxAge = 1;
	      aCookie.path = "/";
	      cocoon.response.addCookie(aCookie);
	      
	      aCookie = cocoon.response.createCookie("pwd", "");
	      aCookie.maxAge = 1;
	      aCookie.path = "/";
	      cocoon.response.addCookie(aCookie);
	
	      aCookie = cocoon.response.createCookie("rememberme", "");
	      aCookie.maxAge = 1;
	      aCookie.path = "/";
	      cocoon.response.addCookie(aCookie);
	      
	      aCookie = cocoon.response.createCookie("locale", "");
	      aCookie.maxAge = 1;
	      aCookie.path = "/";
	      cocoon.response.addCookie(aCookie);
      }
      
      return true;
      
    }
    else if (!isUsernameValid) {
      message = "login.invalidusername";
    }
    else {
      message = "login.passwordincorrect";
    }

    return false;
}

function logout() {
  //removeLocks();
  cocoon.session.invalidate();
  cocoon.redirectTo("cocoon:/redirect.html");
}


/**
 * Unlocks all documents which were locked in the current session.
 */
function removeLocks()
{
  var locks = cocoon.session.getAttribute("locks");
  if (locks != null)
  {
    var it = locks.keySet().iterator();
    while (it.hasNext())
    {
      var key = it.next();
      print("unlock " + key + ":" + locks.get(key));
      var unlockStatus = WebDAVHelper.unlock(key, locks.get(key), authentication.getHttpState());
      locks.remove(key);
    }
    cocoon.session.setAttribute("locks", locks);
  }
}


/**
 * used for ajax update of login box on language selection
 */
function setLocale() {
  cocoon.session.setAttribute("locale", cocoon.request.locale);
  cocoon.sendPage("update-login-box-display-pipeline", {message:""});
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?