class_RightManagementBC.inc.php 12.5 KB
<?php
/**
 * @file	RightManagementBC.inc.php
 * @category freeSN
 * @mailto	code [at] netz.coop
 * @version 0.4.200901
 * @link http://netz.coop
 * 
 *  @copyright Copyright by netz.coop e.G. 2015
 *  
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
//* alle dürfen lesen /schreiben				:	a	all
//* angemeldete dürfen lesen / schreiben		:	u	user
//* nur schreib/lese -berechtigte dürfen		:	r	right
class RightManagementBC extends aLosp {
	protected $PartOfObj = null;
	protected $PartOfObj_BasisClipboards = null;
	protected $PartOfObj_ClassName = null;
	protected $DBRightManagementValues = array();
	private	$CONFIG_RightManagement =null;
	/**
	 *
	 * @var array BasisClipbords Rechte, wird von RightManagementBC::getRightManagementValues generiert
	 *			beinhaltet alle möglichen BasisClipboards mit ihren jeweiligen Rechten
	 */
	protected   $RightManagementValues = null;

	private function __construct(iStorable &$param_DB, &$param_Obj) {
		$this->PartOfObj =& $param_Obj;
		$this->ThisClassName = get_class($this);
		$this->DB =& $param_DB;
		$this->PartOfObj_BasisClipboards = $this->PartOfObj->getObjVar('BasisClipboards');
		$this->PartOfObj_ClassName = $this->PartOfObj->ThisClassName;
		//$this->CONFIG_RightManagement = CONFIG::getConfig('RightManagement');
		$this->CONFIG_RightManagement = CONFIG::getConfigRightManagement($this->PartOfObj->ThisClassName);
	}

	public static function createNewInstance(iStorable &$param_DB, &$param_Obj) {
		return new RightManagementBC($param_DB, $param_Obj);
	}

	public function loadObject(Account &$param_HostAccount, iStorable &$param_iStorable, $param_Parameter=null) {
		if(parent::loadObject($param_HostAccount, $param_iStorable, $param_Parameter)) {
			$this->loadRightManagementValues();
			return $this;
		} else {
			return false;
		}
	}

	public function getBasisClipboardWriteArray(Account &$param_HostAccount, iStorable &$param_iStorable) {
		$this->mkBasisClipboardWriteArray($param_HostAccount, $param_iStorable);
		return $this->BasisClipboardWriteArray;
	}

	public function checkIfPossibleAddToBC(Account &$param_HostAccount, iStorable &$param_iStorable, $param_BasisClipboardName) {
		$this->mkBasisClipboardWriteArray($param_HostAccount, $param_iStorable);

		if($param_BasisClipboardName && is_array( $this->BasisClipboardWriteArray) && FALSE !=array_search($param_BasisClipboardName, $this->BasisClipboardWriteArray)) {
			return true;
		} else {
			return false;
		}

	}

	private static $Cache_BasisClipboardWriteArray = array();
	private $BasisClipboardWriteArray=null;

	/**
	 *	@todo sollte demnächst mal auf php6 SplFixedArray klasse umgestellt werden, macht leider einfach so fehler 100427 @f
	 *
	 * @param Account $param_HostAccount
	 * @param iStorable $param_iStorable
	 */
	private function mkBasisClipboardWriteArray(Account &$param_HostAccount, iStorable &$param_iStorable) {

		$var_PartOfID = $this->PartOfObj->getObjVar('ID');
		if(!array_key_exists($var_PartOfID, self::$Cache_BasisClipboardWriteArray)) {
			$var_CONFIG_Data = & $this->PartOfObj->getObjVar('CONFIG_Data');
			
			self::$Cache_BasisClipboardWriteArray[$var_PartOfID] = array();
			/**
			 * on BasisClipboard is possible to add BasisElement's
			 */
			if(is_array($this->PartOfObj_BasisClipboards)) {
//				self::$Cache_BasisClipboardWriteArray[$var_PartOfID] = new SplFixedArray(count($this->PartOfObj_BasisClipboards));
				$var_HasBasisClipboards = $this->PartOfObj->getObjVar('HasBasisClipboards') ;

				$i=0;
				foreach($this->PartOfObj_BasisClipboards as $var_key => $var_value) {
					if($var_key) {
						if( is_array($var_CONFIG_Data) && array_key_exists('BasisClipboards', $var_CONFIG_Data)
							&&
							array_key_exists($var_key, $var_CONFIG_Data['BasisClipboards'])
							&&
							is_array($var_CONFIG_Data['BasisClipboards'][$var_key])
							&&
							array_key_exists('Singleton', $var_CONFIG_Data['BasisClipboards'][$var_key])
							&&
							$var_CONFIG_Data['BasisClipboards'][$var_key]['Singleton']
							&&
							array_search($var_key, $var_HasBasisClipboards)!==false) {

						} else if($param_HostAccount) {
//D::li($var_PartOfID.'->'.$var_key);
							foreach($param_HostAccount->getPossibleProfileIDArray() as $var_PossibleRequestPID => $var_PossiblePArray) {
								if( $this->checkWriteBC($var_key, $var_PossibleRequestPID, true)) {
//D::ulli('ok');
									self::$Cache_BasisClipboardWriteArray[$var_PartOfID][$i++] = $var_key;
									break;
								}
							}
						}
					}
				}
			} else {
//				self::$Cache_BasisClipboardWriteArray[$var_PartOfID] = new SplFixedArray(0);
			}
			

		}
		$this->BasisClipboardWriteArray =& self::$Cache_BasisClipboardWriteArray[$var_PartOfID];
	}


	/**
	 *
	 * @param array $param_PartOfObj_BasisClipboards	Rechtepriorität
	 * @param array $param_DBRightManagementValues		Rechtepriorität
	 * @param array $param_CONFIG_RightManagementBC		Rechtepriorität
	 * @return array
	 */
	private static function &getRightManagementValues($param_PartOfObj_BasisClipboards, $param_DBRightManagementValues, array $param_CONFIG_RightManagementBC) {
		//D::show($param_PartOfObj_BasisClipboards, '$param_PartOfObj_BasisClipboards');
		$var_CONFIG_BCReadRight = $param_CONFIG_RightManagementBC['Default_BCReadRight'];
		$var_CONFIG_BCWriteRight = $param_CONFIG_RightManagementBC['Default_BCWriteRight'];

		$var_result = array();
		if(is_array($param_PartOfObj_BasisClipboards)) {
			foreach($param_PartOfObj_BasisClipboards as $var_BasisClipboardName => $var_array) {
				if(array_key_exists($var_BasisClipboardName, $param_DBRightManagementValues)) {

					$var_Default_BCReadRight = $param_DBRightManagementValues[$var_BasisClipboardName]['ReadAuthorisation'];
					$var_Default_BCWriteRight = $param_DBRightManagementValues[$var_BasisClipboardName]['WriteAuthorisaton'];
				} else if(is_array($var_array)) {

					if(array_key_exists('Default_BCReadRight', $var_array)) {
						$var_Default_BCReadRight = $var_array['Default_BCReadRight'];
					} else {
						$var_Default_BCReadRight = $var_CONFIG_BCReadRight ;
					}
					if(array_key_exists('Default_BCWriteRight', $var_array)) {
						$var_Default_BCWriteRight = $var_array['Default_BCWriteRight'];
					} else {
						$var_Default_BCWriteRight = $var_CONFIG_BCWriteRight ;
					}
				}else {
					$var_Default_BCWriteRight = $var_CONFIG_BCWriteRight ;
					$var_Default_BCReadRight = $var_CONFIG_BCReadRight ;
				}
				$var_result[$var_BasisClipboardName]['BCWriteRight'] = $var_Default_BCWriteRight;
				$var_result[$var_BasisClipboardName]['BCReadRight'] = $var_Default_BCReadRight;
			}
		}

		return $var_result;
	}

	private function loadRightManagementValues() {
		if(empty ($this->RightManagementValues)) {

			$this->RightManagementValues = & self::getRightManagementValues($this->PartOfObj_BasisClipboards, $this->DBRightManagementValues, $this->CONFIG_RightManagement['RightManagementBC']);

		}
	}

	public function checkWriteBC($param_BasisClipboardname, $param_RequestProfileID, $param_OnlyForToAddToBC=false) {
//		D::li( $this->PartOfObj.'->'.$param_BasisClipboardname.' # '.$param_RequestProfileID. ' ('.$this->RightManagementValues[$param_BasisClipboardname]['BCWriteRight'].')',1,1);
		if(is_array($this->RightManagementValues) && array_key_exists($param_BasisClipboardname, $this->RightManagementValues)) {

			// alle dürfen lesen /schreiben				:	a	all
			if('a'==$this->RightManagementValues[$param_BasisClipboardname]['BCWriteRight']) {
//D::ulli($this.'              a'.$param_RequestProfileID,1,1)				;
				//				D::ulli('a');
				return true;
				// angemeldete dürfen lesen / schreiben		:	u	user
			} else if('u'==$this->RightManagementValues[$param_BasisClipboardname]['BCWriteRight']) {
//D::ulli($this.'              u'.$param_RequestProfileID,1,1)				;
				if($param_RequestProfileID) {
					return true;
				} else {
					return false;
				}
				// nur schreib/lese -berechtigte dürfen		:	r	right
			} else if('r'==$this->RightManagementValues[$param_BasisClipboardname]['BCWriteRight']) {
//D::ulli($this.'              r'.$param_RequestProfileID,1,1)				;
				return $this->PartOfObj->getObjVar('RightManagement')->isPIDinReadRightProfiles($param_RequestProfileID);

			// nue schreib -berechtigte dürfen:		w right
			} else if('w'==$this->RightManagementValues[$param_BasisClipboardname]['BCWriteRight']) {
//D::ulli($this.'              w '.$param_RequestProfileID,1,1)				;
				return $this->PartOfObj->getObjVar('RightManagement')->isPIDinWriteRightProfiles($param_RequestProfileID, $param_OnlyForToAddToBC);

			}
		} else {
//			D::ulli(__CLASS__ .'->'. __FUNCTION__ .': ggggggggggggggg'.$this->PartOfObj.'->'.$param_BasisClipboardname);
			return false;
		}
	}

	public function checkReadBC($param_BasisClipboardname, $param_RequestProfileID) {
		if(is_array($this->RightManagementValues) && array_key_exists($param_BasisClipboardname, $this->RightManagementValues)) {

			// alle dürfen lesen /schreiben				:	a	all
			if('a'==$this->RightManagementValues[$param_BasisClipboardname]['BCReadRight']) {
				return true;

			// angemeldete dürfen lesen / schreiben		:	u	user
			} else if('u'==$this->RightManagementValues[$param_BasisClipboardname]['BCReadRight']) {
				if($param_RequestProfileID) {
					return true;
				} else {
					return false;
				}

			// nur lese -berechtigte dürfen		:	r	right
			} else if('r'==$this->RightManagementValues[$param_BasisClipboardname]['BCReadRight']) {
				return $this->PartOfObj->getObjVar('RightManagement')->isPIDinReadRightProfiles($param_RequestProfileID);

			// nue schreib -berechtigte dürfen:		w right
			} else if('w'==$this->RightManagementValues[$param_BasisClipboardname]['BCReadRight']) {
				return $this->PartOfObj->getObjVar('RightManagement')->isPIDinWriteRightProfiles($param_RequestProfileID);

			}
		} else {
			return false;
		}
	}

	public function getObjDOMElement(Account &$param_HostAccount, iStorable &$param_iStorable, DOMDocument $param_DOMDocument, $param_KindOf=null, array $param_DOMElemente=null) {
		$this->loadRightManagementValues();
		$DOMElement = parent::getObjDOMElement($param_HostAccount, $param_iStorable, $param_DOMDocument, $param_KindOf, $param_DOMElemente);

		if($param_KindOf==='form') {

			foreach($this->RightManagementValues as $var_BasisClipboardname => $var_Rights) {
				{
					$DOM_BasisClipboard = $param_DOMDocument->createElement('BasisClipboard');
					$DOMElement->appendChild($DOM_BasisClipboard);
					$var_BELanguage = Language::getClassLinks($this->PartOfObj_ClassName,$var_BasisClipboardname);
					$FncVar_BasisClipboardnameArray = CONFIG::transformStringToArray($var_BasisClipboardname, 'category');
					$DOM_BasisClipboard->setAttribute('BasisClipboard', $FncVar_BasisClipboardnameArray[0]);
					if(array_key_exists(1, $FncVar_BasisClipboardnameArray)) {
						$DOM_BasisClipboard->setAttribute('Category', $FncVar_BasisClipboardnameArray[1]);
					}
					$DOM_BasisClipboard->setAttribute('name', $var_BasisClipboardname);
					DOMElementHelper::setDOMElementLanguageParameter($DOM_BasisClipboard, $var_BELanguage);
				}

				foreach(array('ReadKeys' => 'BCReadRight', 'WriteKeys' => 'BCWriteRight') as $KindOfRight => $Right) {
					$var_BCLanguage = Language::getBCRightManagement($var_BasisClipboardname);
					$DOM_BCKeys = $param_DOMDocument->createElement($KindOfRight);
					$DOM_BasisClipboard->appendChild($DOM_BCKeys);

					$rightArray = $this->CONFIG_RightManagement['RightManagementBC'];
					if(array_key_exists($var_BasisClipboardname,$rightArray) && array_key_exists($KindOfRight,$rightArray[$var_BasisClipboardname])){
						$rightArray=$rightArray[$var_BasisClipboardname];
					}
					foreach($rightArray[$KindOfRight] as $Key => $var) {
						$DOM_BCKey = $param_DOMDocument->createElement('Key');
						$DOM_BCKeys->appendChild($DOM_BCKey);
						$DOM_BCKey->setAttribute('key', $Key);
						DOMElementHelper::setDOMElementLanguageParameter($DOM_BCKey, $var_BCLanguage[$KindOfRight][$Key]);
						if($this->RightManagementValues[$var_BasisClipboardname][$Right] == $Key) {
							$DOM_BCKey->setAttribute('selected', 'selected');
						}
					}
				}
			}
		}

		return $DOMElement;
	}
}
?>