From 0a21001a0aa1b739121243845bc25866e1d8c862 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Feb 2020 16:28:29 +0100 Subject: [PATCH] Ajout creation utilisateur --- backend/src/Controller/UserController.php | 36 ++++++++++++++++++++ backend/src/Entity/User.php | 7 ++++ misc/projects/ticketing_app.http | 40 ++++++++++++++++++++++- 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/backend/src/Controller/UserController.php b/backend/src/Controller/UserController.php index 2c0519a..4bee261 100644 --- a/backend/src/Controller/UserController.php +++ b/backend/src/Controller/UserController.php @@ -7,6 +7,8 @@ use App\Repository\UserRepository; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Routing\Annotation\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; class UserController extends Controller { @@ -81,4 +83,38 @@ class UserController extends Controller ] ]); } + + /** + * @Route("/api/v1/users", name="api_v1_create_user", methods={"POST"}) + * @IsGranted("ROLE_DEVELOPER") + */ + public function createUser(Request $request, UserPasswordEncoderInterface $passwordEncoder) + { + + $content = $request->getContent(); + $projectData = json_decode($content, true); + + $newUser = new User(); + + if (!isset($projectData['username'])) { + return new ErrorResponse(0, "You must provide a username"); + } + $newUser->setUsername($projectData['username']); + + if (!isset($projectData['password'])) { + return new ErrorResponse(1, "You must provide a password"); + } + $newUser->setPassword($passwordEncoder->encodePassword( + $newUser, + $projectData['password'] + )); + + $manager = $this->getDoctrine()->getManager(); + $manager->persist($newUser); + $manager->flush(); + + return new DataResponse([ + 'user' => $newUser->toArray(), + ]); + } } \ No newline at end of file diff --git a/backend/src/Entity/User.php b/backend/src/Entity/User.php index 8ffbdc6..bda814e 100644 --- a/backend/src/Entity/User.php +++ b/backend/src/Entity/User.php @@ -219,4 +219,11 @@ class User implements UserInterface return $this; } + + public function toArray(): array { + return [ + 'id' => $this->getId(), + 'username' => $this->getUsername(), + ]; + } } diff --git a/misc/projects/ticketing_app.http b/misc/projects/ticketing_app.http index 31169d5..dec0803 100644 --- a/misc/projects/ticketing_app.http +++ b/misc/projects/ticketing_app.http @@ -41,4 +41,42 @@ Content-Type: application/json // List users GET {{baseURL}}/users -Content-Type: application/json \ No newline at end of file +Content-Type: application/json + +### + +// List projects + +GET {{baseURL}}/projects +Content-Type: application/json + +### + +// Show project + +GET {{baseURL}}/projects/17 +Content-Type: application/json + +### + +// Create project + +POST {{baseURL}}/projects +Content-Type: application/json + +{ + "name": "new-project", + "users": [18] +} + +### + +// Create users + +POST {{baseURL}}/users +Content-Type: application/json + +{ + "username": "test12", + "password": "test12" +} \ No newline at end of file