Head directors can add user accounts

Work on #64
This commit is contained in:
Matt Young 2024-08-10 21:42:36 -05:00
parent 9f71e5e3f5
commit d9f80a44f1
4 changed files with 111 additions and 35 deletions

View File

@ -4,14 +4,20 @@ namespace App\Http\Controllers;
use App\Actions\Schools\SetHeadDirector;
use App\Exceptions\AuditionAdminException;
use App\Mail\NewUserPassword;
use App\Models\AuditLogEntry;
use App\Models\School;
use App\Models\SchoolEmailDomain;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
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;
@ -150,4 +156,39 @@ class SchoolController extends Controller
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');
}
}

View File

@ -5,9 +5,10 @@
</head>
<body>
<h1>Hello, {{ $user->first_name }} {{ $user->last_name }}</h1>
<p>Your account has been created. Here are your login details:</p>
<p>Your AuditionAdmin account for {{ auditionSetting('auditionAbbreviation') }} has been created. Here are your login details:</p>
<p><strong>Email:</strong> {{ $user->email }}</p>
<p><strong>Password:</strong> {{ $password }}</p>
<p><strong>Login at: </strong> {{route('login')}}</p>
<p>Please change your password after logging in for the first time.</p>
</body>
</html>

View File

@ -1,3 +1,4 @@
<div x-data="{ showAddDirectorForm: false, changeHeadDirectorForm: false}">
<x-layout.app>
<x-slot:page_title>School Info - {{ $school->name }}</x-slot:page_title>
@ -20,9 +21,20 @@
<x-card.info.row row_name="Directors">
<ul>
@foreach($school->directors as $director)
<li>{{ $director->full_name() }} - <a class='text-indigo-600' href="mailto:{{ $director->email }}">{{ $director->email }}</a></li>
<li>
{{ $director->full_name() }}
@if($director->hasFlag('head_director')) <span class="font-semibold">(head)</span> @endif
-
<a class='text-indigo-600' href="mailto:{{ $director->email }}">{{ $director->email }}</a>
</li>
@endforeach
</ul>
@if(auth()->user()->hasFlag('head_director'))
<div class="grid grid-cols-2 gap-2 mt-3">
<x-form.button type="button" @click="showAddDirectorForm=true">Add Director</x-form.button>
<x-form.button type="button" @click="changeHeadDirectorForm=true">Change Head</x-form.button>
</div>
@endif
</x-card.info.row>
<x-card.info.row row_name="Associated Email Domains">
@ -37,3 +49,24 @@
</div>
</x-layout.app>
@if(auth()->user()->hasFlag('head_director'))
<x-modal-body showVar="showAddDirectorForm">
<x-slot:title>Add Director</x-slot:title>
<x-form.form method="POST" action="{{route('schools.add_director', $school)}}">
<x-form.body-grid>
<x-form.field name="first_name" label_text="First Name" colspan="3" />
<x-form.field name="last_name" label_text="Last Name" colspan="3" />
<x-form.field name="email" type="email" label_text="Email Address" colspan="3" />
<x-form.field name="cell_phone" label_text="Cell Phone" colspan="3" />
<x-form.field name="judging_preference" label_text="Judging Preference" colspan="6" />
</x-form.body-grid>
<x-form.footer submit-button-text="Add Director" />
</x-form.form>
</x-modal-body>
<x-modal-body showVar="changeHeadDirectorForm">
<x-slot:title>Change Head Director</x-slot:title>
</x-modal-body>
@endif
</div>

View File

@ -52,6 +52,7 @@ Route::middleware(['auth', 'verified'])->controller(SchoolController::class)->gr
Route::get('/schools/{school}/edit', 'edit')->name('schools.edit');
Route::get('/schools/{school}', 'show')->name('schools.show');
Route::patch('/schools/{school}', 'update')->name('schools.update');
Route::post('schools/{school}/add_director', 'addDirector')->name('schools.add_director');
});
// Doubler Related Routes