diff --git a/app/Controller/DecayingModelController.php b/app/Controller/DecayingModelController.php index c7c945ec3..979a2c77e 100644 --- a/app/Controller/DecayingModelController.php +++ b/app/Controller/DecayingModelController.php @@ -20,7 +20,7 @@ class DecayingModelController extends AppController } if ($this->request->is('post') || $this->request->is('put')) { - $this->DecayingModel->update($force); + $this->DecayingModel->update($force, $this->Auth->user()); $message = __('Default decaying models updated'); if ($this->_isRest()) { return $this->RestResponse->saveSuccessResponse('DecayingModel', 'update', false, $this->response->type(), $message); diff --git a/app/Controller/DecayingModelMappingController.php b/app/Controller/DecayingModelMappingController.php index a91c3ff55..d677655cd 100644 --- a/app/Controller/DecayingModelMappingController.php +++ b/app/Controller/DecayingModelMappingController.php @@ -38,7 +38,7 @@ class DecayingModelMappingController extends AppController unset($this->request->data['DecayingModelMapping']['attributetypes']); } - $response = $this->DecayingModelMapping->resetMappingForModel($this->request->data['DecayingModelMapping']); + $response = $this->DecayingModelMapping->resetMappingForModel($this->request->data['DecayingModelMapping'], $this->Auth->user()); return $this->RestResponse->viewData($response, $this->response->type()); } else { $this->set('model_id', $model_id); diff --git a/app/Model/AppModel.php b/app/Model/AppModel.php index 71ac48aa1..38ec10f92 100644 --- a/app/Model/AppModel.php +++ b/app/Model/AppModel.php @@ -1215,7 +1215,6 @@ class AppModel extends Model ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; $sqlArray[] = "CREATE TABLE IF NOT EXISTS decaying_model_mappings ( `id` int(11) NOT NULL AUTO_INCREMENT, - `org_id` int(11), `attribute_type` varchar(255) COLLATE utf8_bin NOT NULL, `model_id` int(11) NOT NULL, PRIMARY KEY (id) diff --git a/app/Model/DecayingModel.php b/app/Model/DecayingModel.php index 7dd091ae2..cbc9ca1a2 100644 --- a/app/Model/DecayingModel.php +++ b/app/Model/DecayingModel.php @@ -134,7 +134,7 @@ class DecayingModel extends AppModel return $models; } - public function update($force=false) + public function update($force=false, $user) { $new_models = $this->__load_models($force); $temp = $this->find('all', array( @@ -150,13 +150,13 @@ class DecayingModel extends AppModel if ($force || $new_model['version'] > $existing_model['version']) { $new_model['id'] = $existing_model['id']; $this->save($new_model); - $this->DecayingModelMapping->resetMappingForModel($new_model); + $this->DecayingModelMapping->resetMappingForModel($new_model, $user); } } else { $this->create(); $this->save($new_model); $new_model['id'] = $this->Model->id; - $this->DecayingModelMapping->resetMappingForModel($new_model); + $this->DecayingModelMapping->resetMappingForModel($new_model, $user); } } } @@ -210,7 +210,7 @@ class DecayingModel extends AppModel try { $model = $this->fetchModel($user, $id, $full, $conditions); $models[] = $model; - } catch (MethodNotAllowedException $e) { + } catch (NotFoundException $e) { // Just don't add the model to the result } } @@ -246,7 +246,7 @@ class DecayingModel extends AppModel } if ($full) { - $decayingModel['DecayingModel']['attribute_types'] = $this->DecayingModelMapping->getAssociatedTypes($user, $decayingModel['DecayingModel']['id']); + $decayingModel['DecayingModel']['attribute_types'] = $this->DecayingModelMapping->getAssociatedTypes($user, $decayingModel); } return $decayingModel; } diff --git a/app/Model/DecayingModelMapping.php b/app/Model/DecayingModelMapping.php index 6da2edc19..fed249b97 100644 --- a/app/Model/DecayingModelMapping.php +++ b/app/Model/DecayingModelMapping.php @@ -6,11 +6,6 @@ class DecayingModelMapping extends AppModel public $actsAs = array('Containable'); public $validate = array( - 'org_id' => array( - 'valueNotEmpty' => array( - 'rule' => array('valueNotEmpty'), - ), - ), 'attribute_type' => array( 'valueNotEmpty' => array( 'rule' => array('valueNotEmpty'), @@ -30,14 +25,8 @@ class DecayingModelMapping extends AppModel ) ); - private $__default_type_mapping_reverse = array(); - - public function resetMappingForModel($new_model) { - if (!isset($new_model['org_id'])) { - $new_model['org_id'] = null; - } + public function resetMappingForModel($new_model, $user) { $this->deleteAll(array( - 'DecayingModelMapping.org_id' => $new_model['org_id'], 'model_id' => $new_model['model_id'] )); @@ -47,9 +36,6 @@ class DecayingModelMapping extends AppModel 'attribute_type' => $type, 'model_id' => $new_model['model_id'] ); - if (!is_null($new_model['org_id'])) { - $to_save['org_id'] = $new_model['org_id']; - } $data[] = $to_save; } @@ -63,47 +49,47 @@ class DecayingModelMapping extends AppModel } } - public function getAssociatedTypes($user, $model_id) { - $decaying_model = $this->DecayingModel->find('first', array( - 'conditions' => array('id' => $model_id), - 'recursive' => -1, - )); - if (empty($decaying_model)) { - $associated_types = array(); - } else { - $decaying_model = $decaying_model['DecayingModel']; + public function getAssociatedTypes($user, $model) { + if (is_numeric($model)) { + $model = $this->DecayingModel->fetchModel($user, $model, false); + } + $decaying_model = isset($model['DecayingModel']) ? $model['DecayingModel'] : $model; + if ($decaying_model['isDefault']) { $associated_types = $decaying_model['attribute_types']; + } else { $temp = $this->find('list', array( 'conditions' => array( - 'OR' => array( - array('org_id' => $user['Organisation']['id']), - array('org_id' => null), - ), - 'model_id' => $model_id + 'model_id' => $decaying_model['id'] ), 'recursive' => -1, 'fields' => array('attribute_type') )); - $associated_types = array_values(array_unique(array_merge($associated_types, $temp))); + $associated_types = array_values($temp); } return $associated_types; } public function getAssociatedModels($user, $attribute_type = array()) { - $conditions = array( - 'OR' => array( - array('org_id' => $user['Organisation']['id']), - array('org_id' => null), - ) - ); - if (!empty($attribute_type)) { - $conditions['attribute_type'] = $attribute_type; - } $associated_models = $this->find('all', array( - 'conditions' => $conditions, + 'conditions' => array( + 'attribute_type' => $attribute_type, + 'OR' => array( + 'DecayingModel.org_id' => $user['org_id'], + 'DecayingModel.all_orgs' => true + ) + ), 'recursive' => -1, - // 'group' => 'attribute_type', - 'fields' => array('attribute_type', 'model_id') + 'fields' => array('attribute_type', 'model_id'), + 'joins' => array( // joins has to be done to enforce ACL + array( + 'table' => 'decaying_models', + 'alias' => 'DecayingModel', + 'type' => 'INNER', + 'conditions' => array( + 'DecayingModel.id = DecayingModelMapping.model_id' + ) + ) + ) )); $associated_models = Hash::combine($associated_models, '{n}.DecayingModelMapping.model_id', '{n}.DecayingModelMapping.model_id', '{n}.DecayingModelMapping.attribute_type'); return $associated_models;