Start integrating spatie / laravel-permission

This commit is contained in:
Ryan Scherler
2020-03-16 14:52:58 -07:00
parent 0c33a4ebf1
commit 2db3ca9d7e
7 changed files with 208 additions and 11 deletions
+2 -5
View File
@@ -19,16 +19,13 @@
}
],
"require": {
"php": "^7.2.5",
"doctrine/dbal": "^2.10.0",
"graham-campbell/markdown": "^12.0",
"laravel/ui": "^2.0",
"spatie/laravel-permission": "^3.11",
"thomaswelton/laravel-gravatar": "~1.0",
"watson/active": "^5.0"
},
"require-dev": {
"ext-pdo_sqlite": "*",
"doctrine/dbal": "^2.10.0"
},
"autoload": {
"psr-4": {
"Alpine\\": "src"
@@ -0,0 +1,43 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddIsSuperAdminToUsersTable extends Migration
{
/**
* The name of the table for the migration.
*
* @var string
*/
protected $tableName = 'users';
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (!Schema::hasColumn($this->tableName, 'is_super_admin')) {
Schema::table($this->tableName, function (Blueprint $table) {
$table->boolean('is_super_admin')->default(0)->after('email');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if (Schema::hasColumn($this->tableName, 'is_super_admin')) {
Schema::table($this->tableName, function (Blueprint $table) {
$table->dropColumn('is_super_admin');
});
}
}
}
@@ -0,0 +1,102 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePermissionTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$tableNames = config('permission.table_names');
$columnNames = config('permission.column_names');
Schema::create($tableNames['permissions'], function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('guard_name');
$table->timestamps();
});
Schema::create($tableNames['roles'], function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('guard_name');
$table->timestamps();
});
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
$table->unsignedBigInteger('permission_id');
$table->string('model_type');
$table->unsignedBigInteger($columnNames['model_morph_key']);
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
$table->foreign('permission_id')
->references('id')
->on($tableNames['permissions'])
->onDelete('cascade');
$table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_permission_model_type_primary');
});
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
$table->unsignedBigInteger('role_id');
$table->string('model_type');
$table->unsignedBigInteger($columnNames['model_morph_key']);
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
$table->foreign('role_id')
->references('id')
->on($tableNames['roles'])
->onDelete('cascade');
$table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
'model_has_roles_role_model_type_primary');
});
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
$table->unsignedBigInteger('permission_id');
$table->unsignedBigInteger('role_id');
$table->foreign('permission_id')
->references('id')
->on($tableNames['permissions'])
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on($tableNames['roles'])
->onDelete('cascade');
$table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary');
});
app('cache')
->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
->forget(config('permission.cache.key'));
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$tableNames = config('permission.table_names');
Schema::drop($tableNames['role_has_permissions']);
Schema::drop($tableNames['model_has_roles']);
Schema::drop($tableNames['model_has_permissions']);
Schema::drop($tableNames['roles']);
Schema::drop($tableNames['permissions']);
}
}
+17
View File
@@ -6,6 +6,7 @@ use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Events\Dispatcher;
@@ -46,6 +47,7 @@ class AlpineServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->handleAuth();
$this->handleCommands();
$this->handleEvents();
$this->handleMiddleware();
@@ -91,6 +93,21 @@ class AlpineServiceProvider extends ServiceProvider
// $this->app->bind(ResourceContract::class, config('alpine.models.resource'));
}
/**
* Register the package's auth.
*
* @return void
*/
protected function handleAuth()
{
// Implicitly grant "Super Admin" role all permissions
// This works in the app by using gate-related functions like auth()->user->can() and @can()
// @link https://docs.spatie.be/laravel-permission/v3/basic-usage/super-admin/
Gate::before(function ($user, $ability) {
return $user->is_super_admin ? true : null;
});
}
/**
* Register the package's commands.
*
@@ -0,0 +1,20 @@
<?php
namespace Alpine\Http\Controllers;
use App\User;
class DashboardController extends Controller
{
/**
* Show the dashboard.
*
* @return View
*/
public function __invoke()
{
$title = __('alpine::admin.dashboard');
return view('alpine::dashboard', compact('title'));
}
}
+23 -3
View File
@@ -5,8 +5,29 @@ namespace Alpine\Traits;
use Alpine\Notifications\ResetPassword;
use Illuminate\Support\Facades\Hash;
trait AlpineUser {
trait AlpineUser
{
/**
* Initialize the trait.
*
* @return void
*/
public function initializeAlpineUser()
{
$this->mergeFillable(['is_super_admin']);
$this->mergeCasts(['is_super_admin' => 'boolean']);
}
/**
* Merge fillable attributes.
*
* @return void
*/
protected function mergeFillable(array $fillable)
{
$this->fillable(collect($this->fillable)->merge($fillable)->all());
}
/**
* Send the password reset notification.
*
@@ -28,5 +49,4 @@ trait AlpineUser {
{
$this->attributes['password'] = Hash::make($pass);
}
}
+1 -3
View File
@@ -40,8 +40,6 @@ Route::name('auth.')->namespace('Auth')->group(function () {
// protected admin routes...
Route::name('admin.')->middleware('auth.alpine')->group(function () {
Route::get('/', function () {
return view('alpine::dashboard');
})->name('dashboard');
Route::get('/', 'DashboardController')->name('dashboard');
});