Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Brand3000/nova-roles-and-permissions
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.1
Choose a base ref
...
head repository: Brand3000/nova-roles-and-permissions
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 15 commits
  • 11 files changed
  • 3 contributors

Commits on Apr 24, 2024

  1. Fine-tuned the Publisher class

    brand committed Apr 24, 2024
    Copy the full SHA
    772ed52 View commit details
  2. Fixed namespaces paths

    brand committed Apr 24, 2024
    Copy the full SHA
    96b7e08 View commit details
  3. 1. Fixed default values for the roles fields

    2. Added more information into the README.md file
    brand committed Apr 24, 2024
    Copy the full SHA
    bdf2078 View commit details
  4. 1. Fixed default values for the roles fields

    2. Added more information into the README.md file
    brand committed Apr 24, 2024
    Copy the full SHA
    862519f View commit details
  5. 1. Fixed default values for the roles fields

    2. Added more information into the README.md file
    brand committed Apr 24, 2024
    Copy the full SHA
    1d0601f View commit details
  6. 1. Fixed default values for the roles fields

    2. Added more information into the README.md file
    brand committed Apr 24, 2024
    Copy the full SHA
    d9253f4 View commit details
  7. 1. Fixed default values for the roles fields

    2. Added more information into the README.md file
    brand committed Apr 24, 2024
    Copy the full SHA
    ca43289 View commit details
  8. 1. Fixed default values for the roles fields

    2. Added more information into the README.md file
    brand committed Apr 24, 2024
    Copy the full SHA
    3558fb0 View commit details
  9. Added more information into the README.md file

    brand committed Apr 24, 2024
    Copy the full SHA
    0de37ad View commit details
  10. Added more information into the README.md file

    brand committed Apr 24, 2024
    Copy the full SHA
    5b02cff View commit details
  11. Added more information into the README.md file

    brand committed Apr 24, 2024
    Copy the full SHA
    d872caa View commit details

Commits on Dec 30, 2024

  1. Copy the full SHA
    6875782 View commit details
  2. Merge pull request #1 from supportsitewill/main

    adds seeder to seed the super admin
    Brand3000 authored Dec 30, 2024
    Copy the full SHA
    1487d96 View commit details
  3. Copy the full SHA
    e8836e6 View commit details
  4. Merge pull request #2 from supportsitewill/patch-1

    adds explanation about a default super admin into the README.md file
    Brand3000 authored Dec 30, 2024
    Copy the full SHA
    cbe0893 View commit details
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Laravel Nova Roles and Permissions
This package allows separating roles for admins and other types of users while stored in the same users table. Additionally, it provides functionality for managing permissions (reader/editor) for the admins of a project powered by Laravel Nova.

## Screenshots
![NovaRolesAndPermissions](/docs/nova-roles-and-permissions.png)

## Requirements
Laravel Nova 4

## Installation

@@ -24,5 +31,30 @@ Be aware that the package will create/overwrite the next files:
- app/Policies/CorePermissions.php

```bash
php artisan vendor:publish --provider="Brand3000\NovaRolesAndPermissions\Publisher"
php artisan vendor:publish --provider="Brand3000\NovaRolesAndPermissions\Publisher" --force
```

### Run new migration
```bash
php artisan migrate
```

### Set administrators
Go to your database, find the users table and choose administrators by setting the role_admin column into 1. Additionally, set the supar_admin column into 1 for those who are eligible.
If you have different types of users in your project, feel free to add more role_ columns for future development.

### During development
During development, you have to add all the resources you want to be managed into the array $booleanOptions of the fields() function in the app/Nova/Admin.php nova resource.

### Before switching a project to the production state
Go into the NovaServiceProvider.php file and add the next logic into the gate() function
```php
Gate::define('viewNova', function ($user) {
return $user->role_admin;
});
```

### If you want to seed a default super admin, run the next command
```
php artisan db:seed --class=SuperAdminSeeder
```
2 changes: 1 addition & 1 deletion app/Models/Admin.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace app\Models;
namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2 changes: 1 addition & 1 deletion app/Models/User.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace app\Models;
namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
8 changes: 4 additions & 4 deletions app/Nova/Admin.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace app\Nova;
namespace App\Nova;

use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rules;
@@ -18,9 +18,9 @@ class Admin extends Resource
/**
* The model the resource corresponds to.
*
* @var class-string<\app\Models\Admin>
* @var class-string<\App\Models\Admin>
*/
public static $model = \app\Models\Admin::class;
public static $model = \App\Models\Admin::class;

