Java tutorial
/** * PureInfo Ark * @(#)RightDefBase.java 1.0 2006-10-5 * * 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.force.exception.PureException; import com.pureinfo.force.xml.IXMLSupporter; import com.pureinfo.force.xml.XMLUtil; /** * <P> * Created on 2006-10-5 23:08:17 <BR> * Last modified on 2006-10-5 * </P> * RightDefBase: base implementation for RightDef and CaseDef. * * @author Why * @version 1.0, 2006-10-5 * @since Ark 1.2 */ public class RightDefBase implements IXMLSupporter { private EntityMetadata m_resourceMetadata; private AllowDef[] m_allows; private SwitchDef m_switch; /** * Constructor * * @param _resourceMetadata * the resource entity metadata. */ public RightDefBase(EntityMetadata _resourceMetadata) { super(); this.setResourceMetadata(_resourceMetadata); } /** * Returns the resource metadata. * * @return the resource metadata. */ public EntityMetadata getResourceMetadata() { return m_resourceMetadata; } /** * Sets the resource metadata. * * @param _resourceMetadata * the resource metadata to set. */ public void setResourceMetadata(EntityMetadata _resourceMetadata) { m_resourceMetadata = _resourceMetadata; } /** * Returns the allows. * * @return the allows. */ public AllowDef[] getAllows() { return m_allows; } /** * Sets the allows. * * @param _allows * the allows to set. */ public void setAllows(AllowDef[] _allows) { m_allows = _allows; } /** * Returns the switch. * * @return the switch. */ public SwitchDef getSwitch() { return m_switch; } /** * Sets the switch. * * @param _switch * the switch to set. */ public void setSwitch(SwitchDef _switch) { m_switch = _switch; } //========================================================================= //logic interface for authorization /** * Parses the allowed action right as bits-value. * * @param _actionDictionary * the action dictionary to lookup the action. * */ public void parseRight(IActionDictionary _actionDictionary) throws PureException { AllowDef allow; if (m_allows != null) { for (int i = 0; i < m_allows.length; i++) { allow = m_allows[i]; allow.setRightValue(_actionDictionary.parseRight(allow.getActions())); } } if (m_switch != null) { m_switch.parseRight(_actionDictionary); } } /** * 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 { if (m_allows != null) { for (int i = 0; i < m_allows.length; i++) { Boolean temp = m_allows[i].hasRight(_loginUser, _resource, _nActionIndex); if (temp != null) return temp; } } if (m_switch != null) { return m_switch.hasRight(_loginUser, _resource, _nActionIndex); } return null; //un-defined } /** * Returns <code>true</code> if there is path (ignore detailed rules) 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 { if (m_allows != null) { for (int i = 0; i < m_allows.length; i++) { Boolean temp = m_allows[i].hasPath(_loginUser, _resource, _nActionIndex); if (temp != null) return temp; } } if (m_switch != null) { return m_switch.hasPath(_loginUser, _resource, _nActionIndex); } return null; // undefined } /** * Returns <code>true</code> if there is entry (ignore detailed rules) to * do the action on the specified resource. * * @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 entry to do the * action on the specified resource; <code>Boolean.FALSE</code>, * otherwise. * @throws PureException * if failed. */ public Boolean hasEntry(ArkContent _resource, int _nActionIndex) throws PureException { if (m_allows != null) { for (int i = 0; i < m_allows.length; i++) { if (m_allows[i].hasEntry(_nActionIndex)) return Boolean.TRUE; } } if (m_switch != null) { return m_switch.hasEntry(_resource, _nActionIndex); } return null; //undefined } /** * Returns the restriction rule as SQL logic when the user do the specified * action on the resource. * * @param _loginUser * the current 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;; * 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, ArkContent _resource, int _nActionIndex) throws PureException { if (m_allows != null) { for (int i = 0; i < m_allows.length; i++) { ISQLLogic logic = m_allows[i].getSQLLogicRule(_loginUser, _nActionIndex); if (logic != null) { return logic; } } } if (m_switch != null) { return m_switch.getSQLLogicRule(_loginUser, _resource, _nActionIndex); } //else, the current user has no entry. return null; //undefined } //========================================================================= //implementation for IXMLSupporter /** * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element) */ public void toXMLElement(Element _element) throws PureException { if (m_allows != null) { for (int i = 0; i < m_allows.length; i++) { _element.add(XMLUtil.toXMLElement(m_allows[i], "allow")); } } if (m_switch != null) { _element.add(XMLUtil.toXMLElement(m_switch, "switch")); } } /** * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element) */ public void fromXML(Element _element) throws PureException { //to read allows List list = _element.elements("allow"); if (list == null || list.isEmpty()) { m_allows = null; } else { try { m_allows = new AllowDef[list.size()]; AllowDef allowDef; for (int i = 0; i < list.size(); i++) { allowDef = new AllowDef(m_resourceMetadata); allowDef.fromXML((Element) list.get(i)); m_allows[i] = allowDef; } } finally { list.clear(); } } //to read switch Element ele = _element.element("switch"); if (ele == null) { m_switch = null; } else { m_switch = new SwitchDef(m_resourceMetadata); m_switch.fromXML(ele); } } }