0

I have struts based application in which I wrote a method which authenticate user,after authentication it redirects to the dashboard.action,in my application there are 3 or more modules like uploaddocument.action, reportsection.action, downloaddocument.action.

I want to make this code generic so user can gain directly access from any of the page, currently it is implemented only in loginaction, that means if user tries to access action other than dashboard, he will encounterd and an unauthorized error.

In order to allow user gain access from any where,I will need to write three more action classes and implement the same method, because each time i need to return "success" and on behalf of that I will redirect the action to their intended action class.

So I want to avoid the same. I am looking for some sort of generic implementation which make this method universal and I don't need to write three more classes, this will avoid the lots of boiler plate code.

FLOW

hit localhost:8080/lcsd--->authenticatAD()--success--> dashboard.action

REQUIREMENT:

hit localhost:8080/lcsd/upload--->authenticatAD()--success--> uploaddocument.action


  hit localhost:8080/lcsd/report--->authenticatAD()--success--> reportdocument.action


 hit localhost:8080/lcsd/download--->authenticatAD()--success--> downloaddocument.action

the method define below:-

public String authenticateAD() {
        logger.debug("Start authenticateAD=" + System.currentTimeMillis());
        setPage("login");
        setRole("login");
        boolean validate = true;
        userId = getServletRequest().getRemoteUser();
        if (userId == null) {
            return "error";
        }
        if (userId.lastIndexOf("\\") > 0) {
            userId = userId.substring(userId.lastIndexOf("\\") + 1).trim();
        }
        logger.debug("UserId=" + userId);
        if (validate) {
            LDAPIntegration authentication = new LDAPIntegration();
            String group = "";
            try {
                List<String> groupList = new ArrayList();
                groupList = authentication.getUserGroups(userId);
                String role = "";
                if ((groupList != null) && (!groupList.isEmpty())) {
                    List<String> interList = new ArrayList<String>();
                    for (int i = 0; i < groupList.size(); i++) {
                        group = groupList.get(i);
                        logger.info("groupList group:" + group);
                        StringTokenizer st = new StringTokenizer(group);
                        while (st.hasMoreTokens()) {
                            if (st.countTokens() == 1) {
                                interList.add(st.nextToken());
                            } else {
                                st.nextToken();
                            }
                        }
                    }
                    Collections.sort(interList);
                    logger.info("Sorted InnerList of Role: " + interList.toString());
                    if (interList.contains("Administrators"))
                        role = "Administrators";
                    else if (interList.contains("Operators"))
                        role = "Operators";
                    else if (interList.contains("Guests") && interList.size() == 1)
                        role = "Guests";
                    else
                        for (int j = 0; j < interList.size(); j++) {
                            if (!interList.get(j).equalsIgnoreCase("Guests"))
                                role = interList.get(j);
                        }
                    for (int k = 0; k < groupList.size(); k++) {
                        if (!groupList.get(k).contains("Administrators") || !groupList.get(k).contains("Operators")
                                || !groupList.get(k).contains("Guests"))
                            group = groupList.get(k);
                    }
                    /* group = (String)groupList.get(0); */
                    logger.debug("groupList group:" + group);
                    /*
                     * StringTokenizer st = new StringTokenizer(group); while (st.hasMoreTokens()) {
                     * if (st.countTokens() == 1) { role = st.nextToken(); } else { st.nextToken();
                     * } }
                     */
                } else {
                    throw new CustomException("INVALID_USER");
                }
                if (!role.toLowerCase().equalsIgnoreCase("Administrators".toLowerCase()))
                    if (!role.toLowerCase().equalsIgnoreCase("Operators".toLowerCase()))
                        if (!role.toLowerCase().equalsIgnoreCase("Guests".toLowerCase())) {
                            if (group.toLowerCase().indexOf("area manager".toLowerCase()) != -1) {
                                String areaName = null;
                                List<String> areaNameList = new ArrayList();

                                for (int i = 0; i < groupList.size(); i++) {
                                    group = (String) groupList.get(i);
                                    if (group.toLowerCase().indexOf("area manager".toLowerCase()) != -1) {
                                        logger.debug("areamanager groupList group:" + group);
                                        if (group.toLowerCase().contains("area manager-")) {
                                            areaName = group
                                                    .substring(group.indexOf("Download Area") + 14, group.length())
                                                    .trim();
                                        } else {
                                            areaName = group
                                                    .substring(group.indexOf("Area Manager") + 12, group.length())
                                                    .trim();
                                        }

                                        logger.info("areaName:" + areaName);
                                        if (areaName != null) {
                                            areaNameList.add(areaName);
                                        }
                                    }
                                }
                                logger.debug("areaNameList===" + areaNameList);
                                setArea(areaNameList);
                                role = "area manager";
                            } else {
                                throw new CustomException("INVALID_ROLE");
                            }
                        }
                setUserGroup(groupList);
                logger.info("authenticateAD groupList: " + groupList);
                logger.info("authenticateAD Role: " + role);
                logger.info("authenticateAD userId: " + userId);
                setRole(role);
                setUser(userId);
                logger.info("Get Methods in authenticateAD !!");
                logger.info("authenticateAD Role: " + getRole());
                logger.info("authenticateAD user: " + getUser());
                logger.info("authenticateAD userId: " + getUserId());
                logger.info("authenticateAD group List: " + getUserGroup().toString());
                logger.debug("Get Methods in authenticateAD !!");
                logger.debug("authenticateAD Role: " + getRole());
                logger.debug("authenticateAD user: " + getUser());
                logger.debug("authenticateAD userId: " + getUserId());
                logger.debug("authenticateAD group List: " + getUserGroup().toString());
                logger.debug("End authenticateAD=" + System.currentTimeMillis());
                return "success";
            } catch (CustomException e) {
                logger.error("error in authenticateAD ", e);
                if (e.getMessage().equalsIgnoreCase("NETWORK_ERROR")) {
                    addActionError(manager.getValue("login.network.error"));
                } else if (e.getMessage().equalsIgnoreCase("INVALID_USER")) {
                    addActionError(manager.getValue("login.invalid.user"));
                } else if (e.getMessage().equalsIgnoreCase("INVALID_ROLE")) {
                    addActionError(manager.getValue("login.invalid.role"));
                }
            }
        }

        return "error";
    }
user9634982
  • 565
  • 5
  • 24

0 Answers0