Exemple #1
0
 /**
  * Help to save a model by setting the models properties.
  * Validation is based on the ModelInformation implementation.
  *
  * @param Phprojekt_Model_Interface $model  The model
  * @param array                     $params The parameters used to feed the model.
  *
  * @throws Exception If validation of parameters fails.
  *
  * @return boolean True for a sucessful save.
  */
 protected static function _saveModel(Phprojekt_Model_Interface $model, array $params)
 {
     foreach ($params as $k => $v) {
         if (isset($model->{$k})) {
             // Don't allow to set the id on save, since it is done by the ActiveRecord
             if (!in_array($k, array('id'))) {
                 $model->{$k} = $v;
             }
         }
     }
     if (empty($model->id)) {
         $newItem = true;
     } else {
         $newItem = false;
     }
     // Set the owner
     if ($newItem && isset($model->ownerId)) {
         $model->ownerId = Phprojekt_Auth::getUserId();
     }
     // Parent Project
     if (isset($model->projectId)) {
         $projectId = $model->projectId;
     } else {
         $projectId = 0;
     }
     // Checks
     $moduleName = Phprojekt_Loader::getModuleFromObject($model);
     $moduleId = Phprojekt_Module::getId($moduleName);
     if (!$model->recordValidate()) {
         $errors = $model->getError();
         $error = array_pop($errors);
         throw new Phprojekt_PublishedException($error['label'] . ': ' . $error['message']);
     } else {
         if (!self::_checkModule($moduleId, $projectId)) {
             throw new Phprojekt_PublishedException('The parent project do not have enabled this module');
         } else {
             if (!self::_checkItemRights($model, $moduleName)) {
                 throw new Phprojekt_PublishedException('You do not have access to do this action');
             } else {
                 // Set the projectId to 1 for global modules
                 if (isset($model->projectId) && Phprojekt_Module::saveTypeIsGlobal($moduleId)) {
                     $model->projectId = 1;
                 }
                 $model->save();
                 // Save access only if the user have "admin" right
                 $itemRights = Phprojekt_Loader::getLibraryClass('Phprojekt_Item_Rights');
                 $check = $itemRights->getRights($moduleId, $model->id);
                 if ($check['currentUser']['admin']) {
                     if ($moduleName == 'Core') {
                         $rights = Default_Helpers_Right::getModuleRights($params);
                     } else {
                         $rights = Default_Helpers_Right::getItemRights($params, $moduleId, $newItem);
                     }
                     if (count($rights) > 0) {
                         $model->saveRights($rights);
                     }
                 }
                 return $model;
             }
         }
     }
 }
Exemple #2
0
 /**
  * Test getModuleFromObject
  */
 public function testGetModuleFromObject()
 {
     $object = Phprojekt_Loader::getModel('Todo', 'Todo');
     $this->assertEquals('Todo', Phprojekt_Loader::getModuleFromObject($object));
 }
Exemple #3
0
 /**
  * Help to delete a model.
  *
  * @param Phprojekt_ActiveRecord_Abstract $model The model to delete.
  *
  * @throws Exception If validation fails.
  *
  * @return boolean True for a sucessful delete.
  */
 protected static function _deleteModel(Phprojekt_ActiveRecord_Abstract $model)
 {
     // Checks
     $moduleName = Phprojekt_Loader::getModuleFromObject($model);
     if (!self::_checkItemRights($model, $moduleName)) {
         throw new Phprojekt_PublishedException('You do not have access to do this action');
     } else {
         $return = $model->delete();
         if (isset($return->id) && null === $return->id || null === $return) {
             // ActiveRecord delete the model.
             return true;
         } else {
             if (is_bool($return)) {
                 // An extention returns true or false.
                 return $return;
             } else {
                 // Any other value, is wrong.
                 return false;
             }
         }
     }
 }
