All Data Structures Functions Variables Pages
ReadPermissionsSubscriptionUtil.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 
38  {
39  const TYPE_ADD = 1;
40  const TYPE_DELETE = 2;
41 
42  const STATUS_STARTED = 1;
43  const STATUS_COMPLETED = 2;
44 
48  public static function buildTables()
49  {
50  $readSubscriptionModelClassNames = PathUtil::getAllReadSubscriptionModelClassNames();
51  foreach ($readSubscriptionModelClassNames as $modelClassName)
52  {
53  $readPermissionsSubscriptionTableName = static::getSubscriptionTableName($modelClassName);
54  static::recreateTable($readPermissionsSubscriptionTableName);
55  }
56  static::recreateAccountBuildTable();
58  }
59 
64  public static function recreateTable($modelSubscriptionTableName)
65  {
66  $schema = static::getReadSubscriptionTableSchemaByName($modelSubscriptionTableName);
67  $messageLogger = new MessageLogger();
69  $schema, $messageLogger);
70  }
71 
72  public static function recreateAccountBuildTable()
73  {
74  $schema = static::getReadSubscriptionTableSchemaForAccountTempTable();
75  $messageLogger = new MessageLogger();
77  $schema, $messageLogger);
78  }
79 
80  protected static function getReadSubscriptionTableSchemaByName($tableName)
81  {
82  assert('is_string($tableName) && $tableName != ""');
83  return array($tableName => array(
84  'columns' => array(
85  array(
86  'name' => 'userid',
87  'type' => 'INT(11)',
88  'unsigned' => 'UNSIGNED',
89  'notNull' => 'NOT NULL', // Not Coding Standard
90  'collation' => null,
91  'default' => null,
92  ),
93  array(
94  'name' => 'modelid',
95  'type' => 'INT(11)',
96  'unsigned' => 'UNSIGNED',
97  'notNull' => 'NOT NULL', // Not Coding Standard
98  'collation' => null,
99  'default' => null,
100  ),
101  array(
102  'name' => 'modifieddatetime',
103  'type' => 'DATETIME',
104  'unsigned' => null,
105  'notNull' => 'NULL', // Not Coding Standard
106  'collation' => null,
107  'default' => 'NULL', // Not Coding Standard
108  ),
109  array(
110  'name' => 'subscriptiontype',
111  'type' => 'TINYINT(4)',
112  'unsigned' => null,
113  'notNull' => 'NULL', // Not Coding Standard
114  'collation' => null,
115  'default' => 'NULL', // Not Coding Standard
116  ),
117  ),
118  'indexes' => array('userid_modelid' =>
119  array(
120  'columns' => array('userid', 'modelid'),
121  'unique' => true,
122  ),
123  ),
124  )
125  );
126  }
127 
128  /*
129  * Schema for temp build table for accounts.
130  * After account is created, deleted, or its owner is changed we don't want to rebuild read permission subscription
131  * table for all accounts, but just for those that are actually created/deleted/changed owner.
132  * So we eep list of these account ids in this temp table, and after account read permission table is updated,
133  * we need to remove accounts from temp table.
134  */
135  protected static function getReadSubscriptionTableSchemaForAccountTempTable()
136  {
137  $tableName = static::getAccountSubscriptionTempBuildTableName();
138  return array($tableName => array(
139  'columns' => array(
140  array(
141  'name' => 'accountid',
142  'type' => 'INT(11)',
143  'unsigned' => 'UNSIGNED',
144  'notNull' => 'NOT NULL', // Not Coding Standard
145  'collation' => null,
146  'default' => null,
147  ),
148  ),
149  'indexes' => array(),
150  ),
151  );
152  }
153 
158  protected static function getAccountIdsArrayFromBuildTable()
159  {
160  $tableName = static::getAccountSubscriptionTempBuildTableName();
161  $sql = "select accountid from " . $tableName;
162  return ZurmoRedBean::getCol($sql);
163  }
164 
165  /*
166  * Add account id to account temp build table
167  */
168  protected static function addAccountIdToBuildTable($accountId)
169  {
170  assert('is_int($accountId)');
171  $tableName = static::getAccountSubscriptionTempBuildTableName();
172  // Need to check if accountId already exist in table,
173  // because save and owner change observer events are triggered during adding new account
174  $sql = "select * from $tableName where accountid='$accountId'";
175  $results = ZurmoRedBean::getAll($sql);
176  if (!is_array($results) || empty($results))
177  {
178  $sql = "insert into $tableName (accountid) values ($accountId)";
179  ZurmoRedBean::exec($sql);
180  }
181  }
182 
183  /*
184  * Delete account id to account temp build table
185  */
186  protected static function deleteAccountIdFromBuildTable($accountId)
187  {
188  assert('is_int($accountId)');
189  $tableName = static::getAccountSubscriptionTempBuildTableName();
190  $sql = "delete from $tableName where accountid='$accountId'";
191  ZurmoRedBean::exec($sql);
192  }
193 
194  /*
195  * Delete all users items from read permission subscription table
196  */
197  public static function deleteUserItemsFromAllReadSubscriptionTables($userId)
198  {
199  assert('is_int($userId)');
200  // Check if user exist or not. If user exist we will not delete records
201  try
202  {
203  $user = User::getById($userId);
204  throw new NotSupportedException();
205  }
206  catch (NotFoundException $e)
207  {
208  // Do nothing
209  }
210  $readSubscriptionModelClassNames = PathUtil::getAllReadSubscriptionModelClassNames();
211  foreach ($readSubscriptionModelClassNames as $modelClassName)
212  {
213  $readPermissionsSubscriptionTableName = static::getSubscriptionTableName($modelClassName);
214  $sql = "delete from $readPermissionsSubscriptionTableName where userid='$userId'";
215  ZurmoRedBean::exec($sql);
216  }
217  }
218 
219  /*
220  * Based on account id, we add account id to account temp build table, refrsh account read permission
221  * subscription table for account ids in temp build table, and delete all records from account temp build table
222  */
223  public static function updateAccountReadSubscriptionTableBasedOnBuildTable($accountId)
224  {
225  assert('is_int($accountId)');
226  static::addAccountIdToBuildTable($accountId);
227  Yii::app()->jobQueue->add('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', 5);
228  }
229 
230  protected static function getModelTableName($modelClassName)
231  {
232  assert('is_string($modelClassName) && $modelClassName != ""');
233  return $modelClassName::getTableName();
234  }
235 
236  public static function getSubscriptionTableName($modelClassName)
237  {
238  assert('is_string($modelClassName) && $modelClassName != ""');
239  return static::getModelTableName($modelClassName) . '_read_subscription';
240  }
241 
247  {
248  return 'account_read_subscription_temp_build';
249  }
250 
259  public static function updateAllReadSubscriptionTables(MessageLogger $messageLogger, $modelClassNames = null,
260  $arrayOfModelIdsToUpdate = array())
261  {
262  $loggedUser = Yii::app()->user->userModel;
263  $users = User::getAll();
264  $updateStartTimestamp = time();
265  static::setReadPermissionUpdateStatus(static::STATUS_STARTED);
266  $messageLogger->addDebugMessage(Zurmo::t('ZurmoModule',
267  'Starting read permission building for all users.'));
268 
269  foreach ($users as $user)
270  {
271  if ($user->isSystemUser)
272  {
273  $messageLogger->addDebugMessage(Zurmo::t('ZurmoModule',
274  'Skipping system user with userID: {id}', array('{id}' => $user->id)));
275  continue;
276  }
277  $messageLogger->addDebugMessage(Zurmo::t('ZurmoModule',
278  'Starting read permission building for userID: {id}', array('{id}' => $user->id)));
279  $startTime = microtime(true);
280  Yii::app()->user->userModel = $user;
281 
282  if (!is_array($modelClassNames) || empty($modelClassNames))
283  {
284  $modelClassNames = PathUtil::getAllReadSubscriptionModelClassNames();
285  }
286  if (!empty($modelClassNames) && is_array($modelClassNames))
287  {
288  foreach ($modelClassNames as $modelClassName)
289  {
290  if ($modelClassName != 'Account')
291  {
292  static::updateReadSubscriptionTableByModelClassNameAndUser($modelClassName,
293  Yii::app()->user->userModel, $updateStartTimestamp, true,
294  $messageLogger);
295  }
296  else
297  {
298  static::updateReadSubscriptionTableByModelClassNameAndUser($modelClassName,
299  Yii::app()->user->userModel, $updateStartTimestamp, false,
300  $messageLogger, $arrayOfModelIdsToUpdate);
301  }
302  }
303  }
304  $endTime = microtime(true);
305  $executionTimeMs = $endTime - $startTime;
306  $messageLogger->addDebugMessage(Zurmo::t('ZurmoModule',
307  'Ending read permission building for userID: {id}', array('{id}' => $user->id)));
308  $messageLogger->addDebugMessage(Zurmo::t('ZurmoModule',
309  'Build time for userID: {id} - {miliSeconds}', array('{id}' => $user->id, '{miliSeconds}' => $executionTimeMs)));
310  }
311  Yii::app()->user->userModel = $loggedUser;
312  static::setTimeReadPermissionUpdateTimestamp($updateStartTimestamp);
313  static::setReadPermissionUpdateStatus(static::STATUS_COMPLETED);
314  return true;
315  }
316 
321  public static function updateReadSubscriptionTableFromBuildTable(MessageLogger $messageLogger, $modelClassName = null)
322  {
323  if ($modelClassName == 'Account')
324  {
325  // ToDO: Add pagination - Ivica: I do not think we need it
326  $accountIds = static::getAccountIdsArrayFromBuildTable();
327  ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables($messageLogger, array($modelClassName), $accountIds);
328  if ($modelClassName == 'Account' && !empty($accountIds))
329  {
330  foreach ($accountIds as $accountId)
331  {
332  static::deleteAccountIdFromBuildTable((int)$accountId);
333  }
334  }
335  }
336  }
337 
342  {
343  Yii::app()->jobQueue->add('ReadPermissionSubscriptionUpdate', 5);
344  }
345 
349  public static function groupParentHasChanged()
350  {
351  self::runJobForAccountsWhenRoleOrGroupChanged();
352  }
353 
357  public static function groupHasBeenDeleted()
358  {
359  self::runJobForAccountsWhenRoleOrGroupChanged();
360  }
361 
365  public static function securableItemGivenPermissionsForGroup(SecurableItem $securableItem)
366  {
367  if ($securableItem instanceof Account)
368  {
369  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Account');
370  $account = $securableItem->castDown(array($modelDerivationPathToItem));
371  self::updateAccountReadSubscriptionTableBasedOnBuildTable($account->id);
372  }
373  }
374 
378  public static function securableItemLostPermissionsForGroup(SecurableItem $securableItem)
379  {
380  if ($securableItem instanceof Account)
381  {
382  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Account');
383  $account = $securableItem->castDown(array($modelDerivationPathToItem));
384  self::updateAccountReadSubscriptionTableBasedOnBuildTable($account->id);
385  }
386  }
387 
391  public static function securableItemGivenPermissionsForUser(SecurableItem $securableItem)
392  {
393  if ($securableItem instanceof Account)
394  {
395  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Account');
396  $account = $securableItem->castDown(array($modelDerivationPathToItem));
397  self::updateAccountReadSubscriptionTableBasedOnBuildTable($account->id);
398  }
399  }
400 
404  public static function securableItemLostPermissionsForUser(SecurableItem $securableItem)
405  {
406  if ($securableItem instanceof Account)
407  {
408  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Account');
409  $account = $securableItem->castDown(array($modelDerivationPathToItem));
410  self::updateAccountReadSubscriptionTableBasedOnBuildTable($account->id);
411  }
412  }
413 
417  public static function userCreated()
418  {
419  // ToDo: update jobs just for one user if needed for performance reasons
420  self::runJobForAccountsWhenRoleOrGroupChanged();
421  }
422 
426  public static function userAddedToGroup()
427  {
428  // ToDo: update jobs just for one user if needed for performance reasons
429  self::runJobForAccountsWhenRoleOrGroupChanged();
430  }
431 
435  public static function userRemovedFromGroup()
436  {
437  // ToDo: update jobs just for one user if needed for performance reasons
438  self::runJobForAccountsWhenRoleOrGroupChanged();
439  }
440 
444  public static function userAddedToRole()
445  {
446  // ToDo: update jobs just for one user if needed for performance reasons
447  self::runJobForAccountsWhenRoleOrGroupChanged();
448  }
449 
453  public static function userBeingRemovedFromRole()
454  {
455  // ToDo: update jobs just for one user if needed for performance reasons
456  self::runJobForAccountsWhenRoleOrGroupChanged();
457  }
458 
462  public static function roleParentSet()
463  {
464  self::runJobForAccountsWhenRoleOrGroupChanged();
465  }
466 
470  public static function roleParentBeingRemoved()
471  {
472  // ToDo: This event is not called - check why
473  self::runJobForAccountsWhenRoleOrGroupChanged();
474  }
475 
479  public static function roleHasBeenDeleted()
480  {
481  self::runJobForAccountsWhenRoleOrGroupChanged();
482  }
483 
487  public static function modulePermissionsHasBeenChanged($permitable)
488  {
489  if ($permitable instanceof Group || $permitable instanceof User)
490  {
491  self::runJobForAccountsWhenPermitableChanged();
492  }
493  }
494 
495  protected static function runJobForAccountsWhenRoleOrGroupChanged()
496  {
497  Yii::app()->jobQueue->add('ReadPermissionSubscriptionUpdateForAccount', 5);
498  }
499 
500  protected static function runJobForAccountsWhenAccountPermissionsChanged()
501  {
502  Yii::app()->jobQueue->add('ReadPermissionSubscriptionUpdateForAccount', 5);
503  }
504 
505  protected static function runJobForAccountsWhenPermitableChanged()
506  {
507  Yii::app()->jobQueue->add('ReadPermissionSubscriptionUpdateForAccount', 5);
508  }
509 
520  public static function updateReadSubscriptionTableByModelClassNameAndUser($modelClassName, User $user, $updateStartTimestamp,
521  $onlyOwnedModels = false, MessageLogger $messageLogger,
522  $arrayOfModelIdsToUpdate = array())
523  {
524  assert('$modelClassName === null || is_string($modelClassName) && $modelClassName != ""');
525  assert('is_int($updateStartTimestamp)');
526  $metadata = array();
527  $startTime = microtime(true);
528  $lastReadPermissionUpdateTimestamp = static::getLastReadPermissionUpdateTimestamp();
529  $dateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($lastReadPermissionUpdateTimestamp);
530  $updateDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($updateStartTimestamp);
531 
532  if ($modelClassName == 'Account' && !empty($arrayOfModelIdsToUpdate))
533  {
534  $metadata['clauses'][1] = array(
535  'attributeName' => 'id',
536  'operatorType' => 'oneOf',
537  'value' => $arrayOfModelIdsToUpdate,
538  );
539  $metadata['structure'] = "1";
540  }
541  else
542  {
543  $metadata['clauses'][1] = array(
544  'attributeName' => 'createdDateTime',
545  'operatorType' => 'lessThanOrEqualTo',
546  'value' => $updateDateTime
547  );
548  $metadata['structure'] = "1";
549 
550  if ($onlyOwnedModels)
551  {
552  $metadata['clauses'][2] = array(
553  'attributeName' => 'owner',
554  'operatorType' => 'equals',
555  'value' => $user->id
556  );
557  $metadata['structure'] .= " AND 2";
558  }
559  }
560 
561  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($modelClassName);
562  $where = RedBeanModelDataProvider::makeWhere($modelClassName, $metadata, $joinTablesAdapter);
563  $userModelIds = $modelClassName::getSubsetIds($joinTablesAdapter, null, null, $where);
564  $endTime = microtime(true);
565  $executionTimeMs = $endTime - $startTime;
566  $messageLogger->addDebugMessage(Zurmo::t('ZurmoModule',
567  'SQL time {modelClassName}: {miliSeconds}', array('{modelClassName}' => $modelClassName, '{miliSeconds}' => $executionTimeMs)));
568 
569  // Get models from subscription table
570  $tableName = static::getSubscriptionTableName($modelClassName);
571  $sql = "SELECT modelid FROM $tableName WHERE userid = " . $user->id .
572  " AND subscriptiontype = " . static::TYPE_ADD;
573 
574  if ($modelClassName == 'Account' && !empty($arrayOfModelIdsToUpdate))
575  {
576  $accountIds = static::getAccountIdsArrayFromBuildTable();
577  if (!empty($accountIds))
578  {
579  $list = "'". implode(", ", $accountIds) ."'";
580  $sql .= " AND modelid in ($list)";
581  }
582  }
583  $permissionTableRows = ZurmoRedBean::getAll($sql);
584  $permissionTableIds = array();
585  if (is_array($permissionTableRows) && !empty($permissionTableRows))
586  {
587  foreach ($permissionTableRows as $permissionTableRow)
588  {
589  $permissionTableIds[] = $permissionTableRow['modelid'];
590  }
591  }
592  $modelIdsToAdd = array_diff($userModelIds, $permissionTableIds);
593  $modelIdsToDelete = array_diff($permissionTableIds, $userModelIds);
594  if (is_array($modelIdsToAdd) && !empty($modelIdsToAdd))
595  {
596  foreach ($modelIdsToAdd as $modelId)
597  {
598  $sql = "DELETE FROM $tableName WHERE
599  userid = '" . $user->id . "'
600  AND modelid = '{$modelId}'
601  AND subscriptiontype='" . self::TYPE_DELETE . "'";
602  ZurmoRedBean::exec($sql);
603 
604  $sql = "SELECT * FROM $tableName WHERE
605  userid = '" . $user->id . "'
606  AND modelid = '{$modelId}'
607  AND subscriptiontype='" . self::TYPE_ADD . "'";
608  $results = ZurmoRedBean::getAll($sql);
609 
610  if (!is_array($results) || empty($results))
611  {
612  $sql = "INSERT INTO $tableName VALUES
613  (null, '" . $user->id . "', '{$modelId}', '{$updateDateTime}', '" . self::TYPE_ADD . "')";
614  ZurmoRedBean::exec($sql);
615  }
616  }
617  }
618 
619  if (is_array($modelIdsToDelete) && !empty($modelIdsToDelete))
620  {
621  foreach ($modelIdsToDelete as $modelId)
622  {
623  $sql = "DELETE FROM $tableName WHERE
624  userid = '" . $user->id . "'
625  AND modelid = '{$modelId}'
626  AND subscriptiontype='" . self::TYPE_ADD . "'";
627  ZurmoRedBean::exec($sql);
628 
629  $sql = "SELECT * FROM $tableName WHERE
630  userid = '" . $user->id . "'
631  AND modelid = '{$modelId}'
632  AND subscriptiontype='" . self::TYPE_DELETE . "'";
633  $results = ZurmoRedBean::getAll($sql);
634 
635  if (!is_array($results) || empty($results))
636  {
637  $sql = "INSERT INTO $tableName VALUES
638  (null, '" . $user->id . "', '{$modelId}', '{$updateDateTime}', '" . self::TYPE_DELETE . "')";
639  ZurmoRedBean::exec($sql);
640  }
641  }
642  }
643  }
644 
652  $modelClassName,
653  User $user)
654  {
655  assert('is_int($modelId)');
656  assert('is_string($modelClassName)');
657 
658  $updateStartTimestamp = time();
659  $updateDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($updateStartTimestamp);
660  $tableName = static::getSubscriptionTableName($modelClassName);
661  $sql = "DELETE FROM $tableName WHERE
662  userid = '" . $user->id . "'
663  AND modelid = '{$modelId}'
664  AND subscriptiontype='" . self::TYPE_DELETE . "'";
665  ZurmoRedBean::exec($sql);
666 
667  $sql = "SELECT * FROM $tableName WHERE
668  userid = '" . $user->id . "'
669  AND modelid = '{$modelId}'
670  AND subscriptiontype='" . self::TYPE_ADD . "'";
671  $results = ZurmoRedBean::getAll($sql);
672 
673  if (!is_array($results) || empty($results))
674  {
675  $sql = "INSERT INTO $tableName VALUES
676  (null, '" . $user->id . "', '{$modelId}', '{$updateDateTime}', '" . self::TYPE_ADD . "')";
677  ZurmoRedBean::exec($sql);
678  }
679  }
680 
688  $modelClassName,
689  User $user)
690  {
691  assert('is_int($modelId)');
692  assert('is_string($modelClassName)');
693 
694  $updateStartTimestamp = time();
695  $updateDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($updateStartTimestamp);
696  $tableName = static::getSubscriptionTableName($modelClassName);
697  $sql = "DELETE FROM $tableName WHERE
698  userid = '" . $user->id . "'
699  AND modelid = '{$modelId}'
700  AND subscriptiontype='" . self::TYPE_ADD . "'";
701  ZurmoRedBean::exec($sql);
702 
703  $sql = "SELECT * FROM $tableName WHERE
704  userid = '" . $user->id . "'
705  AND modelid = '{$modelId}'
706  AND subscriptiontype='" . self::TYPE_DELETE . "'";
707  $results = ZurmoRedBean::getAll($sql);
708 
709  if (!is_array($results) || empty($results))
710  {
711  $sql = "INSERT INTO $tableName VALUES
712  (null, '" . $user->id . "', '{$modelId}', '{$updateDateTime}', '" . self::TYPE_DELETE . "')";
713  ZurmoRedBean::exec($sql);
714  }
715  }
716 
722  $modelClassName)
723  {
724  assert('is_int($modelId)');
725  assert('is_string($modelClassName)');
726 
727  $updateStartTimestamp = time();
728  $updateDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($updateStartTimestamp);
729  $tableName = static::getSubscriptionTableName($modelClassName);
730  $sql = "UPDATE $tableName set
731  subscriptiontype='" . self::TYPE_DELETE . "', modifieddatetime='" . $updateDateTime . "'
732  WHERE
733  modelid = '{$modelId}'
734  AND subscriptiontype='" . self::TYPE_ADD . "'";
735  ZurmoRedBean::exec($sql);
736  }
737 
747  $modelClassName,
748  User $user)
749  {
750  static::deleteOnlyModelToReadSubscriptionTableByModelIdAndModelClassName($modelId, $modelClassName);
751  static::addModelToReadSubscriptionTableByModelIdAndModelClassNameAndUser($modelId, $modelClassName, $user);
752  }
753 
764  public static function getAddedOrDeletedModelsFromReadSubscriptionTable($serviceName, $modelClassName,
765  $lastUpdateTimestamp, $type, $user,
766  $checkIfModelCreationApiSyncUtilIsNull = true)
767  {
768  assert('$user instanceof User');
769  $tableName = static::getSubscriptionTableName($modelClassName);
770  $dateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($lastUpdateTimestamp);
771  if ($type == ReadPermissionsSubscriptionUtil::TYPE_DELETE)
772  {
773  $sql = "SELECT {$tableName}.modelid FROM $tableName" .
774  " WHERE {$tableName}.userid = " . $user->id .
775  " AND {$tableName}.subscriptiontype = " . $type .
776  " AND {$tableName}.modifieddatetime >= '" . $dateTime . "'" .
777  " order by {$tableName}.modifieddatetime ASC, {$tableName}.modelid ASC";
778  }
779  else
780  {
781  $sql = "SELECT {$tableName}.modelid FROM $tableName" .
782  " left join " . ModelCreationApiSyncUtil::TABLE_NAME . " isct " .
783  " on isct.modelid = {$tableName}.modelid" .
784  " AND isct.servicename = '" . $serviceName . "'" .
785  " AND isct.modelclassname = '" . $modelClassName . "'" .
786  " WHERE {$tableName}.userid = " . $user->id .
787  " AND {$tableName}.subscriptiontype = " . $type .
788  " AND {$tableName}.modifieddatetime >= '" . $dateTime . "'";
789  if ($checkIfModelCreationApiSyncUtilIsNull)
790  {
791  $sql .= " AND isct.modelid is null";
792  }
793  $sql .= " order by {$tableName}.modifieddatetime ASC, {$tableName}.modelid ASC";
794  }
795  $modelIdsRows = ZurmoRedBean::getAll($sql);
796  $modelIds = array();
797  if (is_array($modelIdsRows) && !empty($modelIdsRows))
798  {
799  foreach ($modelIdsRows as $modelIdRow)
800  {
801  $modelIds[] = (int)$modelIdRow['modelid'];
802  }
803  }
804  return $modelIds;
805  }
806 
815  public static function getAddedModelNamesAndIdsFromReadSubscriptionTable($serviceName,
816  $modelClassName,
817  $lastUpdateTimestamp,
818  $user)
819  {
820  assert('$user instanceof User');
821  $tableName = self::getSubscriptionTableName($modelClassName);
822  $modelTableName = $modelClassName::getTableName();
823  $dateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($lastUpdateTimestamp);
824  $sql = "SELECT {$tableName}.modelid, {$modelTableName}.name FROM $tableName" .
825  " left join " . ModelCreationApiSyncUtil::TABLE_NAME . " isct " .
826  " on isct.modelid = {$tableName}.modelid" .
827  " AND isct.servicename = '" . $serviceName . "'" .
828  " AND isct.modelclassname = '" . $modelClassName . "'" .
829  " left join {$modelTableName} on {$modelTableName}.id = {$tableName}.modelid" .
830  " WHERE {$tableName}.userid = " . $user->id .
831  " AND {$tableName}.subscriptiontype = " . self::TYPE_ADD .
832  " AND {$tableName}.modifieddatetime >= '" . $dateTime . "'" .
833  " AND isct.modelid is null" .
834  " order by {$tableName}.modifieddatetime ASC, {$tableName}.modelid ASC";
835  $modelIdsRows = ZurmoRedBean::getAll($sql);
836  $modelIds = array();
837  if (is_array($modelIdsRows) && !empty($modelIdsRows))
838  {
839  foreach ($modelIdsRows as $modelIdRow)
840  {
841  $modelIds[$modelIdRow['modelid']] = $modelIdRow['name'];
842  }
843  }
844  return $modelIds;
845  }
846 
851  public static function getReadSubscriptionUpdateDetails()
852  {
853  $readSubscriptionUpdateDetails = ZurmoConfigurationUtil::getByModuleName('ZurmoModule',
854  'readSubscriptionUpdateDetails');
855  return $readSubscriptionUpdateDetails;
856  }
857 
862  public static function setReadSubscriptionUpdateDetails($readSubscriptionUpdateDetails)
863  {
864  ZurmoConfigurationUtil::setByModuleName('ZurmoModule', 'readSubscriptionUpdateDetails',
865  $readSubscriptionUpdateDetails);
866  }
867 
872  public static function getLastReadPermissionUpdateTimestamp()
873  {
874  $readSubscriptionUpdateDetails = static::getReadSubscriptionUpdateDetails();
875  if (isset($readSubscriptionUpdateDetails['lastReadPermissionUpdateTimestamp']))
876  {
877  return $readSubscriptionUpdateDetails['lastReadPermissionUpdateTimestamp'];
878  }
879  else
880  {
881  return 0;
882  }
883  }
884 
889  public static function setTimeReadPermissionUpdateTimestamp($lastReadPermissionUpdateTimestamp)
890  {
891  $readSubscriptionUpdateDetails = static::getReadSubscriptionUpdateDetails();
892  $readSubscriptionUpdateDetails['lastReadPermissionUpdateTimestamp'] = $lastReadPermissionUpdateTimestamp;
893  static::setReadSubscriptionUpdateDetails($readSubscriptionUpdateDetails);
894  }
895 
900  public static function setReadPermissionUpdateStatus($status)
901  {
902  $readSubscriptionUpdateDetails = static::getReadSubscriptionUpdateDetails();
903  $readSubscriptionUpdateDetails['status'] = $status;
904  static::setReadSubscriptionUpdateDetails($readSubscriptionUpdateDetails);
905  }
906 
911  public static function getReadPermissionUpdateStatus()
912  {
913  $readSubscriptionUpdateDetails = static::getReadSubscriptionUpdateDetails();
914  if (isset($readSubscriptionUpdateDetails['status']))
915  {
916  return $readSubscriptionUpdateDetails['status'];
917  }
918  else
919  {
920  return static::STATUS_STARTED;
921  }
922  }
923 
929  {
930  if (static::getReadPermissionUpdateStatus() == static::STATUS_COMPLETED)
931  {
932  return true;
933  }
934  else
935  {
936  return false;
937  }
938  }
939  }
940 ?>
static updateAllReadSubscriptionTables(MessageLogger $messageLogger, $modelClassNames=null, $arrayOfModelIdsToUpdate=array())
static getAddedOrDeletedModelsFromReadSubscriptionTable($serviceName, $modelClassName, $lastUpdateTimestamp, $type, $user, $checkIfModelCreationApiSyncUtilIsNull=true)
Definition: User.php:37
static getAddedModelNamesAndIdsFromReadSubscriptionTable($serviceName, $modelClassName, $lastUpdateTimestamp, $user)
static setReadSubscriptionUpdateDetails($readSubscriptionUpdateDetails)
static recreateTable($modelSubscriptionTableName)
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
static deleteOnlyModelToReadSubscriptionTableByModelIdAndModelClassName($modelId, $modelClassName)
static generateOrUpdateTableBySchemaDefinition(array $schemaDefinition, &$messageLogger, $validate=true)
static updateReadSubscriptionTableByModelClassNameAndUser($modelClassName, User $user, $updateStartTimestamp, $onlyOwnedModels=false, MessageLogger $messageLogger, $arrayOfModelIdsToUpdate=array())
static securableItemLostPermissionsForGroup(SecurableItem $securableItem)
static changeOwnerOfModelInReadSubscriptionTableByModelIdAndModelClassNameAndUser($modelId, $modelClassName, User $user)
static setTimeReadPermissionUpdateTimestamp($lastReadPermissionUpdateTimestamp)
Definition: Group.php:37
static securableItemLostPermissionsForUser(SecurableItem $securableItem)
static getByModuleName($moduleName, $key, $cache=true)
addDebugMessage($message)
static getModelDerivationPathToItem($modelClassName)
Definition: RuntimeUtil.php:79
static deleteModelFromReadSubscriptionTableByModelIdAndModelClassNameAndUser($modelId, $modelClassName, User $user)
static getById($id, $modelClassName=null)
castDown(array $derivedModelClassNames)
static securableItemGivenPermissionsForGroup(SecurableItem $securableItem)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static securableItemGivenPermissionsForUser(SecurableItem $securableItem)
static updateReadSubscriptionTableFromBuildTable(MessageLogger $messageLogger, $modelClassName=null)
static addModelToReadSubscriptionTableByModelIdAndModelClassNameAndUser($modelId, $modelClassName, User $user)
static setByModuleName($moduleName, $key, $value, $cache=true)
Generated on Tue Jun 2 2020 07:10:36