From 9f785db7ba674638a6f456aa3626f3f8100911f1 Mon Sep 17 00:00:00 2001 From: Yassine Doghri Date: Thu, 3 Nov 2022 17:41:50 +0000 Subject: [PATCH] fix(contributors): add prefix to podcast group to delete contributor --- modules/Auth/Controllers/ContributorController.php | 2 +- modules/Auth/Helpers/auth_helper.php | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/Auth/Controllers/ContributorController.php b/modules/Auth/Controllers/ContributorController.php index d40f0581..2a022870 100644 --- a/modules/Auth/Controllers/ContributorController.php +++ b/modules/Auth/Controllers/ContributorController.php @@ -228,7 +228,7 @@ class ContributorController extends BaseController ->delete("podcast#{$this->podcast->id}_contributors"); // remove contributor from podcast group - $this->contributor->removeGroup(get_podcast_group($this->contributor, $this->podcast->id)); + $this->contributor->removeGroup(get_podcast_group($this->contributor, $this->podcast->id, false)); return redirect() ->route('contributor-list', [$this->podcast->id]) diff --git a/modules/Auth/Helpers/auth_helper.php b/modules/Auth/Helpers/auth_helper.php index 2d73d9d9..d424ef77 100644 --- a/modules/Auth/Helpers/auth_helper.php +++ b/modules/Auth/Helpers/auth_helper.php @@ -136,7 +136,7 @@ if (! function_exists('set_instance_group')) { } if (! function_exists('get_podcast_group')) { - function get_podcast_group(User $user, int $podcastId): ?string + function get_podcast_group(User $user, int $podcastId, bool $removePrefix = true): ?string { $podcastGroups = array_filter($user->getGroups() ?? [], static function ($group) use ($podcastId): bool { return str_starts_with($group, "podcast#{$podcastId}"); @@ -154,8 +154,12 @@ if (! function_exists('get_podcast_group')) { $user->removeGroup(...$podcastGroups); } - // strip the `podcast#{id}.` prefix when returning group - return substr((string) $podcastGroup, strlen('podcast#' . $podcastId . '-')); + if ($removePrefix) { + // strip the `podcast#{id}.` prefix when returning group + return substr((string) $podcastGroup, strlen('podcast#' . $podcastId . '-')); + } + + return $podcastGroup; } } @@ -184,7 +188,7 @@ if (! function_exists('get_podcast_groups')) { // extract all podcast ids from groups foreach ($podcastGroups as $podcastGroup) { // extract podcast id from group and add it to the list of ids - preg_match('~podcast#([0-9]+)-[a-z]+~', $podcastGroup, $matches); + preg_match('~podcast#(\d+)-[a-z]+~', (string) $podcastGroup, $matches); $userPodcastIds[] = $matches[1]; }