# Private Assets per User

**URL:** <https://discourse.getcockpit.com/t/private-assets-per-user/1560>\
**Category:** General\
**Created:** [July 21, 2020, 9:54pm UTC](https://discourse.getcockpit.com/t/private-assets-per-user/1560 "2020-07-21T21:54:17Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![pauloamgomes](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/pauloamgomes/32/48_2.png) [@pauloamgomes](https://discourse.getcockpit.com/u/pauloamgomes)\
**Post date:** [July 23, 2020, 9:27pm UTC](https://discourse.getcockpit.com/t/private-assets-per-user/1560/3 "2020-07-23T21:27:21Z")

</div>

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:

```auto
<?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'];
});

```

---

_[View the full topic](https://discourse.getcockpit.com/t/private-assets-per-user/1560)._
