user()->cannot('create', School::class)) { abort(403); } request()->validate([ 'name' => ['required', 'min:3', 'max:30'], 'address' => ['required'], 'city' => ['required'], 'state' => ['required', 'min:2', 'max:2'], 'zip' => ['required', 'min:5', 'max:10'], ]); $school = School::create([ 'name' => request('name'), 'address' => request('address'), 'city' => request('city'), 'state' => request('state'), 'zip' => request('zip'), ]); $message = 'Created school #'.$school->id.' - '.$school->name.' with address
'.$school->address.'
'.$school->city.', '.$school->state.' '.$school->zip; AuditLogEntry::create([ 'user' => auth()->user()->email, 'ip_address' => request()->ip(), 'message' => $message, 'affected' => ['schools' => [$school->id]], ]); if (! Auth::user()->school) { Auth::user()->update([ 'school_id' => $school->id, ]); $message = 'Set user '.auth()->user()->full_name().' ('.auth()->user()->email.') as a director at '.$school->name.'(#'.$school->id.')'; AuditLogEntry::create([ 'user' => auth()->user()->email, 'ip_address' => request()->ip(), 'message' => $message, 'affected' => [ 'users' => [auth()->user()->id], 'schools' => [$school->id], ], ]); SchoolEmailDomain::create([ 'school_id' => $school->id, 'domain' => Auth::user()->emailDomain(), ]); $message = 'Added '.auth()->user()->emailDomain().' as an email domain for '.$school->name.' (#'.$school->id.')'; AuditLogEntry::create([ 'user' => auth()->user()->email, 'ip_address' => request()->ip(), 'message' => $message, 'affected' => [ 'schools' => [$school->id], ], ]); auth()->user()->refresh(); try { $headSetter->setHeadDirector(auth()->user()); } catch (AuditionAdminException $e) { redirect(route('schools.show', $school))->with('error', 'Could not set as head director'); } } return redirect('/schools/'.$school->id); } 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(Request $request, School $school) { if ($request->user()->cannot('update', $school)) { abort(403); } request()->validate([ 'name' => ['required', 'min:3', 'max:30'], 'address' => ['required'], 'city' => ['required'], 'state' => ['required', 'min:2', 'max:2'], 'zip' => ['required', 'min:5', 'max:10'], ]); $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
'.$school->address.'
'.$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 my_school() { if (Auth::user()->school) { return redirect('/schools/'.Auth::user()->school->id); } return redirect('/schools/create'); } 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'); } }