From 643ac80374a327ae2827d22ee3e3a4e1593d9353 Mon Sep 17 00:00:00 2001
From: Moreau Elise <moreau.elise13@gmail.com>
Date: Sat, 31 Oct 2020 13:12:23 +0100
Subject: [PATCH] resolve group id bug

---
 .../Actions/Group/AddUsersGroupAction.php          | 14 ++++++++++----
 .../Actions/Group/CreateGroupAction.php            |  2 +-
 src/Application/Actions/User/AccountAction.php     |  2 +-
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/src/Application/Actions/Group/AddUsersGroupAction.php b/src/Application/Actions/Group/AddUsersGroupAction.php
index e970da8..2d1905e 100755
--- a/src/Application/Actions/Group/AddUsersGroupAction.php
+++ b/src/Application/Actions/Group/AddUsersGroupAction.php
@@ -14,23 +14,29 @@ class AddUsersGroupAction extends GroupAction
      */
     protected function action(): Response
     {
+        $parsedRequestBody = (array)$this->request->getParsedBody();
         $groupId = (int) $this->resolveArg('id');
-        $group = $this->groupRepository->find($groupId);
+        if (!isset($parsedRequestBody['ids']))
+        {
+            return $this->response
+            ->withHeader('Location', '/groups/' . $groupId)
+            ->withStatus(302);
+        }
 
-        $parsedRequestBody = (array)$this->request->getParsedBody();
+        $group = $this->groupRepository->find($groupId);
         $user_ids = $parsedRequestBody['ids'];
 
         if (!isset($group)) {
             throw new GroupNotFoundException();
         }
-        
+
         $users_query = $this->userRepository->createQueryBuilder('u');
         $users_list = $users_query
             ->where('u.id IN (:user_ids)')
             ->setParameter('user_ids', $user_ids)
             ->getQuery()
             ->getResult();
-                    
+
         foreach ($users_list as $user) {
             if (!$group->hasUser($user->getId())) {
                 $user->addGroup($group);
diff --git a/src/Application/Actions/Group/CreateGroupAction.php b/src/Application/Actions/Group/CreateGroupAction.php
index bd86d84..ea94e86 100755
--- a/src/Application/Actions/Group/CreateGroupAction.php
+++ b/src/Application/Actions/Group/CreateGroupAction.php
@@ -50,7 +50,7 @@ class CreateGroupAction extends GroupAction
         $this->logger->info("Group has been created.");
 
         return $this->response
-            ->withHeader('Location', '/groups' . $groupId)
+            ->withHeader('Location', '/groups' . $group->getId())
             ->withStatus(302);
     }
 
diff --git a/src/Application/Actions/User/AccountAction.php b/src/Application/Actions/User/AccountAction.php
index b6084b2..bb1f559 100755
--- a/src/Application/Actions/User/AccountAction.php
+++ b/src/Application/Actions/User/AccountAction.php
@@ -19,7 +19,7 @@ class AccountAction extends UserAction
                 ->withHeader('Location', '/login')
                 ->withStatus(302);
         }
-        
+
         $user = $this->userRepository->find($_SESSION['userId']);
 
         if (!isset($user)) {
-- 
GitLab