Last active
December 10, 2015 05:18
-
-
Save ryansnowden/4386623 to your computer and use it in GitHub Desktop.
saveField for $this->request->data and multiple models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function saveField() { | |
$this->autoRender = false; | |
if (!$this->request->data) { | |
return json_encode(array( | |
'failed' => true, | |
'newValue' => '', | |
'message' => 'Data missing', | |
)); | |
} | |
// User or Profile Model | |
$model = key($this->request->data); | |
// Profile ID or User ID | |
$id = $this->request->data[$model]['id']; | |
// Check that the logged in user is the owner of the profile | |
// Skip if admin | |
$role = $this->Auth->user('Role.alias'); | |
if ($role != "admin") { | |
if (!$this->Auth->user("$model.id") == $id) { | |
return json_encode(array( | |
'failed' => true, | |
'newValue' => '', | |
'message' => 'Security error' | |
)); | |
} | |
} | |
// Validate data and save with fieldList | |
$this->{$model}->id = $Id; | |
if (!$this->{$model}->save($this->request->data)) { | |
return json_encode(array( | |
'failed' => true, | |
'newValue' => $value, | |
'message' => "Saving $fieldName failed" | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment