Any way to reproduce afterFind inside a Behavior in 4.x

I’m encrypting some fileds on my database.
I’ve created an EncryptBehavior and I load it on desired Tables passing desired columns to it.

It is working fine but I’d like to override retrieved data depending on the group of the user. If the user is an admin I want those fields values to be changed to placeholders like ********.

Since I created an encrypted Type, I tried to get user group from Session or with UserAuth methods but I can access these (or I don’t know how) from EncryptedType Class (located in src/Database/Type.
If I could know the user group by that time, I could make toPHP() method to return data depending on user group.

Then, I tried using afterFind method but it was deprecated.


I don’t want to use mapReduce() on every get(), find() or query() statement.

Is it possible to replace retrieved data with a Behavior?
(or inside EncryptedType::toPHP() method, *see above)


I’ve found a solution.
Althought, I appreciate is you can improve this or know a better workaround. Thanks.

Inside the Behavior:

     * Depending on the user's group, it returns placeholders instead of the field
    public function beforeFind(Event $event, Query $query, $options, $primary)
        $session = new Session();
        //debug($this->_config); exit;
        $group_id = (int)$session->read('Auth.User.user_group_id');
        if (in_array($group_id, $this->_config['hide_to_groups'])) {
            $query->formatResults(function (\Cake\Collection\CollectionInterface $rows) {
                return $rows->map(function ($row) {
                    foreach ($this->_config['fields'] as $field) {
                        if (isset($row[$field]) && !empty($row[$field])) {
                            $random_length = strlen($row[$field]) + rand(0,6)-3; // I remove or add up to 3 characters
                            $random_length = $random_length <= 0 ? 1 : $random_length; // Minimum must be one character
                            $row[$field] = str_repeat($this->_config['placeholder'], $random_length); // I replace it

                    return $row;