2020-07-22 02:58:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2020-07-23 15:08:31 +01:00
|
|
|
use App\Core\Controller;
|
2020-07-25 03:06:55 +01:00
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Form;
|
|
|
|
use function App\Core\I18n\_m;
|
2020-08-13 02:23:22 +01:00
|
|
|
use App\Entity\GSActor;
|
2020-07-25 03:06:55 +01:00
|
|
|
use App\Entity\LocalUser;
|
|
|
|
use App\Security\Authenticator;
|
|
|
|
use App\Security\EmailVerifier;
|
|
|
|
use app\Util\Common;
|
|
|
|
use App\Util\Nickname;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
|
2020-07-22 02:58:25 +01:00
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
2020-07-25 03:06:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraints\Length;
|
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
2020-07-22 02:58:25 +01:00
|
|
|
|
2020-07-23 15:08:31 +01:00
|
|
|
class Security extends Controller
|
2020-07-22 02:58:25 +01:00
|
|
|
{
|
2020-07-23 18:55:06 +01:00
|
|
|
public function login(AuthenticationUtils $authenticationUtils)
|
2020-07-22 02:58:25 +01:00
|
|
|
{
|
|
|
|
if ($this->getUser()) {
|
|
|
|
return $this->redirectToRoute('main_all');
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the login error if there is one
|
|
|
|
$error = $authenticationUtils->getLastAuthenticationError();
|
|
|
|
// last username entered by the user
|
2020-07-23 15:08:31 +01:00
|
|
|
$last_username = $authenticationUtils->getLastUsername();
|
2020-07-22 02:58:25 +01:00
|
|
|
|
2020-07-23 15:08:31 +01:00
|
|
|
return ['_template' => 'security/login.html.twig', 'last_username' => $last_username, 'error' => $error];
|
2020-07-22 02:58:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
|
|
|
|
}
|
2020-07-25 03:06:55 +01:00
|
|
|
|
|
|
|
public function register(Request $request,
|
|
|
|
EmailVerifier $email_verifier,
|
|
|
|
GuardAuthenticatorHandler $guard_handler,
|
|
|
|
Authenticator $authenticator)
|
|
|
|
{
|
|
|
|
$form = Form::create([
|
|
|
|
['nickname', TextType::class, [
|
|
|
|
'label' => _m('Nickname'),
|
|
|
|
'constraints' => [new Length([
|
|
|
|
'min' => 1,
|
|
|
|
'minMessage' => _m('Your password should be at least {{ limit }} characters long'),
|
|
|
|
'max' => 64,
|
|
|
|
'maxMessage' => _m('Your password should be at most {{ limit }} characters long'), ]),
|
|
|
|
],
|
|
|
|
]],
|
|
|
|
['email', EmailType::class, ['label' => _m('Email')]],
|
|
|
|
['password', PasswordType::class, [
|
|
|
|
'label' => _m('Password'),
|
|
|
|
'mapped' => false,
|
|
|
|
'constraints' => [
|
|
|
|
new NotBlank(['message' => _m('Please enter a password')]),
|
|
|
|
new Length(['min' => 6, 'minMessage' => _m('Your password should be at least {{ limit }} characters'), 'max' => 60]),
|
|
|
|
],
|
|
|
|
]],
|
|
|
|
['register', SubmitType::class, ['label' => _m('Register')]],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$data = $form->getData();
|
|
|
|
$data['password'] = $form->get('password')->getData();
|
|
|
|
|
|
|
|
$valid_nickname = Nickname::isValid($data['nickname'], Nickname::CHECK_USED);
|
|
|
|
if (!$valid_nickname) {
|
|
|
|
throw new \Exception(_m('Invalid nickname'));
|
|
|
|
}
|
|
|
|
|
2020-08-13 02:23:22 +01:00
|
|
|
$actor = GSActor::create(['nickname' => $data['nickname']]);
|
2020-08-19 15:00:57 +01:00
|
|
|
$user = LocalUser::create([
|
|
|
|
'nickname' => $data['nickname'],
|
|
|
|
'email' => $data['email'],
|
|
|
|
'password' => LocalUser::hashPassword($data['password']),
|
|
|
|
]);
|
2020-07-25 03:06:55 +01:00
|
|
|
|
|
|
|
DB::persist($user);
|
2020-08-13 02:23:22 +01:00
|
|
|
DB::persist($actor);
|
2020-07-25 03:06:55 +01:00
|
|
|
|
|
|
|
// generate a signed url and email it to the user
|
|
|
|
if (Common::config('site', 'use_email')) {
|
|
|
|
$email_verifier->sendEmailConfirmation(
|
|
|
|
'verify_email',
|
|
|
|
$user,
|
|
|
|
(new TemplatedEmail())
|
|
|
|
->from(new Address(Common::config('site', 'email'), Common::config('site', 'nickname')))
|
|
|
|
->to($user->getOutgoingEmail())
|
|
|
|
->subject(_m('Please Confirm your Email'))
|
|
|
|
->htmlTemplate('security/confirmation_email.html.twig')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$user->setIsEmailVerified(true);
|
|
|
|
}
|
|
|
|
|
2020-08-19 15:00:57 +01:00
|
|
|
DB::flush();
|
|
|
|
|
2020-07-25 03:06:55 +01:00
|
|
|
return $guard_handler->authenticateUserAndHandleSuccess(
|
|
|
|
$user,
|
|
|
|
$request,
|
|
|
|
$authenticator,
|
|
|
|
'main' // firewall name in security.yaml
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'_template' => 'security/register.html.twig',
|
|
|
|
'registration_form' => $form->createView(),
|
|
|
|
];
|
|
|
|
}
|
2020-07-22 02:58:25 +01:00
|
|
|
}
|