Skip to content

Commit

Permalink
minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihabafia committed May 7, 2024
1 parent 2f230a2 commit 19aaba8
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 46 deletions.
78 changes: 59 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
[![Total Downloads](https://img.shields.io/packagist/dt/ihabafia/permissions-admin.svg?style=flat-square)](https://packagist.org/packages/ihabafia/permissions-admin)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
This package is a cool livewire GUI for [Spatie Laravel Permission](https://github.com/spatie/laravel-permission) **(not included)**, and it will help you to:
- Create role.
- Create permission.
- Assign role to user.
- Give permission to role.
- Assign role to permission.
- Remove permission from role.

## Support us

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/permissions-admin.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/permissions-admin)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
All this via 3 livewire components that will show you the information in a dynamic table which is searchable, sortable and filtered.

## Installation

Expand All @@ -23,24 +24,63 @@ You can install the package via composer:
composer require ihabafia/permissions-admin
```

You can publish and run the migrations with:

### Additional Steps for installation
1. This package is depend on [Spatie Laravel Permission](https://github.com/spatie/laravel-permission), to install it if you didn't already:
```bash
php artisan vendor:publish --tag="permissions-admin-migrations"
php artisan migrate
composer require spatie/laravel-permission
```

You can publish the config file with:

2. [Livewire](https://github.com/livewire/livewire) is required, to install it if you didn't already:
```bash
php artisan vendor:publish --tag="permissions-admin-config"
composer require livewire/livewire
```
3. You need to create and empty ```App\Models\Role``` and ```App\Models\Permission``` class and add ```HasPermissionAdmin``` trait as follows:
```php
namespace App\Models;

use IhabAfia\PermissionsAdmin\Traits\HasPermissionAdmin;
use Spatie\Permission\Models\Role as SpatieRole;

class Role extends SpatieRole
{
use HasPermissionAdmin;
}

````
```php
namespace App\Models;

use IhabAfia\PermissionsAdmin\Traits\HasPermissionAdmin;
use Spatie\Permission\Models\Permission as SpatiePermission;

class Permission extends SpatiePermission
{
use HasPermissionAdmin;
}
````
**These classes will not affect your application since these classes extends the original class.**

4. You need to add ```HasPermissionAdmin``` trait in your ```User::class``` like follows:
```php
namespace App\Models;

use IhabAfia\PermissionsAdmin\Traits\HasPermissionAdmin;

This is the contents of the published config file:
class User extends Authenticatable
{
use HasPermissionAdmin;
...
````
This trait is needed for adding the search functionality to the 3 models.

5. Finally, you need to use this route in ```web.php```
```php
return [
];
Route::rolesPermissionsAdmin();
```

You might need to publish the config file to change routes with:

```bash
php artisan vendor:publish --tag="permissions-admin-config"
```

Optionally, you can publish the views using
Expand All @@ -59,7 +99,7 @@ echo $permissionsAdmin->echoPhrase('Hello, IhabAfia!');
## Testing

```bash
composer test
Need Help
```

## Changelog
Expand Down
30 changes: 24 additions & 6 deletions config/permissions-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,42 @@
*/
return [
/*
* This is the route of your home application.
* This is the route of your home application for admins.
*/
'home' => 'admin.dashboard',
'admin-home' => 'dashboard',

/*
* This is the route of your home application for users (it can be the same as the above).
*/
'user-home' => 'dashboard',

/*
* This is the route of your user profile.
*/
'user-profile' => 'user/profile',

/*
* In case you want to change the route of the user management route.
*/
'users_index' => 'admin.users',
'users_management' => 'admin.users-management.index',
'users-index' => 'users',

/*
* In case you want to change the route of the roles management route.
*/
'roles_index' => 'admin.roles',
'roles-index' => 'roles',

/*
* In case you want to change the route of the permissions management route.
*/
'permissions_index' => 'admin.permissions',
'permissions-index' => 'permissions',

/*
* Using Gravatar in the Users list (Pending)
*/
'use-gravatar' => false,

/*
* The url to Gravatar website. (Pending)
*/
'gravatar_url' => 'https://www.gravatar.com/avatar/',
];
32 changes: 14 additions & 18 deletions resources/views/admin-navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,25 @@
<div class="flex">
<!-- Logo -->
<div class="shrink-0 flex items-center">
@hasanyrole(['Super Admin', 'Admin'])
<a href="{{ route(config('permissions-admin.home')) }}">
<a href="{{ route(config('permissions-admin.user-home')) }}">
<x-application-logo class="block h-9 w-auto fill-current text-gray-800 dark:text-gray-200" />
</a>
@endhasrole
</div>

<!-- Navigation Links -->
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
@can('Manage Roles & Permissions')
<x-nav-link :href="route(config('permissions-admin.home'))" :active="request()->routeIs(config('permissions-admin.home'))" wire:navigate>
<x-nav-link :href="route(config('permissions-admin.admin-home'))" :active="request()->routeIs(config('permissions-admin.admin-home'))" wire:navigate>
{{ __('Dashboard') }}
</x-nav-link>
<x-nav-link :href="route(config('permissions-admin.users_index'))" :active="request()->routeIs(config('permissions-admin.users_index'))" wire:navigate>
<x-nav-link :href="route(config('permissions-admin.users-index'))" :active="request()->routeIs(config('permissions-admin.users-index'))" wire:navigate>
{{ __('Users') }}
</x-nav-link>
<x-nav-link :href="route(config('permissions-admin.roles_index'))" :active="request()->routeIs(config('permissions-admin.roles_index'))" wire:navigate>
<x-nav-link :href="route(config('permissions-admin.roles-index'))" :active="request()->routeIs(config('permissions-admin.roles-index'))" wire:navigate>
{{ __('Roles') }}
</x-nav-link>
<x-nav-link :href="route(config('permissions-admin.permissions_index'))" :active="request()->routeIs(config('permissions-admin.permissions_index'))" wire:navigate>
<x-nav-link :href="route(config('permissions-admin.permissions-index'))" :active="request()->routeIs(config('permissions-admin.permissions-index'))" wire:navigate>
{{ __('Permissions') }}
</x-nav-link>
@endhasanyrole
</div>
</div>

Expand All @@ -47,20 +43,20 @@
</x-slot>

<x-slot name="content">
<x-dropdown-link :href="route('profile.edit')" wire:navigate>
<x-dropdown-link :href="config('permissions-admin.user-profile')" wire:navigate>
{{ __('Profile') }}
</x-dropdown-link>

<!-- Authentication -->
<form method="POST" action="{{ route('logout') }}">
{{--<form method="POST" action="{{ route('logout') ?? 'logout' }}">
@csrf
<x-dropdown-link :href="route('logout')"
<x-dropdown-link :href="route('logout') ?? 'logout'"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Log Out') }}
</x-dropdown-link>
</form>
</form>--}}
</x-slot>
</x-dropdown>
</div>
Expand All @@ -81,11 +77,11 @@
<div :class="{'block': open, 'hidden': ! open}" class="hidden sm:hidden">
<div class="pt-2 pb-3 space-y-1">
@hasanyrole(['Admin', 'Super Admin'])
<x-responsive-nav-link :href="route('admin.dashboard')" :active="request()->routeIs('admin.dashboard')">
<x-responsive-nav-link :href="config('permissions-admin.admin-home')" :active="request()->routeIs('admin.dashboard')">
<x-application-logo class="block h-9 w-auto fill-current text-gray-800 dark:text-gray-200" />
</x-responsive-nav-link>>
@else
<x-responsive-nav-link :href="route('user.dashboard')" :active="request()->routeIs('user.dashboard')">
<x-responsive-nav-link :href="config('permissions-admin.user-home')" :active="request()->routeIs('user.dashboard')">
<x-application-logo class="block h-9 w-auto fill-current text-gray-800 dark:text-gray-200" />
</x-responsive-nav-link>>
@endhasanyrole
Expand All @@ -98,8 +94,8 @@
<div class="font-medium text-sm text-gray-500">{{ Auth::user()->email }}</div>
</div>

<div class="mt-3 space-y-1">
<x-responsive-nav-link :href="route('profile.edit')">
{{--<div class="mt-3 space-y-1">
<x-responsive-nav-link :href="config('permissions-admin.user-profile')">
{{ __('Profile') }}
</x-responsive-nav-link>
Expand All @@ -113,7 +109,7 @@
{{ __('Log Out') }}
</x-responsive-nav-link>
</form>
</div>
</div>--}}
</div>
</div>
</nav>
Expand Down
40 changes: 40 additions & 0 deletions resources/views/components/dynamic-classes.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<div class="px-1 py-1 bg-amber-500 hover:bg-amber-600 text-white rounded">x</div>
<div class="px-1 py-1 bg-sky-500 hover:bg-sky-600 text-white rounded">x</div>
<div class="px-1 py-1 bg-fuchsia-500 hover:bg-fuchsia-600 text-white rounded">x</div>

<span class="inline-flex items-center gap-x-1.5 rounded-md px-2 py-1 text-xs font-medium text-white ring-1 ring-inset ring-gray-800">x</span>
<span class="inline-flex items-center gap-x-1.5 rounded-md px-2 py-1 text-xs font-medium text-white ring-1 ring-inset ring-gray-800">x</span>
<span class="inline-flex items-center gap-x-1.5 rounded-md px-2 py-1 text-xs font-medium text-white ring-1 ring-inset ring-gray-800">x</span>

<span class="h-1.5 w-1.5 fill-purple-400">x</span>
<span class="h-1.5 w-1.5 fill-yellow-400">x</span>
<span class="h-1.5 w-1.5 fill-green-400">x</span>

<button type="button" class="font-medium text-center
text-sm px-5 py-[0.55rem] ml-3
rounded-lg border-2
focus:ring-2
focus:outline-none
ark:border-sky-500
text-sky-500
hover:text-gray-100
border-sky-500
hover:border-sky-300
hover:bg-sky-600
focus:ring-sky-300">x</button>

<button class = "
font-medium text-center
text-sm px-5 py-[0.55rem] ml-3
rounded-lg border-2
focus:ring-2
focus:outline-none
ark:border-teal-500
text-teal-500
hover:text-gray-100
border-teal-500
hover:border-teal-300
hover:bg-teal-600
focus:ring-teal-300
"
>x</button>
2 changes: 1 addition & 1 deletion resources/views/components/forms/button-color.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{{ $attributes }}
{{ $attributes->merge(['class' => "
font-medium text-center
text-sm px-5 py-[0.55rem] ml-3
text-sm px-5 py-2 ml-3
rounded-lg border-2
focus:ring-2
focus:outline-none
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/permission-component.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class="h-4 w-4 rounded border-gray-300 text-teal-600 focus:ring-teal-600"
</div>
<button type="submit"
class="mt-2 text-white dark:text-gray-100 inline-flex items-center bg-teal-700 dark:bg-teal-500 hover:bg-teal-800 dark:hover:bg-teal-600 focus:ring-2 focus:outline-none focus:ring-teal-300 dark:focus:ring-teal-400 font-medium rounded-lg text-sm px-5 py-2.5 text-center">
Update Permission
<span x-text="title"></span>
</button>
</form>
</x-slot:body>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/role-component.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class="h-4 w-4 rounded border-gray-300 text-teal-600 focus:ring-teal-600"
</div>
<button type="submit"
class="mt-2 text-white dark:text-gray-100 inline-flex items-center bg-teal-700 dark:bg-teal-500 hover:bg-teal-800 dark:hover:bg-teal-600 focus:ring-2 focus:outline-none focus:ring-teal-300 dark:focus:ring-teal-400 font-medium rounded-lg text-sm px-5 py-2.5 text-center">
Update Permission
<span x-text="title"></span>
</button>
</form>
</x-slot:body>
Expand Down
1 change: 1 addition & 0 deletions src/Livewire/PermissionComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function updateRole(Role $role)

public function editPermission(Permission $permission)
{
$this->resetErrorBag();
$this->permissionForm->name = $permission->name;
$this->permissionForm->roles = $permission->roles->pluck('name')->toArray();

Expand Down
1 change: 1 addition & 0 deletions src/Livewire/RoleComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function storeRole()

public function editRole(Role $role)
{
$this->resetErrorBag();
$this->roleForm->name = $role->name;
$this->roleForm->permissions = $role->permissions->pluck('name')->toArray();

Expand Down

0 comments on commit 19aaba8

Please sign in to comment.