Skip to content

Commit 32bd732

Browse files
author
Sandip Patel
committed
user permissions fix
1 parent 3b6324c commit 32bd732

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

Diff for: composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
"autoload": {
1515
"psr-4": {
1616
"PCB\\Laravel\\": "src/"
17-
}
17+
},
18+
"files": [
19+
"src/LaravelHelper.php"
20+
]
1821
},
1922
"extra": {
2023
"laravel": {

Diff for: src/AdminPolicy.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Created by PhpStorm.
5+
* User: spatel
6+
* Date: 19/09/17
7+
* Time: 12:00 PM
8+
*/
9+
namespace PCB\Laravel;
10+
11+
use Illuminate\Auth\Access\HandlesAuthorization;
12+
13+
class AdminPolicy
14+
{
15+
use HandlesAuthorization;
16+
17+
public function allowed($user, $permission=null, $plugin=null)
18+
{
19+
// if user has no access to module
20+
$permission = $permission ?: request()->route()->getName();
21+
if( $plugin && !config('modules.enabled.' .$plugin) ) {
22+
return false;
23+
}
24+
25+
return $user->isSuperAdmin() ?: $user->checkPermission($permission);
26+
}
27+
}

Diff for: src/LaravelHelper.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* Created by PhpStorm.
5+
* User: spatel
6+
* Date: 19/09/17
7+
* Time: 12:00 PM
8+
*/
9+
10+
if (!function_exists('is_module_enabled') )
11+
{
12+
function has_module_permission($module, $permission)
13+
{
14+
if (auth()->user()->can('allowed', $permission, $module))
15+
{
16+
return true;
17+
}
18+
19+
return false;
20+
}
21+
}

Diff for: src/ServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PCB\Laravel;
1212

1313
use PCBLaravel;
14+
use Illuminate\Support\Facades\Gate;
1415
use Illuminate\Support\Facades\Route;
1516
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
1617

@@ -55,5 +56,8 @@ public function boot()
5556
$this->publishes([
5657
__DIR__. '/config.php' => config_path('modules.php')
5758
]);
59+
60+
// register admin policy
61+
Gate::define('allowed', 'PCB\Laravel\AdminPolicy@allowed');
5862
}
5963
}

0 commit comments

Comments
 (0)