Update SchoolController to use actions.
This commit is contained in:
parent
5ebef46be7
commit
d7134a948b
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Actions\Schools\AssignUserToSchool;
|
||||||
|
use App\Actions\Schools\CreateSchool;
|
||||||
use App\Actions\Schools\SetHeadDirector;
|
use App\Actions\Schools\SetHeadDirector;
|
||||||
use App\Exceptions\AuditionAdminException;
|
use App\Exceptions\AuditionAdminException;
|
||||||
use App\Mail\NewUserPassword;
|
use App\Mail\NewUserPassword;
|
||||||
|
|
@ -29,56 +31,27 @@ class SchoolController extends Controller
|
||||||
if ($request->user()->cannot('create', School::class)) {
|
if ($request->user()->cannot('create', School::class)) {
|
||||||
abort(403);
|
abort(403);
|
||||||
}
|
}
|
||||||
request()->validate([
|
$validData = request()->validate([
|
||||||
'name' => ['required', 'min:3', 'max:30'],
|
'name' => ['required', 'min:3', 'max:30', 'unique:schools,name'],
|
||||||
'address' => ['required'],
|
'address' => ['required'],
|
||||||
'city' => ['required'],
|
'city' => ['required'],
|
||||||
'state' => ['required', 'min:2', 'max:2'],
|
'state' => ['required', 'min:2', 'max:2'],
|
||||||
'zip' => ['required', 'min:5', 'max:10'],
|
'zip' => ['required', 'min:5', 'max:10'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$school = School::create([
|
$creator = app(CreateSchool::class);
|
||||||
'name' => request('name'),
|
|
||||||
'address' => request('address'),
|
$school = $creator(
|
||||||
'city' => request('city'),
|
$validData['name'],
|
||||||
'state' => request('state'),
|
$validData['address'],
|
||||||
'zip' => request('zip'),
|
$validData['city'],
|
||||||
]);
|
$validData['state'],
|
||||||
$message = 'Created school #'.$school->id.' - '.$school->name.' with address <br>'.$school->address.'<br>'.$school->city.', '.$school->state.' '.$school->zip;
|
$validData['zip'],
|
||||||
AuditLogEntry::create([
|
);
|
||||||
'user' => auth()->user()->email,
|
|
||||||
'ip_address' => request()->ip(),
|
$assigner = app(AssignUserToSchool::class);
|
||||||
'message' => $message,
|
$assigner(auth()->user(), $school);
|
||||||
'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();
|
auth()->user()->refresh();
|
||||||
try {
|
try {
|
||||||
$headSetter->setHeadDirector(auth()->user());
|
$headSetter->setHeadDirector(auth()->user());
|
||||||
|
|
@ -86,13 +59,13 @@ class SchoolController extends Controller
|
||||||
redirect(route('schools.show', $school))->with('error', 'Could not set as head director');
|
redirect(route('schools.show', $school))->with('error', 'Could not set as head director');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return redirect('/schools/'.$school->id);
|
return redirect('/schools/'.$school->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show(Request $request, School $school)
|
public function show(
|
||||||
{
|
Request $request,
|
||||||
|
School $school
|
||||||
|
) {
|
||||||
if ($request->user()->cannot('view', $school)) {
|
if ($request->user()->cannot('view', $school)) {
|
||||||
abort(403);
|
abort(403);
|
||||||
}
|
}
|
||||||
|
|
@ -100,8 +73,9 @@ class SchoolController extends Controller
|
||||||
return view('schools.show', ['school' => $school]);
|
return view('schools.show', ['school' => $school]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create(Request $request)
|
public function create(
|
||||||
{
|
Request $request
|
||||||
|
) {
|
||||||
if ($request->user()->cannot('create', School::class)) {
|
if ($request->user()->cannot('create', School::class)) {
|
||||||
abort(403);
|
abort(403);
|
||||||
}
|
}
|
||||||
|
|
@ -109,8 +83,10 @@ class SchoolController extends Controller
|
||||||
return view('schools.create');
|
return view('schools.create');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function edit(Request $request, School $school)
|
public function edit(
|
||||||
{
|
Request $request,
|
||||||
|
School $school
|
||||||
|
) {
|
||||||
if ($request->user()->cannot('update', $school)) {
|
if ($request->user()->cannot('update', $school)) {
|
||||||
abort(403);
|
abort(403);
|
||||||
}
|
}
|
||||||
|
|
@ -118,8 +94,10 @@ class SchoolController extends Controller
|
||||||
return view('schools.edit', ['school' => $school]);
|
return view('schools.edit', ['school' => $school]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(Request $request, School $school)
|
public function update(
|
||||||
{
|
Request $request,
|
||||||
|
School $school
|
||||||
|
) {
|
||||||
if ($request->user()->cannot('update', $school)) {
|
if ($request->user()->cannot('update', $school)) {
|
||||||
abort(403);
|
abort(403);
|
||||||
}
|
}
|
||||||
|
|
@ -158,8 +136,9 @@ class SchoolController extends Controller
|
||||||
return redirect('/schools/create');
|
return redirect('/schools/create');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addDirector(School $school)
|
public function addDirector(
|
||||||
{
|
School $school
|
||||||
|
) {
|
||||||
|
|
||||||
if (auth()->user()->school_id !== $school->id) {
|
if (auth()->user()->school_id !== $school->id) {
|
||||||
return redirect()->back()->with('error', 'No adding directors to another school');
|
return redirect()->back()->with('error', 'No adding directors to another school');
|
||||||
|
|
@ -193,8 +172,11 @@ class SchoolController extends Controller
|
||||||
return redirect()->back()->with('success', 'Director added');
|
return redirect()->back()->with('success', 'Director added');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setHeadDirector(School $school, User $user, SetHeadDirector $headSetter)
|
public function setHeadDirector(
|
||||||
{
|
School $school,
|
||||||
|
User $user,
|
||||||
|
SetHeadDirector $headSetter
|
||||||
|
) {
|
||||||
if (auth()->user()->school_id !== $school->id) {
|
if (auth()->user()->school_id !== $school->id) {
|
||||||
return redirect()->back()->with('error', 'No setting the head director for another school');
|
return redirect()->back()->with('error', 'No setting the head director for another school');
|
||||||
}
|
}
|
||||||
|
|
@ -213,8 +195,9 @@ class SchoolController extends Controller
|
||||||
return redirect()->back()->with('success', 'New head director set');
|
return redirect()->back()->with('success', 'New head director set');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addDomain(School $school)
|
public function addDomain(
|
||||||
{
|
School $school
|
||||||
|
) {
|
||||||
if (auth()->user()->school_id !== $school->id) {
|
if (auth()->user()->school_id !== $school->id) {
|
||||||
return redirect()->back()->with('error', 'No adding domains for another school');
|
return redirect()->back()->with('error', 'No adding domains for another school');
|
||||||
}
|
}
|
||||||
|
|
@ -239,8 +222,9 @@ class SchoolController extends Controller
|
||||||
return redirect()->back()->with('success', 'Domain added');
|
return redirect()->back()->with('success', 'Domain added');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deleteDomain(SchoolEmailDomain $domain)
|
public function deleteDomain(
|
||||||
{
|
SchoolEmailDomain $domain
|
||||||
|
) {
|
||||||
if (auth()->user()->school_id !== $domain->school_id) {
|
if (auth()->user()->school_id !== $domain->school_id) {
|
||||||
return redirect()->back()->with('error', 'No deleting domains for another school');
|
return redirect()->back()->with('error', 'No deleting domains for another school');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue