From 9a2af856d1ef63c926ff6f0fe6e346ad57e8a495 Mon Sep 17 00:00:00 2001 From: reza Date: Wed, 25 Nov 2020 19:46:25 +0330 Subject: [PATCH 01/10] adds roles --- config/calendar.php | 10 ++++++++++ src/AppServiceProvider.php | 2 ++ src/Services/Calendars.php | 9 +++++++++ 3 files changed, 21 insertions(+) create mode 100644 config/calendar.php diff --git a/config/calendar.php b/config/calendar.php new file mode 100644 index 0000000..f112a81 --- /dev/null +++ b/config/calendar.php @@ -0,0 +1,10 @@ + [ + BirthdayCalendar::class => Roles::keys() + ] +]; diff --git a/src/AppServiceProvider.php b/src/AppServiceProvider.php index 44043c1..3cf0337 100644 --- a/src/AppServiceProvider.php +++ b/src/AppServiceProvider.php @@ -25,6 +25,8 @@ private function load() { $this->loadRoutesFrom(__DIR__.'/../routes/api.php'); + $this->mergeConfigFrom(__DIR__.'/../config/calendar.php', 'enso.calendar'); + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); $this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-enso/calendar'); diff --git a/src/Services/Calendars.php b/src/Services/Calendars.php index 37b70cd..ab054ea 100644 --- a/src/Services/Calendars.php +++ b/src/Services/Calendars.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Config; use LaravelEnso\Calendar\Calendars\BirthdayCalendar; use LaravelEnso\Calendar\Contracts\Calendar as Contract; use LaravelEnso\Calendar\Models\Calendar; @@ -49,6 +50,7 @@ public function register($calendars) (new Collection($calendars)) ->map(fn ($calendar) => is_string($calendar) ? new $calendar() : $calendar) ->reject(fn ($calendar) => $this->registered($calendar)) + ->filter(fn ($calendar) => $this->canAccess($calendar)) ->each(fn (Contract $calendar) => $this->calendars->push($calendar)); } @@ -62,4 +64,11 @@ private function registered($calendar) return $this->calendars ->contains(fn ($existing) => ($existing->getKey() === $calendar->getKey())); } + + private function canAccess($calendar) + { + $roles = Config::get('enso.calendar.roles')[get_class($calendar)] ?? null; + + return $roles === null || Collection::wrap($roles)->contains(Auth::user()->role_id); + } } From 6e196e268817c2dfdeb7dfb2d8d629d3b67c0997 Mon Sep 17 00:00:00 2001 From: Adrian Ocneanu Date: Wed, 25 Nov 2020 16:16:42 +0000 Subject: [PATCH 02/10] Apply fixes from StyleCI --- config/calendar.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/calendar.php b/config/calendar.php index f112a81..b7538a3 100644 --- a/config/calendar.php +++ b/config/calendar.php @@ -5,6 +5,6 @@ return [ 'roles' => [ - BirthdayCalendar::class => Roles::keys() - ] + BirthdayCalendar::class => Roles::keys(), + ], ]; From e01e5da712020295830356f8ebb93a90065f2990 Mon Sep 17 00:00:00 2001 From: reza Date: Thu, 26 Nov 2020 10:42:10 +0330 Subject: [PATCH 03/10] adds CalendarTest --- tests/features/CalendarTest.php | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/features/CalendarTest.php diff --git a/tests/features/CalendarTest.php b/tests/features/CalendarTest.php new file mode 100644 index 0000000..c4cde05 --- /dev/null +++ b/tests/features/CalendarTest.php @@ -0,0 +1,57 @@ +seed(); + + $this->actingAs(User::first()); + + $this->calendar = Calendar::factory()->create(); + } + + /** @test */ + public function create_calendar() + { + $result = $this->post(route('core.calendar.store'), [ + 'name' => 'test', + 'color' => Colors::Red, + 'private' => false, + ]); + + $this->assertTrue(Calendar::whereName('test')->exists()); + } + + /** @test */ + public function cannot_get_calendar_without_role_access() + { + Config::set('enso.calendar.roles.'.BirthdayCalendar::class, []); + + Calendars::register(BirthdayCalendar::class); + + $result = $this->get(route('core.calendar.index')); + + $this->assertEmpty(Calendars::all() + ->filter(fn ($calendar) => BirthdayCalendar::class === get_class($calendar))); + } +} From ba2962bf5e679b8ece27e3023a8c25c59fa7f33c Mon Sep 17 00:00:00 2001 From: reza Date: Thu, 26 Nov 2020 10:44:34 +0330 Subject: [PATCH 04/10] adds publish --- src/AppServiceProvider.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/AppServiceProvider.php b/src/AppServiceProvider.php index 3cf0337..849cca5 100644 --- a/src/AppServiceProvider.php +++ b/src/AppServiceProvider.php @@ -18,6 +18,7 @@ public function boot() ->publishProvider() ->publishFactories() ->publishMail() + ->publishConfig() ->commands(SendReminders::class); } @@ -70,4 +71,13 @@ private function publishMail() return $this; } + + private function publishConfig() + { + $this->publishes([ + __DIR__.'/../config' => config_path('enso'), + ], 'calendar-config'); + + return $this; + } } From 08caa73133e6d41bf3aabbf2e13dc1101e6ba319 Mon Sep 17 00:00:00 2001 From: reza Date: Thu, 26 Nov 2020 16:55:11 +0330 Subject: [PATCH 05/10] limit birthday roles --- config/calendar.php | 11 +++++------ src/Calendars/BirthdayCalendar.php | 4 ++++ src/Services/Calendars.php | 9 --------- tests/features/CalendarTest.php | 16 +++++----------- tests/features/CreateTest.php | 2 -- tests/features/EventTest.php | 2 -- tests/features/SequenceTest.php | 2 -- 7 files changed, 14 insertions(+), 32 deletions(-) diff --git a/config/calendar.php b/config/calendar.php index b7538a3..c89517c 100644 --- a/config/calendar.php +++ b/config/calendar.php @@ -1,10 +1,9 @@ [ - BirthdayCalendar::class => Roles::keys(), - ], + 'options' => [ + 'birthday' => [ + 'roles' => ['*'], + ] + ] ]; diff --git a/src/Calendars/BirthdayCalendar.php b/src/Calendars/BirthdayCalendar.php index e8a378b..8f437f8 100644 --- a/src/Calendars/BirthdayCalendar.php +++ b/src/Calendars/BirthdayCalendar.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Config; use LaravelEnso\Calendar\Contracts\CustomCalendar; use LaravelEnso\Calendar\Enums\Colors; use LaravelEnso\People\Models\Person; @@ -42,8 +43,11 @@ public function events(Carbon $startDate, Carbon $endDate): Collection { $this->startDate = $startDate; $this->endDate = $endDate; + $roles = Config::get('enso.calendar.options.birthday.roles') ?? ['*']; return Person::query() + ->unless($roles === ['*'], fn($query) => $query + ->whereHas('user', fn($query) => $query->whereIn('role_id', $roles))) ->when(! $this->withinSameYear(), $this->differentYearQuery()) ->when( $this->withinSameYear() && $this->withinSameMonth(), diff --git a/src/Services/Calendars.php b/src/Services/Calendars.php index ab054ea..37b70cd 100644 --- a/src/Services/Calendars.php +++ b/src/Services/Calendars.php @@ -4,7 +4,6 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Config; use LaravelEnso\Calendar\Calendars\BirthdayCalendar; use LaravelEnso\Calendar\Contracts\Calendar as Contract; use LaravelEnso\Calendar\Models\Calendar; @@ -50,7 +49,6 @@ public function register($calendars) (new Collection($calendars)) ->map(fn ($calendar) => is_string($calendar) ? new $calendar() : $calendar) ->reject(fn ($calendar) => $this->registered($calendar)) - ->filter(fn ($calendar) => $this->canAccess($calendar)) ->each(fn (Contract $calendar) => $this->calendars->push($calendar)); } @@ -64,11 +62,4 @@ private function registered($calendar) return $this->calendars ->contains(fn ($existing) => ($existing->getKey() === $calendar->getKey())); } - - private function canAccess($calendar) - { - $roles = Config::get('enso.calendar.roles')[get_class($calendar)] ?? null; - - return $roles === null || Collection::wrap($roles)->contains(Auth::user()->role_id); - } } diff --git a/tests/features/CalendarTest.php b/tests/features/CalendarTest.php index c4cde05..22791f1 100644 --- a/tests/features/CalendarTest.php +++ b/tests/features/CalendarTest.php @@ -1,7 +1,5 @@ get(route('core.calendar.index')); + Config::set('enso.calendar.options.birthday.roles', []); - $this->assertEmpty(Calendars::all() - ->filter(fn ($calendar) => BirthdayCalendar::class === get_class($calendar))); + $this->assertEmpty((new BirthdayCalendar()) + ->events(Person::first()->birthday, Person::first()->birthday->addDay())); } } diff --git a/tests/features/CreateTest.php b/tests/features/CreateTest.php index 5526744..8b76572 100644 --- a/tests/features/CreateTest.php +++ b/tests/features/CreateTest.php @@ -1,7 +1,5 @@ Date: Thu, 26 Nov 2020 13:25:28 +0000 Subject: [PATCH 06/10] Apply fixes from StyleCI --- config/calendar.php | 4 ++-- src/Calendars/BirthdayCalendar.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/calendar.php b/config/calendar.php index c89517c..9077213 100644 --- a/config/calendar.php +++ b/config/calendar.php @@ -4,6 +4,6 @@ 'options' => [ 'birthday' => [ 'roles' => ['*'], - ] - ] + ], + ], ]; diff --git a/src/Calendars/BirthdayCalendar.php b/src/Calendars/BirthdayCalendar.php index 8f437f8..146336f 100644 --- a/src/Calendars/BirthdayCalendar.php +++ b/src/Calendars/BirthdayCalendar.php @@ -46,8 +46,8 @@ public function events(Carbon $startDate, Carbon $endDate): Collection $roles = Config::get('enso.calendar.options.birthday.roles') ?? ['*']; return Person::query() - ->unless($roles === ['*'], fn($query) => $query - ->whereHas('user', fn($query) => $query->whereIn('role_id', $roles))) + ->unless($roles === ['*'], fn ($query) => $query + ->whereHas('user', fn ($query) => $query->whereIn('role_id', $roles))) ->when(! $this->withinSameYear(), $this->differentYearQuery()) ->when( $this->withinSameYear() && $this->withinSameMonth(), From 275b05545ca22d96c9a49798122522aa8231b9f8 Mon Sep 17 00:00:00 2001 From: Adrian Ocneanu Date: Thu, 26 Nov 2020 16:35:22 +0200 Subject: [PATCH 07/10] Update calendar.php --- config/calendar.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/config/calendar.php b/config/calendar.php index 9077213..8767050 100644 --- a/config/calendar.php +++ b/config/calendar.php @@ -1,9 +1,7 @@ [ - 'birthday' => [ - 'roles' => ['*'], - ], + 'birthdays' => [ + 'roles' => ['*'], ], ]; From fda4141e09cd9ef565879cad1f301be4a01aa08f Mon Sep 17 00:00:00 2001 From: Adrian Ocneanu Date: Thu, 26 Nov 2020 16:36:05 +0200 Subject: [PATCH 08/10] Update AppServiceProvider.php --- src/AppServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AppServiceProvider.php b/src/AppServiceProvider.php index 849cca5..517c44e 100644 --- a/src/AppServiceProvider.php +++ b/src/AppServiceProvider.php @@ -76,7 +76,7 @@ private function publishConfig() { $this->publishes([ __DIR__.'/../config' => config_path('enso'), - ], 'calendar-config'); + ], ['enso-config', 'calendar-config']); return $this; } From 9c915c68e1d09192fcb95fe1e22528311a0ae203 Mon Sep 17 00:00:00 2001 From: Adrian Ocneanu Date: Thu, 26 Nov 2020 16:36:40 +0200 Subject: [PATCH 09/10] Update BirthdayCalendar.php --- src/Calendars/BirthdayCalendar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Calendars/BirthdayCalendar.php b/src/Calendars/BirthdayCalendar.php index 146336f..f54d22f 100644 --- a/src/Calendars/BirthdayCalendar.php +++ b/src/Calendars/BirthdayCalendar.php @@ -43,7 +43,7 @@ public function events(Carbon $startDate, Carbon $endDate): Collection { $this->startDate = $startDate; $this->endDate = $endDate; - $roles = Config::get('enso.calendar.options.birthday.roles') ?? ['*']; + $roles = Config::get('enso.calendar.birthdays.roles'); return Person::query() ->unless($roles === ['*'], fn ($query) => $query From 375c9925e59f40fec6e5dd78b668b51d7443da28 Mon Sep 17 00:00:00 2001 From: Adrian Ocneanu Date: Thu, 26 Nov 2020 16:37:16 +0200 Subject: [PATCH 10/10] Update CalendarTest.php --- tests/features/CalendarTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/features/CalendarTest.php b/tests/features/CalendarTest.php index 22791f1..50bc7ac 100644 --- a/tests/features/CalendarTest.php +++ b/tests/features/CalendarTest.php @@ -43,7 +43,7 @@ public function create_calendar() /** @test */ public function can_limit_birthday_calendar_roles() { - Config::set('enso.calendar.options.birthday.roles', []); + Config::set('enso.calendar.birthdays.roles', []); $this->assertEmpty((new BirthdayCalendar()) ->events(Person::first()->birthday, Person::first()->birthday->addDay()));