diff --git a/src/Controller/APIUserController.php b/src/Controller/APIUserController.php index 8889f5d4..250880c6 100644 --- a/src/Controller/APIUserController.php +++ b/src/Controller/APIUserController.php @@ -141,7 +141,7 @@ class APIUserController extends BaseController $this->denyAccessUnlessGranted('apiuser.add', null, 'No access.'); // create new row - // API and secret keys are generated with the call to new APIUser() + // API and secret keys are generated with the call to new APIUser() $em = $this->getDoctrine()->getManager(); $obj = new APIUser(); @@ -198,6 +198,112 @@ class APIUserController extends BaseController } } + public function updateForm($id) + { + $this->denyAccessUnlessGranted('apiuser.update', null, 'No access.'); + + $params = $this->initParameters('api_ser_list'); + $params['mode'] = 'update'; + + // get row data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(APIUser::class)->find($id); + + // make sure this row exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + // get roles + $params['roles'] = $em->getRepository(APIRole::class)->findAll(); + + $params['obj'] = $obj; + + // response + return $this->render('api-user/form.html.twig', $params); + } + + public function updateSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('apiuser.update', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(APIUser::class)->find($id); + + // make sure this row exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + // set and save values + $obj->setName($req->request->get('name')) + ->setEnabled($req->request->get('enabled') ? true : false) + ->clearRoles(); + + // set roles + $roles = $req->request->get('roles'); + + if (!empty($roles)) { + foreach ($roles as $role_id) { + // check if role exists + $role = $em->getRepository(APIRole::class)->find($role_id); + + if (!empty($role)) + $obj->addRole($role); + } + } + + // validate + $errors = $validator->validate($obj); + + // initialize error list + $error_array = []; + + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array)) { + // return validation failure response + return $this->json([ + 'success' => false, + 'errors' => $error_array + ], 422); + } else { + // validated! save the entity + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + } + + public function destroy($id) + { + $this->denyAccessUnlessGranted('apiuser.delete', null, 'No access.'); + + $params = $this->initParameters('api_user_list'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(APIUser::class)->find($id); + + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + // delete this row + $em->remove($obj); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); + } + // check if datatable filter is present and append to query protected function setQueryFilters($datatable, &$query) { if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { diff --git a/templates/api-user/list.html.twig b/templates/api-user/list.html.twig index 7a8bb6a8..a31988a1 100644 --- a/templates/api-user/list.html.twig +++ b/templates/api-user/list.html.twig @@ -117,7 +117,7 @@ } if (row.meta.delete_url != '') { - actions += ''; + actions += ''; } return actions;