Permissions for files without a set in Concrete5

The question

Concrete5 community member BlueFractals asked:

In Dashboard > File Manager, is there a way to prevent specific users or group from accessing 'Files in no sets'?

After digging through the advanced permissions I discovered it wasn't possible; permissions are assigned to file sets and not files themselves.

The solution

Let's fight fire with fire and assign set-less files to a set!

Manually assigning files to a set1 defeats the purpose of them being set-less, thankfully we can automate this by extending the events below along with a new fileset named 'Setless'.

on_file_add

Add file to 'Setless'.

on_file_added_to_set

If file is in multiple sets and 'Setless' is one of them, remove from 'Setless'.

on_file_removed_from_set

If file is no longer in any sets, add to 'Setless'.

If you would like to take a closer look at the code or use it on one of your Concrete5 sites, I've made a packaged version on GitHub. View the 5.6 branch if you have a legacy 5.6 site, otherwise stick to the Master branch.

View on GitHub


Update 1: Updated for 5.7+

1 There is a one-time requirement of manually adding existing files without a set to the 'Setless' set, but then you're set.