add email to user and allow to connect through email or username

This commit is contained in:
2018-07-10 12:53:44 +02:00
parent 45a9937dbb
commit 25d00877ae
12 changed files with 395 additions and 0 deletions

View File

@@ -14,6 +14,21 @@ Chill\MainBundle\Entity\User:
username:
type: string
length: 80
usernameCanonical:
name: username_canonical
type: string
length: 80
unique: true
email:
type: string
length: 150
nullable: true
emailCanonical:
name: email_canonical
type: string
length: 150
nullable: true
unique: true
password:
type: string
length: 255

View File

@@ -9,4 +9,9 @@ services:
chill.main.role_provider:
class: Chill\MainBundle\Security\RoleProvider
chill.main.user_provider:
class: Chill\MainBundle\Security\UserProvider\UserProvider
arguments:
$em: '@Doctrine\ORM\EntityManagerInterface'

View File

@@ -5,3 +5,9 @@ services:
- "@chill.main.security.authorization.helper"
tags:
- { name: "validator.constraint_validator" }
Chill\MainBundle\Validation\Validator\UserUniqueEmailAndUsername:
arguments:
$em: '@Doctrine\ORM\EntityManagerInterface'
tags:
- { name: "validator.constraint_validator" }

View File

@@ -16,9 +16,12 @@ Chill\MainBundle\Entity\User:
- Length:
max: 70
min: 3
email:
- Email: ~
constraints:
- Callback:
callback: isGroupCenterPresentOnce
- \Chill\MainBundle\Validation\Constraint\UserUniqueEmailAndUsernameConstraint: ~
Chill\MainBundle\Entity\RoleScope:
constraints:

View File

@@ -0,0 +1,87 @@
<?php declare(strict_types=1);
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* add username and username canonical, email and email canonical columns
* to users
*/
final class Version20180709181423 extends AbstractMigration
{
public function up(Schema $schema) : void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('ALTER TABLE users ADD usernameCanonical VARCHAR(80) DEFAULT NULL');
$this->addSql('UPDATE users SET usernameCanonical=LOWER(UNACCENT(username))');
$this->addSql('ALTER TABLE users ALTER usernameCanonical DROP NOT NULL');
$this->addSql('ALTER TABLE users ALTER usernameCanonical SET DEFAULT NULL');
$this->addSql('ALTER TABLE users ADD email VARCHAR(150) DEFAULT NULL');
$this->addSql('ALTER TABLE users ADD emailCanonical VARCHAR(150) DEFAULT NULL');
$this->addSql('CREATE UNIQUE INDEX UNIQ_1483A5E9F5A5DC32 ON users (usernameCanonical)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_1483A5E9885281E ON users (emailCanonical)');
$this->addSql(<<<'SQL'
CREATE OR REPLACE FUNCTION canonicalize_user_on_update() RETURNS TRIGGER AS
$BODY$
BEGIN
IF NEW.username <> OLD.username OR NEW.email <> OLD.email OR OLD.emailcanonical IS NULL OR OLD.usernamecanonical IS NULL THEN
UPDATE users SET usernamecanonical=LOWER(UNACCENT(NEW.username)), emailcanonical=LOWER(UNACCENT(NEW.email)) WHERE id=NEW.id;
END IF;
RETURN NEW;
END;
$BODY$ LANGUAGE PLPGSQL
SQL
);
$this->addSql(<<<SQL
CREATE TRIGGER canonicalize_user_on_update
AFTER UPDATE
ON users
FOR EACH ROW
EXECUTE PROCEDURE canonicalize_user_on_update();
SQL
);
$this->addSql(<<<'SQL'
CREATE OR REPLACE FUNCTION canonicalize_user_on_insert() RETURNS TRIGGER AS
$BODY$
BEGIN
UPDATE users SET usernamecanonical=LOWER(UNACCENT(NEW.username)), emailcanonical=LOWER(UNACCENT(NEW.email)) WHERE id=NEW.id;
RETURN NEW;
END;
$BODY$ LANGUAGE PLPGSQL;
SQL
);
$this->addSql(<<<SQL
CREATE TRIGGER canonicalize_user_on_insert
AFTER INSERT
ON users
FOR EACH ROW
EXECUTE PROCEDURE canonicalize_user_on_insert();
SQL
);
}
public function down(Schema $schema) : void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('DROP INDEX UNIQ_1483A5E9F5A5DC32');
$this->addSql('DROP INDEX UNIQ_1483A5E9885281E');
$this->addSql('ALTER TABLE users DROP usernameCanonical');
$this->addSql('ALTER TABLE users DROP email');
$this->addSql('ALTER TABLE users DROP emailCanonical');
$this->addSql('DROP TRIGGER canonicalize_user_on_insert ON users');
$this->addSql('DROP FUNCTION canonicalize_user_on_insert()');
$this->addSql('DROP TRIGGER canonicalize_user_on_update ON users');
$this->addSql('DROP FUNCTION canonicalize_user_on_update()');
}
}

View File

@@ -8,6 +8,7 @@
{{ form_start(edit_form) }}
{{ form_row(edit_form.username) }}
{{ form_row(edit_form.email) }}
{{ form_row(edit_form.enabled, { 'label': "User'status"}) }}
{{ form_widget(edit_form.submit, { 'attr': { 'class' : 'sc-button green center' } } ) }}

View File

@@ -7,6 +7,7 @@
{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.email) }}
{{ form_row(form.plainPassword.password) }}
{{ form_widget(form.submit, { 'attr' : { 'class': 'sc-button blue' } }) }}
{{ form_end(form) }}