/** * Override method because admin app requires special rights * @see tine20/Setup/Setup_Initialize#_createInitialRights($_application) * * @todo make hard coded role name ('admin role') configurable */ protected function _createInitialRights(Tinebase_Model_Application $_application) { //do not call parent::_createInitialRights(); because this app is fopr admins only $roles = Tinebase_Acl_Roles::getInstance(); $adminRole = $roles->getRoleByName('admin role'); $allRights = Tinebase_Application::getInstance()->getAllRights($_application->getId()); foreach ($allRights as $right) { $roles->addSingleRight($adminRole->getId(), $_application->getId(), $right); } }
/** * sets record related properties * * @param string _name of property * @param mixed _value of property * @throws Tinebase_Exception_InvalidArgument */ public function __set($_name, $_value) { switch ($_name) { case 'application_id': $_value = Tinebase_Model_Application::convertApplicationIdToInt($_value); } parent::__set($_name, $_value); }
/** * get shared container * * @return Tinebase_Model_Container */ protected function _getSharedContainer() { if (!$this->_sharedContainer) { $search = Tinebase_Container::getInstance()->search(new Tinebase_Model_ContainerFilter(array('application_id' => $this->_application->getId(), 'name' => 'shared', 'type' => Tinebase_Model_Container::TYPE_SHARED))); $this->_sharedContainer = count($search) > 0 ? $search->getFirstRecord() : Tinebase_Container::getInstance()->addContainer(new Tinebase_Model_Container(array('name' => 'shared', 'type' => Tinebase_Model_Container::TYPE_SHARED, 'backend' => 'sql', 'application_id' => $this->_application->getId()))); } return $this->_sharedContainer; }
/** * create inital rights * * @todo make hard coded role names ('user role' and 'admin role') configurable * * @param Tinebase_Application $application * @return void */ protected function _createInitialRights(Tinebase_Model_Application $_application) { $roleRights = array('user role' => $this->_userRoleRights, 'admin role' => Tinebase_Application::getInstance()->getAllRights($_application->getId())); foreach ($roleRights as $roleName => $rights) { try { $role = Tinebase_Acl_Roles::getInstance()->getRoleByName($roleName); } catch (Tinebase_Exception_NotFound $tenf) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ' . $tenf->getMessage()); continue; } foreach ($rights as $right) { try { Tinebase_Acl_Roles::getInstance()->addSingleRight($role->getId(), $_application->getId(), $right); } catch (Exception $e) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Cannot add right: ' . $right . ' for application: ' . $_application->name . ' - ' . $roleName . ' - ' . print_r($e->getMessage(), true)); } } } }
/** * testSetContainerInPathRecord * * @todo move this to Tinebase? */ public function testSetContainerInPathRecord() { $flatpath = Filemanager_Controller_Node::getInstance()->addBasePath('/' . Tinebase_Model_Container::TYPE_PERSONAL . '/' . Tinebase_Core::getUser()->accountLoginName . '/' . $this->_personalContainer->name); $path = Tinebase_Model_Tree_Node_Path::createFromPath($flatpath); $path->setContainer($this->_sharedContainer); $this->assertEquals('/' . $this->_application->getId() . '/folders/shared/' . $this->_sharedContainer->getId(), $path->statpath); // move it back $path->setContainer($this->_personalContainer); $this->assertEquals('/' . $this->_application->getId() . '/folders/personal/' . Tinebase_Core::getUser()->getId() . '/' . $this->_personalContainer->getId(), $path->statpath, 'wrong statpath: ' . print_r($path->toArray(), TRUE)); }
/** * sets record related properties * * @param string _name of property * @param mixed _value of property * @throws Tinebase_Exception_InvalidArgument */ public function __set($_name, $_value) { switch ($_name) { case 'application_id': $_value = Tinebase_Model_Application::convertApplicationIdToInt($_value); } /** * @TODO * - ask model for datatype * - cope with record/recordSet */ if (is_string($_value) && strlen($_value) == 19 && preg_match('/^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$/', $_value)) { $_value = new Tinebase_DateTime($_value); } parent::__set($_name, $_value); }
/** * get registry data from application frontend json class * * @param Tinebase_Model_Application $application * @return array * @throws Tinebase_Exception_InvalidArgument */ protected function _getCustomAppRegistry(Tinebase_Model_Application $application) { $jsonAppName = $application->name . '_Frontend_Json'; if (!class_exists($jsonAppName)) { return array(); } if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Getting registry data for app ' . $application->name); } try { $applicationJson = new $jsonAppName(); $registryData = $applicationJson->getRegistryData(); } catch (Exception $e) { Tinebase_Exception::log($e); if (!$e instanceof Tinebase_Exception_AccessDenied && !in_array($application->name, array('Tinebase', 'Addressbook', 'Admin'))) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Disabling ' . $application->name . ': ' . $e); Tinebase_Application::getInstance()->setApplicationState(array($application->getId()), Tinebase_Application::DISABLED); } return array(); } // TODO get this from app controller / modelconfig foreach ($applicationJson->getRelatableModels() as $relModel) { $registryData[$relModel['ownApp']]['relatableModels'][] = $relModel; } $registryData['models'] = $applicationJson->getModelsConfiguration(); $registryData['defaultModel'] = $applicationJson->getDefaultModel(); return $registryData; }
/** * returns one config value identified by config name and application id * -> value in config.inc.php overwrites value in db if $_fromFile is TRUE * * @deprecated * @param string $_name config name/key * @param string $_applicationId application id [optional] * @param mixed $_default the default value [optional] * @param boolean $_fromFile get from config.inc.php [optional] * @return Tinebase_Model_Config the config record * @throws Tinebase_Exception_NotFound */ public function getConfig($_name, $_applicationId = NULL, $_default = NULL, $_fromFile = TRUE) { $applicationId = $_applicationId !== NULL ? Tinebase_Model_Application::convertApplicationIdToInt($_applicationId) : Tinebase_Application::getInstance()->getApplicationByName('Tinebase')->getId(); $result = $this->_loadConfig($_name, $applicationId); if (!$result) { $result = new Tinebase_Model_Config(array('application_id' => $applicationId, 'name' => $_name, 'value' => $_default), TRUE); } // check config.inc.php and get value from there $configFileData = $this->_getConfigFileData(); if ($_fromFile && array_key_exists($_name, $configFileData)) { Setup_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Overwriting config setting "' . $_name . '" with value from config.inc.php.'); $result->value = $configFileData[$_name]; } return $result; }
/** * load a config record from database * * @param string $_name * @param mixed $_application * @return Tinebase_Model_Config */ protected function _loadConfig($_name, $_application = null) { $applicationId = Tinebase_Model_Application::convertApplicationIdToInt($_application ? $_application : $this->_appName); $cache = Tinebase_Core::getCache(); $cacheId = '_loadConfig_' . sha1($applicationId . $_name); if ($cache && $cache->test($cacheId)) { $result = $cache->load($cacheId); if (is_object($result)) { return $result; } } $filter = new Tinebase_Model_ConfigFilter(array(array('field' => 'application_id', 'operator' => 'equals', 'value' => $applicationId), array('field' => 'name', 'operator' => 'equals', 'value' => $_name))); $result = $this->_getBackend()->search($filter)->getFirstRecord(); if ($cache) { $cache->save($result, $cacheId, array('config'), 60); } return $result; }
/** * check if one of the roles the user is in has a given right for a given application * - admin right includes all other rights * * @param string|Tinebase_Model_Application $_application the application (one of: app name, id or record) * @param int $_accountId the numeric id of a user account * @param int $_right the right to check for * @return bool * @throws Tinebase_Exception_AccessDenied */ public function hasRight($_application, $_accountId, $_right) { try { $appId = Tinebase_Model_Application::convertApplicationIdToInt($_application); } catch (Tinebase_Exception_NotFound $tenf) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Application ' . $_application . ' is not installed.'); return false; } $application = Tinebase_Application::getInstance()->getApplicationById($appId); if ($application->status != 'enabled') { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Application ' . $_application . ' is disabled.'); return false; } $roleMemberships = $this->getRoleMemberships($_accountId); if (empty($roleMemberships)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' ' . $_accountId . ' has no role memberships.'); return false; } $select = $this->_roleRightsTable->select(); $select->where($this->_db->quoteInto($this->_db->quoteIdentifier('role_id') . ' IN (?)', $roleMemberships))->where('(' . $this->_db->quoteInto($this->_db->quoteIdentifier('right') . ' = ?', $_right) . ' OR ' . $this->_db->quoteInto($this->_db->quoteIdentifier('right') . ' = ?', Tinebase_Acl_Rights::ADMIN) . ')')->where($this->_db->quoteInto($this->_db->quoteIdentifier('application_id') . ' = ?', $application->getId())); //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $select->__toString()); if (!($row = $this->_roleRightsTable->fetchRow($select))) { $result = false; } else { $result = true; } return $result; }
/** * delete containers, configs and other data of an application * * NOTE: if a table with foreign key constraints to applications is added, we need to make sure that the data is deleted here * * @param Tinebase_Model_Application $_applicationName * @return void */ public function removeApplicationData(Tinebase_Model_Application $_application) { $dataToDelete = array('container' => array('tablename' => ''), 'config' => array('tablename' => ''), 'customfield' => array('tablename' => ''), 'rights' => array('tablename' => 'role_rights'), 'definitions' => array('tablename' => 'importexport_definition'), 'filter' => array('tablename' => 'filter'), 'modlog' => array('tablename' => 'timemachine_modlog'), 'import' => array('tablename' => 'import')); $countMessage = ' Deleted'; $where = array($this->_getDb()->quoteInto($this->_getDb()->quoteIdentifier('application_id') . '= ?', $_application->getId())); foreach ($dataToDelete as $dataType => $info) { switch ($dataType) { case 'container': $count = Tinebase_Container::getInstance()->deleteContainerByApplicationId($_application->getId()); break; case 'config': $count = Tinebase_Config::getInstance()->deleteConfigByApplicationId($_application->getId()); break; case 'customfield': $count = Tinebase_CustomField::getInstance()->deleteCustomFieldsForApplication($_application->getId()); break; default: if ((isset($info['tablename']) || array_key_exists('tablename', $info)) && !empty($info['tablename'])) { try { $count = $this->_getDb()->delete(SQL_TABLE_PREFIX . $info['tablename'], $where); } catch (Zend_Db_Statement_Exception $zdse) { Tinebase_Exception::log($zdse); $count = 0; } } else { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' No tablename defined for ' . $dataType); $count = 0; } } $countMessage .= ' ' . $count . ' ' . $dataType . '(s) /'; } $countMessage .= ' for application ' . $_application->name; Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . $countMessage); }
/** * fill class cache with all config records for this app */ protected function _loadAllAppConfigsInCache() { if (empty($this->_appName)) { if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' appName not set'); } $this->_cachedApplicationConfig = array(); } if (!Tinebase_Application::getInstance()->getInstance()->isInstalled('Tinebase')) { $this->_cachedApplicationConfig = array(); return; } $cache = Tinebase_Core::getCache(); if (!is_object($cache)) { Tinebase_Core::setupCache(); $cache = Tinebase_Core::getCache(); } if (Tinebase_Core::get(Tinebase_Core::SHAREDCACHE)) { if ($cachedApplicationConfig = $cache->load('cachedAppConfig_' . $this->_appName)) { $this->_cachedApplicationConfig = $cachedApplicationConfig; return; } } try { $applicationId = Tinebase_Model_Application::convertApplicationIdToInt($this->_appName); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Loading all configs for app ' . $this->_appName); } $filter = new Tinebase_Model_ConfigFilter(array(array('field' => 'application_id', 'operator' => 'equals', 'value' => $applicationId))); $allConfigs = $this->_getBackend()->search($filter); } catch (Zend_Db_Exception $zdae) { // DB might not exist or tables are not created, yet Tinebase_Exception::log($zdae); $this->_cachedApplicationConfig = array(); return; } if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Found ' . count($allConfigs) . ' configs.'); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($allConfigs->toArray(), TRUE)); } foreach ($allConfigs as $config) { $this->_cachedApplicationConfig[$config->name] = $config; } if (Tinebase_Core::get(Tinebase_Core::SHAREDCACHE)) { $cache->save($this->_cachedApplicationConfig, 'cachedAppConfig_' . $this->_appName); } }
/** * update installed application * * @param Tinebase_Model_Application $_application * @param string $_majorVersion * @return array messages * @throws Setup_Exception if current app version is too high */ public function updateApplication(Tinebase_Model_Application $_application, $_majorVersion) { $setupXml = $this->getSetupXml($_application->name); $messages = array(); switch (version_compare($_application->version, $setupXml->version)) { case -1: $message = "Executing updates for " . $_application->name . " (starting at " . $_application->version . ")"; $messages[] = $message; Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' ' . $message); $version = $_application->getMajorAndMinorVersion(); $minor = $version['minor']; $className = ucfirst($_application->name) . '_Setup_Update_Release' . $_majorVersion; if (!class_exists($className)) { $nextMajorRelease = $_majorVersion + 1 . ".0"; Setup_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . " Update class {$className} does not exists, skipping release {$_majorVersion} for app " . "{$_application->name} and increasing version to {$nextMajorRelease}"); $_application->version = $nextMajorRelease; Tinebase_Application::getInstance()->updateApplication($_application); } else { $update = new $className($this->_backend); $classMethods = get_class_methods($update); // we must do at least one update do { $functionName = 'update_' . $minor; try { $db = Setup_Core::getDb(); $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction($db); Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Updating ' . $_application->name . ' - ' . $functionName); $update->{$functionName}(); Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId); } catch (Exception $e) { Tinebase_TransactionManager::getInstance()->rollBack(); Setup_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . ' ' . $e->getMessage()); Setup_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . ' ' . $e->getTraceAsString()); throw $e; } $minor++; } while (array_search('update_' . $minor, $classMethods) !== false); } $messages[] = "<strong> Updated " . $_application->name . " successfully to " . $_majorVersion . '.' . $minor . "</strong>"; // update app version $updatedApp = Tinebase_Application::getInstance()->getApplicationById($_application->getId()); $_application->version = $updatedApp->version; Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Updated ' . $_application->name . " successfully to " . $_application->version); $this->_updatedApplications++; break; case 0: Setup_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' No update needed for ' . $_application->name); break; case 1: throw new Setup_Exception('Current application version is higher than version from setup.xml: ' . $_application->version . ' > ' . $setupXml->version); break; } Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Clearing cache after update ...'); $this->_enableCaching(); Tinebase_Core::getCache()->clean(Zend_Cache::CLEANING_MODE_ALL); return $messages; }
/** * update existing definition or create new from file * - use backend functions (create/update) directly because we do not want any default controller handling here * - calling function needs to make sure that user has admin right! * * @param string $_filename * @param Tinebase_Model_Application $_application * @param string $_name * @return Tinebase_Model_ImportExportDefinition */ public function updateOrCreateFromFilename($_filename, $_application, $_name = NULL) { $definition = $this->getFromFile($_filename, $_application->getId(), $_name); // try to get definition and update if it exists try { $existing = $this->getByName($definition->name); Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Updating definition: ' . $definition->name); $copyFields = array('filename', 'plugin_options', 'description'); foreach ($copyFields as $field) { $existing->{$field} = $definition->{$field}; } $result = $this->_backend->update($existing); } catch (Tinebase_Exception_NotFound $tenf) { // does not exist Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Creating import/export definion from file: ' . $_filename); $result = $this->_backend->create($definition); } return $result; }
/** * add TMA to "in class" cache and to Zend Cache * * @param Tinebase_Model_Application $_application */ protected function _addToClassCache(Tinebase_Model_Application $_application) { $this->_applicationCache[$_application->getId()] = $_application; $this->_applicationCache[$_application->name] = $_application; }
/** * delete custom fields for an application * * @param string|Tinebase_Model_Application $_applicationId * @return integer numer of deleted custom fields */ public function deleteCustomFieldsForApplication($_applicationId) { $this->_clearCache(); $applicationId = Tinebase_Model_Application::convertApplicationIdToInt($_applicationId); $filterValues = array(array('field' => 'application_id', 'operator' => 'equals', 'value' => $applicationId)); $filter = new Tinebase_Model_CustomField_ConfigFilter($filterValues); $filter->customfieldACLChecks(FALSE); $customFields = $this->_backendConfig->search($filter); $deletedCount = 0; foreach ($customFields as $customField) { $this->deleteCustomField($customField); $deletedCount++; } return $deletedCount; }
/** * removes modlog entries for that application * * @param Tinebase_Model_Application $applicationName * * @return void */ public function removeApplication(Tinebase_Model_Application $_application) { $this->_backend->deleteByProperty($_application->getId(), 'application_id'); }
/** * create inital rights * * @todo make hard coded role names ('user role' and 'admin role') configurable * * @param Tinebase_Application $application * @return void */ public static function createInitialRights(Tinebase_Model_Application $_application) { Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Creating initial rights for application ' . $_application->name); $allRights = Tinebase_Application::getInstance()->getAllRights($_application->getId()); $userRights = static::$_userRoleRights; if (in_array(Tinebase_Acl_Rights::USE_PERSONAL_TAGS, $allRights)) { $userRights[] = Tinebase_Acl_Rights::USE_PERSONAL_TAGS; } $roleRights = array('user role' => $userRights, 'admin role' => $allRights); foreach ($roleRights as $roleName => $rights) { try { $role = Tinebase_Acl_Roles::getInstance()->getRoleByName($roleName); } catch (Tinebase_Exception_NotFound $tenf) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ' . $tenf->getMessage()); continue; } foreach ($rights as $right) { try { Tinebase_Acl_Roles::getInstance()->addSingleRight($role->getId(), $_application->getId(), $right); } catch (Exception $e) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Cannot add right: ' . $right . ' for application: ' . $_application->name . ' - ' . $roleName . ' - ' . print_r($e->getMessage(), true)); } } } }
/** * removes app id (and /folders namespace) from a path * * @param string $_flatpath * @param Tinebase_Model_Application $_application * @return string */ public static function removeAppIdFromPath($_flatpath, $_application) { $appId = $_application->getId(); return preg_replace('@^/' . $appId . '/folders@', '', $_flatpath); }