Administrators can make a user head director

Work on #64
This commit is contained in:
Matt Young 2024-08-10 18:59:30 -05:00
parent df9b64a4e2
commit 46441268c9
2 changed files with 26 additions and 7 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Actions\Schools\SetHeadDirector;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Mail\NewUserPassword; use App\Mail\NewUserPassword;
use App\Models\AuditLogEntry; use App\Models\AuditLogEntry;
@ -13,6 +14,8 @@ use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use function auditionLog;
class UserController extends Controller class UserController extends Controller
{ {
public function index() public function index()
@ -45,7 +48,7 @@ class UserController extends Controller
return view('admin.users.create', ['schools' => $schools]); return view('admin.users.create', ['schools' => $schools]);
} }
public function update(Request $request, User $user) public function update(Request $request, User $user, SetHeadDirector $headSetter)
{ {
if (! Auth::user()->is_admin) { if (! Auth::user()->is_admin) {
abort(403); abort(403);
@ -63,6 +66,7 @@ class UserController extends Controller
]); ]);
$validData['is_admin'] = $request->get('is_admin') == 'on' ? 1 : 0; $validData['is_admin'] = $request->get('is_admin') == 'on' ? 1 : 0;
$validData['is_tab'] = $request->get('is_tab') == 'on' ? 1 : 0; $validData['is_tab'] = $request->get('is_tab') == 'on' ? 1 : 0;
$validData['is_head'] = $request->get('is_head') == 'on' ? 1 : 0;
$user->update([ $user->update([
'first_name' => $validData['first_name'], 'first_name' => $validData['first_name'],
'last_name' => $validData['last_name'], 'last_name' => $validData['last_name'],
@ -76,11 +80,11 @@ class UserController extends Controller
$user->refresh(); $user->refresh();
$logged_school = $user->school_id ? $user->school->name : 'No School'; $logged_school = $user->school_id ? $user->school->name : 'No School';
$message = 'Updated user #'.$user->id.' - '.$oldEmail $message = 'Updated user #'.$user->id.' - '.$oldEmail
.'<br>Name: '.$user->full_name() .'<br>Name: '.$user->full_name()
.'<br>Email: '.$user->email .'<br>Email: '.$user->email
.'<br>Cell Phone: '.$user->cell_phone .'<br>Cell Phone: '.$user->cell_phone
.'<br>Judging Pref: '.$user->judging_preference .'<br>Judging Pref: '.$user->judging_preference
.'<br>School: '.$logged_school; .'<br>School: '.$logged_school;
AuditLogEntry::create([ AuditLogEntry::create([
'user' => auth()->user()->email, 'user' => auth()->user()->email,
@ -106,6 +110,16 @@ class UserController extends Controller
'affected' => ['users' => [$user->id]], 'affected' => ['users' => [$user->id]],
]); ]);
} }
if ($user->hasFlag('head_director') != $validData['is_head'] && ! is_null($user->school_id)) {
if ($validData['is_head']) {
$headSetter->setHeadDirector($user);
} else {
$user->removeFlag('head_director');
$logMessage = 'Removed '.$user->full_name().' as head director at '.$user->school->name;
$logAffected = ['users' => [$user->id], 'schools' => [$user->school_id]];
auditionLog($logMessage, $logAffected);
}
}
return redirect('/admin/users'); return redirect('/admin/users');
} }

View File

@ -22,7 +22,7 @@
<x-form.field name="cell_phone" label_text="Cell Phone" colspan="3" value="{{ $user->cell_phone }}"/> <x-form.field name="cell_phone" label_text="Cell Phone" colspan="3" value="{{ $user->cell_phone }}"/>
<x-form.field name="judging_preference" label_text="Judging Preference" colspan="6" <x-form.field name="judging_preference" label_text="Judging Preference" colspan="6"
value="{{ $user->judging_preference }}"/> value="{{ $user->judging_preference }}"/>
<x-form.select name="school_id" colspan="6"> <x-form.select name="school_id" colspan="4">
<x-slot:label>School</x-slot:label> <x-slot:label>School</x-slot:label>
<option value="">No School</option> <option value="">No School</option>
@foreach ($schools as $school) @foreach ($schools as $school)
@ -31,6 +31,11 @@
@endforeach @endforeach
</x-form.select> </x-form.select>
<div class="col-span-2 pt-7">
<x-form.checkbox name="is_head" :checked="$user->hasFlag('head_director')">
<x-slot:label>Head Director</x-slot:label>
</x-form.checkbox>
</div>
<div class="col-span-3"> <div class="col-span-3">
<x-form.checkbox name="is_admin" :checked="$user->is_admin"> <x-form.checkbox name="is_admin" :checked="$user->is_admin">
<x-slot:label>Administrator</x-slot:label> <x-slot:label>Administrator</x-slot:label>