in struts.xml
--------------
<struts>
<package name="default" extends="struts-default" namespace="">
<result-types>
<result-type name="tiles"
class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<interceptors>
<interceptor name="loginInterceptor"
class="com.cah.trackinggadget.admin.GadgetLoginInterceptor">
</interceptor>
<interceptor-stack name="ideInterceptorsStack">
<interceptor-ref name="alias" />
<interceptor-ref name="params" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="exception" />
<interceptor-ref name="prepare" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="params" />
<interceptor-ref name="conversionError" />
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel</param>
</interceptor-ref>
<interceptor-ref name="loginInterceptor" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="ideInterceptorsStack"></default-interceptor-ref>
<global-results>
<result name="redirecthome" type="redirect">home.jsp</result>
</global-results>
<action name="login" method="login" class="loginAction">
<result name="admin">home.jsp</result>
<result name="user">Login.jsp</result>
<!-- <result name="user" type="redirectAction">
<param name="actionName">searchGadget.action</param>
</result>-->
<!-- <result name="changepassword" type="tiles">changepassword</result> -->
<result name="input">Login.jsp</result>
<result name="error">Login.jsp</result>
</action>
</package>
</struts>
GadgetLoginInterceptor.java:
------------------------------
package com.cah.trackinggadget.admin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import com.cah.trackinggadget.to.Authorizations;
import com.cah.trackinggadget.to.Guser;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
*
* Handles all Authorizations related operations.
*
*/
public class GadgetLoginInterceptor implements Interceptor {
private static final long serialVersionUID = 1L;
Logger logger = Logger.getLogger(GadgetLoginInterceptor.class);
@Override
public void destroy() {
}
@Override
public void init() {
}
@Override
public String intercept(ActionInvocation actInvoc) throws Exception {
System.out.println("from interceptor actionssssssssssssssssssssssssssss");
logger.info("Begin----GadgetLoginInterceptor:intercept");
ActionContext context = actInvoc.getInvocationContext();
Map<String, Object> session = context.getSession();
Map<String, Object> application = ActionContext.getContext()
.getApplication();
if (application != null && !application.containsKey("authorizations")) {
AuthorizationsLoader authLoadAction = new AuthorizationsLoader();
List<Authorizations> authorizations = authLoadAction
.loadAuthorizations();
if (authorizations != null) {
Set<Integer> userTypes = new HashSet<Integer>();
for (Authorizations auth : authorizations) {
userTypes.add(auth.getUserType().getUserTypeId());
}
Map<Integer, List<String>> userMap = new HashMap<Integer, List<String>>();
for (Integer userType : userTypes) {
List<String> actions = new ArrayList<String>();
for (Authorizations auth : authorizations) {
if (userType == auth.getUserType().getUserTypeId()) {
actions.add(auth.getActions().getAction());
}
}
userMap.put(userType, actions);
}
application.put("authorizations", userMap);
}
}
if (actInvoc.getProxy().getActionName().equals("gvHome")
||actInvoc.getProxy().getActionName().equals("login")
|| actInvoc.getProxy().getActionName().equals("forgotPassword")
|| actInvoc.getProxy().getActionName().equals("getPassword")
|| actInvoc.getProxy().getActionName().equals("searchGadget")
|| actInvoc.getProxy().getActionName().equals("preRegister")
|| actInvoc.getProxy().getActionName().equals("driverAction")
|| actInvoc.getProxy().getActionName()
.equals("preVehicleRegister")
|| actInvoc.getProxy().getActionName()
.equals("vehicleRegisterAction")
|| actInvoc.getProxy().getActionName()
.equals("preGadgetRegister")
|| actInvoc.getProxy().getActionName()
.equals("gadgetRegisterAction")
|| actInvoc.getProxy().getActionName().equals("preEditVehicle")
|| actInvoc.getProxy().getActionName()
.equals("viewDriversAction")
|| actInvoc.getProxy().getActionName()
.equals("prepareEditDriver")
|| actInvoc.getProxy().getActionName()
.equals("updateDriverAction")
|| actInvoc.getProxy().getActionName().equals("editVehicle")
|| actInvoc.getProxy().getActionName().equals("formValidation")) {
logger.debug("Inside GadgetLoginInterceptor:intercept if . . . "
+ actInvoc.getProxy().getActionName());
return actInvoc.invoke();
} else {
Guser webuser = (Guser) session.get("ideuser");
if (webuser == null) {
logger.info("redirect--GadgetLoginInterceptor:intercept IeUser Not Found"
+ actInvoc.getProxy().getActionName());
return "redirecthome";
}
Integer userType = webuser.getUserType().getUserTypeId();
Map<Integer, List<String>> usermap = (Map<Integer, List<String>>) application
.get("authorizations");
List<String> actions = usermap.get(userType);
if (!isAuthorized(userType, actions, actInvoc.getProxy()
.getActionName())) {
System.out.println("!isAuthorized(userType, actions, actInvoc.getProxy()getActionName()");
logger.info("GadgetLoginInterceptor:intercept"
+ actInvoc.getProxy().getActionName());
logger.info("GadgetLoginInterceptor:intercept :Not an authorized user");
return "redirecthome";
}
logger.info("GadgetLoginInterceptor:intercept -- Begininnadaddsdsnnng");
}
if (session != null) {
if (session.values().size() == 0) {
logger.info("GadgetLoginInterceptor:intercept :There is no session Available");
return "redirecthome";
}
}
logger.info("End----GadgetLoginInterceptor:intercept"
+ actInvoc.getProxy().getActionName());
return actInvoc.invoke();
}
/**
* Check given user is authorizated user or not.
*
* @param userType
* @param actions
* @param action
* @return true or false
*/
private boolean isAuthorized(Integer userType, List<String> actions,
String action) {
for (String actionName : actions) {
if (actionName.equals(action)) {
return true;
}
}
return false;
}
}
LoginAction.java:
------------------
package com.cah.trackinggadget.admin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.usertype.UserVersionType;
import com.cah.trackinggadget.gadget.GadgetManagementService;
import com.cah.trackinggadget.gadget.UserService;
import com.cah.trackinggadget.to.Gadget;
import com.cah.trackinggadget.to.Guser;
import com.cah.trackinggadget.to.UserType;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* Handles login,forgotPassword,changePassword functionalities of user.
*
* @author mounika.
*/
public class LoginAction extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 1L;
private Logger logger = Logger.getLogger(LoginAction.class);
private UserService userService;
public UsersView usersView;
private Map<String, Object> session=new HashMap<String, Object>();
private UsersView tempuser;
private Integer usertype;
private GadgetManagementService gadgetService=new GadgetManagementService();
/**
* @return the gadgetList
*/
public List<Gadget> getGadgetList() {
return gadgetList;
}
/**
* @param gadgetList
* the gadgetList to set
*/
public void setGadgetList(List<Gadget> gadgetList) {
this.gadgetList = gadgetList;
}
private List<Gadget> gadgetList = new ArrayList<Gadget>();
/**
* Puts user in session.
*
* @param sessionUser
*/
/*private void openUserSessioin(Guser sessionUser) {
Map<String, Object> context = new HashMap<String, Object>();
context.put("ideuser", sessionUser);
ActionContext.getContext().getValueStack().push(context);
session.put("ideuser", sessionUser);
try {
gadgetList = gadgetService.getAllGadgets();
} catch (Exception e) {
// TODO Auto-generated catch block
addActionMessage("Wrong Login ID or Password.");
}
if (gadgetList != null) {
session.put("ideuserplan", gadgetList.get(0).getGuser().getId());
} else {
session.put("ideuserplan", 0);
}
}*/
private void openUserSessioin(Guser sessionUser) {
System.out.println("from openUserSession methodddddddddddddddddddd");
Map<String, Object> context = new HashMap<String, Object>();
context.put("user", sessionUser);
ActionContext.getContext().getValueStack().push(context);
session.put("user", sessionUser);
Guser guser=(Guser) session.get("user");
System.out.println("from login sesionnnnnnnnnnnnnnnn "+guser.getLoginId());
try {
gadgetList = gadgetService.getAllGadgets();
} catch (Exception e) {
// TODO Auto-generated catch block
addActionMessage("Wrong Login ID or Password.");
}
}
/**
* Removes user from session.
*
* @return success if user removed else input.
*/
public String logOut() {
logger.info("Begin--LoginAction:UserLogout-Begin");
System.out.println("from logout methoddddddddddddddd");
try {
if (session != null && session.keySet() != null) {
Set<String> keys = session.keySet();
for (String key : keys) {
session.remove(key);
((org.apache.struts2.dispatcher.SessionMap) session)
.invalidate();
}
session = null;
}
} catch (Exception e) {
// e.printStackTrace();
logger.error(e.getStackTrace());
}
logger.info("End--LoginAction:UserLogout");
return SUCCESS;
}
public Integer getUsertype() {
return usertype;
}
public void setUsertype(Integer usertype) {
this.usertype = usertype;
}
/**
* Redirects to home page..
*
* @return to home page.
*/
public String passthru() {
return "loginhome";
}
/**
* Compares credentials from user with data in database,navigates to home
* page based on user type .
*
* @return success if credentials are matched else input.
*/
public String login() {
System.out.println("from login methoddddddddddddddddddddddddddd");
logger.info("Begin--LoginAction:User Login-Begin");
clearErrors();
clearMessages();
System.out.println("above get method");
Guser sessionUser = (Guser) session.get("ideuser");
System.out.println("below get method");
String result = null;
boolean loginId = false;
boolean password = false;
try {
if (sessionUser != null) {
tempuser = new UsersView();
System.out.println("from try methoddddddddddd");
tempuser.setLoginId(sessionUser.getLoginId());
/*String subtext = Encryption.decryptTwoLevel(sessionUser
.getPassword());
String subb = subtext.substring(0, subtext.indexOf("XuBL"));
String detext = Encryption.decryptOneLevel(subb);*/
System.out.println("from secongd try methoddddddddd");
tempuser.setPassword(sessionUser.getPassword());
}
if (usersView != null && tempuser == null) {
if (usersView.getLoginId().equals("")
&& usersView.getPassword().equals("")) {
addActionError("Please enter Login ID and Password.");
result = "error";
}
}
if (usersView != null || tempuser != null) {
Guser guser;
if (sessionUser == null) {
guser = userService.login(usersView);
} else {
guser = userService.login(tempuser);
}
if (guser != null) {
if (usersView != null) {
loginId = guser.getLoginId().equals(
usersView.getLoginId());
}
if (tempuser != null) {
loginId = guser.getLoginId().equals(
tempuser.getLoginId());
}
/* String decryptedPassword_TwoLevel = Encryption
.decryptTwoLevel(guser.getPassword());
String tempPassword = decryptedPassword_TwoLevel.substring(
0, decryptedPassword_TwoLevel.indexOf("XuBL"));
String password_db = Encryption
.decryptOneLevel(tempPassword);*/
if (usersView != null) {
password = (guser.getPassword()).equals(usersView.getPassword());
}
if (tempuser != null) {
password = (guser.getPassword()).equals(tempuser.getPassword());
}
}
if (guser == null && usersView != null) {
if (!(usersView.getLoginId().equals("") && usersView
.getPassword().equals(""))) {
addActionError("The Login ID and Password you entered are incorrect.\n Please try again.");
}
result = "error";
} else if (password == false && tempuser == null) {
addActionError("Please enter valid Password.");
result = "error";
} /*else if (guser.getUserType().getType().equals("BUYER")
&& guser.getStatus().equals("3")) {
result = ReturnTypeConstants.CHANGEPASSWORD;
} */
else if (guser.getUserType().getUserType().equals("admin")) {
result = "admin";
} /*else if (guser.getUserType().getType().equals("OWNER")
&& guser.getStatus().equals("3")) {
result = ReturnTypeConstants.CHANGEPASSWORD;
}*/
else {
result = "user";
}
if (guser != null) {
openUserSessioin(guser);
}
}
} catch (Exception e) {
logger.error(e.getMessage());
usersView = null;
tempuser = null;
logger.error(e.getStackTrace());
result = "input";
}
usersView = null;
tempuser = null;
logger.info("End--LoginAction:User Login -End");
return result;
}
//(or)
/*String result = null;
boolean password = true;
clearErrors();
clearActionErrors();
try {
if(usersView.getLoginId().equals("") && usersView.getPassword().equals("")){
addActionError("Please enter Login ID and Password.");
result = "input";
}
else{
Guser ieUser;
ieUser = userService.login(usersView);
if (ieUser != null) {
password = (ieUser.getPassword()).equals(
usersView.getPassword());
}
else{
addActionError("Please enter valid Login ID and Password.");
return result="input";
}
if (password == false) {
addActionError("Please enter valid Password.");
result = "input";
} else {
if (ieUser != null) {
if(ieUser.getUserType().getUserType().equals("admin")) {
result = "admin";
}
else if(ieUser.getUserType().getUserType().equals("user"))
{
System.out.println("frommmmmmmmmm userrrrrrrrrrr");
result = "user";
}
openUserSessioin(ieUser);
Guser sessionUser = (Guser) session.get("ideuser");
}
}
}
} catch (Exception e) {
e.printStackTrace();
usersView = null;
result = "INPUT";
}
usersView = null;
System.out.println("result issssssssssssss "+result);
return result;
}*/
/**
* @return the userTypes
*/
public List<UserType> getUserTypes() {
return userTypes;
}
/**
* @param userTypes
* the userTypes to set
*/
public void setUserTypes(List<UserType> userTypes) {
this.userTypes = userTypes;
}
private List<UserType> userTypes;
/**
* @return the usersView
*/
public UsersView getUsersView() {
return usersView;
}
/**
* @param usersView
* the usersView to set
*/
public void setUsersView(UsersView usersView) {
this.usersView = usersView;
}
/**
* @return the tempuser
*/
public UsersView getTempuser() {
return tempuser;
}
/**
* @param tempuser
* the tempuser to set
*/
public void setTempuser(UsersView tempuser) {
this.tempuser = tempuser;
}
/**
* @param userService
* the userService to set
*/
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public void setSession(Map<String, Object> session) {
this.session=session;
// TODO Auto-generated method stub
}
}
Autherizations.java
-------------------
package com.cah.trackinggadget.admin;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.cah.trackinggadget.dao.AuthorizationsDao;
import com.cah.trackinggadget.to.Authorizations;
/**
* Handle authorization operation.
*
* @author Asman.
*/
public class AuthorizationsLoader {
/**
* Getting authorizations.
*
* @return authorizations
*/
public List<Authorizations> loadAuthorizations() {
List<Authorizations> authorizations = null;
Session session = null;
try {
SessionFactory sessionFactory = new AnnotationConfiguration()
.configure("/hibernate.cfg.xml").buildSessionFactory();
session = sessionFactory.openSession();
AuthorizationsDao authoDao = new AuthorizationsDao();
authorizations = authoDao.getAllAuthorizations(session);
session.close();
} catch (HibernateException he) {
// he.printStackTrace();
}
return authorizations;
}
public static void main(String s[]) {
}
}
UsersView.java
-----------------
package com.cah.trackinggadget.admin;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
* Handles users related information.
*/
public class UsersView {
private String loginId;
private String password;
/**
* @return the loginId
*/
public String getLoginId() {
return loginId;
}
/**
* @param loginId the loginId to set
*/
public void setLoginId(String loginId) {
this.loginId = loginId;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
This program need actions and autherization tables
No comments:
Post a Comment