/**
  * all grants for configs given by array of ids
  * 
  * @param string $_accountId
  * @param array $_ids => account_grants
  * @return array
  */
 public function getAclForIds($_accountId, $_ids)
 {
     $result = array();
     if (empty($_ids)) {
         return $result;
     }
     $select = $this->_getAclSelect(array('id' => 'customfield_config.id', 'account_grants' => $this->_dbCommand->getAggregate('customfield_acl.account_grant')));
     $select->where($this->_db->quoteInto($this->_db->quoteIdentifier('customfield_config.id') . ' IN (?)', (array) $_ids))->group(array('customfield_config.id', 'customfield_acl.account_type', 'customfield_acl.account_id'));
     Tinebase_Container::addGrantsSql($select, $_accountId, Tinebase_Model_CustomField_Grant::getAllGrants(), 'customfield_acl');
     //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $select->__toString());
     $stmt = $this->_db->query($select);
     $rows = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
     foreach ($rows as $row) {
         $result[$row['id']] = $row['account_grants'];
     }
     return $result;
 }
 /**
  * test custom fields
  *
  * - add custom field
  * - get custom fields for app
  * - delete custom field
  */
 public function testCustomFields()
 {
     // create
     $customField = self::getCustomField();
     $createdCustomField = $this->_instance->addCustomField($customField);
     $this->assertEquals($customField->name, $createdCustomField->name);
     $this->assertNotNull($createdCustomField->getId());
     // fetch
     $application = Tinebase_Application::getInstance()->getApplicationByName('Tinebase');
     $appCustomFields = $this->_instance->getCustomFieldsForApplication($application->getId());
     $this->assertGreaterThan(0, count($appCustomFields));
     $this->assertEquals($application->getId(), $appCustomFields[0]->application_id);
     // check with model name
     $appCustomFieldsWithModelName = $this->_instance->getCustomFieldsForApplication($application->getId(), $customField->model);
     $this->assertGreaterThan(0, count($appCustomFieldsWithModelName));
     $this->assertEquals($customField->model, $appCustomFieldsWithModelName[0]->model, 'didn\'t get correct model name');
     // check if grants are returned
     $this->_instance->resolveConfigGrants($appCustomFields);
     $accountGrants = $appCustomFields->getFirstRecord()->account_grants;
     sort($accountGrants);
     $this->assertEquals(Tinebase_Model_CustomField_Grant::getAllGrants(), $accountGrants);
     // delete
     $this->_instance->deleteCustomField($createdCustomField);
     $this->setExpectedException('Tinebase_Exception_NotFound');
     $this->_instance->getCustomField($createdCustomField->getId());
 }
Example #3
0
 /**
  * update to 3.16
  * - add customfield_acl table
  */
 public function update_15()
 {
     $declaration = new Setup_Backend_Schema_Table_Xml('<table>
         <name>customfield_acl</name>
         <version>1</version>
         <declaration>
             <field>
                 <name>id</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>true</notnull>
             </field>
             <field>
                 <name>customfield_id</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>true</notnull>
             </field>
             <field>
                 <name>account_type</name>
                 <type>enum</type>
                 <value>anyone</value>
                 <value>user</value>
                 <value>group</value>
                 <notnull>true</notnull>
             </field>
             <field>
                 <name>account_id</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>true</notnull>
             </field>
             <field>
                 <name>account_grant</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>true</notnull>
             </field>
             <index>
                 <name>customfield_id-account-type-account_id-account_grant</name>
                 <primary>true</primary>
                 <field>
                     <name>id</name>
                 </field>
                 <field>
                     <name>customfield_id</name>
                 </field>
                 <field>
                     <name>account_type</name>
                 </field>
                 <field>
                     <name>account_id</name>
                 </field>
                 <field>
                     <name>account_grant</name>
                 </field>
             </index>
             <index>
                 <name>id-account_type-account_id</name>
                 <field>
                     <name>customfield_id</name>
                 </field>
                 <field>
                     <name>account_type</name>
                 </field>
                 <field>
                     <name>account_id</name>
                 </field>
             </index>
             <index>
                 <name>customfield_acl::customfield_id--customfield_config::id</name>
                 <field>
                     <name>customfield_id</name>
                 </field>
                 <foreign>true</foreign>
                 <reference>
                     <table>customfield_config</table>
                     <field>id</field>
                     <ondelete>cascade</ondelete>
                 </reference>
             </index>
         </declaration>
     </table>');
     $this->createTable('customfield_acl', $declaration);
     // add grants to existing customfields
     $configBackend = new Tinebase_CustomField_Config();
     $allCfConfigs = $configBackend->search();
     foreach ($allCfConfigs as $cfConfig) {
         Tinebase_CustomField::getInstance()->setGrants($cfConfig, Tinebase_Model_CustomField_Grant::getAllGrants());
     }
     $this->setApplicationVersion('Tinebase', '3.16');
 }
Example #4
0
 /**
  * set grants for custom field
  *
  * @param   string|Tinebase_Model_CustomField_Config $_customfieldId
  * @param   array $_grants list of grants to add
  * @param   string $_accountType
  * @param   int $_accountId
  * @return  void
  */
 public function setGrants($_customfieldId, $_grants = array(), $_accountType = NULL, $_accountId = NULL)
 {
     $cfId = $_customfieldId instanceof Tinebase_Model_CustomField_Config ? $_customfieldId->getId() : $_customfieldId;
     try {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Setting grants for custom field ' . $cfId . ' -> ' . print_r($_grants, TRUE));
         }
         $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb());
         $this->_backendACL->deleteByProperty($cfId, 'customfield_id');
         foreach ($_grants as $grant) {
             if (in_array($grant, Tinebase_Model_CustomField_Grant::getAllGrants())) {
                 $newGrant = new Tinebase_Model_CustomField_Grant(array('customfield_id' => $cfId, 'account_type' => $_accountType !== NULL ? $_accountType : Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE, 'account_id' => $_accountId !== NULL ? $_accountId : 0, 'account_grant' => $grant));
                 $this->_backendACL->create($newGrant);
             }
         }
         Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId);
         $this->_clearCache();
     } catch (Exception $e) {
         Tinebase_TransactionManager::getInstance()->rollBack();
         throw new Tinebase_Exception_Backend($e->getMessage());
     }
 }