Ajout creation utilisateur

This commit is contained in:
wpetit 2020-02-19 16:28:29 +01:00
parent dacf0a8aec
commit 0a21001a0a
3 changed files with 82 additions and 1 deletions

View File

@ -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(),
]);
}
}

View File

@ -219,4 +219,11 @@ class User implements UserInterface
return $this;
}
public function toArray(): array {
return [
'id' => $this->getId(),
'username' => $this->getUsername(),
];
}
}

View File

@ -42,3 +42,41 @@ Content-Type: application/json
GET {{baseURL}}/users
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"
}