Package com.casamind.adware.server.utils

Source Code of com.casamind.adware.server.utils.LoginHelper

/**
* Copyright 2010 Daniel Guermeur and Amy Unruh
*
*   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.
*
*   See http://connectrapp.appspot.com/ for a demo, and links to more information
*   about this app and the book that it accompanies.
*/
package com.casamind.adware.server.utils;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.casamind.adware.server.domain.UserAccount;
import com.casamind.adware.server.proxy.DatastoreProxy;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class LoginHelper extends RemoteServiceServlet {

  private static Logger logger = Logger.getLogger(LoginHelper.class.getName());

  static public String getApplitionURL(HttpServletRequest request) {

    if (ServletHelper.isDevelopment(request)) {
      return "http://127.0.0.1:8888/Adware.html?gwt.codesvr=127.0.0.1:9997";
    } else {
      return ServletUtils.getBaseUrl(request);
    }

  }

  static public UserAccount getLoggedInUser(HttpSession session) {

    if (session == null)
      return null; // user not logged in

    String userId = (String) session.getAttribute("userId");
    if (userId == null)
      return null; // user not logged in

    Long id = Long.parseLong(userId.trim());

    UserAccount user = DatastoreProxy.getUserAccountById(id);

    return user;

  }

  static public boolean isLoggedIn(HttpServletRequest req) {

    if (req == null)
      return false;
    else {
      HttpSession session = req.getSession();
      if (session == null) {
        logger.info("Session is null...");
        return false;
      } else {
        Boolean isLoggedIn = (Boolean) session.getAttribute("loggedin");
        if (isLoggedIn == null) {
          logger.info("Session found, but did not find loggedin attribute in it: user not logged in");
          return false;
        } else if (isLoggedIn) {
          logger.info("User is logged in...");
          return true;
        } else {
          logger.info("User not logged in");
          return false;
        }
      }
    }
  }

  public UserAccount loginStarts(HttpSession session, UserAccount user, boolean isAdmin) {
    UserAccount loggedUser = DatastoreProxy.getUserAccountByUniqueId(user.getUniqueId());
    if (loggedUser != null) {
      session.setAttribute("userId", String.valueOf(loggedUser.getId()));
      if (isAdmin)
        session.setAttribute("accessLevel", 1);
      else {
        session.setAttribute("accessLevel", loggedUser.getAccessLevel());
      }
    } else {
      session.setAttribute("accessLevel", 0);
    }
    session.setAttribute("loggedin", true);
    return loggedUser;
  }

}
TOP

Related Classes of com.casamind.adware.server.utils.LoginHelper

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.