From befb64c868ca08abddd94b71a2041b4a7725a2d1 Mon Sep 17 00:00:00 2001 From: Sebastian Blank Date: Fri, 30 Aug 2024 16:31:52 +0200 Subject: [PATCH] Fix bug when you have an empty roles. (#200) * Fix bug when you have an empty roles. * Use @method getGroup() --- src/Adapter/User/UserTrait.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Adapter/User/UserTrait.php b/src/Adapter/User/UserTrait.php index 6c3a870..b18680c 100644 --- a/src/Adapter/User/UserTrait.php +++ b/src/Adapter/User/UserTrait.php @@ -6,6 +6,9 @@ use Pimcore\Model\DataObject\ClassDefinition\Data\Password; use Symfony\Component\Security\Core\User\UserInterface; +/** + * @method GroupInterface[]|null getGroups() + */ trait UserTrait { private array $roles = []; @@ -51,9 +54,8 @@ public function isEqualTo(UserInterface $user): bool public function getRoles(): array { $roles = $this->roles; - - /** @var GroupInterface $group */ - foreach ($this->getGroups() as $group) { + $groups = $this->getGroups() ?? []; + foreach ($groups as $group) { $groupRoles = $group->getRoles(); $roles[] = is_array($groupRoles) ? $groupRoles : []; } @@ -67,8 +69,8 @@ public function getRoles(): array public function getGroupNames(): array { $names = []; - /** @var GroupInterface $group */ - foreach ($this->getGroups() as $group) { + $groups = $this->getGroups() ?? []; + foreach ($groups as $group) { $names[] = $group->getName(); } @@ -82,7 +84,6 @@ public function hasGroupId(GroupInterface $userGroup): bool { $groups = $this->getGroups() ?? []; $groupIds = array_map(static function ($group) { - /* @var GroupInterface $group */ return $group->getId(); }, $groups); @@ -115,7 +116,6 @@ public function removeGroup(GroupInterface $userGroup): void { $groups = $this->getGroups() ?? []; $groupIds = array_map(static function ($group) { - /* @var GroupInterface $group */ return $group->getId(); }, $groups);