Shyim
7 years ago
11 changed files with 241 additions and 42 deletions
File diff suppressed because one or more lines are too long
@ -1,20 +1,36 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
|
|
||||||
namespace App\Controller; |
namespace App\Controller; |
||||||
|
|
||||||
|
|
||||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
||||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
||||||
|
use Symfony\Component\Config\Util\XmlUtils; |
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse; |
||||||
|
use Symfony\Component\HttpFoundation\Response; |
||||||
|
|
||||||
|
/** |
||||||
|
* Class Index |
||||||
|
* @author Soner Sayakci <shyim@posteo.de> |
||||||
|
*/ |
||||||
class Index extends Controller |
class Index extends Controller |
||||||
{ |
{ |
||||||
/** |
/** |
||||||
* @Route(path="/", name="index") |
* @Route(path="/", name="index") |
||||||
* @author Soner Sayakci <shyim@posteo.de> |
* @author Soner Sayakci <shyim@posteo.de> |
||||||
*/ |
*/ |
||||||
public function index() |
public function index(): Response |
||||||
{ |
{ |
||||||
return $this->render('index.twig'); |
return $this->render('index.twig'); |
||||||
} |
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @Route(path="/api/settings") |
||||||
|
* @author Soner Sayakci <shyim@posteo.de> |
||||||
|
*/ |
||||||
|
public function settings(): JsonResponse |
||||||
|
{ |
||||||
|
return new JsonResponse([ |
||||||
|
'registrationEnabled' => XmlUtils::phpize($this->container->getParameter('registrationEnabled')) |
||||||
|
]); |
||||||
|
} |
||||||
} |
} |
@ -0,0 +1,92 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace App\Controller; |
||||||
|
|
||||||
|
use App\Entity\User; |
||||||
|
use App\Repository\UserRepository; |
||||||
|
use Doctrine\ORM\ORMException; |
||||||
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
||||||
|
use Symfony\Component\Config\Util\XmlUtils; |
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse; |
||||||
|
use Symfony\Component\HttpFoundation\Request; |
||||||
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Route("/api") |
||||||
|
* Class Register |
||||||
|
* @author Soner Sayakci <shyim@posteo.de> |
||||||
|
*/ |
||||||
|
class Register extends Controller |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var UserRepository |
||||||
|
*/ |
||||||
|
private $repository; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var UserPasswordEncoderInterface |
||||||
|
*/ |
||||||
|
private $userPasswordEncoder; |
||||||
|
|
||||||
|
/** |
||||||
|
* Register constructor. |
||||||
|
* @param UserRepository $repository |
||||||
|
* @param UserPasswordEncoderInterface $userPasswordEncoder |
||||||
|
* @author Soner Sayakci <shyim@posteo.de> |
||||||
|
*/ |
||||||
|
public function __construct(UserRepository $repository, UserPasswordEncoderInterface $userPasswordEncoder) |
||||||
|
{ |
||||||
|
$this->repository = $repository; |
||||||
|
$this->userPasswordEncoder = $userPasswordEncoder; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @Route(path="/register") |
||||||
|
* @author Soner Sayakci <shyim@posteo.de> |
||||||
|
* @param Request $request |
||||||
|
* @return JsonResponse |
||||||
|
*/ |
||||||
|
public function index(Request $request): JsonResponse |
||||||
|
{ |
||||||
|
if (!$this->isRegistrationEnabled()) { |
||||||
|
return new JsonResponse(['message' => 'Registration is disabled'], 500); |
||||||
|
} |
||||||
|
|
||||||
|
$data = $request->request->all(); |
||||||
|
|
||||||
|
if ($this->repository->findOneBy(['username' => $data['username']])) { |
||||||
|
return new JsonResponse(['message' => 'Username is already taken'], 500); |
||||||
|
} |
||||||
|
|
||||||
|
if ($this->repository->findOneBy(['email' => $data['email']])) { |
||||||
|
return new JsonResponse(['message' => 'Email is already taken'], 500); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
$user = new User(); |
||||||
|
$user->setUsername($data['username']); |
||||||
|
$user->setPassword($this->userPasswordEncoder->encodePassword($user, $data['password'])); |
||||||
|
$user->setEmail($data['email']); |
||||||
|
|
||||||
|
$manager = $this->get('doctrine.orm.default_entity_manager'); |
||||||
|
|
||||||
|
try { |
||||||
|
$manager->persist($user); |
||||||
|
$manager->flush(); |
||||||
|
} catch (ORMException $e) { |
||||||
|
return new JsonResponse(['message' => $e->getMessage()], 500); |
||||||
|
} |
||||||
|
|
||||||
|
return new JsonResponse(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @return bool |
||||||
|
* @author Soner Sayakci <shyim@posteo.de> |
||||||
|
*/ |
||||||
|
private function isRegistrationEnabled(): bool |
||||||
|
{ |
||||||
|
return XmlUtils::phpize($this->container->getParameter('registrationEnabled')); |
||||||
|
} |
||||||
|
} |
@ -1,24 +0,0 @@ |
|||||||
var Encore = require('@symfony/webpack-encore'); |
|
||||||
|
|
||||||
Encore |
|
||||||
// the project directory where compiled assets will be stored
|
|
||||||
.setOutputPath('public/build/') |
|
||||||
// the public path used by the web server to access the previous directory
|
|
||||||
.setPublicPath('/build') |
|
||||||
.cleanupOutputBeforeBuild() |
|
||||||
.enableSourceMaps(!Encore.isProduction()) |
|
||||||
// uncomment to create hashed filenames (e.g. app.abc123.css)
|
|
||||||
// .enableVersioning(Encore.isProduction())
|
|
||||||
|
|
||||||
// uncomment to define the assets of the project
|
|
||||||
// .addEntry('js/app', './assets/js/app.js')
|
|
||||||
// .addStyleEntry('css/app', './assets/css/app.scss')
|
|
||||||
|
|
||||||
// uncomment if you use Sass/SCSS files
|
|
||||||
// .enableSassLoader()
|
|
||||||
|
|
||||||
// uncomment for legacy applications that require $/jQuery as a global variable
|
|
||||||
// .autoProvidejQuery()
|
|
||||||
; |
|
||||||
|
|
||||||
module.exports = Encore.getWebpackConfig(); |
|
Loading…
Reference in new issue