Exemple #4
0
 /**
  * Help to delete a model.
  *
  * @param Phprojekt_ActiveRecord_Abstract $model The model to delete.
  *
  * @throws Exception If validation fails.
  *
  * @return boolean True for a sucessful delete.
  */
 protected static function _deleteModel(Phprojekt_ActiveRecord_Abstract $model)
 {
     // Checks
     $moduleName = Phprojekt_Loader::getModuleFromObject($model);
     if (!self::_checkItemRights($model, $moduleName)) {
         throw new Zend_Controller_Action_Exception('You do not have access to do this action', 400);
     } else {
         $return = $model->delete();
         if (is_bool($return)) {
             // An extention returns true or false.
             return $return;
         } else {
             if (is_null($return) || is_a($return, 'Phprojekt_ActiveRecord_Abstract') && is_null($return->id)) {
                 // ActiveRecord delete the model.
                 return true;
             } else {
                 // Any other value, is wrong.
                 return false;
             }
         }
     }
 }
Exemple #5
0
 /**
  * Help to delete a model.
  *
  * @param Phprojekt_Model_Interface $model The model to delete.
  *
  * @throws Exception If validation fails.
  *
  * @return boolean True for a sucessful delete.
  */
 protected static function _deleteModel(Phprojekt_Model_Interface $model)
 {
     // Checks
     $moduleName = Phprojekt_Loader::getModuleFromObject($model);
     if (!self::_checkItemRights($model, $moduleName)) {
         throw new Phprojekt_PublishedException('You do not have access to do this action');
     } else {
         return $model->delete();
     }
 }
Exemple #6
0
 /**
  * Help to save a model by setting the models properties.
  * Validation is based on the ModelInformation implementation.
  *
  * @param Phprojekt_Model_Interface $model  The model
  * @param array                     $params The parameters used to feed the model.
  *
  * @throws Exception If validation of parameters fails.
  *
  * @return boolean True for a sucessful save.
  */
 protected static function _saveModel(Phprojekt_Model_Interface $model, array $params)
 {
     $newItem = empty($params['id']);
     $model = self::parameterToModel($model, $params, $newItem);
     $projectId = $model->hasField('projectId') ? $model->projectId : 0;
     $userId = Phprojekt_Auth_Proxy::getEffectiveUserId();
     $moduleName = Phprojekt_Loader::getModuleFromObject($model);
     $moduleId = Phprojekt_Module::getId($moduleName);
     if (!$model->recordValidate()) {
         $errors = $model->getError();
         $error = array_pop($errors);
         throw new Zend_Controller_Action_Exception($error['label'] . ': ' . $error['message'], 400);
     }
     if (!self::_checkModule($moduleId, $projectId)) {
         throw new Zend_Controller_Action_Exception('The parent project do not have enabled this module', 400);
     }
     $rights = Default_Helpers_Right::getRights($params);
     if ($model instanceof Phprojekt_Item_Abstract) {
         if ($newItem && !Phprojekt_Module::saveTypeIsGlobal($moduleId)) {
             $project = new Project_Models_Project();
             $project->find($projectId);
             if (!$project->hasRight($userId, Phprojekt_Acl::CREATE)) {
                 throw new Zend_Controller_Action_Exception('You do not have the necessary create right', 403);
             }
             $rights[$userId] = Phprojekt_Acl::ALL;
         } else {
             if (!$model->hasRight($userId, Phprojekt_Acl::WRITE)) {
                 throw new Zend_Controller_Action_Exception('You do not have the necessary write right', 403);
             }
         }
         // Set the projectId to 1 for global modules
         // @TODO Remove the Timecard limitation
         if ($model->hasField('projectId') && Phprojekt_Module::saveTypeIsGlobal($moduleId) && Phprojekt_Module::getModuleName($moduleId) != 'Timecard') {
             $model->projectId = 1;
         }
         $model->save();
         // Save access only if the user have "admin" right
         if ($newItem || $model->hasRight(Phprojekt_Auth_Proxy::getEffectiveUserId(), Phprojekt_Acl::ADMIN)) {
             if (!Phprojekt_Auth_Proxy::isAdminUser() && count($rights) <= 0) {
                 throw new Zend_Controller_Action_Exception('At least one person must have access to this item', 400);
             }
             $model->saveRights($rights);
         }
     } else {
         $model->save();
         $model->saveRights($rights);
     }
     return $model;
 }