Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#116 - Add school disabling #125

Merged
6 changes: 5 additions & 1 deletion app/Http/Controllers/ContestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class ContestController extends Controller
{
public function index(): Response
{
$schools = School::all()->sortBy("name");
$schools = School::query()
->where("is_disabled", false)
->where("is_admin_school", false)
->orderBy("name")
->get();

return Inertia::render("Home", ["schools" => SchoolResource::collection($schools)]);
}
Expand Down
46 changes: 41 additions & 5 deletions app/Http/Controllers/SchoolsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Cache;
use Inertia\Inertia;
Expand All @@ -26,15 +27,18 @@

class SchoolsController extends Controller
{
public function index(SortHelper $sorter): Response
public function index(SortHelper $sorter, Request $request): Response
{
$query = $sorter->sort(School::query(), ["id", "name", "regon", "updated_at", "created_at"], ["students", "address"]);
$query = School::query()->where("is_admin_school", false);
$query = $sorter->sort($query, ["id", "name", "regon", "updated_at", "created_at"], ["students", "address"]);
$query = $this->sortByStudents($query, $sorter);
$query = $this->sortByAddress($query, $sorter);
$query = $this->filterDisabledSchools($query, $request);
$query = $sorter->search($query, "name");
$schools = $sorter->paginate($query);

return Inertia::render("Admin/SchoolsPanel", ["schools" => SchoolResource::collection($schools)]);
return Inertia::render("Admin/SchoolsPanel", [
"schools" => SchoolResource::collection($sorter->paginate($query)),
]);
}

public function store(SchoolRequest $request): RedirectResponse
Expand All @@ -59,7 +63,28 @@ public function destroy(School $school): RedirectResponse
$school->delete();

return redirect()
->back();
->back()
->with("status", "Szkoła została usunięta.");
}

public function disable(School $school): RedirectResponse
{
$school->is_disabled = true;
$school->save();

return redirect()
->back()
->with("status", "Szkoła została zablokowa.");
}

public function enable(School $school): RedirectResponse
{
$school->is_disabled = false;
$school->save();

return redirect()
->back()
->with("status", "Szkoła została odblokowana.");
}
EwelinaSkrzypacz marked this conversation as resolved.
Show resolved Hide resolved

/**
Expand Down Expand Up @@ -131,4 +156,15 @@ private function sortByAddress(Builder $query, SortHelper $sorter): Builder

return $query;
}

private function filterDisabledSchools(Builder $query, Request $request): Builder
{
$showDisabled = $request->query("disabled", "false") === "true";

if (!$showDisabled) {
return $query->where("is_disabled", false);
}

return $query;
}
}
12 changes: 0 additions & 12 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace App\Http\Controllers;

use App\Http\Requests\UserRequest;
use App\Http\Resources\SchoolResource;
use App\Http\Resources\UserResource;
use App\Models\School;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand All @@ -25,16 +23,6 @@ public function index(Request $request): Response
]);
}

public function edit(User $user): Response
{
$this->authorize("update", $user);

return Inertia::render("Admin/EditUser", [
"user" => UserResource::make($user),
"schools" => SchoolResource::collection(School::all()),
]);
}

public function update(UserRequest $request, User $user): RedirectResponse
{
$this->authorize("update", $user);
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Requests/Auth/RegisterUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Http\Requests\Auth;

use App\Rules\IsSchoolValidForRegularUsers;
use Illuminate\Foundation\Http\FormRequest;

class RegisterUserRequest extends FormRequest
Expand All @@ -20,7 +21,7 @@ public function rules(): array
"firstname" => ["required", "string", "max:255"],
"surname" => ["required", "string", "max:255"],
"password" => ["required", "string", "min:8"],
"school_id" => ["required", "integer", "exists:schools,id"],
"school_id" => ["required", "integer", "exists:schools,id", new IsSchoolValidForRegularUsers()],
];
}
}
3 changes: 2 additions & 1 deletion app/Http/Requests/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Http\Requests;

use App\Rules\IsSchoolValidForRegularUsers;
use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
Expand All @@ -19,7 +20,7 @@ public function rules(): array
"email" => ["required", "string", "email", "max:255", "unique:users,email," . $this->user->id],
"firstname" => ["required", "string", "max:255"],
"surname" => ["required", "string", "max:255"],
"school_id" => ["required", "integer", "exists:schools,id"],
"school_id" => ["required", "integer", "exists:schools,id", new IsSchoolValidForRegularUsers()],
];
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/SchoolResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function toArray(Request $request): array
"regon" => $this->regon,
"city" => $this->city,
"street" => $this->street,
"isDisabled" => $this->is_disabled,
"buildingNumber" => $this->building_number,
"apartmentNumber" => $this->apartment_number,
"zipCode" => $this->zip_code,
Expand Down
2 changes: 2 additions & 0 deletions app/Models/School.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @property string $building_number
* @property string $apartment_number
* @property string $zip_code
* @property boolean $is_disabled
* @property boolean $is_admin_school
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Collection<User> $users
Expand Down
31 changes: 31 additions & 0 deletions app/Policies/SchoolPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\School;
use App\Models\User;

class SchoolPolicy
{
public function update(User $user, School $school): bool
{
return !$school->is_admin_school && !$school->is_disabled;
}

public function delete(User $user, School $school): bool
{
return $school->users()->count() === 0 && !$school->is_disabled && !$school->is_admin_school;
}

public function disable(User $user, School $school): bool
{
return !$school->is_disabled && !$school->is_admin_school;
}

public function enable(User $user, School $school): bool
{
return $school->is_disabled && !$school->is_admin_school;
}
}
26 changes: 26 additions & 0 deletions app/Rules/IsSchoolValidForRegularUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Rules;

use App\Models\School;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Lang;

class IsSchoolValidForRegularUsers implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$school = School::query()->find($value);

if (!$school) {
return;
}

if ($school->is_disabled | $school->is_admin_school) {
$fail(Lang::get("validation.custom.school_id.exists"));
}
}
}
16 changes: 16 additions & 0 deletions database/factories/SchoolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,25 @@ public function definition(): array
"building_number" => fake()->buildingNumber(),
"apartment_number" => fake()->buildingNumber(),
"zip_code" => fake()->randomNumber(2) . "-" . fake()->randomNumber(3),
"is_disabled" => false,
"is_admin_school" => false,
];
}

public function disabled()
{
return $this->state(fn(array $attributes): array => [
"is_disabled" => true,
]);
}

public function adminSchool()
{
return $this->state(fn(array $attributes): array => [
"is_admin_school" => true,
]);
}

public function withoutApartment(): static
{
return $this->state(fn(array $attributes): array => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function up(): void
$table->string("token");
$table->timestamp("created_at")->nullable();
});

Schema::create("sessions", function (Blueprint $table): void {
$table->string("id")->primary();
$table->foreignId("user_id")->nullable()->index();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public function up(): void
$table->string("building_number");
$table->string("apartment_number")->nullable();
$table->string("zip_code");
$table->boolean("is_disabled")->default(false);
$table->boolean("is_admin_school")->default(false);
$table->timestamps();
});
}
Expand Down
18 changes: 16 additions & 2 deletions database/seeders/AdminSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ class AdminSeeder extends Seeder
{
public function run(): void
{
$school = School::firstOrCreate(
["is_admin_school" => true],
[
"name" => "Admin school",
"regon" => "",
"city" => "",
"street" => "",
"building_number" => "",
"zip_code" => "",
"is_disabled" => true,
"is_admin_school" => true,
],
);

$superAdmin = User::firstOrCreate(
["email" => "[email protected]"],
[
Expand All @@ -23,7 +37,7 @@ public function run(): void
"email_verified_at" => Carbon::now(),
"password" => Hash::make("password"),
"remember_token" => Str::random(10),
"school_id" => School::factory()->create()->id,
"school_id" => $school->id,
],
);
$superAdmin->syncRoles("super_admin");
Expand All @@ -36,7 +50,7 @@ public function run(): void
"email_verified_at" => Carbon::now(),
"password" => Hash::make("password"),
"remember_token" => Str::random(10),
"school_id" => School::factory()->create()->id,
"school_id" => $school->id,
],
);
$admin->syncRoles("admin");
Expand Down
3 changes: 0 additions & 3 deletions resources/js/Pages/Admin/CreateAdmin.vue

This file was deleted.

3 changes: 0 additions & 3 deletions resources/js/Pages/Admin/EditAdmin.vue

This file was deleted.

3 changes: 0 additions & 3 deletions resources/js/Pages/Admin/EditUser.vue

This file was deleted.

Loading
Loading