Skip to content

Commit

Permalink
change uniqkey to gate
Browse files Browse the repository at this point in the history
  • Loading branch information
hexters committed Mar 5, 2020
1 parent eecf91a commit 3a2c196
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 33 deletions.
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ You can install this package via composer:
$ composer require hexters/rolevel
```

Publish vendor from Rolevel
Publish vendor
```
$ php artisan vendor:publish --tag=rolevel
```
Expand All @@ -34,7 +34,7 @@ class User extends Authenticatable {
. . .
```

Installing database
Migrate rolevel database
```
$ php artisan migrate
```
Expand All @@ -48,7 +48,7 @@ Open file menu & permission in folder `app/Roles/menu_and_permissions.php`. In t
return [
[
'display' => 'Menu Access', // display name
'uniqkey' => 'module.access.index', // must uniq
'gate' => 'module.access.index', // must uniq
'url' => null, // URL can set null if menu have submenu
'classId' => '', // id attribute
'className' => '', // class style attribute
Expand All @@ -60,15 +60,15 @@ Open file menu & permission in folder `app/Roles/menu_and_permissions.php`. In t
'childs' => [
[
'display' => 'Assign Permissions',
'uniqkey' => 'module.access.assign.permission.index',
'gate' => 'module.access.assign.permission.index',
'url' => '/admin/assign/permission',
'classId' => '',
'className' => '',
'classIcon' => '',
'permissions' => [
[
'uniqkey' => 'module.access.assign.permission.show',
'gate' => 'module.access.assign.permission.show',
'name' => 'Detail Permissions', // title
'description' => 'Show detail for permission' // info description
],
Expand Down Expand Up @@ -99,7 +99,7 @@ After that please put script in your master layout, for showing menu access.
$viewMenu = function($menus) use (&$viewMenu, $permissions) {
$html = '';
foreach($menus as $menu) {
if(in_array($menu['uniqkey'], $permissions)) {
if(in_array($menu['gate'], $permissions)) {
$html .= view('vendor.rolevel.menu', ['menu' => $menu, 'view' => $viewMenu]);
}
}
Expand All @@ -122,7 +122,7 @@ You should assign role to your User account before.
$user = App\User::find(1);
$role = App\Role::find(1);
$user->roles()->sync([ $role->id ]);
$user->roles()->sync([ $role->id ], false);
```

In your any controller you should declaration `Gate` to provide access to your module, for example
Expand All @@ -131,9 +131,8 @@ In your any controller you should declaration `Gate` to provide access to your m
namespace App\Http\Controllers;
use Gate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
class MenuAccessController extends Controller {
Expand All @@ -145,7 +144,7 @@ class MenuAccessController extends Controller {
public function index()
{
/**
* Get module.access.assign.permission.index from uniqkey in file menu_and_permissions.php
* Get module.access.assign.permission.index from gate in file menu_and_permissions.php
*/
if(Gate::denies('module.access.assign.permission.index')) abort(403);
Expand All @@ -154,7 +153,7 @@ class MenuAccessController extends Controller {
. . .
```
If you have provide access to the button and any condition you can set, for example
In your blade
```
@can('module.access.assign.permission.store')
<a href="{{ url('/admin/account/create') }}">Create Account</a>
Expand Down
2 changes: 1 addition & 1 deletion src/Config/rolevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
'content' => 'content'
],

'show_uniqkey' => in_array(env('APP_ENV'), ['local', 'staging']),
'show_gate' => in_array(env('APP_ENV'), ['local', 'staging']),

];
2 changes: 1 addition & 1 deletion src/Controllers/RolevelAssignController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function assigned(Request $request, $id) {
if (Gate::denies('module.access.assign.permission.show')) abort(403);

$role = $this->role->findOrFail($id);
$permissions = isset( $request->uniqkeys) ? $request->uniqkeys : [];
$permissions = isset( $request->gates) ? $request->gates : [];
$role->update([
'permissions' => $permissions
]);
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public function findKeys ($menus = []) {
$keys = [];
$subKeys = [];
foreach($menus as $menu) {
if(isset($menu['uniqkey'])) {
$keys[] = $menu['uniqkey'];
if(isset($menu['gate'])) {
$keys[] = $menu['gate'];
}

if(isset($menu['childs'])) {
Expand Down
6 changes: 3 additions & 3 deletions src/Helpers/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public function findKeys ($menus = []) {
$permissions = [];
$childPermissions = [];
foreach($menus as $menu) {
$permissions[] = $menu['uniqkey'];
$permissions[] = $menu['gate'];
if(isset($menu['permissions']) && is_array($menu['permissions'])) {
foreach($menu['permissions'] as $permission) {
if(isset($permission['uniqkey'])) {
$permissions[] = $permission['uniqkey'];
if(isset($permission['gate'])) {
$permissions[] = $permission['gate'];
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Roles/menu_and_permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
return [
[
'display' => ' Module Access',
'uniqkey' => 'module.access',
'gate' => 'module.access',
'url' => null,
'classId' => '',
'className' => '',
Expand All @@ -28,7 +28,7 @@
'childs' => [
[
'display' => 'Assign Permissions',
'uniqkey' => 'module.access.assign.permission.index',
'gate' => 'module.access.assign.permission.index',
'url' => '/admin/roles',
'classId' => '',
'className' => '',
Expand All @@ -43,7 +43,7 @@
*/
'permissions' => [
[
'uniqkey' => 'module.access.assign.permission.show',
'gate' => 'module.access.assign.permission.show',
'name' => 'Show detail role',
'description' => 'Module for assign permission to any module'
]
Expand Down
8 changes: 4 additions & 4 deletions src/Views/assign/detail.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
foreach($menus as $menu) {
$buttonAssign = view('rolevel::assign.partials.button', ['menu' => $menu, 'permissions' => $permissions]);
$html .= '<li>';
$checked = in_array($menu['uniqkey'], $permissions) ? 'checked' : '';
$html .= '<input type="checkbox" name="uniqkeys[]" ' . $checked . ' value="' . $menu['uniqkey'] . '" /> ';
$checked = in_array($menu['gate'], $permissions) ? 'checked' : '';
$html .= '<input type="checkbox" name="gates[]" ' . $checked . ' value="' . $menu['gate'] . '" /> ';
$html .= $menu['display'] . $buttonAssign;
if(config('rolevel.show_uniqkey')) {
if(config('rolevel.show_gate')) {
$html .= '<div class="bg-dark text-light p-2">
<small>' . $menu['uniqkey'] . '</small>
<small>' . $menu['gate'] . '</small>
</div>';
}
Expand Down
16 changes: 8 additions & 8 deletions src/Views/assign/partials/button.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@if(isset($menu['permissions']) && count($menu['permissions']) > 0)
<a href="#" class="float-right" data-toggle="modal" data-target="#{{ Str::slug($menu['uniqkey']) }}">
<a href="#" class="float-right" data-toggle="modal" data-target="#{{ Str::slug($menu['gate']) }}">
Assign Permission
</a>
@endif

<div class="modal fade" id="{{ Str::slug($menu['uniqkey']) }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal fade" id="{{ Str::slug($menu['gate']) }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
Expand All @@ -13,7 +13,7 @@
</h5>

<div>
{!! in_array($menu['uniqkey'], $permissions) ? '&#10003;' : '&times;' !!}
{!! in_array($menu['gate'], $permissions) ? '&#10003;' : '&times;' !!}
</div>
</div>
<div class="modal-body">
Expand All @@ -23,22 +23,22 @@
@forelse($menu['permissions'] as $item)
<li>

@if(in_array($menu['uniqkey'], $permissions))
@if(in_array($menu['gate'], $permissions))
<div class="float-right">
<input type="checkbox" id="permission-{{ Str::slug($item['uniqkey']) }}" name="uniqkeys[]" {{ in_array($item['uniqkey'], $permissions) ? 'checked' : '' }} value="{{ $item['uniqkey'] }}">
<input type="checkbox" id="permission-{{ Str::slug($item['gate']) }}" name="gates[]" {{ in_array($item['gate'], $permissions) ? 'checked' : '' }} value="{{ $item['gate'] }}">
</div>
@endif

<label style="cursor:pointer;" for="permission-{{ Str::slug($item['uniqkey']) }}">
<label style="cursor:pointer;" for="permission-{{ Str::slug($item['gate']) }}">
<div>
<strong>{{ $item['name'] }}</strong>
</div>
</label>
<p class="text-muted">
{{ $item['description'] }}
@if(config('rolevel.show_uniqkey'))
@if(config('rolevel.show_gate'))
<div class="bg-dark text-light p-2">
<small>{{ $item['uniqkey'] }}</small>
<small>{{ $item['gate'] }}</small>
</div>
@endif
</p>
Expand Down

0 comments on commit 3a2c196

Please sign in to comment.