Private Assets per User

A long time ago had similar request but also isolating in the filesystem the uploads of each user, I ended to doing something like below:

<?php

$this->on('cockpit.assets.find', function(&$options) use ($app) {
  $user = $app->module('cockpit')->getUser();
  $options['filter']['_by'] = $user['_id'];
});

$this->on('cockpit.asset.upload', function (&$asset, &$_meta, &$opts, &$file, &$path) use ($app) {
  $user = $app->module('cockpit')->getUser();
  if (!$app->helper('fs')->path("assets://{$user['_id']}")) {
    $app->helper('fs')->mkdir("assets://{$user['_id']}");
  }

  $clean = uniqid().preg_replace('/[^a-zA-Z0-9-_\.]/','', str_replace(' ', '-', $asset['title']));
  $path  = '/' . $user['_id'] . '/' .date('Y/m/d') . '/' . $clean;
  $asset['path'] = $path;
});


$this->on('cockpit.assetsfolders.find.before', function(&$options) use ($app) {
  $user = $app->module('cockpit')->getUser();
  $options['filter']['_by'] = $user['_id'];
});