entityaccesstag.java

来自「开源项目CRM之OpenCustomer」· Java 代码 · 共 132 行

JAVA
132
字号
/*******************************************************************************
 * ***** BEGIN LICENSE BLOCK Version: MPL 1.1
 * 
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (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.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * 
 * The Original Code is the OpenCustomer CRM.
 * 
 * The Initial Developer of the Original Code is Thomas Bader (Bader & Jene
 * Software-Ingenieurb黵o). Portions created by the Initial Developer are
 * Copyright (C) 2005 the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s): Thomas Bader <thomas.bader@bader-jene.de>
 * 
 * ***** END LICENSE BLOCK *****
 */

package org.opencustomer.web.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;
import org.opencustomer.application.db.vo.system.UserVO;
import org.opencustomer.application.web.Globals;
import org.opencustomer.db.EntityAccessUtility;
import org.opencustomer.db.vo.EntityAccess;

public class EntityAccessTag extends TagSupport
{
    private static final long serialVersionUID = 3257567287094620467L;

    private final static Logger log = Logger.getLogger(EntityAccessTag.class);

    private static final String DELIMITER = ",";

    private String scope;

    private String name;

    private EntityAccess.Access access;

    @Override
    public int doStartTag() throws JspException
    {
        if (condition())
            return EVAL_BODY_INCLUDE;
        else
            return SKIP_BODY;
    }

    @Override
    public int doEndTag() throws JspException
    {
        return EVAL_PAGE;
    }

    @Override
    public void release()
    {
        super.release();
        name = null;
        scope = null;
        access = null;
    }

    private boolean condition()
    {
        boolean hasAccess = false;

        if (access == null)
            hasAccess = true;
        else
        {
            UserVO user = (UserVO) pageContext.getSession().getAttribute(Globals.USER_KEY);

            EntityAccess entity = null;
            if (scope == null)
                entity = (EntityAccess) pageContext.findAttribute(name);
            else if (scope.equals("page"))
                entity = (EntityAccess) pageContext.getAttribute(name);
            else if (scope.equals("request"))
                entity = (EntityAccess) pageContext.getRequest().getAttribute(name);
            else if (scope.equals("session"))
                entity = (EntityAccess) pageContext.getSession().getAttribute(name);
            else if (scope.equals("application"))
                entity = (EntityAccess) pageContext.getServletContext().getAttribute(name);

            if (EntityAccessUtility.isAccessGranted(user, entity, access))
                hasAccess = true;
        }

        return hasAccess;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getScope()
    {
        return scope;
    }

    public void setScope(String scope)
    {
        this.scope = scope;
    }

    public EntityAccess.Access getAccess()
    {
        return access;
    }

    public void setAccess(EntityAccess.Access access)
    {
        this.access = access;
    }

}

⌨️ 快捷键说明

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