1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| <?php
namespace InApps\WebServicesV1Bundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\Util\Codes;
use InApps\UserBundle\Entity\User;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AccountStatusException;
class UserController extends FOSRestController implements ClassResourceInterface
{
/**
* Post action
* @var Request $request
* @return View|array
*
* @Rest\View()
*
* create an account
*/
public function postAction(Request $request)
{
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->createUser();
$username = $request->get('username');
$password = $request->get('password');
$email = $request->get('email');
$user->setUsername($username);
$user->setPlainPassword($password);
$user->setEmail($email);
$validator = $this->get('validator');
$request->setLocale('en_US');
$errorList = $validator->validate($user, array('Registration'));
if (count($errorList) > 0) {
$array = array();
foreach($errorList as $error) {
$array[] = $error->getMessage();
}
$message = array('errors' => $array);
return $this->view($message, Codes::HTTP_BAD_REQUEST);
}
try {
$userManager->updateUser($user);
}
catch(\Exception $e) {
return $this->view('An error has occurred', Codes::HTTP_BAD_REQUEST);
}
return $this->view(array('code' => Codes::HTTP_CREATED, 'message' => 'User successfully created'),Codes::HTTP_CREATED);
}
} |
Partager