Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
reminec
Limaju Server Symfony
Commits
5091e7d9
Commit
5091e7d9
authored
Sep 30, 2019
by
Dominique Merle
💬
Browse files
Add registration to the documentation, and fix its signature.
parent
4e79e530
Changes
3
Hide whitespace changes
Inline
Side-by-side
features/bootstrap/ApiRestFeatureContext.php
View file @
5091e7d9
...
...
@@ -16,11 +16,11 @@ class ApiRestFeatureContext extends BaseFeatureContext
public
function
actorRegistersWithEmailAndPassword
(
$actor
,
$try
,
$email
,
$password
)
{
$this
->
actor
(
$actor
,
true
)
->
api
(
'POST'
,
"/users"
,
null
,
'POST'
,
"/users"
,
[
'email'
=>
$email
,
'password'
=>
[
'plain'
=>
$password
],
// weird, eh?
],
!
empty
(
$try
)
'password'
=>
$password
,
],
[],
!
empty
(
$try
)
);
}
...
...
src/Controller/RegistrationController.php
View file @
5091e7d9
...
...
@@ -15,6 +15,10 @@ use Symfony\Component\Messenger\MessageBusInterface;
use
Symfony\Component\Routing\Annotation\Route
;
/**
* Register a new User.
* This documentation never appears anywhere, does it?
* See App\Entity\UserDocumentation
*
* @Route("/api/users", name="api_register", methods={"POST"})
*/
final
class
RegistrationController
...
...
@@ -26,22 +30,21 @@ final class RegistrationController
EntityManagerInterface
$em
):
Response
{
$data
=
[];
// if (\is_string($token = $request->query->get('token')) && null !== $invitation = $em->find(UserInvitation::class, $token)) {
// $data['email'] = $invitation->getEmail();
// }
$this
->
adaptRequest
(
$request
);
$form
=
$formFactory
->
createNamed
(
''
,
RegistrationType
::
class
,
$data
,
[
'csrf_protection'
=>
false
]);
$form
->
handleRequest
(
$request
);
if
(
$form
->
isSubmitted
()
&&
$form
->
isValid
())
{
$data
=
$form
->
getData
();
// $data['invitation_token'] = $token;
$bus
->
dispatch
(
new
CreateUser
(
$data
));
return
new
JsonResponse
([
'message'
=>
"api.registration.success"
,
'data'
=>
$data
,
// maybe instead output the user, with id?
],
Response
::
HTTP_OK
);
],
JsonResponse
::
HTTP_CREATED
);
// also provide ['Location' => $locationUrl]
}
$error
=
"api.registration.failure.message"
;
...
...
@@ -56,4 +59,32 @@ final class RegistrationController
return
new
JsonResponse
([
'error'
=>
$error
],
Response
::
HTTP_BAD_REQUEST
);
}
/**
* Since ApiPlatform generated doc recommends usage of the Content instead of POST vars,
* let's convert what's in the content to POST vars before feeding it to the form.
* Whe also handle here the password['plain'] array quirk.
*
* This mutates the $request.
*
* @param Request $request
*/
protected
function
adaptRequest
(
Request
$request
)
{
$content
=
$request
->
getContent
();
if
(
!
empty
(
$content
))
{
try
{
$content
=
json_decode
(
$content
,
true
,
3
,
JSON_BIGINT_AS_STRING
&
JSON_OBJECT_AS_ARRAY
);
}
catch
(
\
JsonException
$e
)
{
return
;
}
catch
(
\
Exception
$e
)
{
return
;
}
if
(
isset
(
$content
[
"email"
]))
{
$request
->
request
->
set
(
'email'
,
$content
[
"email"
]);
}
if
(
isset
(
$content
[
"password"
]))
{
$request
->
request
->
set
(
'password'
,
[
'plain'
=>
$content
[
"password"
]]);
}
}
}
}
src/Entity/UserDocumentation.php
0 → 100644
View file @
5091e7d9
<?php
namespace
App\Entity
;
use
ApiPlatform\Core\Annotation\ApiResource
;
use
Symfony\Component\Serializer\Annotation\Groups
;
// This class exists as a (hacky) way to provide documentation.
/**
* You.
*
* @ApiResource(
* shortName="User",
* order=10,
* itemOperations={},
* collectionOperations={
* "post"={
* "controller"="App\Controller\RegistrationController",
* },
* },
* )
*/
class
UserDocumentation
{
/**
* @var string Primary e-mail address
* @Groups({"user:read", "user:write"})
*/
public
$email
;
/**
* @var string|null Plain password (Required in "write")
* @Groups({"user:write"})
*/
public
$password
;
}
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment