⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 securityaction.java

📁 关于 Jaoso新闻文章发布系统 --- --- --- --- --- --- --- --- --- --- --- --- --- -- 版本信息:Jaoso新闻文章发布系统 0.9.1b
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package jaoso.framework.web.action;

import jaoso.framework.domain.Account;
import jaoso.framework.domain.Right;
import jaoso.framework.domain.Role;

import jaoso.framework.exception.AccountAlreadyExistException;
import jaoso.framework.exception.BusinessException;
import jaoso.framework.exception.GroupExistException;
import jaoso.framework.exception.RightExistException;

import jaoso.framework.service.SecurityService;

import jaoso.framework.util.MyUtils;

import org.apache.commons.beanutils.BeanUtils;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.validator.DynaValidatorForm;

import java.io.IOException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

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


/**
 * security action include login logout register and no right
 *
 * @author Edgeloner
 */
public class SecurityAction extends BaseAction {

    //~ Instance fields ========================================================

    /** get manager service */
    private SecurityService securityService = getServiceLocator()
                                                  .getSecurityService();

    //~ Methods ================================================================

    /**
     * change account role
     *
     * @param mapping actionMapping
     * @param form actionForm
     * @param request http request
     * @param response http response
     *
     * @return actionforward
     *
     * @throws IOException IO error
     * @throws ServletException any error
     */
    public final ActionForward changeAccountGroup(final ActionMapping mapping,
        final ActionForm form, final HttpServletRequest request,
        final HttpServletResponse response)
        throws IOException, ServletException {

        ActionErrors errors = new ActionErrors();
        String accountId = request.getParameter("accountId");
        String groupId = request.getParameter("groupId");

        try {

            securityService.changeAccountRole(accountId, groupId);
        } catch (BusinessException e) {

            log.error("changeAccountGroup error: " + e);
            errors.add("changeAccountGroup",
                new ActionError("errors.UnKnowError"));
        }

        if (!errors.isEmpty()) {

            saveErrors(request, errors);

            return mapping.findForward("faile");
        } else {

            removeAttribute(mapping, request);

            return (mapping.findForward("success"));
        }
    }

    /**
     * create new right
     *
     * @param mapping actionMapping
     * @param form actionForm
     * @param request http request
     * @param response http response
     *
     * @return actionforward
     *
     * @throws IOException IO error
     * @throws ServletException any error
     */
    public final ActionForward createRight(final ActionMapping mapping,
        final ActionForm form, final HttpServletRequest request,
        final HttpServletResponse response)
        throws IOException, ServletException {

        ActionErrors errors = new ActionErrors();
        Right right = new Right();

        try {

            BeanUtils.copyProperties(right, form);
        } catch (Exception e) {

            log.error("copy form to right error: " + e);
            errors.add("create right", new ActionError("errors.UnKnowError"));
        }

        try {

            securityService.createRight(right);
        } catch (BusinessException e) {

            log.error("create right error: " + e);
            errors.add("create right", new ActionError("errors.UnKnowError"));
        } catch (RightExistException e) {

            errors.add("create right", new ActionError("errors.RightExist"));
        }

        if (!errors.isEmpty()) {

            saveErrors(request, errors);

            return mapping.findForward("faile");
        } else {

            removeAttribute(mapping, request);

            return (mapping.findForward("success"));
        }
    }

    /**
     * create a new group
     *
     * @param mapping actionMapping
     * @param form actionForm
     * @param request http request
     * @param response http response
     *
     * @return actionforward
     *
     * @throws IOException IO error
     * @throws ServletException any error
     */
    public final ActionForward createRole(final ActionMapping mapping,
        final ActionForm form, final HttpServletRequest request,
        final HttpServletResponse response)
        throws IOException, ServletException {

        ActionErrors errors = new ActionErrors();
        Role group = (Role) ((DynaActionForm) form).get("group");
        String[] rights = request.getParameterValues("right");

        try {

            securityService.createRole(group, rights);
        } catch (BusinessException e) {

            log.error("create group error: " + e);
            errors.add("create right", new ActionError("errors.UnKnowError"));
        } catch (GroupExistException e) {

            errors.add("create right", new ActionError("errors.GroupExist"));
        }

        if (!errors.isEmpty()) {

            saveErrors(request, errors);

            return mapping.findForward("faile");
        } else {

            removeAttribute(mapping, request);

            return (mapping.findForward("success"));
        }
    }

    /**
     * list all account
     *
     * @param mapping mapping
     * @param form form
     * @param request request
     * @param response response
     *
     * @return ActionForward
     *
     * @throws IOException IOException
     * @throws ServletException ServletException
     */
    public final ActionForward listAccount(final ActionMapping mapping,
        final ActionForm form, final HttpServletRequest request,
        final HttpServletResponse response)
        throws IOException, ServletException {

        ActionErrors errors = new ActionErrors();
        Account[] accounts = null;
        Role[] groups = null;

        try {

            accounts = securityService.findAllAccount();

            for (int i = 0, n = accounts.length; i < n; i++) {

                if (accounts[i].getGroup() == null) {

                    accounts[i].setGroup(new Role());
                }
            }

            groups = securityService.findAllGroup();
        } catch (BusinessException e) {

            log.error("listAccount error: " + e);
            errors.add("listAccount", new ActionError("errors.UnKnowError"));
        }

        if (!errors.isEmpty()) {

            saveErrors(request, errors);

            return mapping.findForward("faile");
        }

        removeAttribute(mapping, request);
        request.setAttribute("accounts", accounts);
        request.setAttribute("groups", groups);

        return mapping.findForward("success");
    }

    /**
     * list all right
     *
     * @param mapping actionMapping
     * @param form actionForm
     * @param request http request
     * @param response http response
     *
     * @return actionforward
     *
     * @throws IOException IO error
     * @throws ServletException any error
     */
    public final ActionForward listRight(final ActionMapping mapping,
        final ActionForm form, final HttpServletRequest request,
        final HttpServletResponse response)
        throws IOException, ServletException {

        ActionErrors errors = new ActionErrors();
        Right[] rights = null;

        try {

            rights = securityService.findAllRight();
        } catch (BusinessException e) {

            log.error("listAccount error: " + e);
            errors.add("listAccount", new ActionError("errors.UnKnowError"));
        }

        if (!errors.isEmpty()) {

            saveErrors(request, errors);

            return mapping.findForward("faile");
        }

        request.setAttribute("rights", rights);

        return mapping.findForward("success");
    }

    /**
     * list all groups
     *
     * @param mapping action mapping
     * @param form action form
     * @param request http request
     * @param response http response
     *
     * @return action forward
     *
     * @throws IOException io error
     * @throws ServletException servlet exception
     */
    public final ActionForward listRole(final ActionMapping mapping,
        final ActionForm form, final HttpServletRequest request,
        final HttpServletResponse response)
        throws IOException, ServletException {

        ActionErrors errors = new ActionErrors();
        Role[] groups = null;
        Right[] rights = null;

        try {

            groups = securityService.findAllGroup();
            rights = securityService.findAllRight();
        } catch (BusinessException e) {

            log.error("listAccount error: " + e);
            errors.add("listAccount", new ActionError("errors.UnKnowError"));
        }

        if (!errors.isEmpty()) {

            saveErrors(request, errors);

            return mapping.findForward("faile");
        }

        request.setAttribute("groups", groups);
        request.setAttribute("rights", rights);

        return mapping.findForward("success");
    }

    /**
     * login system
     *
     * @param mapping actionMapping
     * @param form actionForm
     * @param request http request
     * @param response http response
     *
     * @return actionforward
     *
     * @throws IOException IO error
     * @throws ServletException any error
     */
    public final ActionForward login(final ActionMapping mapping,
        final ActionForm form, final HttpServletRequest request,
        final HttpServletResponse response)
        throws IOException, ServletException {

        HttpSession session = request.getSession(false);

        if (session == null) {

            session = request.getSession(true);
        }

        ActionErrors errors = new ActionErrors();
        DynaActionForm loginForm = (DynaActionForm) form;
        String userName = (String) loginForm.get("name");
        String password = (String) loginForm.get("password");
/*
        if (!request.getSession()
                        .getAttribute("chkCode")
                        .equals(request.getParameter("chkCode"))) {

            errors.add(ActionErrors.GLOBAL_ERROR,
                new ActionError("errors.chkCodeNoMatch"));
            saveErrors(request, errors);

            return mapping.findForward("faile");
        }
*/
        Account account = null;

        try {

            account = securityService.login(userName, password);

            if ((account == null)
                    || (account.getGroup() == null)
                    || MyUtils.isBlank(account.getGroup().getId())) {

                errors.add("User login", new ActionError("errors.LoginError"));
            }
        } catch (BusinessException e) {

            errors.add("User login", new ActionError("errors.LoginError"));
        }

        // Report any errors we have discovered back to the original form
        if (!errors.isEmpty()) {

            saveErrors(request, errors);

            return mapping.findForward("faile");
        }

        // Remove the obsolete form bean
        removeAttribute(mapping, request);

        //Set account Object to session
        session.setAttribute("account", account.getId());
        session.setAttribute("group", account.getGroup().getId());

        final String destinatedUrl = request.getParameter("DEST_URL");

        // If there is a destinated url,forward to this url
        if ((destinatedUrl != null) && (destinatedUrl.length() > 0)) {

            return new ActionForward(destinatedUrl);
        } else {

            // Forward control to the specified success URI
            return (mapping.findForward("success"));
        }
    }

    /**
     * logout system
     *
     * @param mapping mapping
     * @param form form
     * @param request request
     * @param response response
     *
     * @return ActionForward
     *
     * @throws IOException IOException
     * @throws ServletException ServletException
     */
    public final ActionForward logout(final ActionMapping mapping,
        final ActionForm form, final HttpServletRequest request,
        final HttpServletResponse response)
        throws IOException, ServletException {

        if (request.getSession(false) != null) {

            request.getSession(false)
                   .invalidate();
        }

        // Remove the obsolete form bean
        removeAttribute(mapping, request);

        return mapping.findForward("success");
    }

    /**
     * no right
     *
     * @param mapping mapping
     * @param form form
     * @param request request
     * @param response response
     *
     * @return ActionForward
     *

⌨️ 快捷键说明

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