oneduo / nova-file-manager

The most advanced File Manager for Laravel Nova, feature rich and robust build.
https://oneduo.github.io/nova-file-manager/
MIT License
138 stars 38 forks source link

Different root for specific user roles #104

Closed Elvinca closed 1 year ago

Elvinca commented 1 year ago

Hey there, I would like to know how can I set root directory for file manager for different user roles. For instance, we have admin and employee user role. My folder structure looks like this

public // main root ----users
-------employee -----------bob // bob's root -----------frank -------admin -----------admin1 -----------admin2 ----other ......

I want for instance Bob to be able to see just his folder and everything in that folder. And Admins to see everything from main root

I tried to create on-demand filesystem like this:

image

But it saved media to database with "default" disk which i don't have (I am using public) and path relative to bob's root. I need every image to be saved relative to main root.

Any ideas ?

Thanks in advance.

Rezrazi commented 1 year ago

Hi,

The on-demand filesystem feature on the file manager saves the disk name as default as your disk wouldn't actually have a name, you need to apply your same business logic when you'll need to retrieve the assets from the disks.

Otherwise you'll need to create those disks in filesystems.php with specific names and then reuse that name with Storage::disk() if you want ease of access to the assets.

Regards Charaf

mikaelpopowicz commented 1 year ago

You may add a method on your user model which provides the disk instance. So you will be able to configure the filemanager and also retrieve the user's files

<?php

namespace App\Models;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Oneduo\NovaFileManager\Casts\Asset;

class User extends Authenticatable
{
    protected $fillable = [
        'role',
        'slug',
        'avatar',
        'name',
        'email',
        'password',
    ];

    protected $casts = [
        'avatar' => Asset::class,
    ];

    public function storage(): Filesystem
    {
        return Storage::build([
            'driver' => 'local',
            'root' => storage_path("app/public/pouzivatelia/{$this->role}/{$this->slug}"),
            'url' => config('app.url')."/storage/pouzivatelia/{$this->role}/{$this->slug}",
        ]);
    }

    // this is an example on how to use the custom filesystem
    public function avatarExists(): bool
    {
        if (!$this->avatar) {
            return false;
        }

        return $this->storage()->exists($this->avatar->path);
    }
}

You may configure the field like this :

<?php

use Laravel\Nova\Http\Requests\NovaRequest;
use Oneduo\NovaFileManager\FileManager;

class User extends Resource
{
    // ...

    public function fields(NovaRequest $request): array
    {
        return [
            // ... any other fields
            FileManager::make(__('Avatar'), 'avatar')
                ->filesystem(fn(NovaRequest $request) => $request->user()->storage()),
        ];
    }
}