Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use the aclRule array and change pemission with respect to user in runtime? #12

Closed
Ahmadzia307 opened this issue Feb 8, 2019 · 9 comments

Comments

@Ahmadzia307
Copy link

Ahmadzia307 commented Feb 8, 2019

->file-manager file as follow:

['public'], 'middleware' => ['web','fm-acl'], 'acl' => true, 'aclHideFromFM' => true, 'aclStrategy' => 'blacklist', 'aclRepository' => Alexusmai\LaravelFileManager\ACLService\ConfigACLRepository::class, 'aclRules' => [ 1 => [ ['disk' => 'public', 'path' => 'files/*', 'access' => 2], ], ], ];
As I am setting the acl mode to true and using acl rule.
my view code is in the picture.
![screenshot from 2019-02-08 12-10-35](https://user-images.githubusercontent.com/24617277/52477655-9d911600-2b9a-11e9-8caf-853dcb88c976.png)
but when I run my application and check the file manager. The directory it shows is different then, what is set in the aclRule array, as shown below
![screenshot from 2019-02-08 12-25-55](https://user-images.githubusercontent.com/24617277/52478256-b4386c80-2b9c-11e9-81ca-0e757787e23e.png)
@alexusmai
Copy link
Owner

You probably did not configure everything as necessary. ACL is needed only to restrict user access to files and folders. And the main directory that will be displayed is set in the disk settings. See Laravel docs - https://laravel.com/docs/5.6/filesystem , in /config/filesystems.php set your disks, and then add disks what you want to FM config (/config/file-manager.php)

// Example
/**
     * list of disk names that you want to use
     * (from config/filesystems)
     */
    'diskList'  => ['images', 'docs', 'template', 'sftp', 'yandex'],

@Ahmadzia307
Copy link
Author

Thank you for replying. One more thing, how I can use aclRule array in the laravel. Can you add some example in the documentation that would be very helpful?
So far, I am able to use the file manager with disklist =>['local']. Just I wasn't able to apply the aclRule array on the directory.

@alexusmai
Copy link
Owner

alexusmai commented Feb 8, 2019

Example:

I have disk 'images' in /config/filesystems.php for folder /public/images

'disks' => [

        'images' => [
            'driver' => 'local',
            'root'   => public_path('images'),
            'url'    => '/images/',
        ],
]

This disk contain:

/                  // disk root folder
|-- nature    // folder
|-- cars        // folder
|-- icons
|-- image1.jpg   // file
|-- image2.jpg
|-- avatar.png

I add this disk to file-manager config file

'diskList'  => ['images'],

'aclStrategy'   => 'blacklist',

// now it's a black list
'aclRules'      => [
       // null - for not authenticated users
        null => [
            ['disk' => 'images', 'path' => 'nature', 'access' => 0],    // guest don't have access for this folder
            ['disk' => 'images', 'path' => 'icons', 'access' => 1],    // only read - guest can't change folder - rename, delete
            ['disk' => 'images', 'path' => 'icons/*', 'access' => 1],     // only read all files and foders in this folder
            ['disk' => 'images', 'path' => 'image*.jpg', 'access' => 0], // can't read and write (preview, rename, delete..)
            ['disk' => 'images', 'path' => 'avatar.png', 'access' => 1],     // only read (view)

        ],
        // for user with ID = 1
        1 => [
            ['disk' => 'images', 'path' => 'cars', 'access' => 0],    // don't have access
            ['disk' => 'public', 'path' => 'image*.jpg', 'access' => 1],    // only read (view)
        ],
    ],

@Ahmadzia307
Copy link
Author

Thanks alot. I just tried it and it worked.
In case of DB , Can I make my own class of the Dbrespository with two functions getID() and getRules?
Can you give me some idea how I can do it?
Right now, it is in vendor and with User table. Also,I want to change the table with my own table.

@alexusmai
Copy link
Owner

Yes, you can create your own class, for example in the folder /app/HTTP

<?php

namespace App\Http;

use Alexusmai\LaravelFileManager\ACLService\ACLRepository;

class MyACLRepository implements ACLRepository
{
    /**
     * Get user ID
     *
     * @return mixed
     */
    public function getUserID()
    {
        return \Auth::id();
    }

    /**
     * Get ACL rules list for user
     *
     * @return array
     */
    public function getRules(): array
    {
        return \DB::table('acl_rules')
            ->where('user_id', $this->getUserID())
            ->get(['disk', 'path', 'access'])
            ->map(function ($item) {
                return get_object_vars($item);
            })
            ->all();
    }
}

in /config/file-manager change:

    'aclRepository' => \App\Http\MyACLRepository::class,

Warning the getRules() method must return an array, not a object !!! see my example!

@Ahmadzia307
Copy link
Author

Ahmadzia307 commented Feb 11, 2019

Hi alex,
I am having a problem using acl rules with "whitelist". My Aclrule is below:
I have folders in local which is in
"public/files/"
-1
-2
-img

'aclStrategy' => 'whitelist',
aclRules' => [
null => [
['disk' => 'local', 'path' => 'img', 'access' => 2],
['disk' => 'local', 'path' => '1', 'access' => 0],
],
]`

Then when I run this, I got the error, Acess Denied.
screenshot from 2019-02-11 07-48-50

@Ahmadzia307
Copy link
Author

Ahmadzia307 commented Feb 11, 2019

In case of blacklist, It is working.
screenshot from 2019-02-11 07-52-24

@alexusmai
Copy link
Owner

alexusmai commented Feb 11, 2019

whitelist - Deny everything, that not allowed by the ACL rules list.

Main folder too!!!

Add new rule:

['disk' => 'local', 'path' => '/', 'access' => 2],

OR

['disk' => 'local', 'path' => '/', 'access' => 1],

@Ahmadzia307
Copy link
Author

Thank you. It is working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants