All Data Structures Functions Variables Pages
AllPermissionsOptimizationCache.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
49  {
50  const READ = 'R';
51 
52  const CHANGE = 'C';
53 
57  public static $mungeIdsCachePrefix = 'MI:';
58 
63  private static $securableItemToPermitableToReadPermissions = array();
64 
68  private static $mungeIdsByUser = array();
69 
75  private static $securableItemToPermitableToChangePermissions = array();
76 
80  public static $cacheType = 'APO:';
81 
82 //todo: figure out how to best refactor to support write more easily... without needing total duplication.
83 
90  public static function getHasReadPermissionOnSecurableItem(SecurableItem $securableItem, Permitable $permitable)
91  {
92  if ($securableItem->getClassId('SecurableItem') == 0 ||
93  $permitable ->getClassId('Permitable') == 0)
94  {
95  throw new NotFoundException();
96  }
97 
98  $securableItemModelIdentifer = $securableItem->getClassId('SecurableItem');
99  $permitableModelIdentifier = $permitable ->getClassId('Permitable');
100 
101  if (static::supportsAndAllowsPhpCaching())
102  {
103  if (isset(static::$securableItemToPermitableToReadPermissions
104  [$securableItemModelIdentifer]
105  [$permitableModelIdentifier]))
106  {
107  return static::$securableItemToPermitableToReadPermissions
108  [$securableItemModelIdentifer]
109  [$permitableModelIdentifier];
110  }
111  }
112 
113  if (static::supportsAndAllowsMemcache())
114  {
115  $prefix = static::getCachePrefix($securableItemModelIdentifer) . self::READ;
116  $permitablesHasReadPermission = static::getCachedValueAndValidateChecksum($prefix . $securableItemModelIdentifer);
117  if ($permitablesHasReadPermission !== false)
118  {
119  assert('is_array($permitablesHasReadPermission)');
120  if (isset($permitablesHasReadPermission[$permitableModelIdentifier]))
121  {
122  $hasReadPermission = $permitablesHasReadPermission[$permitableModelIdentifier];
123  if (static::supportsAndAllowsPhpCaching())
124  {
125  static::$securableItemToPermitableToReadPermissions
126  [$securableItemModelIdentifer]
127  [$permitableModelIdentifier] = $hasReadPermission;
128  }
129  return $hasReadPermission;
130  }
131  }
132  }
133  throw new NotFoundException();
134  }
135 
141  public static function cacheHasReadPermissionOnSecurableItem(SecurableItem $securableItem, Permitable $permitable,
142  $hasReadPermission)
143  {
144  assert('is_bool($hasReadPermission)');
145 
146  if ($securableItem->getClassId('SecurableItem') == 0 ||
147  $permitable ->getClassId('Permitable') == 0)
148  {
149  return;
150  }
151 
152  $securableItemModelIdentifer = $securableItem->getClassId('SecurableItem');
153  $permitableModelIdentifier = $permitable ->getClassId('Permitable');
154 
155  if (static::supportsAndAllowsPhpCaching())
156  {
157  static::$securableItemToPermitableToReadPermissions
158  [$securableItemModelIdentifer]
159  [$permitableModelIdentifier] = $hasReadPermission;
160  }
161 
162  if (static::supportsAndAllowsMemcache())
163  {
164  $prefix = static::getCachePrefix($securableItemModelIdentifer). self::READ;
165  $permitablesHasReadPermission = static::getCachedValueAndValidateChecksum($prefix . $securableItemModelIdentifer);
166  if ($permitablesHasReadPermission === false)
167  {
168  $permitablesHasReadPermission = array($permitableModelIdentifier => $hasReadPermission);
169  static::cacheValueAndChecksum($prefix . $securableItemModelIdentifer, $permitablesHasReadPermission);
170  }
171  else
172  {
173  assert('is_array($permitablesHasReadPermission)');
174  $permitablesHasReadPermission[$permitableModelIdentifier] = $hasReadPermission;
175  static::cacheValueAndChecksum($prefix . $securableItemModelIdentifer, $permitablesHasReadPermission);
176  }
177  }
178  }
179 
185  public static function getMungeIdsByUser(User $user)
186  {
187  if ($user->getClassId('Permitable') == 0)
188  {
189  throw new NotFoundException();
190  }
191  $userModelIdentifier = $user->getModelIdentifier();
192  if (static::supportsAndAllowsPhpCaching())
193  {
194  if (isset(static::$mungeIdsByUser[$user->id]))
195  {
196  return static::$mungeIdsByUser[$user->id];
197  }
198  }
199  if (static::supportsAndAllowsMemcache())
200  {
201  $prefix = static::getCachePrefix($userModelIdentifier) . static::$mungeIdsCachePrefix;
202  $data = static::getCachedValueAndValidateChecksum($prefix . $userModelIdentifier);
203  if ($data !== false)
204  {
205  return $data;
206  }
207  }
208  throw new NotFoundException();
209  }
210 
215  public static function cacheMungeIdsByUser(User $user, array $mungeIds)
216  {
217  if ($user->getClassId('Permitable') == 0)
218  {
219  return;
220  }
221  $userModelIdentifier = $user->getModelIdentifier();
222  if (static::supportsAndAllowsPhpCaching())
223  {
224  static::$mungeIdsByUser[$user->id] = $mungeIds;
225  }
226  if (static::supportsAndAllowsMemcache())
227  {
228  $prefix = static::getCachePrefix($userModelIdentifier) . static::$mungeIdsCachePrefix;
229 
230  static::cacheValueAndChecksum($prefix . $userModelIdentifier, $mungeIds);
231  }
232  }
233 
237  public static function forgetSecurableItemForRead(SecurableItem $securableItem)
238  {
239  if ($securableItem->getClassId('SecurableItem') == 0)
240  {
241  return;
242  }
243  $securableItemModelIdentifer = $securableItem->getClassId('SecurableItem');
244  if (static::supportsAndAllowsPhpCaching())
245  {
246  static::$securableItemToPermitableToReadPermissions[$securableItemModelIdentifer] = array();
247  }
248 
249  if (static::supportsAndAllowsMemcache())
250  {
251  $prefix = static::getCachePrefix($securableItemModelIdentifer) . static::READ;
252  Yii::app()->cache->delete($prefix . $securableItemModelIdentifer);
253  }
254  }
255 
259  public static function forgetSecurableItemForChange(SecurableItem $securableItem)
260  {
261  if ($securableItem->getClassId('SecurableItem') == 0)
262  {
263  return;
264  }
265  $securableItemModelIdentifer = $securableItem->getClassId('SecurableItem');
266  if (static::supportsAndAllowsPhpCaching())
267  {
268  static::$securableItemToPermitableToChangePermissions[$securableItemModelIdentifer] = array();
269  }
270 
271  if (static::supportsAndAllowsMemcache())
272  {
273  $prefix = static::getCachePrefix($securableItemModelIdentifer) . static::CHANGE;
274  Yii::app()->cache->delete($prefix . $securableItemModelIdentifer);
275  }
276  }
277 
278  public static function forgetAll()
279  {
280  if (static::supportsAndAllowsPhpCaching())
281  {
282  static::$securableItemToPermitableToReadPermissions = array();
283  static::$securableItemToPermitableToChangePermissions = array();
284  static::$mungeIdsByUser = array();
285  }
286  if (static::supportsAndAllowsMemcache())
287  {
288  static::incrementCacheIncrementValue(static::$cacheType);
289  static::resolveToSetFlashMessageOnForgetAll();
290  }
291  }
292 
298  protected static function resolveToSetFlashMessageOnForgetAll()
299  {
300  if ((bool)Yii::app()->params['showFlashMessageWhenSecurityCacheShouldBeRebuilt'])
301  {
302  Yii::app()->user->setFlash('permissionsOptimization',
303  Zurmo::t('ZurmoModule', 'The security cache should be rebuilt to improve performance. ' .
304  'Please ask your administrator to perform this action.'));
305  }
306  }
307  }
308 ?>
static cacheHasReadPermissionOnSecurableItem(SecurableItem $securableItem, Permitable $permitable, $hasReadPermission)
static cacheMungeIdsByUser(User $user, array $mungeIds)
Definition: User.php:37
getClassId($modelClassName)
static forgetSecurableItemForRead(SecurableItem $securableItem)
static getHasReadPermissionOnSecurableItem(SecurableItem $securableItem, Permitable $permitable)
static forgetSecurableItemForChange(SecurableItem $securableItem)
Generated on Thu May 28 2020 07:10:36