Create admin users

This commit is contained in:
bad 2021-11-20 22:12:35 +01:00
parent f725731a6a
commit abe2939542
7 changed files with 85 additions and 2 deletions

View file

@ -55,6 +55,7 @@ class Kernel extends HttpKernel
*/ */
protected $routeMiddleware = [ protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class, 'auth' => \App\Http\Middleware\Authenticate::class,
'auth.admin' => \App\Http\Middleware\AdminAuth::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class,

View file

@ -0,0 +1,27 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class AdminAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$user = Auth::user();
if($user && $user->isAdmin()) {
return $next($request);
}
throw new AccessDeniedHttpException("You must be an admin to access this path");
}
}

View file

@ -45,6 +45,10 @@ class User extends Authenticatable
'email_verified_at' => 'datetime', 'email_verified_at' => 'datetime',
]; ];
public function isAdmin() {
return $this->admin;
}
public function cart() { public function cart() {
return $this->belongsToMany(Product::class, "cart_items", "userID", "productID"); return $this->belongsToMany(Product::class, "cart_items", "userID", "productID");
} }

7
config/admin.php Normal file
View file

@ -0,0 +1,7 @@
<?php
return [
'email' => env("ADMIN_EMAIL"),
'username' => env("ADMIN_USERNAME"),
'password' => env("ADMIN_PASSWORD"),
];

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AdminFieldUser extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table("users", function (Blueprint $blueprint) {
$blueprint->boolean("admin")->default(false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table("users", function (Blueprint $blueprint) {
$blueprint->dropColumn("admin");
});
}
}

View file

@ -3,7 +3,9 @@
namespace Database\Seeders; namespace Database\Seeders;
use App\Models\Image; use App\Models\Image;
use App\Models\User;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
class DatabaseSeeder extends Seeder class DatabaseSeeder extends Seeder
{ {
@ -14,7 +16,17 @@ class DatabaseSeeder extends Seeder
*/ */
public function run() public function run()
{ {
\App\Models\User::factory(10)->create(); User::factory(10)->create();
$creds = [
'email' => config('admin.email'),
'name' => config('admin.username'),
'password' => config('admin.password'),
];
$creds["password"] = Hash::make($creds["password"]);
$user = User::create($creds);
$user->admin = true;
$user->save();
\App\Models\Product::factory(10)->has(Image::factory()->count(3))->create(); \App\Models\Product::factory(10)->has(Image::factory()->count(3))->create();
} }
} }

View file

@ -38,4 +38,4 @@ Route::post("/product/{product}/removeFromCart", [CartController::class, "remove
Route::resource("product", ProductController::class); Route::resource("product", ProductController::class);
Route::resource("order", OrderController::class); Route::resource("order", OrderController::class);
Route::resource("image", ImageController::class)->only(["store", "delete", "create"]); Route::resource("image", ImageController::class)->only(["store", "delete", "create"])->middleware("auth.admin");