00001 <?php 00002 /********************************************************************************* 00003 * Zurmo is a customer relationship management program developed by 00004 * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc. 00005 * 00006 * Zurmo is free software; you can redistribute it and/or modify it under 00007 * the terms of the GNU Affero General Public License version 3 as published by the 00008 * Free Software Foundation with the addition of the following permission added 00009 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK 00010 * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY 00011 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 00012 * 00013 * Zurmo is distributed in the hope that it will be useful, but WITHOUT 00014 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00015 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more 00016 * details. 00017 * 00018 * You should have received a copy of the GNU Affero General Public License along with 00019 * this program; if not, see http://www.gnu.org/licenses or write to the Free 00020 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00021 * 02110-1301 USA. 00022 * 00023 * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive 00024 * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com. 00025 * 00026 * The interactive user interfaces in original and modified versions 00027 * of this program must display Appropriate Legal Notices, as required under 00028 * Section 5 of the GNU Affero General Public License version 3. 00029 * 00030 * In accordance with Section 7(b) of the GNU Affero General Public License version 3, 00031 * these Appropriate Legal Notices must retain the display of the Zurmo 00032 * logo and Zurmo copyright notice. If the display of the logo is not reasonably 00033 * feasible for technical reasons, the Appropriate Legal Notices must display the words 00034 * "Copyright Zurmo Inc. 2017. All rights reserved". 00035 ********************************************************************************/ 00036 00045 class ExplicitReadWriteModelPermissions 00046 { 00051 protected $readOnlyPermitables = array(); 00052 00057 protected $readWritePermitables = array(); 00058 00064 protected $readOnlyPermitablesToRemove = array(); 00065 00071 protected $readWritePermitablesToRemove = array(); 00072 00078 public function addReadOnlyPermitable(Permitable $permitable) 00079 { 00080 assert('$permitable instanceof Permitable'); 00081 $this->resolveIsReadOnlySupported(); 00082 $key = $this->resolvePermitableKey($permitable); 00083 if (!isset($this->readOnlyPermitables[$key])) 00084 { 00085 $this->readOnlyPermitables[$key] = $permitable; 00086 } 00087 else 00088 { 00089 throw new NotSupportedException(); 00090 } 00091 } 00092 00098 public function addReadWritePermitable(Permitable $permitable) 00099 { 00100 assert('$permitable instanceof Permitable'); 00101 $key = $this->resolvePermitableKey($permitable); 00102 if (!isset($this->readWritePermitables[$key])) 00103 { 00104 $this->readWritePermitables[$key] = $permitable; 00105 } 00106 else 00107 { 00108 throw new NotSupportedException(); 00109 } 00110 } 00111 00117 public function addReadOnlyPermitableToRemove(Permitable $permitable) 00118 { 00119 assert('$permitable instanceof Permitable'); 00120 $this->resolveIsReadOnlySupported(); 00121 $key = $this->resolvePermitableKey($permitable); 00122 if (!isset($this->readOnlyPermitablesToRemove[$key])) 00123 { 00124 $this->readOnlyPermitablesToRemove[$key] = $permitable; 00125 } 00126 else 00127 { 00128 throw new NotSupportedException(); 00129 } 00130 } 00131 00137 public function addReadWritePermitableToRemove(Permitable $permitable) 00138 { 00139 assert('$permitable instanceof Permitable'); 00140 $key = $this->resolvePermitableKey($permitable); 00141 if (!isset($this->readWritePermitablesToRemove[$key])) 00142 { 00143 $this->readWritePermitablesToRemove[$key] = $permitable; 00144 } 00145 else 00146 { 00147 throw new NotSupportedException(); 00148 } 00149 } 00150 00151 public function removeAllReadWritePermitables() 00152 { 00153 foreach ($this->readWritePermitables as $permitable) 00154 { 00155 $key = $this->resolvePermitableKey($permitable); 00156 if (!isset($this->readWritePermitablesToRemove[$key])) 00157 { 00158 $this->readWritePermitablesToRemove[$key] = $permitable; 00159 } 00160 } 00161 } 00162 00166 public function getReadOnlyPermitablesCount() 00167 { 00168 return count($this->readOnlyPermitables); 00169 } 00170 00174 public function getReadWritePermitablesCount() 00175 { 00176 return count($this->readWritePermitables); 00177 } 00178 00182 public function getReadOnlyPermitablesToRemoveCount() 00183 { 00184 return count($this->readOnlyPermitablesToRemove); 00185 } 00186 00190 public function getReadWritePermitablesToRemoveCount() 00191 { 00192 return count($this->readWritePermitablesToRemove); 00193 } 00194 00198 public function getReadOnlyPermitables() 00199 { 00200 return $this->readOnlyPermitables; 00201 } 00202 00206 public function getReadWritePermitables() 00207 { 00208 return $this->readWritePermitables; 00209 } 00210 00214 public function getReadOnlyPermitablesToRemove() 00215 { 00216 return $this->readOnlyPermitablesToRemove; 00217 } 00218 00222 public function getReadWritePermitablesToRemove() 00223 { 00224 return $this->readWritePermitablesToRemove; 00225 } 00226 00232 public function isReadOrReadWritePermitable(Permitable $permitable) 00233 { 00234 assert('$permitable instanceof Permitable'); 00235 $key = $this->resolvePermitableKey($permitable); 00236 if (isset($this->readWritePermitables[$key]) || 00237 isset($this->readOnlyPermitables[$key])) 00238 { 00239 return true; 00240 } 00241 return false; 00242 } 00243 00250 public function resolvePermitableKey(Permitable $permitable) 00251 { 00252 return $permitable->getClassId('Permitable'); 00253 } 00254 00262 protected function resolveIsReadOnlySupported() 00263 { 00264 if ((bool)Yii::app()->params['processReadMungeAsWriteMunge']) 00265 { 00266 return new NotSupportedException(); 00267 } 00268 } 00269 } 00270 ?>