/** * get grants for records * * @param Tinebase_Record_RecordSet $records */ public function getGrantsForRecords(Tinebase_Record_RecordSet $records) { $recordIds = $records->getArrayOfIds(); if (empty($recordIds)) { return; } $select = $this->_getAclSelectByRecordIds($recordIds)->group(array('record_id', 'account_type', 'account_id')); Tinebase_Backend_Sql_Abstract::traitGroup($select); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $select); } $stmt = $this->_db->query($select); $grantsData = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' grantsData: ' . print_r($grantsData, true)); } foreach ($grantsData as $grantData) { $givenGrants = explode(',', $grantData['account_grants']); foreach ($givenGrants as $grant) { $grantData[$grant] = TRUE; } $recordGrant = new $this->_modelName($grantData, true); unset($recordGrant->account_grant); $record = $records->getById($recordGrant->record_id); if (!$record->grants instanceof Tinebase_Record_RecordSet) { $record->grants = new Tinebase_Record_RecordSet($this->_modelName); } $record->grants->addRecord($recordGrant); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Records with grants: ' . print_r($records->toArray(), true)); } }
/** * testOffsetUnset(). */ public function testOffsetUnset() { unset($this->object[1]); unset($this->object[3]); $this->assertEquals(2, count($this->object)); $this->assertEquals(array(0), $this->object->getIdLessIndexes(), 'wrong idLess indexes'); $this->assertEquals(array(1), $this->object->getArrayOfIds(), 'wrong idFull indexes'); }
/** * Tears down the fixture * This method is called after a test is executed. * * @access protected */ protected function tearDown() { $this->_groupIdsToDelete = $this->_groupsToDelete->getArrayOfIds(); if ($this->_schemaConfigChanged) { Courses_Config::getInstance()->set(Courses_Config::STUDENTS_USERNAME_SCHEMA, $this->_schemaConfig); } if ($this->_usernameLengthConfigChanged) { Tinebase_Config::getInstance()->set(Tinebase_Config::MAX_USERNAME_LENGTH, $this->_usernameLengthConfig); } if ($this->_defaultDepartmentConfigChanged) { Courses_Config::getInstance()->set(Courses_Config::DEFAULT_DEPARTMENT, $this->_defaultDepartmentConfig); } parent::tearDown(); }
/** * getPathsForRecords * * @param Tinebase_Record_Interface|Tinebase_Record_RecordSet $records * @return Tinebase_Record_RecordSet * @throws Tinebase_Exception_NotFound */ public function getPathsForRecords($records) { $ids = $records instanceof Tinebase_Record_Interface ? array($records->getId()) : $records->getArrayOfIds(); return $this->search(new Tinebase_Model_PathFilter(array(array('field' => 'record_id', 'operator' => 'in', 'value' => $ids)))); }
/** * create new folders or get existing folders from db and return record set * * @param array $_folders * @param Felamimail_Model_Account $_account * @param string $_parentFolder * @return Tinebase_Record_RecordSet of Felamimail_Model_Folder * * @todo move delete sync to extra function */ protected function _getOrCreateFolders(array $_folders, $_account, $_parentFolder) { $parentFolder = $_parentFolder !== NULL ? $_parentFolder : ''; $result = new Tinebase_Record_RecordSet('Felamimail_Model_Folder'); $systemFolders = Felamimail_Controller_Folder::getInstance()->getSystemFolders($_account); // get configured account standard folders here if (strtolower($_account->sent_folder) != $systemFolders[2]) { $systemFolders[2] = strtolower($_account->sent_folder); } if (strtolower($_account->trash_folder) != $systemFolders[5]) { $systemFolders[5] = strtolower($_account->trash_folder); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_folders, TRUE)); } // do some mapping and save folder in db (if it doesn't exist foreach ($_folders as $folderData) { try { $folderData['localName'] = Felamimail_Model_Folder::decodeFolderName($folderData['localName']); $folderData['globalName'] = Felamimail_Model_Folder::decodeFolderName($folderData['globalName']); $isSelectable = $this->_isSelectable($folderData, $_account); $folder = Felamimail_Controller_Folder::getInstance()->getByBackendAndGlobalName($_account->getId(), $folderData['globalName']); $folder->is_selectable = $isSelectable; $folder->supports_condstore = $this->_supportsCondStore($folder, $_account); $folder->imap_status = Felamimail_Model_Folder::IMAP_STATUS_OK; $folder->has_children = $folderData['hasChildren'] == '1'; $folder->parent = $parentFolder; if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Update cached folder ' . $folderData['globalName']); } } catch (Tinebase_Exception_NotFound $tenf) { // create new folder if (empty($folderData['localName'])) { // skip if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Do not add folder ' . $folderData['globalName'] . '. Localname is empty.'); } continue; } else { $delimiter = strlen($folderData['delimiter']) === 1 ? $folderData['delimiter'] : ''; $folder = new Felamimail_Model_Folder(array('localname' => $folderData['localName'], 'globalname' => $folderData['globalName'], 'is_selectable' => $isSelectable, 'supports_condstore' => $this->_supportsCondStore($folderData['globalName'], $_account), 'has_children' => $folderData['hasChildren'] == '1', 'account_id' => $_account->getId(), 'imap_timestamp' => Tinebase_DateTime::now(), 'imap_status' => Felamimail_Model_Folder::IMAP_STATUS_OK, 'user_id' => Tinebase_Core::getUser()->getId(), 'parent' => $parentFolder, 'system_folder' => in_array(strtolower($folderData['localName']), $systemFolders), 'delimiter' => $delimiter)); // update delimiter setting of account if ($folder->delimiter && $folder->delimiter !== $_account->delimiter && $folder->localname === 'INBOX') { $_account->delimiter = $folder->delimiter; $_account = Felamimail_Controller_Account::getInstance()->update($_account); } if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Adding new folder ' . $folderData['globalName'] . ' to cache.'); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . print_r($folder->toArray(), true)); } $folder = $this->_backend->create($folder); } } $result->addRecord($folder); } if (count($_folders) > 0) { $this->_removeFromCache($_account, $parentFolder, $result->getArrayOfIds()); } return $result; }
/** * deletes user in tine20 db that no longer exist in sync backend * * @param Tinebase_Record_RecordSet $usersInSyncBackend */ protected static function _syncDeletedUsers(Tinebase_Record_RecordSet $usersInSyncBackend) { $userIdsInSqlBackend = Tinebase_User::getInstance()->getAllUserIdsFromSqlBackend(); $deletedInSyncBackend = array_diff($userIdsInSqlBackend, $usersInSyncBackend->getArrayOfIds()); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' About to delete ' . count($deletedInSyncBackend) . ' users in SQL backend...'); } foreach ($deletedInSyncBackend as $userToDelete) { $user = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $userToDelete, 'Tinebase_Model_FullUser'); Tinebase_User::getInstance()->deleteUserInSqlBackend($userToDelete); if (Tinebase_Application::getInstance()->isInstalled('Addressbook') === true && !empty($user->contact_id)) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Deleting user contact of ' . $user->accountLoginName); } $contactsBackend = Addressbook_Backend_Factory::factory(Addressbook_Backend_Factory::SQL); $contactsBackend->delete($user->contact_id); } } }
/** * deletes user in tine20 db that no longer exist in sync backend * * @param Tinebase_Record_RecordSet $usersInSyncBackend */ protected static function _syncDeletedUsers(Tinebase_Record_RecordSet $usersInSyncBackend) { $userIdsInSqlBackend = Tinebase_User::getInstance()->getAllUserIdsFromSqlBackend(); $deletedInSyncBackend = array_diff($userIdsInSqlBackend, $usersInSyncBackend->getArrayOfIds()); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' About to delete / expire ' . count($deletedInSyncBackend) . ' users in SQL backend...'); } foreach ($deletedInSyncBackend as $userToDelete) { $user = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $userToDelete, 'Tinebase_Model_FullUser'); if (in_array($user->accountLoginName, self::getSystemUsernames())) { return; } // at first, we expire+deactivate the user $now = Tinebase_DateTime::now(); if (!$user->accountExpires || $user->accountStatus !== Tinebase_Model_User::ACCOUNT_STATUS_DISABLED) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Disable user and set expiry date of ' . $user->accountLoginName . ' to ' . $now); } $user->accountExpires = $now; $user->accountStatus = Tinebase_Model_User::ACCOUNT_STATUS_DISABLED; Tinebase_User::getInstance()->updateUserInSqlBackend($user); } else { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' User already expired ' . print_r($user->toArray(), true)); } // TODO make time span configurable? if ($user->accountExpires->isEarlier($now->subYear(1))) { // if he or she is already expired longer than configured expiry, we remove them! Tinebase_User::getInstance()->deleteUserInSqlBackend($userToDelete); if (Tinebase_Application::getInstance()->isInstalled('Addressbook') === true && !empty($user->contact_id)) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Deleting user contact of ' . $user->accountLoginName); } $contactsBackend = Addressbook_Backend_Factory::factory(Addressbook_Backend_Factory::SQL); $contactsBackend->delete($user->contact_id); } } else { // keep user in expiry state } } } }
/** * get all alarms of given record(s) / adds record_id index to result set * * @param string $_model model to get alarms for * @param string|array|Tinebase_Record_Interface|Tinebase_Record_RecordSet $_recordId record id(s) to get alarms for * @param boolean $_onlyIds * @return Tinebase_Record_RecordSet|array of ids */ public function getAlarmsOfRecord($_model, $_recordId, $_onlyIds = FALSE) { if ($_recordId instanceof Tinebase_Record_RecordSet) { $recordId = $_recordId->getArrayOfIds(); } else { if ($_recordId instanceof Tinebase_Record_Interface) { $recordId = $_recordId->getId(); } else { $recordId = $_recordId; } } //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " model: '$_model' id:" . print_r((array)$recordId, true)); $filter = new Tinebase_Model_AlarmFilter(array(array('field' => 'model', 'operator' => 'equals', 'value' => $_model), array('field' => 'record_id', 'operator' => 'in', 'value' => (array) $recordId))); $result = $this->_backend->search($filter, NULL, $_onlyIds); // NOTE: Adding indices to empty recordsets breaks empty tests if (count($result) > 0 && $result instanceof Tinebase_Record_RecordSet) { $result->addIndices(array('record_id')); } return $result; }
/** * clears flags in local database * * @param Tinebase_Record_RecordSet $_messagesToFlag * @param array $_flags * @param array $_folderCounts * @return array folder counts */ protected function _clearFlagsOnCache(Tinebase_Record_RecordSet $_messagesToUnflag, $_flags, $_folderCounts) { $folderCounts = $_folderCounts; // set flags in local database $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb()); // store flags in local cache foreach ($_messagesToUnflag as $message) { if (in_array(Zend_Mail_Storage::FLAG_SEEN, $_flags) && in_array(Zend_Mail_Storage::FLAG_SEEN, $message->flags)) { // count messages with seen flag for the first time $folderCounts[$message->folder_id]['incrementUnreadCounter']++; } $this->_backend->clearFlag($message, $_flags); } // mark message as changed in the cache backend $this->_backend->updateMultiple($_messagesToUnflag->getArrayOfIds(), array('timestamp' => Tinebase_DateTime::now()->get(Tinebase_Record_Abstract::ISO8601LONG))); Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId); return $folderCounts; }
/** * Tears down the fixture * This method is called after a test is executed. * * @access protected */ protected function tearDown() { $this->_groupIdsToDelete = $this->_groupsToDelete->getArrayOfIds(); parent::tearDown(); }
/** * compares two recordsets / only compares the ids / returns all records that are different in an array: * - removed -> all records that are in $this but not in $_recordSet * - added -> all records that are in $_recordSet but not in $this * - modified -> array of diffs for all different records that are in both record sets * * @param Tinebase_Record_RecordSet $recordSet * @return Tinebase_Record_RecordSetDiff */ public function diff($recordSet) { if (!$recordSet instanceof Tinebase_Record_RecordSet) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Did not get Tinebase_Record_RecordSet, skipping diff(' . $this->_recordClass . ')'); } return new Tinebase_Record_RecordSetDiff(array('model' => $this->getRecordClassName())); } if ($this->getRecordClassName() !== $recordSet->getRecordClassName()) { throw new Tinebase_Exception_InvalidArgument('can only compare recordsets with the same type of records'); } $existingRecordsIds = $this->getArrayOfIds(); $toCompareWithRecordsIds = $recordSet->getArrayOfIds(); $removedIds = array_diff($existingRecordsIds, $toCompareWithRecordsIds); $addedIds = array_diff($toCompareWithRecordsIds, $existingRecordsIds); $modifiedIds = array_intersect($existingRecordsIds, $toCompareWithRecordsIds); $removed = new Tinebase_Record_RecordSet($this->getRecordClassName()); $added = new Tinebase_Record_RecordSet($this->getRecordClassName()); $modified = new Tinebase_Record_RecordSet('Tinebase_Record_Diff'); foreach ($addedIds as $id) { $added->addRecord($recordSet->getById($id)); } // consider records without id, too foreach ($recordSet->getIdLessIndexes() as $index) { $added->addRecord($recordSet->getByIndex($index)); } foreach ($removedIds as $id) { $removed->addRecord($this->getById($id)); } // consider records without id, too foreach ($this->getIdLessIndexes() as $index) { $removed->addRecord($this->getByIndex($index)); } foreach ($modifiedIds as $id) { $diff = $this->getById($id)->diff($recordSet->getById($id)); if (!$diff->isEmpty()) { $modified->addRecord($diff); } } $result = new Tinebase_Record_RecordSetDiff(array('model' => $this->getRecordClassName(), 'added' => $added, 'removed' => $removed, 'modified' => $modified)); return $result; }
/** * resolve config grants * * @param Tinebase_Record_RecordSet $_cfConfigs */ public function resolveConfigGrants($_cfConfigs) { $user = Tinebase_Core::getUser(); if (!is_object($user)) { return; // do nothing } $cfAcl = $this->_backendConfig->getAclForIds($user->getId(), $_cfConfigs->getArrayOfIds()); foreach ($_cfConfigs as $config) { $config->account_grants = array_key_exists($config->getId(), $cfAcl) ? explode(',', $cfAcl[$config->getId()]) : array(); } }
/** * sets notes of a record * * @param Tinebase_Record_Abstract $_record the record object * @param string $_backend backend (default: 'Sql') * @param string $_notesProperty the property in the record where the tags are in (default: 'notes') * * @todo add update notes ? */ public function setNotesOfRecord($_record, $_backend = 'Sql', $_notesProperty = 'notes') { $model = get_class($_record); $backend = ucfirst(strtolower($_backend)); //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_record->toArray(), TRUE)); $currentNotesIds = $this->getNotesOfRecord($model, $_record->getId(), $backend)->getArrayOfIds(); $notes = $_record->{$_notesProperty}; if ($notes instanceof Tinebase_Record_RecordSet) { $notesToSet = $notes; } else { if (count($notes) > 0 && $notes[0] instanceof Tinebase_Record_Abstract) { // array of notes records given $notesToSet = new Tinebase_Record_RecordSet('Tinebase_Model_Note', $notes); } else { // array of arrays given $notesToSet = new Tinebase_Record_RecordSet('Tinebase_Model_Note'); foreach ($notes as $noteData) { if (!empty($noteData)) { $noteArray = !is_array($noteData) ? array('note' => $noteData) : $noteData; if (!isset($noteArray['note_type_id'])) { // get default note type $defaultNote = $this->getNoteTypeByName('note'); $noteArray['note_type_id'] = $defaultNote->getId(); } try { $note = new Tinebase_Model_Note($noteArray); $notesToSet->addRecord($note); } catch (Tinebase_Exception_Record_Validation $terv) { // discard invalid notes here Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Note is invalid! ' . $terv->getMessage()); } } } } } //$toAttach = array_diff($notesToSet->getArrayOfIds(), $currentNotesIds); $toDetach = array_diff($currentNotesIds, $notesToSet->getArrayOfIds()); // delete detached/deleted notes $this->deleteNotes($toDetach); // add new notes Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Adding ' . count($notesToSet) . ' note(s) to record.'); foreach ($notesToSet as $note) { //if (in_array($note->getId(), $toAttach)) { if (!$note->getId()) { $note->record_model = $model; $note->record_backend = $backend; $note->record_id = $_record->getId(); $this->addNote($note); } } }
/** * compares two recordsets / only compares the ids / returns all records that are different in an array: * - removed -> all records that are in $this but not in $_recordSet * - added -> all records that are in $_recordSet but not in $this * - modified -> array of diffs for all different records that are in both record sets * * @param Tinebase_Record_RecordSet $_recordSet * @return array */ public function diff($_recordSet) { if (!$_recordSet instanceof Tinebase_Record_RecordSet) { if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' ' . ' Did not get Tinebase_Record_RecordSet, skipping diff()'); } return array(); } if ($this->getRecordClassName() !== $_recordSet->getRecordClassName()) { throw new Tinebase_Exception_InvalidArgument('can only compare recordsets with the same type of records'); } $removed = new Tinebase_Record_RecordSet($this->getRecordClassName()); $added = new Tinebase_Record_RecordSet($this->getRecordClassName()); $modified = array(); $result = array(); $migration = $this->getMigration($_recordSet->getArrayOfIds()); foreach ($migration['toDeleteIds'] as $id) { $added->addRecord($this->getById($id)); } foreach ($migration['toCreateIds'] as $id) { $removed->addRecord($_recordSet->getById($id)); } foreach ($migration['toUpdateIds'] as $id) { $diff = $this->getById($id)->diff($_recordSet->getById($id)); if (!empty($diff)) { $modified[$id] = $diff; } } foreach (array('removed', 'added', 'modified') as $subresult) { if (count(${$subresult}) > 0) { $result[$subresult] = ${$subresult}; } } return $result; }