Probably there are some fields in your models, usually holding sensitive data, that you do not want to display to the end users. In my case these are passwords, activation codes, etc. I want to ensure that values stored in these fields will be never exposed, neither intentionally nor by accident. So I came up with this little solution.
This is implementation of afterFind() callback function that should be placed inside of app_model.php file.
// app_model.php class AppModel extends Model { var $hiddenFields = array(); function afterFind($result, $primary = false) { foreach ($result as &$row) foreach ($this->hiddenFields as $field) if (isset($row[$this->name][$field])) unset($row[$this->name][$field]); return $result; } }
The last thing to do is to define $hiddenFields variable in your model.
var $hiddenFields = array('password', 'activation_code');
As you may have noticed, values of these fields are retrieved from the database and later unset. If you don’t want them to be retrieved at all (this would be more secure), you should take care of filtering this fields out in beforeFind() callback method, however this could be quite cumbersome. For me it was OK enough to just unset them after retrieval, so I even didn’t try to do it through beforeFind().
There are also drawbacks. For example you can’t retrieve someone’s password and send it by e-mail when someone forgets it (in fact you can still get it with model’s query() method, but it’s not a ‘clean’ way). In such situations you should generate new random password instead of reminding the forgotten one.
If you have any views on this, please leave a comment.