Skip to content

Commit

Permalink
o need to create a Roles and Permission classes in the application
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihabafia committed May 10, 2024
1 parent 060cb3f commit ab24934
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `permissions-admin` will be documented in this file.

## 1.0.3 - 2024-05-10
- No need to create a Roles and Permission classes in the application.
- Minor bug fixes.

## 1.0.2 - 2024-05-08
- Fixes the UserComponent modal and the form.
- Added the option to disable eding the User Name, especially if are using the User model as first_name, last_name instead of name.
Expand Down
28 changes: 1 addition & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,7 @@ composer require spatie/laravel-permission
```bash
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:
3. You need to add ```HasPermissionAdmin``` trait in your ```User::class``` like follows:
```php
namespace App\Models;

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
"require": {
"php": "^8.2",
"illuminate/contracts": "^10.0||^11.0",
"livewire/livewire": "*",
"spatie/laravel-package-tools": "^1.16"
},
"require-dev": {
"larastan/larastan": "^2.9",
"laravel/laravel": "^11.0",
"laravel/pint": "^1.14",
"livewire/livewire": "*",
"nunomaduro/collision": "^8.1.1||^7.10.0",
"orchestra/testbench": "^9.0.0||^8.22.0",
"pestphp/pest": "^2.34",
Expand All @@ -32,6 +33,7 @@
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"spatie/laravel-permission": "^6.7",
"spatie/laravel-ray": "^1.35"
},
"autoload": {
Expand Down
1 change: 1 addition & 0 deletions resources/views/components/livewire-layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@
</div>
</main>
</div>
@stack('scripts')
</body>
</html>
14 changes: 12 additions & 2 deletions resources/views/livewire/permission-component.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
</svg>
</div>
<input
class="dark:bg-gray-700 bg-gray-50 border border-gray-300 dark:border-gray-500 text-gray-900 dark:text-gray-100 text-sm rounded-lg focus:ring-teal-500 focus:border-teal-500 block w-full pl-10 p-2 "
wire:model.live.debounce.400ms="search"
id="search"
name="search"
type="text"
class="dark:bg-gray-700 bg-gray-50 border border-gray-300 dark:border-gray-500 text-gray-900 dark:text-gray-100 text-sm rounded-lg focus:ring-teal-500 focus:border-teal-500 block w-full pl-10 p-2 "
placeholder="Search" required="">
placeholder="Search"
>
</div>
</div>
<div class="flex space-x-0">
Expand Down Expand Up @@ -160,3 +163,10 @@ class="h-4 w-4 rounded border-gray-300 text-teal-600 focus:ring-teal-600"
</x-slot:body>
</x-permissions-admin::modal>
</div>
@push('scripts')
<script>
window.addEventListener('livewire:load', () => {
document.querySelector('input').focus()
})
</script>
@endpush
16 changes: 13 additions & 3 deletions resources/views/livewire/role-component.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
</svg>
</div>
<input
class="dark:bg-gray-700 bg-gray-50 border border-gray-300 dark:border-gray-500 text-gray-900 dark:text-gray-100 text-sm rounded-lg focus:ring-teal-500 focus:border-teal-500 block w-full pl-10 p-2 "
wire:model.live.debounce.400ms="search"
id="search"
name="search"
type="text"
class="dark:bg-gray-700 bg-gray-50 border border-gray-300 dark:border-gray-500 text-gray-900 dark:text-gray-100 text-sm rounded-lg focus:ring-teal-500 focus:border-teal-500 block w-full pl-10 p-2 "
placeholder="Search" required="">
placeholder="Search"
>
</div>
</div>
<div class="flex space-x-0 itemx-center">
<div class="flex space-x-0">
<div class="flex space-x-0 items-center">
<label class="w-40 text-sm font-medium text-gray-900 dark:text-gray-300">User Type :</label>
<select
Expand Down Expand Up @@ -160,3 +163,10 @@ class="h-4 w-4 rounded border-gray-300 text-teal-600 focus:ring-teal-600"
</x-slot:body>
</x-permissions-admin::modal>
</div>
@push('scripts')
<script>
window.addEventListener('livewire:load', () => {
document.querySelector('input').focus()
})
</script>
@endpush
16 changes: 13 additions & 3 deletions resources/views/livewire/user-component.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@
</svg>
</div>
<input
class="dark:bg-gray-700 bg-gray-50 border border-gray-300 dark:border-gray-500 text-gray-900 dark:text-gray-100 text-sm rounded-lg focus:ring-teal-500 focus:border-teal-500 block w-full pl-10 p-2 "
wire:model.live.debounce.400ms="search"
id="search"
name="search"
type="text"
class="dark:bg-gray-700 bg-gray-50 border border-gray-300 dark:border-gray-500 text-gray-900 dark:text-gray-100 text-sm rounded-lg focus:ring-teal-500 focus:border-teal-500 block w-full pl-10 p-2 "
placeholder="Search" required="">
placeholder="Search"
>
</div>
</div>
<div class="flex space-x-0">
<div class="flex space-x-0 items-center">
<label class="w-40 text-sm font-medium text-gray-900 dark:text-gray-300">User Type :</label>
<select
wire:model.live="role"
wire:model.live="filter"
class="bg-gray-50 dark:bg-gray-700 border border-gray-300 dark:border-gray-500 text-gray-900 dark:text-gray-100 text-sm rounded-lg focus:ring-teal-500 focus:border-teal-500 block w-full p-2.5 ">
<option value="">All</option>
<option value="Admin">Admins</option>
Expand Down Expand Up @@ -193,3 +196,10 @@ class="h-4 w-4 rounded border-gray-300 text-teal-600 focus:ring-teal-600"
</x-slot:body>
</x-permissions-admin::modal>
</div>
@push('scripts')
<script>
window.addEventListener('livewire:load', () => {
document.querySelector('input').focus()
})
</script>
@endpush
4 changes: 2 additions & 2 deletions src/Livewire/PermissionComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace IhabAfia\PermissionsAdmin\Livewire;

use App\Models\Permission;
use App\Models\Role;
use IhabAfia\PermissionsAdmin\Livewire\Forms\PermissionForm;
use IhabAfia\PermissionsAdmin\Models\Permission;
use IhabAfia\PermissionsAdmin\Models\Role;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
Expand Down
4 changes: 2 additions & 2 deletions src/Livewire/RoleComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace IhabAfia\PermissionsAdmin\Livewire;

use App\Models\Permission;
use App\Models\Role;
use IhabAfia\PermissionsAdmin\Livewire\Forms\RoleForm;
use IhabAfia\PermissionsAdmin\Models\Permission;
use IhabAfia\PermissionsAdmin\Models\Role;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
Expand Down
4 changes: 2 additions & 2 deletions src/Livewire/UserComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class UserComponent extends Component
public string $search = '';

#[Url(history: true)]
public string $role = '';
public string $filter = '';

#[Url(history: true)]
public string $sortBy = 'created_at';
Expand All @@ -46,7 +46,7 @@ public function mount()
public function users()
{
return User::search($this->search)

Check failure on line 48 in src/Livewire/UserComponent.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method App\Models\User::search().

Check failure on line 48 in src/Livewire/UserComponent.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method App\Models\User::search().
->when($this->role !== '', function ($query) {
->when($this->filter !== '', function ($query) {
$query->whereHas('roles', function ($query) {
$query->where('name', 'like', "%{$this->role}%");

Check failure on line 51 in src/Livewire/UserComponent.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property IhabAfia\PermissionsAdmin\Livewire\UserComponent::$role.

Check failure on line 51 in src/Livewire/UserComponent.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property IhabAfia\PermissionsAdmin\Livewire\UserComponent::$role.
});
Expand Down
11 changes: 11 additions & 0 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace IhabAfia\PermissionsAdmin\Models;

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

class Permission extends SpatiePermission
{
use HasPermissionAdmin;
}
13 changes: 13 additions & 0 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace IhabAfia\PermissionsAdmin\Models;

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

class Role extends SpatieRole
{
use HasPermissionAdmin;
use HasRoles;
}

0 comments on commit ab24934

Please sign in to comment.