Package jease.cms.web

Source Code of jease.cms.web.Login

/*
    Copyright (C) 2011 maik.jablonski@jease.org

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package jease.cms.web;

import jease.Names;
import jease.Registry;
import jease.Version;
import jease.cmf.domain.Node;
import jease.cmf.service.Nodes;
import jease.cmf.web.JeaseSession;
import jease.cmf.web.node.NodeFilter;
import jease.cms.domain.Content;
import jease.cms.domain.User;
import jease.cms.service.Authenticator;
import jease.cms.web.content.Configuration;
import jfix.db4o.Database;
import jfix.util.Reflections;
import jfix.util.Validations;
import jfix.zk.ActionListener;
import jfix.zk.LoginWindow;
import jfix.zk.Modal;
import jfix.zk.Sessions;
import jfix.zk.ZK;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.ClientInfoEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;

/**
* Login into JeaseCMS: init user session and display tab-navigation.
*
* If a user is already stored within the session, we don't reinforce a login,
* but recreate the desktop from scratch. This avoids showing the login page
* when the users refreshes the browser.
*/
public class Login extends LoginWindow {

  private String queryString = ZK.getQueryString();

  public Login() {
    User user = JeaseSession.get(User.class);
    if (user != null) {
      notifyAboutMaintenance(user);
      initSession(user);
    }
  }

  public String getTitle() {
    return String.format("Jease %s", Version.getName());
  }

  public void doLogin(String login, String password) {
    User user = getAuthenticator().identify(login, password);
    if (user != null) {
      notifyAboutMaintenance(user);
      initSession(user);
    }
  }

  private Authenticator getAuthenticator() {
    try {
      String authenticator = Registry
          .getParameter(Names.JEASE_CMS_AUTHENTICATOR);
      if (Validations.isNotEmpty(authenticator)) {
        return (Authenticator) Reflections.newInstance(authenticator);
      }
    } catch (RuntimeException e) {
      Modal.exception(e);
    }
    return new Authenticator();
  }

  private void initSession(User user) {
    initBrowserInfo();
    JeaseSession.set(user);
    if (Validations.isNotEmpty(user.getRoots())) {
      JeaseSession.setConfig(new Configuration());
      JeaseSession.setFilter(new NodeFilter(JeaseSession.getConfig()
          .newNodes()));
      JeaseSession.setRoots(user.getRoots());
      if (queryString != null) {
        Node node = Nodes.getByPath(queryString);
        if (JeaseSession.getFilter().isAccepted(node)) {
          JeaseSession.setContainer(node);
        }
      }
      if (JeaseSession.getContainer() == null
          || !JeaseSession.getContainer().isDescendant(
              user.getRoots())) {
        JeaseSession.setContainer(user.getRoots()[0]);
      }
    }
    show(new Navigation(user));
    updatePageTitle(user);
  }

  private void initBrowserInfo() {
    getRoot().addEventListener(Events.ON_CLIENT_INFO, new EventListener() {
      public void onEvent(Event evt) throws Exception {
        ClientInfoEvent ce = (ClientInfoEvent) evt;
        JeaseSession.set(Names.JEASE_CMS_HEIGHT, ce.getDesktopHeight());
        JeaseSession.set(Names.JEASE_CMS_WIDTH, ce.getDesktopWidth());
      }
    });
  }

  private void notifyAboutMaintenance(User user) {
    String message = Registry.getParameter(Names.JEASE_CMS_MAINTENANCE);
    if (Validations.isNotEmpty(message)) {
      if (user.isAdministrator()) {
        Modal.info(message);
      } else {
        Modal.info(message, new ActionListener() {
          public void actionPerformed(Event event) {
            Sessions.invalidate();
          }
        });
      }
    }
  }

  private void updatePageTitle(User user) {
    String status = String.format("%s - %s",
        ((Content) Nodes.getRoot()).getTitle(), user.getLogin());
    if (user.isAdministrator()) {
      status += " - " + Database.ext().getBlobDirectory();
    }
    Executions.getCurrent().getDesktop().getFirstPage().setTitle(status);
  }

}
TOP

Related Classes of jease.cms.web.Login

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.