From 8237e1abfd285cf79f0e3821570445287133732d Mon Sep 17 00:00:00 2001 From: Benjamin Takats Date: Thu, 1 Dec 2022 17:39:17 +0100 Subject: [PATCH] policies updated --- app/Models/Participant.php | 7 ++-- app/Nova/Course.php | 3 +- app/Nova/Event.php | 52 +++++++++++++++++++---------- app/Nova/Participant.php | 28 ++++++++-------- app/Policies/EventPolicy.php | 6 ++-- app/Policies/ParticipantPolicy.php | 14 +++++++- app/Policies/RegistrationPolicy.php | 11 +++++- 7 files changed, 83 insertions(+), 38 deletions(-) diff --git a/app/Models/Participant.php b/app/Models/Participant.php index 05a79af9..01c68490 100644 --- a/app/Models/Participant.php +++ b/app/Models/Participant.php @@ -11,17 +11,20 @@ class Participant extends Model /** * The attributes that aren't mass assignable. - * * @var array */ protected $guarded = []; /** * The attributes that should be cast to native types. - * * @var array */ protected $casts = [ 'id' => 'integer', ]; + + public function registrations() + { + return $this->hasMany(\App\Models\Registration::class); + } } diff --git a/app/Nova/Course.php b/app/Nova/Course.php index 27ed267c..87afba26 100644 --- a/app/Nova/Course.php +++ b/app/Nova/Course.php @@ -23,7 +23,7 @@ class Course extends Resource * The single value that should be used to represent the resource when being displayed. * @var string */ - public static $title = 'id'; + public static $title = 'name'; /** * The columns that should be searched. @@ -31,6 +31,7 @@ class Course extends Resource */ public static $search = [ 'id', + 'name', ]; public static function relatableLecturers(NovaRequest $request, $query, Field $field) diff --git a/app/Nova/Event.php b/app/Nova/Event.php index 46cef740..0636213f 100644 --- a/app/Nova/Event.php +++ b/app/Nova/Event.php @@ -2,56 +2,70 @@ namespace App\Nova; -use Laravel\Nova\Fields\ID; +use Carbon\CarbonInterval; use Illuminate\Http\Request; -use Laravel\Nova\Fields\DateTime; use Laravel\Nova\Fields\BelongsTo; +use Laravel\Nova\Fields\DateTime; +use Laravel\Nova\Fields\Field; +use Laravel\Nova\Fields\ID; +use Laravel\Nova\Http\Requests\NovaRequest; class Event extends Resource { /** * The model the resource corresponds to. - * * @var string */ public static $model = \App\Models\Event::class; - - /** - * The single value that should be used to represent the resource when being displayed. - * - * @var string - */ - public static $title = 'id'; - /** * The columns that should be searched. - * * @var array */ public static $search = [ 'id', + 'course.name', ]; + public static function relatableCourses(NovaRequest $request, $query, Field $field) + { + if ($field instanceof BelongsTo) { + $query->whereHas('lecturer', function ($query) use ($request) { + $query->where('team_id', $request->user()->id); + }); + } + + return $query; + } + + public function title() + { + return $this->from.' - '.$this->venue->name.' - '.$this->course->name; + } + /** * Get the fields displayed by the resource. * * @param \Illuminate\Http\Request $request + * * @return array */ public function fields(Request $request) { return [ - ID::make()->sortable(), + ID::make() + ->sortable(), DateTime::make('From') - ->rules('required'), + ->rules('required') + ->step(CarbonInterval::minutes(30)), DateTime::make('To') - ->rules('required'), + ->rules('required') + ->step(CarbonInterval::minutes(30)), BelongsTo::make('Course'), - BelongsTo::make('Venue'), - + BelongsTo::make('Venue') + ->searchable(), ]; } @@ -60,6 +74,7 @@ class Event extends Resource * Get the cards available for the request. * * @param \Illuminate\Http\Request $request + * * @return array */ public function cards(Request $request) @@ -71,6 +86,7 @@ class Event extends Resource * Get the filters available for the resource. * * @param \Illuminate\Http\Request $request + * * @return array */ public function filters(Request $request) @@ -82,6 +98,7 @@ class Event extends Resource * Get the lenses available for the resource. * * @param \Illuminate\Http\Request $request + * * @return array */ public function lenses(Request $request) @@ -93,6 +110,7 @@ class Event extends Resource * Get the actions available for the resource. * * @param \Illuminate\Http\Request $request + * * @return array */ public function actions(Request $request) diff --git a/app/Nova/Participant.php b/app/Nova/Participant.php index 1d588412..a512e136 100644 --- a/app/Nova/Participant.php +++ b/app/Nova/Participant.php @@ -2,45 +2,44 @@ namespace App\Nova; -use Laravel\Nova\Fields\ID; use Illuminate\Http\Request; +use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Text; class Participant extends Resource { /** * The model the resource corresponds to. - * * @var string */ public static $model = \App\Models\Participant::class; - - /** - * The single value that should be used to represent the resource when being displayed. - * - * @var string - */ - public static $title = 'id'; - /** * The columns that should be searched. - * * @var array */ public static $search = [ 'id', + 'first_name', + 'last_name', ]; + public function title() + { + return $this->first_name.' '.$this->last_name; + } + /** * Get the fields displayed by the resource. * * @param \Illuminate\Http\Request $request + * * @return array */ public function fields(Request $request) { return [ - ID::make()->sortable(), + ID::make() + ->sortable(), Text::make('First name') ->rules('required', 'string'), @@ -48,7 +47,6 @@ class Participant extends Resource Text::make('Last name') ->rules('required', 'string'), - ]; } @@ -56,6 +54,7 @@ class Participant extends Resource * Get the cards available for the request. * * @param \Illuminate\Http\Request $request + * * @return array */ public function cards(Request $request) @@ -67,6 +66,7 @@ class Participant extends Resource * Get the filters available for the resource. * * @param \Illuminate\Http\Request $request + * * @return array */ public function filters(Request $request) @@ -78,6 +78,7 @@ class Participant extends Resource * Get the lenses available for the resource. * * @param \Illuminate\Http\Request $request + * * @return array */ public function lenses(Request $request) @@ -89,6 +90,7 @@ class Participant extends Resource * Get the actions available for the resource. * * @param \Illuminate\Http\Request $request + * * @return array */ public function actions(Request $request) diff --git a/app/Policies/EventPolicy.php b/app/Policies/EventPolicy.php index ceba797c..2b9f25a3 100644 --- a/app/Policies/EventPolicy.php +++ b/app/Policies/EventPolicy.php @@ -30,7 +30,7 @@ class EventPolicy */ public function view(User $user, Event $event) { - // + return $user->is_lecturer; } /** @@ -41,7 +41,7 @@ class EventPolicy */ public function create(User $user) { - // + return $user->is_lecturer; } /** @@ -53,7 +53,7 @@ class EventPolicy */ public function update(User $user, Event $event) { - // + return $user->belongsToTeam($event->course->lecturer->team); } /** diff --git a/app/Policies/ParticipantPolicy.php b/app/Policies/ParticipantPolicy.php index b920bdc3..dd9a3155 100644 --- a/app/Policies/ParticipantPolicy.php +++ b/app/Policies/ParticipantPolicy.php @@ -14,6 +14,7 @@ class ParticipantPolicy * Determine whether the user can view any models. * * @param \App\Models\User $user + * * @return \Illuminate\Auth\Access\Response|bool */ public function viewAny(User $user) @@ -26,17 +27,24 @@ class ParticipantPolicy * * @param \App\Models\User $user * @param \App\Models\Participant $participant + * * @return \Illuminate\Auth\Access\Response|bool */ public function view(User $user, Participant $participant) { - // + if ($participant->registrations) { + return $participant->whereHas('registrations.event.course.lecturer', + fn($q) => $q->where('team_id', $user->current_team_id)) + ->exists(); + } + return false; } /** * Determine whether the user can create models. * * @param \App\Models\User $user + * * @return \Illuminate\Auth\Access\Response|bool */ public function create(User $user) @@ -49,6 +57,7 @@ class ParticipantPolicy * * @param \App\Models\User $user * @param \App\Models\Participant $participant + * * @return \Illuminate\Auth\Access\Response|bool */ public function update(User $user, Participant $participant) @@ -61,6 +70,7 @@ class ParticipantPolicy * * @param \App\Models\User $user * @param \App\Models\Participant $participant + * * @return \Illuminate\Auth\Access\Response|bool */ public function delete(User $user, Participant $participant) @@ -73,6 +83,7 @@ class ParticipantPolicy * * @param \App\Models\User $user * @param \App\Models\Participant $participant + * * @return \Illuminate\Auth\Access\Response|bool */ public function restore(User $user, Participant $participant) @@ -85,6 +96,7 @@ class ParticipantPolicy * * @param \App\Models\User $user * @param \App\Models\Participant $participant + * * @return \Illuminate\Auth\Access\Response|bool */ public function forceDelete(User $user, Participant $participant) diff --git a/app/Policies/RegistrationPolicy.php b/app/Policies/RegistrationPolicy.php index b859e94d..8cf66ad7 100644 --- a/app/Policies/RegistrationPolicy.php +++ b/app/Policies/RegistrationPolicy.php @@ -14,6 +14,7 @@ class RegistrationPolicy * Determine whether the user can view any models. * * @param \App\Models\User $user + * * @return \Illuminate\Auth\Access\Response|bool */ public function viewAny(User $user) @@ -26,17 +27,21 @@ class RegistrationPolicy * * @param \App\Models\User $user * @param \App\Models\Registration $registration + * * @return \Illuminate\Auth\Access\Response|bool */ public function view(User $user, Registration $registration) { - // + return $registration->whereHas('event.course.lecturer', + fn($q) => $q->where('team_id', $user->current_team_id)) + ->exists(); } /** * Determine whether the user can create models. * * @param \App\Models\User $user + * * @return \Illuminate\Auth\Access\Response|bool */ public function create(User $user) @@ -49,6 +54,7 @@ class RegistrationPolicy * * @param \App\Models\User $user * @param \App\Models\Registration $registration + * * @return \Illuminate\Auth\Access\Response|bool */ public function update(User $user, Registration $registration) @@ -61,6 +67,7 @@ class RegistrationPolicy * * @param \App\Models\User $user * @param \App\Models\Registration $registration + * * @return \Illuminate\Auth\Access\Response|bool */ public function delete(User $user, Registration $registration) @@ -73,6 +80,7 @@ class RegistrationPolicy * * @param \App\Models\User $user * @param \App\Models\Registration $registration + * * @return \Illuminate\Auth\Access\Response|bool */ public function restore(User $user, Registration $registration) @@ -85,6 +93,7 @@ class RegistrationPolicy * * @param \App\Models\User $user * @param \App\Models\Registration $registration + * * @return \Illuminate\Auth\Access\Response|bool */ public function forceDelete(User $user, Registration $registration)