School policy added
This commit is contained in:
parent
f7f82f0d20
commit
afcdd34532
|
|
@ -4,16 +4,20 @@ namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\School;
|
use App\Models\School;
|
||||||
use App\Models\SchoolEmailDomain;
|
use App\Models\SchoolEmailDomain;
|
||||||
|
use Illuminate\Auth\Access\Gate;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use function abort;
|
||||||
use function dd;
|
use function dd;
|
||||||
use function redirect;
|
use function redirect;
|
||||||
use function request;
|
use function request;
|
||||||
|
|
||||||
class SchoolController extends Controller
|
class SchoolController extends Controller
|
||||||
{
|
{
|
||||||
public function store()
|
public function store(Request $request): RedirectResponse
|
||||||
{
|
{
|
||||||
|
if ($request->user()->cannot('create', School::class)) abort(403);
|
||||||
request()->validate([
|
request()->validate([
|
||||||
'name' => ['required', 'min:3', 'max:30'],
|
'name' => ['required', 'min:3', 'max:30'],
|
||||||
'address' => ['required'],
|
'address' => ['required'],
|
||||||
|
|
@ -47,24 +51,28 @@ class SchoolController extends Controller
|
||||||
return redirect('/schools/' . $school->id);
|
return redirect('/schools/' . $school->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show(School $school)
|
public function show(Request $request, School $school)
|
||||||
{
|
{
|
||||||
|
if ($request->user()->cannot('view',$school)) abort(403);
|
||||||
|
|
||||||
return view('schools.show', ['school' => $school]);
|
return view('schools.show', ['school' => $school]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create()
|
public function create(Request $request)
|
||||||
{
|
{
|
||||||
|
if ($request->user()->cannot('create', School::class)) abort(403);
|
||||||
return view('schools.create');
|
return view('schools.create');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function edit(School $school)
|
public function edit(Request $request, School $school)
|
||||||
{
|
{
|
||||||
// TODO Restrict the editing of schools to directors or admin
|
if ($request->user()->cannot('update',$school)) abort(403);
|
||||||
return view('schools.edit', ['school' => $school]);
|
return view('schools.edit', ['school' => $school]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(School $school)
|
public function update(School $school)
|
||||||
{
|
{
|
||||||
|
if ($request->user()->cannot('update',$school)) abort(403);
|
||||||
request()->validate([
|
request()->validate([
|
||||||
'name' => ['required', 'min:3', 'max:30'],
|
'name' => ['required', 'min:3', 'max:30'],
|
||||||
'address' => ['required'],
|
'address' => ['required'],
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,75 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Policies;
|
||||||
|
|
||||||
|
use App\Models\School;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Auth\Access\Response;
|
||||||
|
use function is_null;
|
||||||
|
|
||||||
|
class SchoolPolicy
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Grand admin users access to all functions
|
||||||
|
*/
|
||||||
|
public function before(User $user, string $ability): bool|null
|
||||||
|
{
|
||||||
|
if($user->is_admin) return true;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Determine whether the user can view any models.
|
||||||
|
*/
|
||||||
|
public function viewAny(User $user): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can view the model.
|
||||||
|
*/
|
||||||
|
public function view(User $user, School $school): bool
|
||||||
|
{
|
||||||
|
return $school->id == $user->school_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can create models.
|
||||||
|
*/
|
||||||
|
public function create(User $user): bool
|
||||||
|
{
|
||||||
|
return is_null($user->school_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can update the model.
|
||||||
|
*/
|
||||||
|
public function update(User $user, School $school): bool
|
||||||
|
{
|
||||||
|
return $school->id == $user->school_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can delete the model.
|
||||||
|
*/
|
||||||
|
public function delete(User $user, School $school): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can restore the model.
|
||||||
|
*/
|
||||||
|
public function restore(User $user, School $school): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can permanently delete the model.
|
||||||
|
*/
|
||||||
|
public function forceDelete(User $user, School $school): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -30,6 +30,7 @@ class UserFactory extends Factory
|
||||||
'email' => fake()->unique()->safeEmail(),
|
'email' => fake()->unique()->safeEmail(),
|
||||||
'judging_preference' => fake()->randomElement($judingPrefPossibilities),
|
'judging_preference' => fake()->randomElement($judingPrefPossibilities),
|
||||||
'cell_phone' => fake()->phoneNumber(),
|
'cell_phone' => fake()->phoneNumber(),
|
||||||
|
'profile_image_url' => 'https://picsum.photos/200',
|
||||||
'email_verified_at' => now(),
|
'email_verified_at' => now(),
|
||||||
'password' => static::$password ??= Hash::make('password'),
|
'password' => static::$password ??= Hash::make('password'),
|
||||||
'remember_token' => Str::random(10),
|
'remember_token' => Str::random(10),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue