auditionadmin-23 Need ability to make a user an administrator or tab #26

Merged
okorpheus merged 1 commits from auditionadmin-23 into master 2024-07-16 15:15:51 +00:00
6 changed files with 40 additions and 38 deletions
Showing only changes of commit 89eb7e1052 - Show all commits

View File

@ -50,7 +50,7 @@ class UserController extends Controller
abort(403);
}
request()->validate([
$validData = $request->validate([
'first_name' => ['required'],
'last_name' => ['required'],
'email' => ['required', 'email'],
@ -58,14 +58,18 @@ class UserController extends Controller
'judging_preference' => ['required'],
'school_id' => ['required', 'exists:schools,id'],
]);
$validData['is_admin'] = $request->get('is_admin') == 'on' ? 1 : 0;
$validData['is_tab'] = $request->get('is_tab') == 'on' ? 1 : 0;
$user->update([
'first_name' => request('first_name'),
'last_name' => request('last_name'),
'email' => request('email'),
'cell_phone' => request('cell_phone'),
'judging_preference' => request('judging_preference'),
'school_id' => request('school_id'),
'first_name' => $validData['first_name'],
'last_name' => $validData['last_name'],
'email' => $validData['email'],
'cell_phone' => $validData['cell_phone'],
'judging_preference' => $validData['judging_preference'],
'school_id' => $validData['school_id'],
'is_admin' => $validData['is_admin'],
'is_tab' => $validData['is_tab'],
]);
return redirect('/admin/users');
@ -82,7 +86,7 @@ class UserController extends Controller
// Generate a random password
$randomPassword = Str::random(12);
$user = \App\Models\User::make([
$user = User::make([
'first_name' => request('first_name'),
'last_name' => request('last_name'),
'email' => request('email'),
@ -104,12 +108,13 @@ class UserController extends Controller
return redirect('/admin/users');
}
public function destroy(Request $request, User $user)
public function destroy(User $user)
{
if (! Auth::user()->is_admin) {
abort(403);
}
$user->delete();
return redirect()->route('admin.users.index')->with('success', 'User deleted successfully');
}
}

View File

@ -30,6 +30,8 @@ class User extends Authenticatable implements MustVerifyEmail
'password',
'profile_image_url',
'school_id',
'is_tab',
'is_admin',
];
/**
@ -122,33 +124,23 @@ class User extends Authenticatable implements MustVerifyEmail
return $this->belongsToMany(BonusScoreDefinition::class, 'bonus_score_judge_assignment');
}
public function advancementVotes(): HasMany
{
return $this->hasMany(JudgeAdvancementVote::class);
}
public function isJudge(): bool
{
return $this->judgingAssignments()->count() > 0 || $this->bonusJudgingAssignments()->count() > 0;
}
/**
* Return an array of schools using the users email domain
*
* @return SchoolEmailDomain[]
*/
public function possibleSchools(): Collection
{
if ($this->school_id) {
$return[] = $this->school;
return $return;
return collect($return);
}
return SchoolEmailDomain::with('school')->where('domain', '=', $this->emailDomain())->get();
}
public function canTab()
public function canTab(): bool
{
if ($this->is_admin) {
return true;

View File

@ -30,6 +30,17 @@
@endforeach
</x-form.select>
<div class="col-span-3">
<x-form.checkbox name="is_admin" :checked="$user->is_admin">
<x-slot:label>Administrator</x-slot:label>
</x-form.checkbox>
</div>
<div class="col-span-3">
<x-form.checkbox name="is_tab" :checked="$user->is_tab">
<x-slot:label>Tabulator</x-slot:label>
</x-form.checkbox>
</div>
</x-form.body-grid>
<x-form.footer class="py-5">
<x-form.button>Update User</x-form.button>

View File

@ -16,16 +16,24 @@
<x-table.th>Email</x-table.th>
<x-table.th>Cell Phone</x-table.th>
<x-table.th>Judging Preference</x-table.th>
<x-table.th>Privileges</x-table.th>
</tr>
</thead>
<x-table.body>
@foreach($users as $user)
<tr>
<tr class="hover:bg-gray-50">
<x-table.td><a href="{{ route('admin.users.edit',$user) }}">{{ $user->full_name(true) }}</a></x-table.td>
<x-table.td>{{ $user->has_school() ? $user->school->name : ' ' }}</x-table.td>
<x-table.td>{{ $user->email }}</x-table.td>
<x-table.td>{{ $user->cell_phone }}</x-table.td>
<x-table.td>{{ $user->judging_preference }}</x-table.td>
<x-table.td>
@if($user->is_admin)
Admin
@elseif($user->is_tab)
Tabulator
@endif
</x-table.td>
</tr>
@endforeach
</x-table.body>

View File

@ -1,7 +1,6 @@
<?php
use App\Models\Entry;
use App\Models\JudgeAdvancementVote;
use App\Models\Room;
use App\Models\School;
use App\Models\SchoolEmailDomain;
@ -91,21 +90,6 @@ it('has rooms also called judgingAssignments', function () {
->and($this->user->judgingAssignments->first())->toBeInstanceOf(Room::class);
});
it('has advancement votes', function () {
// Arrange
$entries = Entry::factory()->count(5)->create();
foreach ($entries as $entry) {
JudgeAdvancementVote::create([
'user_id' => $this->user->id,
'entry_id' => $entry->id,
'vote' => fake()->randomElement(['yes', 'no', 'dq']),
]);
}
// Act & Assert
expect($this->user->advancementVotes->count())->toBe(5)
->and($this->user->advancementVotes->first())->toBeInstanceOf(JudgeAdvancementVote::class);
});
it('knows if it is a judge', function () {
expect($this->user->isJudge())->toBeFalse();
Room::factory()->create()->addJudge($this->user);

View File

@ -58,6 +58,8 @@ it('has all needed fields', function () {
'email',
'cell_phone',
'judging_preference',
'is_admin',
'is_tab',
];
// Act & Assert
$response = get(route('admin.users.edit', $this->users[0]));