Head directors can manage school email domains

Work on #64
Work on #66
This commit is contained in:
Matt Young 2024-08-10 23:08:13 -05:00
parent 4d3369fb11
commit 1100e711f8
3 changed files with 70 additions and 8 deletions

View File

@ -9,6 +9,7 @@ use App\Models\AuditLogEntry;
use App\Models\School; use App\Models\School;
use App\Models\SchoolEmailDomain; use App\Models\SchoolEmailDomain;
use App\Models\User; use App\Models\User;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
@ -211,4 +212,46 @@ 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)
{
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');
}
} }

View File

@ -36,14 +36,31 @@
</div> </div>
@endif @endif
</x-card.info.row> </x-card.info.row>
@if(auth()->user()->hasFlag('head_director'))
<x-card.info.row row_name="Associated Email Domains"> <x-card.info.row row_name="Associated Email Domains">
<ul> <p class="text-sm text-gray-400 border-b">Users with emails in these domains (the part after the @) that don't already have a school
@foreach($school->emailDomains as $domain) will be able to join your school.</p>
<li>{{ $domain->domain }}</li> <ul>
@endforeach @foreach($school->emailDomains as $domain)
</ul> <li class="flex my-2">
</x-card.info.row> <a href="{{route('schools.delete_domain',$domain)}}">
<x-icons.circled-x color="red" />
</a>
{{ $domain->domain }}
</li>
@endforeach
<li class="border-t">
<p class="text-base">Add Domain</p>
<x-form.form method="post" action="{{route('schools.add_domain',$school)}}" class="-ml-8">
<div class="grid grid-cols-3">
<x-form.field name="domain" label_text="Domain (do not include @)" colspan="2" />
<x-form.button class="mt-6 ml-2">Add</x-form.button>
</div>
</x-form.form>
</li>
</ul>
</x-card.info.row>
@endif
</x-card.info.body> </x-card.info.body>
</x-card.card> </x-card.card>
</div> </div>

View File

@ -54,6 +54,8 @@ Route::middleware(['auth', 'verified'])->controller(SchoolController::class)->gr
Route::patch('/schools/{school}', 'update')->name('schools.update'); Route::patch('/schools/{school}', 'update')->name('schools.update');
Route::post('schools/{school}/add_director', 'addDirector')->name('schools.add_director'); Route::post('schools/{school}/add_director', 'addDirector')->name('schools.add_director');
Route::get('/schools/{school}/set_head_director/{user}', 'setHeadDirector')->name('schools.set_head_director'); Route::get('/schools/{school}/set_head_director/{user}', 'setHeadDirector')->name('schools.set_head_director');
Route::post('/schools/{school}/add_domain', 'addDomain')->name('schools.add_domain');
Route::get('/schools/delete_domain/{domain}', 'deleteDomain')->name('schools.delete_domain');
}); });
// Doubler Related Routes // Doubler Related Routes