200 lines
7.0 KiB
PHP
200 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Actions\Schools\AssignUserToSchool;
|
|
use App\Actions\Schools\CreateSchool;
|
|
use App\Actions\Schools\SetHeadDirector;
|
|
use App\Exceptions\AuditionAdminException;
|
|
use App\Http\Requests\SchoolStoreRequest;
|
|
use App\Mail\NewUserPassword;
|
|
use App\Models\AuditLogEntry;
|
|
use App\Models\School;
|
|
use App\Models\SchoolEmailDomain;
|
|
use App\Models\User;
|
|
use Illuminate\Database\UniqueConstraintViolationException;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Str;
|
|
|
|
use function abort;
|
|
use function auditionLog;
|
|
use function redirect;
|
|
use function request;
|
|
|
|
class SchoolController extends Controller
|
|
{
|
|
public function store(SchoolStoreRequest $request, SetHeadDirector $headSetter): RedirectResponse
|
|
{
|
|
$creator = app(CreateSchool::class);
|
|
|
|
$school = $creator(
|
|
$request['name'],
|
|
$request['address'],
|
|
$request['city'],
|
|
$request['state'],
|
|
$request['zip'],
|
|
);
|
|
|
|
$assigner = app(AssignUserToSchool::class);
|
|
$assigner(auth()->user(), $school);
|
|
|
|
auth()->user()->refresh();
|
|
|
|
$headSetter->setHeadDirector(auth()->user());
|
|
|
|
return redirect(route('schools.show', $school));
|
|
}
|
|
|
|
public function show(Request $request, School $school)
|
|
{
|
|
if ($request->user()->cannot('view', $school)) {
|
|
abort(403);
|
|
}
|
|
|
|
return view('schools.show', ['school' => $school]);
|
|
}
|
|
|
|
public function create(Request $request)
|
|
{
|
|
if ($request->user()->cannot('create', School::class)) {
|
|
abort(403);
|
|
}
|
|
|
|
return view('schools.create');
|
|
}
|
|
|
|
public function edit(Request $request, School $school)
|
|
{
|
|
if ($request->user()->cannot('update', $school)) {
|
|
abort(403);
|
|
}
|
|
|
|
return view('schools.edit', ['school' => $school]);
|
|
}
|
|
|
|
public function update(SchoolStoreRequest $request, School $school)
|
|
{
|
|
$school->update([
|
|
'name' => $request['name'],
|
|
'address' => $request['address'],
|
|
'city' => $request['city'],
|
|
'state' => $request['state'],
|
|
'zip' => $request['zip'],
|
|
]);
|
|
$message = 'Modified school #'.$school->id.' - '.$school->name.' with address <br>'.$school->address.'<br>'.$school->city.', '.$school->state.' '.$school->zip;
|
|
AuditLogEntry::create([
|
|
'user' => auth()->user()->email,
|
|
'ip_address' => request()->ip(),
|
|
'message' => $message,
|
|
'affected' => ['schools' => [$school->id]],
|
|
]);
|
|
|
|
return redirect()->route('schools.show', $school->id)->with('success', 'School details updated');
|
|
}
|
|
|
|
public function addDirector(School $school)
|
|
{
|
|
|
|
if (auth()->user()->school_id !== $school->id) {
|
|
return redirect()->back()->with('error', 'No adding directors to another school');
|
|
}
|
|
if (! auth()->user()->hasFlag('head_director')) {
|
|
return redirect()->back()->with('error', 'Only the head director can add directors to a school');
|
|
}
|
|
$validData = request()->validate([
|
|
'first_name' => ['required'],
|
|
'last_name' => ['required'],
|
|
'email' => ['required', 'email', 'unique:users'],
|
|
'cell_phone' => ['required'],
|
|
'judging_preference' => ['required'],
|
|
]);
|
|
// Generate a random password
|
|
$randomPassword = Str::random(12);
|
|
$newUser = User::create([
|
|
'first_name' => $validData['first_name'],
|
|
'last_name' => $validData['last_name'],
|
|
'email' => $validData['email'],
|
|
'cell_phone' => $validData['cell_phone'],
|
|
'judging_preference' => $validData['judging_preference'],
|
|
'password' => Hash::make($randomPassword),
|
|
'school_id' => auth()->user()->school_id,
|
|
]);
|
|
$logMessage = 'Created user '.$newUser->full_name().' - '.$newUser->email.' as a director at '.$newUser->school->name;
|
|
$logAffected = ['users' => [$newUser->id], 'schools' => [$newUser->school_id]];
|
|
auditionLog($logMessage, $logAffected);
|
|
Mail::to($newUser->email)->send(new NewUserPassword($newUser, $randomPassword));
|
|
|
|
return redirect()->back()->with('success', 'Director added');
|
|
}
|
|
|
|
public function setHeadDirector(
|
|
School $school,
|
|
User $user,
|
|
SetHeadDirector $headSetter
|
|
) {
|
|
if (auth()->user()->school_id !== $school->id) {
|
|
return redirect()->back()->with('error', 'No setting the head director for another school');
|
|
}
|
|
if (! auth()->user()->hasFlag('head_director')) {
|
|
return redirect()->back()->with('error', 'Only the head director can name a new head director');
|
|
}
|
|
if ($school->id !== $user->school_id) {
|
|
return redirect()->back()->with('error', 'The proposed head director must be at your school');
|
|
}
|
|
try {
|
|
$headSetter->setHeadDirector($user);
|
|
} catch (AuditionAdminException $e) {
|
|
return redirect()->back()->with('error', $e->getMessage());
|
|
}
|
|
|
|
return redirect()->back()->with('success', 'New head director set');
|
|
}
|
|
|
|
public function addDomain(
|
|
School $school
|
|
) {
|
|
if (auth()->user()->school_id !== $school->id) {
|
|
return redirect()->back()->with('error', 'No adding domains for another school');
|
|
}
|
|
if (! auth()->user()->hasFlag('head_director')) {
|
|
return redirect()->back()->with('error', 'Only the head director can add domains');
|
|
}
|
|
$verifiedData = request()->validate([
|
|
'domain' => ['required'],
|
|
]);
|
|
try {
|
|
SchoolEmailDomain::create([
|
|
'school_id' => $school->id,
|
|
'domain' => $verifiedData['domain'],
|
|
]);
|
|
} catch (UniqueConstraintViolationException $e) {
|
|
return redirect()->back()->with('error', 'That domain is already associated with your school');
|
|
}
|
|
$logMessage = 'Added domain '.$verifiedData['domain'].' to school '.$school->name;
|
|
$logAffected = ['schools' => [$school->id]];
|
|
auditionLog($logMessage, $logAffected);
|
|
|
|
return redirect()->back()->with('success', 'Domain added');
|
|
}
|
|
|
|
public function deleteDomain(
|
|
SchoolEmailDomain $domain
|
|
) {
|
|
if (auth()->user()->school_id !== $domain->school_id) {
|
|
return redirect()->back()->with('error', 'No deleting domains for another school');
|
|
}
|
|
if (! auth()->user()->hasFlag('head_director')) {
|
|
return redirect()->back()->with('error', 'Only the head director can delete domains');
|
|
}
|
|
$logMessage = 'Deleted domain '.$domain->domain.' from school '.$domain->school->name;
|
|
$logAffected = ['schools' => [$domain->school_id]];
|
|
auditionLog($logMessage, $logAffected);
|
|
$domain->delete();
|
|
|
|
return redirect()->back()->with('success', 'Domain deleted');
|
|
}
|
|
}
|