com.pureinfo.ark.auth2.model.AllowDef.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.ark.auth2.model.AllowDef.java

Source

/**
 * PureInfo Ark
 * @(#)AllowDef.java   1.0 2006-9-29
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.ark.auth2.model;

import java.util.List;

import org.dom4j.Element;

import com.pureinfo.ark.auth.model.IUser;
import com.pureinfo.ark.content.model.ArkContent;
import com.pureinfo.dolphin.mapping.EntityMetadata;
import com.pureinfo.dolphin.query.logic.ISQLLogic;
import com.pureinfo.dolphin.query.logic.SQLLogicString;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.lang.BitsValue;
import com.pureinfo.force.xml.IXMLSupporter;
import com.pureinfo.force.xml.XMLUtil;

/**
 * <P>
 * Created on 2006-9-29 22:01:31 <BR>
 * Last modified on 2006-9-29
 * </P>
 * AllowDef: definition for rights allowed.
 * 
 * @author Why
 * @version 1.0, 2006-9-29
 * @since Ark 1.2
 */
public class AllowDef implements IXMLSupporter {
    //context var
    private EntityMetadata m_resourceMetadata;

    //properties
    private String m_sActions;

    private RuleDef[] m_rules;

    //cached data
    private BitsValue m_rightValue;

    /**
     * Constructor: default
     */
    public AllowDef(EntityMetadata _resourceMetadata) {
        super();
        this.setResourceMetadata(_resourceMetadata);
    }

    /**
     * Returns the resourceMetadata.
     * 
     * @return the resourceMetadata.
     */
    public EntityMetadata getResourceMetadata() {
        return m_resourceMetadata;
    }

    /**
     * Sets the resourceMetadata.
     * 
     * @param _resourceMetadata
     *            the resourceMetadata to set.
     */
    public void setResourceMetadata(EntityMetadata _resourceMetadata) {
        m_resourceMetadata = _resourceMetadata;
    }

    /**
     * Returns the actions.
     * 
     * @return the actions.
     */
    public String getActions() {
        return m_sActions;
    }

    /**
     * Sets the actions.
     * 
     * @param _sActions
     *            the actions to set.
     */
    public void setActions(String _sActions) {
        m_sActions = _sActions;
    }

    /**
     * Returns the rightValue.
     * 
     * @return the rightValue.
     */
    public BitsValue getRightValue() {
        return m_rightValue;
    }

    /**
     * Sets the rightValue.
     * 
     * @param _rightValue
     *            the rightValue to set.
     */
    public void setRightValue(BitsValue _rightValue) {
        m_rightValue = _rightValue;
    }

    /**
     * Returns the rules.
     * 
     * @return the rules.
     */
    public RuleDef[] getRules() {
        return m_rules;
    }

    /**
     * Sets the rules.
     * 
     * @param _rules
     *            the rules to set.
     */
    public void setRules(RuleDef[] _rules) {
        m_rules = _rules;
    }

    //=========================================================================
    //login interface for authorization

    /**
     * Returns <code>true</code> if the user has right to do the action on the
     * specified resource.
     * 
     * @param _loginUser
     *            the current login user
     * @param _resource
     *            the resource to be operated
     * @param _nActionIndex
     *            index of the action
     * @return <code>null</code> if no definition of the specified action;
     *         <code>Boolean.TRUE</code> if the user has right to do the
     *         action on the specified resource; <code>Boolean.FALSE</code>,
     *         otherwise.
     * @throws PureException
     *             if failed.
     */
    public Boolean hasRight(IUser _loginUser, ArkContent _resource, int _nActionIndex) throws PureException {
        //1. to check the allowed actions list
        if (!hasEntry(_nActionIndex))
            return null; //undefined

        //2. to check the rule
        if (m_rules == null || m_rules.length == 0)
            return Boolean.TRUE;

        //else, to check if any rule can be matched
        for (int i = 0; i < m_rules.length; i++) {
            if (m_rules[i].test(_loginUser, _resource))
                return Boolean.TRUE;
        }

        //finally, no rule has been passed.
        return Boolean.FALSE;
    }

    /**
     * Returns <code>true</code> if there is path to do the action on the
     * specified resource.
     * 
     * @param _loginUser
     *            the current login user
     * @param _resource
     *            the resource to be operated
     * @param _nActionIndex
     *            index of the action
     * @return <code>null</code> if no definition of the specified action;
     *         <code>Boolean.TRUE</code> if there is path to do the action on
     *         the specified resource; <code>Boolean.FALSE</code>, otherwise.
     * @throws PureException
     *             if failed.
     */
    public Boolean hasPath(IUser _loginUser, ArkContent _resource, int _nActionIndex) throws PureException {
        // 1. to check the allowed actions list
        if (!hasEntry(_nActionIndex))
            return null; // undefined

        // 2. to check the rule
        if (m_rules == null || m_rules.length == 0)
            return Boolean.TRUE;

        // else, to check if any rule can be matched
        for (int i = 0; i < m_rules.length; i++) {
            if (m_rules[i].hasPath(_loginUser, _resource))
                return Boolean.TRUE;
        }

        // finally, no rule has been passed.
        return Boolean.FALSE;
    }

    /**
     * Returns <code>true</code> if there is entry (ignore detailed rules) to
     * do the action on the specified resource.
     * 
     * @param _nActionIndex
     *            index of the action
     * @return <code>null</code> if no definition of the specified action;
     *         <code>Boolean.TRUE</code> if there is entry to do the
     *         action on the specified resource; <code>Boolean.FALSE</code>,
     *         otherwise.
     * @throws PureException
     *             if failed.
     */
    public boolean hasEntry(int _nActionIndex) throws PureException {
        return m_rightValue.get(_nActionIndex);
    }

    /**
     * Returns the restriction rule as SQL logic when the user do the specified
     * action on the resource.
     * 
     * @param _loginUser
     *            the current user
     * @param _nActionIndex
     *            index of the action
     * @return <code>null</code>, if no definition of the specified action;;
     *         othwise, the restriction rule as SQL logic when the user do the
     *         specified action on the resource.
     * @throws PureException
     *             if failed to render the logic.
     */
    public ISQLLogic getSQLLogicRule(IUser _loginUser, int _nActionIndex) throws PureException {
        if (!hasEntry(_nActionIndex))
            return null; //undefined

        //else
        if (m_rules == null)
            return SQLLogicString.TRUE;

        //else
        return RuleDef.getSQLLogicRule(_loginUser, m_rules);
    }

    //=========================================================================
    //implementation for IXMLSupporter

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        _element.addAttribute("action", m_sActions);
        if (m_rules != null) {
            for (int i = 0; i < m_rules.length; i++) {
                _element.add(XMLUtil.toXMLElement(m_rules[i], "rule"));
            }
        }
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        this.setActions(XMLUtil.getAttributeValueTrim(_element, "action"));

        List list = _element.elements("rule");
        if (list == null || list.isEmpty()) {
            m_rules = null;
        } else {
            try {
                m_rules = new RuleDef[list.size()];
                RuleDef ruleDef;
                for (int i = 0; i < list.size(); i++) {
                    ruleDef = new RuleDef(m_resourceMetadata);
                    ruleDef.fromXML((Element) list.get(i));
                    m_rules[i] = ruleDef;
                }
            } finally {
                list.clear();
            }
        } //endif
    }

}