/**
* The single value that should be used to represent the resource when being displayed.
@@ -118,7 +118,7 @@ class Admin extends Resource
return $resource->id == 1;
}),

Hidden::make('role_admin', 'role_admin')->default(true),
Hidden::make('role_admin', 'role_admin')->default(1),
];
}

8 changes: 4 additions & 4 deletions app/Nova/User.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace app\Nova;
namespace App\Nova;

use Illuminate\Validation\Rules;
use Laravel\Nova\Fields\Gravatar;
@@ -15,9 +15,9 @@ class User extends Resource
/**
* The model the resource corresponds to.
*
* @var class-string<\app\Models\User>
* @var class-string<\App\Models\User>
*/
public static $model = \app\Models\User::class;
public static $model = \App\Models\User::class;

/**
* The single value that should be used to represent the resource when being displayed.
@@ -75,7 +75,7 @@ class User extends Resource
->creationRules('required', Rules\Password::defaults())
->updateRules('nullable', Rules\Password::defaults()),

Hidden::make('role_user', 'role_user')->default(true),
Hidden::make('role_user', 'role_user')->default(1),
];
}
}
4 changes: 2 additions & 2 deletions app/Policies/AdminPolicy.stub
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace app\Policies;
namespace App\Policies;

use app\Models\User as Admin;
use App\Models\User as Admin;

class AdminPolicy
{
4 changes: 2 additions & 2 deletions app/Policies/CorePermissions.stub
Original file line number Diff line number Diff line change
@@ -2,9 +2,9 @@

declare(strict_types=1);

namespace app\Policies;
namespace App\Policies;

use app\Models\User as Admin;
use App\Models\User as Admin;
use Illuminate\Support\Facades\Auth;

class CorePermissions
4 changes: 2 additions & 2 deletions app/Policies/UserPolicy.stub
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace app\Policies;
namespace App\Policies;

use app\Nova\User;
use App\Nova\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class UserPolicy extends CorePermissions
25 changes: 25 additions & 0 deletions database/seeders/SuperAdminSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class SuperAdminSeeder extends Seeder
{
public function run(): void
{
DB::table('users')->insert([
'name' => 'dev',
'email' => 'root@example.com',
'password' => Hash::make('12345678'),
'role_admin' => true,
'role_user' => false,
'super_admin' => true,
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
]);
}
}
Binary file added docs/nova-roles-and-permissions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 10 additions & 7 deletions src/Publisher.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@ class Publisher extends ServiceProvider
{
public function register(): void
{
if ($this->app->runningInConsole()) {
$this->publish();
}
}

public function boot(): void
@@ -21,17 +24,17 @@ protected function publish(): void
__DIR__.'/../database/migrations/2024_04_22_100734_add_roles_and_permissions_to_users_table.stub' => database_path('migrations/2024_04_22_100734_add_roles_and_permissions_to_users_table.php'),

// models
__DIR__.'/../stubs/app/Models/Admin.stub' => app_path('Models/Admin.php'),
__DIR__.'/../stubs/app/Models/User.stub' => app_path('Models/User.php'),
__DIR__.'/../app/Models/Admin.stub' => app_path('Models/Admin.php'),
__DIR__.'/../app/Models/User.stub' => app_path('Models/User.php'),

// resources
__DIR__.'/../stubs/app/Nova/Admin.stub' => app_path('Nova/Admin.php'),
__DIR__.'/../stubs/app/Nova/User.stub' => app_path('Nova/User.php'),
__DIR__.'/../app/Nova/Admin.stub' => app_path('Nova/Admin.php'),
__DIR__.'/../app/Nova/User.stub' => app_path('Nova/User.php'),

// policies
__DIR__.'/../stubs/app/Policies/AdminPolicy.stub' => app_path('Policies/AdminPolicy.php'),
__DIR__.'/../stubs/app/Policies/UserPolicy.stub' => app_path('Policies/UserPolicy.php'),
__DIR__.'/../stubs/app/Policies/CorePermissions.stub' => app_path('Policies/CorePermissions.php'),
__DIR__.'/../app/Policies/AdminPolicy.stub' => app_path('Policies/AdminPolicy.php'),
__DIR__.'/../app/Policies/UserPolicy.stub' => app_path('Policies/UserPolicy.php'),
__DIR__.'/../app/Policies/CorePermissions.stub' => app_path('Policies/CorePermissions.php'),
]);
}
}