Skip to content

Instantly share code, notes, and snippets.

@ryansnowden
Last active December 10, 2015 05:18
Show Gist options
  • Save ryansnowden/4386623 to your computer and use it in GitHub Desktop.
Save ryansnowden/4386623 to your computer and use it in GitHub Desktop.
saveField for $this->request->data and multiple models
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