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;
|
2021-07-28 22:38:27 +01:00
|
|
|
use App\Core\Log;
|
2021-04-15 23:26:05 +01:00
|
|
|
use App\Core\VisibilityScope;
|
2021-09-18 03:22:27 +01:00
|
|
|
use App\Entity\Actor;
|
2020-10-11 21:27:50 +01:00
|
|
|
use App\Entity\Follow;
|
2020-07-25 03:06:55 +01:00
|
|
|
use App\Entity\LocalUser;
|
2020-12-02 22:57:32 +00:00
|
|
|
use App\Entity\Note;
|
2020-07-25 03:06:55 +01:00
|
|
|
use App\Security\Authenticator;
|
2021-09-06 19:49:03 +01:00
|
|
|
use App\Security\EmailVerifier;
|
|
|
|
use App\Util\Common;
|
2021-09-18 03:22:27 +01:00
|
|
|
use App\Util\Exception\DuplicateFoundException;
|
2021-07-28 22:38:27 +01:00
|
|
|
use App\Util\Exception\EmailTakenException;
|
2021-09-18 03:22:27 +01:00
|
|
|
use App\Util\Exception\NicknameEmptyException;
|
|
|
|
use App\Util\Exception\NicknameReservedException;
|
2021-04-11 12:03:32 +01:00
|
|
|
use App\Util\Exception\NicknameTakenException;
|
2021-09-18 03:22:27 +01:00
|
|
|
use App\Util\Exception\NicknameTooLongException;
|
|
|
|
use App\Util\Exception\NicknameTooShortException;
|
|
|
|
use App\Util\Exception\NotImplementedException;
|
2021-07-28 22:38:27 +01:00
|
|
|
use App\Util\Exception\ServerException;
|
2021-08-06 12:12:10 +01:00
|
|
|
use App\Util\Form\FormFields;
|
2020-07-25 03:06:55 +01:00
|
|
|
use App\Util\Nickname;
|
2021-04-11 12:03:32 +01:00
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
2020-07-25 03:06:55 +01:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-09-18 03:22:27 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2020-07-25 03:06:55 +01:00
|
|
|
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-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Log a user in
|
|
|
|
*/
|
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
|
2021-07-28 22:38:27 +01:00
|
|
|
$last_login_id = $authenticationUtils->getLastUsername();
|
2020-07-22 02:58:25 +01:00
|
|
|
|
2021-07-28 22:38:27 +01:00
|
|
|
return [
|
|
|
|
'_template' => 'security/login.html.twig',
|
|
|
|
'last_login_id' => $last_login_id,
|
|
|
|
'error' => $error,
|
|
|
|
'notes_fn' => fn () => Note::getAllNotes(VisibilityScope::$instance_scope),
|
|
|
|
];
|
2020-07-22 02:58:25 +01:00
|
|
|
}
|
|
|
|
|
2021-07-28 22:38:27 +01:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
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
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
2021-09-15 00:25:16 +01:00
|
|
|
*
|
2020-11-06 19:47:15 +00:00
|
|
|
* Register a user, making sure the nickname is not reserved and
|
|
|
|
* possibly sending a confirmation email
|
2021-09-15 00:25:16 +01:00
|
|
|
*
|
2021-09-18 03:22:27 +01:00
|
|
|
* @param Request $request
|
2021-09-15 00:25:16 +01:00
|
|
|
* @param GuardAuthenticatorHandler $guard_handler
|
2021-09-18 03:22:27 +01:00
|
|
|
* @param Authenticator $authenticator
|
|
|
|
*
|
2021-09-15 00:25:16 +01:00
|
|
|
* @throws EmailTakenException
|
|
|
|
* @throws NicknameTakenException
|
|
|
|
* @throws ServerException
|
|
|
|
* @throws DuplicateFoundException
|
|
|
|
* @throws NicknameEmptyException
|
|
|
|
* @throws NicknameReservedException
|
|
|
|
* @throws NicknameTooLongException
|
|
|
|
* @throws NicknameTooShortException
|
|
|
|
* @throws NotImplementedException
|
2021-09-18 03:22:27 +01:00
|
|
|
*
|
|
|
|
* @return null|array|Response
|
2020-11-06 19:47:15 +00:00
|
|
|
*/
|
2021-09-18 03:22:27 +01:00
|
|
|
public function register(Request $request,
|
2020-07-25 03:06:55 +01:00
|
|
|
GuardAuthenticatorHandler $guard_handler,
|
2021-09-18 03:22:27 +01:00
|
|
|
Authenticator $authenticator)
|
2020-07-25 03:06:55 +01:00
|
|
|
{
|
|
|
|
$form = Form::create([
|
|
|
|
['nickname', TextType::class, [
|
|
|
|
'label' => _m('Nickname'),
|
2021-08-16 21:43:27 +01:00
|
|
|
'help' => _m('Your desired nickname (e.g., j0hnD03)'),
|
2021-07-28 22:06:10 +01:00
|
|
|
'constraints' => [
|
|
|
|
new NotBlank(['message' => _m('Please enter a nickname')]),
|
|
|
|
new Length([
|
2021-07-28 22:38:27 +01:00
|
|
|
'min' => Common::config('nickname', 'min_length'),
|
|
|
|
'minMessage' => _m(['Your nickname must be at least # characters long'], ['count' => Common::config('nickname', 'min_length')]),
|
|
|
|
'max' => Nickname::MAX_LEN,
|
|
|
|
'maxMessage' => _m(['Your nickname must be at most # characters long'], ['count' => Nickname::MAX_LEN]), ]),
|
2020-07-25 03:06:55 +01:00
|
|
|
],
|
2021-09-18 03:22:27 +01:00
|
|
|
'block_name' => 'nickname',
|
|
|
|
'label_attr' => ['class' => 'section-form-label'],
|
2021-09-15 00:25:16 +01:00
|
|
|
'invalid_message' => _m('Nickname not valid. Please provide a valid nickname.'),
|
2020-07-25 03:06:55 +01:00
|
|
|
]],
|
2021-07-28 22:06:10 +01:00
|
|
|
['email', EmailType::class, [
|
2021-09-18 03:22:27 +01:00
|
|
|
'label' => _m('Email'),
|
|
|
|
'help' => _m('Desired email for this account (e.g., john@provider.com)'),
|
|
|
|
'constraints' => [ new NotBlank(['message' => _m('Please enter an email') ])],
|
|
|
|
'block_name' => 'email',
|
|
|
|
'label_attr' => ['class' => 'section-form-label'],
|
2021-09-15 00:25:16 +01:00
|
|
|
'invalid_message' => _m('Email not valid. Please provide a valid email.'),
|
2021-07-28 22:06:10 +01:00
|
|
|
]],
|
2021-08-03 11:24:01 +01:00
|
|
|
FormFields::repeated_password(),
|
2020-07-25 03:06:55 +01:00
|
|
|
['register', SubmitType::class, ['label' => _m('Register')]],
|
2021-08-17 20:51:06 +01:00
|
|
|
], form_options: ['block_prefix' => 'register']);
|
2020-07-25 03:06:55 +01:00
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$data = $form->getData();
|
|
|
|
$data['password'] = $form->get('password')->getData();
|
|
|
|
|
2021-07-28 22:38:27 +01:00
|
|
|
// This will throw the appropriate errors, result ignored
|
|
|
|
$user = LocalUser::findByNicknameOrEmail($data['nickname'], $data['email']);
|
|
|
|
if ($user !== null) {
|
2021-09-15 00:25:16 +01:00
|
|
|
|
2021-07-28 22:38:27 +01:00
|
|
|
// If we do find something, there's a duplicate
|
2021-09-15 00:25:16 +01:00
|
|
|
if ($user->getNickname() === $data['nickname']) {
|
|
|
|
// Register page feedback on nickname already in use
|
2021-09-18 03:22:27 +01:00
|
|
|
$this->addFlash('verify_nickname_error', _m('Nickname is already in use on this server.'));
|
2021-07-28 22:38:27 +01:00
|
|
|
throw new NicknameTakenException;
|
|
|
|
} else {
|
2021-09-15 00:25:16 +01:00
|
|
|
// Register page feedback on email already in use
|
2021-09-18 03:22:27 +01:00
|
|
|
$this->addFlash('verify_email_error', _m('Email is already taken.'));
|
2021-07-28 22:38:27 +01:00
|
|
|
throw new EmailTakenException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$valid_nickname = Nickname::validate($data['nickname'], check_already_used: false);
|
2020-07-25 03:06:55 +01:00
|
|
|
|
2021-04-11 12:03:32 +01:00
|
|
|
try {
|
2021-07-28 22:38:27 +01:00
|
|
|
// This already checks if the nickname is being used
|
2021-09-18 03:22:27 +01:00
|
|
|
$actor = Actor::create(['nickname' => $valid_nickname]);
|
2021-04-23 13:54:25 +01:00
|
|
|
$user = LocalUser::create([
|
2021-07-28 22:38:27 +01:00
|
|
|
'nickname' => $valid_nickname,
|
2021-04-11 12:03:32 +01:00
|
|
|
'outgoing_email' => $data['email'],
|
|
|
|
'incoming_email' => $data['email'],
|
|
|
|
'password' => LocalUser::hashPassword($data['password']),
|
|
|
|
]);
|
2021-04-23 13:54:25 +01:00
|
|
|
DB::persistWithSameId(
|
|
|
|
$actor,
|
|
|
|
$user,
|
|
|
|
// Self follow
|
|
|
|
fn (int $id) => DB::persist(Follow::create(['follower' => $id, 'followed' => $id]))
|
|
|
|
);
|
|
|
|
DB::flush();
|
2021-08-03 11:24:01 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-04-11 12:03:32 +01:00
|
|
|
} catch (UniqueConstraintViolationException $e) {
|
2021-07-28 22:38:27 +01:00
|
|
|
// _something_ was duplicated, but since we already check if nickname is in use, we can't tell what went wrong
|
|
|
|
$e = 'An error occurred while trying to register';
|
|
|
|
Log::critical($e . " with nickname: '{$valid_nickname}' and email '{$data['email']}'");
|
|
|
|
throw new ServerException(_m($e));
|
2021-04-11 12:03:32 +01:00
|
|
|
}
|
2021-08-03 11:24:01 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-07-25 03:06:55 +01:00
|
|
|
|
|
|
|
// generate a signed url and email it to the user
|
2021-08-04 17:48:00 +01:00
|
|
|
if ($_ENV['APP_ENV'] !== 'dev' && Common::config('site', 'use_email')) {
|
2021-08-03 11:24:01 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-09-06 19:49:03 +01:00
|
|
|
EmailVerifier::sendEmailConfirmation($user);
|
2021-08-03 11:24:01 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-07-25 03:06:55 +01:00
|
|
|
} else {
|
|
|
|
$user->setIsEmailVerified(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $guard_handler->authenticateUserAndHandleSuccess(
|
|
|
|
$user,
|
|
|
|
$request,
|
|
|
|
$authenticator,
|
|
|
|
'main' // firewall name in security.yaml
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'_template' => 'security/register.html.twig',
|
|
|
|
'registration_form' => $form->createView(),
|
2021-07-28 22:38:27 +01:00
|
|
|
'notes_fn' => fn () => Note::getAllNotes(VisibilityScope::$instance_scope),
|
2020-07-25 03:06:55 +01:00
|
|
|
];
|
|
|
|
}
|
2020-07-22 02:58:25 +01:00
|
|
|
}
|