we now use email for authentication

This commit is contained in:
overflowerror 2021-01-08 18:26:17 +01:00
parent f2a8ed241b
commit 34fcd20940
7 changed files with 32 additions and 13 deletions

View file

@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210108165208 extends AbstractMigration
final class Version20210108172234 extends AbstractMigration
{
public function getDescription(): string
{
@ -20,7 +20,8 @@ final class Version20210108165208 extends AbstractMigration
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE user CHANGE id id BINARY(16) NOT NULL, CHANGE roles roles LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\'');
$this->addSql('ALTER TABLE user ADD email VARCHAR(180) NOT NULL, CHANGE id id BINARY(16) NOT NULL, CHANGE roles roles LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\'');
$this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649E7927C74 ON user (email)');
$this->addSql('ALTER TABLE video CHANGE id id BINARY(16) NOT NULL, CHANGE uploader_id uploader_id BINARY(16) NOT NULL');
$this->addSql('ALTER TABLE video_link CHANGE id id BINARY(16) NOT NULL, CHANGE video_id video_id BINARY(16) NOT NULL, CHANGE creator_id creator_id BINARY(16) NOT NULL');
$this->addSql('ALTER TABLE view DROP FOREIGN KEY FK_FEFDAB8E29C1004E');
@ -33,7 +34,8 @@ final class Version20210108165208 extends AbstractMigration
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE user CHANGE id id BINARY(16) NOT NULL, CHANGE roles roles LONGTEXT CHARACTER SET utf8mb4 NOT NULL COLLATE `utf8mb4_bin`');
$this->addSql('DROP INDEX UNIQ_8D93D649E7927C74 ON user');
$this->addSql('ALTER TABLE user DROP email, CHANGE id id BINARY(16) NOT NULL, CHANGE roles roles LONGTEXT CHARACTER SET utf8mb4 NOT NULL COLLATE `utf8mb4_bin`');
$this->addSql('ALTER TABLE video CHANGE id id BINARY(16) NOT NULL, CHANGE uploader_id uploader_id BINARY(16) NOT NULL');
$this->addSql('ALTER TABLE video_link CHANGE id id BINARY(16) NOT NULL, CHANGE video_id video_id BINARY(16) NOT NULL, CHANGE creator_id creator_id BINARY(16) NOT NULL');
$this->addSql('ALTER TABLE `view` DROP FOREIGN KEY FK_FEFDAB8E29C1004E');

View file

@ -29,7 +29,7 @@ class UserController extends AbstractController
*/
public function userProfilePicture($username): Response
{
$user = $this->userService->getUserByName($username);
$user = $this->userService->getUserByEmail($username);
if (!$user) {
throw new NotFoundHttpException();
}

View file

@ -23,6 +23,7 @@ class UserFixtures extends Fixture
$admin = new User();
$admin->setName("admin");
$admin->setEmail("admin@mytube");
$admin->setPassword($this->passwordEncoder->encodePassword($admin, "password"));
$admin->setRoles(["ROLE_ADMIN"]);
$manager->persist($admin);

View file

@ -23,6 +23,11 @@ class User implements UserInterface
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $email;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
@ -73,7 +78,7 @@ class User implements UserInterface
*/
public function getUsername(): string
{
return (string)$this->name;
return (string)$this->email;
}
/**
@ -168,4 +173,15 @@ class User implements UserInterface
return $role;
}, $this->getRoles());
}
public function getEmail(): string
{
return $this->email;
}
public function setEmail($email): self
{
$this->email = $email;
return $this;
}
}

View file

@ -52,13 +52,13 @@ class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
public function getCredentials(Request $request)
{
$credentials = [
'name' => $request->request->get('name'),
'email' => $request->request->get('email'),
'password' => $request->request->get('password'),
'csrf_token' => $request->request->get('_csrf_token'),
];
$request->getSession()->set(
Security::LAST_USERNAME,
$credentials['name']
$credentials['email']
);
return $credentials;
@ -71,11 +71,11 @@ class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
throw new InvalidCsrfTokenException();
}
$user = $this->entityManager->getRepository(User::class)->findOneBy(['name' => $credentials['name']]);
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);
if (!$user) {
// fail authentication with a custom error
throw new CustomUserMessageAuthenticationException('Name could not be found.');
throw new CustomUserMessageAuthenticationException('Email could not be found.');
}
return $user;

View file

@ -27,7 +27,7 @@ class UserService
return null;
}
return $this->getUserByName($user->getUsername());
return $this->getUserByEmail($user->getUsername());
}
public function getUsers(): array
@ -35,8 +35,8 @@ class UserService
return $this->userRepository->findAll();
}
public function getUserByName($username): ?User
public function getUserByEmail($email): ?User
{
return $this->userRepository->findOneByName($username);
return $this->userRepository->findOneByEmail($email);
}
}

View file

@ -16,7 +16,7 @@
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
<label for="inputName">Name</label>
<input type="text" value="{{ last_username }}" name="name" id="inputName" class="form-control" required
<input type="text" value="{{ last_username }}" name="email" id="inputName" class="form-control" required
autofocus>
<label for="inputPassword">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" required>