workspace icon indicating copy to clipboard operation
workspace copied to clipboard

Ensure groups is an array before try to access them

Open solracsf opened this issue 2 years ago • 3 comments

Should fix:

Error: Undefined array key "groups" at /var/www/nextcloud/apps/workspace/lib/Service/UserService.php#65

solracsf avatar Jun 06 '23 09:06 solracsf

Hi @solracsf :wave:

Thank you for your contribution :pray:

What is the case that causes this problem ?

Your proposal is not bad :

if (isset($space['groups']) && is_array($space['groups'])) {
  // for
  // if
}

But, I am not a big fan of nesting too deeply.

I try to follow the best practices : https://github.com/piotrplenik/clean-code-php?tab=readme-ov-file#avoid-nesting-too-deeply-and-return-early-part-1

What do you think of this rewrite ?

if (!isset($space['groups'])) {
  throw new \Exception('The "groups" key is not present');
}

if (!is_array($space['groups'])) {
  throw new \Exception('The "groups" key is not an array');
}

foreach ($this->groupManager->getUserGroups($user) as $group) {
	if (in_array($group->getGID(), array_keys($space['groups']))) {
		array_push($groups, $group->getGID());
	}
}

Normally, I've solved the problem

zak39 avatar Mar 04 '24 14:03 zak39

Fine by me 🆗 Feel free to commit :-)

solracsf avatar Mar 04 '24 15:03 solracsf

Nice ! Your PR @solracsf is updated and I applied my suggestions

We will test your PR as quickly as possible :wink:

zak39 avatar Mar 21 '24 15:03 zak39

backport is progressing to stable3.2

zak39 avatar Jul 19 '24 14:07 zak39