-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
779 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
## ACL | ||
|
||
You can use the access control system to differentiate access to files and folders for different users. | ||
For this you need to make the following settings. | ||
Open configuration file - config/file-manager.php | ||
|
||
1. Turn ON ACL system and add fm-acl middleware | ||
|
||
```php | ||
// set true | ||
'acl' => true, | ||
|
||
// add acl middleware to your array | ||
'middleware' => ['web', 'fm-acl'], | ||
``` | ||
|
||
2. You can hide files and folders to which the user does not have access(access = 0). | ||
|
||
```php | ||
'aclHideFromFM' => true, | ||
``` | ||
3. ACL system operation strategies: | ||
|
||
```php | ||
/** | ||
* ACL strategy | ||
* | ||
* positive - Allow everything that is not forbidden by the ACL rules list | ||
* positive access - 2 (r/w) | ||
* | ||
* negative - Deny anything, that not allowed by the ACL rules list | ||
* negative access - 0 (deny) | ||
*/ | ||
'aclStrategy' => 'positive', | ||
``` | ||
|
||
4. Set the rule repository, the default is the configuration file. | ||
|
||
```php | ||
/** | ||
* ACL rules repository | ||
* | ||
* default - config file(ConfigACLRepository) | ||
*/ | ||
'aclRepository' => \Alexusmai\LaravelFileManager\ACLService\ConfigACLRepository::class, | ||
``` | ||
|
||
Now you can add your rules in 'aclRules' array. But if you want to store your rules in another place, such as a database, you need to create your own class, and implements two functions from ACLRepository. | ||
|
||
I have already made a similar class for an example, and if it suits you, you can use it. You only need to replace the repository name in the configuration file. And add a new migration to the database. | ||
|
||
```php | ||
php artisan vendor:publish --tag=fm-migrations | ||
``` | ||
|
||
See [/src/ACLService/DBACLRepository.php](./../src/ACLService/DBACLRepository.php) and [/migrations/2019_02_06_174631_make_acl_rules_table.php](./../migrations/2019_02_06_174631_make_acl_rules_table.php) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class MakeAclRulesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('acl_rules', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->unsignedInteger('user_id')->nullable(); | ||
$table->string('disk'); | ||
$table->string('path'); | ||
$table->tinyInteger('access'); | ||
$table->timestamps(); | ||
|
||
$table->foreign('user_id')->references('id')->on('users'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('acl_rules'); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Alexusmai\LaravelFileManager\ACLService; | ||
|
||
use Cache; | ||
|
||
class ACL | ||
{ | ||
/** | ||
* @var ACLRepository | ||
*/ | ||
public $aclRepository; | ||
|
||
/** | ||
* ACL constructor. | ||
* | ||
* @param ACLRepository $aclRepository | ||
*/ | ||
public function __construct(ACLRepository $aclRepository) | ||
{ | ||
$this->aclRepository = $aclRepository; | ||
} | ||
|
||
/** | ||
* Get access level for selected path | ||
* | ||
* @param $disk | ||
* @param string $path | ||
* | ||
* @return int | ||
*/ | ||
public function getAccessLevel($disk, $path = '/') | ||
{ | ||
// get rules list | ||
$rules = $this->rulesForDisk($disk); | ||
|
||
// find the first rule where the paths are equal | ||
$firstRule = array_first($rules, function ($value) use ($path) { | ||
return fnmatch($value['path'], $path); | ||
}); | ||
|
||
if ($firstRule) { | ||
return $firstRule['access']; | ||
} | ||
|
||
// positive or negative ACL strategy | ||
return config('file-manager.aclStrategy') === 'positive' ? 2 : 0; | ||
} | ||
|
||
/** | ||
* Select rules for disk | ||
* | ||
* @param $disk | ||
* | ||
* @return array | ||
*/ | ||
protected function rulesForDisk($disk) | ||
{ | ||
return array_where($this->rulesList(), | ||
function ($value) use ($disk) { | ||
return $value['disk'] === $disk; | ||
}); | ||
} | ||
|
||
/** | ||
* Get rules list from ACL Repository | ||
* | ||
* @return array|mixed | ||
*/ | ||
protected function rulesList() | ||
{ | ||
// if cache on | ||
if ($minutes = config('file-manager.aclRulesCache')) { | ||
$cacheName = 'fm_acl_'.$this->aclRepository->getUserID(); | ||
|
||
return Cache::remember($cacheName, $minutes, function () { | ||
return $this->aclRepository->getRules(); | ||
}); | ||
} | ||
|
||
return $this->aclRepository->getRules(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Alexusmai\LaravelFileManager\ACLService; | ||
|
||
/** | ||
* Interface ACLRepository | ||
* | ||
* @package Alexusmai\LaravelFileManager\ACLService | ||
*/ | ||
interface ACLRepository | ||
{ | ||
/** | ||
* Get user ID | ||
* | ||
* @return mixed | ||
*/ | ||
public function getUserID(); | ||
|
||
/** | ||
* Get ACL rules list for user | ||
* | ||
* You need to return an array, like this: | ||
* | ||
* 0 => [ | ||
* "disk" => "public" | ||
* "path" => "music" | ||
* "access" => 0 | ||
* ], | ||
* 1 => [ | ||
* "disk" => "public" | ||
* "path" => "images" | ||
* "access" => 1 | ||
* ] | ||
* | ||
* OR [] - if no results for selected user | ||
* | ||
* @return array | ||
*/ | ||
public function getRules(): array; | ||
} |
Oops, something went wrong.