create user and migration
This commit is contained in:
		
							parent
							
								
									0f351f7ac7
								
							
						
					
					
						commit
						769e389787
					
				
					 4 changed files with 230 additions and 2 deletions
				
			
		|  | @ -1,7 +1,15 @@ | |||
| security: | ||||
|     encoders: | ||||
|         App\Entity\User: | ||||
|             algorithm: auto | ||||
| 
 | ||||
|     # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers | ||||
|     providers: | ||||
|         users_in_memory: { memory: null } | ||||
|         # used to reload user from session & other features (e.g. switch_user) | ||||
|         app_user_provider: | ||||
|             entity: | ||||
|                 class: App\Entity\User | ||||
|                 property: email | ||||
|     firewalls: | ||||
|         dev: | ||||
|             pattern: ^/(_(profiler|wdt)|css|images|js)/ | ||||
|  | @ -9,7 +17,7 @@ security: | |||
|         main: | ||||
|             anonymous: true | ||||
|             lazy: true | ||||
|             provider: users_in_memory | ||||
|             provider: app_user_provider | ||||
| 
 | ||||
|             # activate different ways to authenticate | ||||
|             # https://symfony.com/doc/current/security.html#firewalls-authentication | ||||
|  |  | |||
							
								
								
									
										35
									
								
								migrations/Version20210422125046.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								migrations/Version20210422125046.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,35 @@ | |||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace DoctrineMigrations; | ||||
| 
 | ||||
| use Doctrine\DBAL\Schema\Schema; | ||||
| use Doctrine\Migrations\AbstractMigration; | ||||
| 
 | ||||
| /** | ||||
|  * Auto-generated Migration: Please modify to your needs! | ||||
|  */ | ||||
| final class Version20210422125046 extends AbstractMigration | ||||
| { | ||||
|     public function getDescription() : string | ||||
|     { | ||||
|         return ''; | ||||
|     } | ||||
| 
 | ||||
|     public function up(Schema $schema) : void | ||||
|     { | ||||
|         // this up() migration is auto-generated, please modify it to your needs
 | ||||
|         $this->addSql('CREATE SEQUENCE "user_id_seq" INCREMENT BY 1 MINVALUE 1 START 1'); | ||||
|         $this->addSql('CREATE TABLE "user" (id INT NOT NULL, email VARCHAR(180) NOT NULL, roles JSON NOT NULL, password VARCHAR(255) NOT NULL, PRIMARY KEY(id))'); | ||||
|         $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649E7927C74 ON "user" (email)'); | ||||
|     } | ||||
| 
 | ||||
|     public function down(Schema $schema) : void | ||||
|     { | ||||
|         // this down() migration is auto-generated, please modify it to your needs
 | ||||
|         $this->addSql('CREATE SCHEMA public'); | ||||
|         $this->addSql('DROP SEQUENCE "user_id_seq" CASCADE'); | ||||
|         $this->addSql('DROP TABLE "user"'); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										118
									
								
								src/Entity/User.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										118
									
								
								src/Entity/User.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,118 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace App\Entity; | ||||
| 
 | ||||
| use App\Repository\UserRepository; | ||||
| use Doctrine\ORM\Mapping as ORM; | ||||
| use Symfony\Component\Security\Core\User\UserInterface; | ||||
| 
 | ||||
| /** | ||||
|  * @ORM\Entity(repositoryClass=UserRepository::class) | ||||
|  * @ORM\Table(name="`user`") | ||||
|  */ | ||||
| class User implements UserInterface | ||||
| { | ||||
|     /** | ||||
|      * @ORM\Id | ||||
|      * @ORM\GeneratedValue | ||||
|      * @ORM\Column(type="integer") | ||||
|      */ | ||||
|     private $id; | ||||
| 
 | ||||
|     /** | ||||
|      * @ORM\Column(type="string", length=180, unique=true) | ||||
|      */ | ||||
|     private $email; | ||||
| 
 | ||||
|     /** | ||||
|      * @ORM\Column(type="json") | ||||
|      */ | ||||
|     private $roles = []; | ||||
| 
 | ||||
|     /** | ||||
|      * @var string The hashed password | ||||
|      * @ORM\Column(type="string") | ||||
|      */ | ||||
|     private $password; | ||||
| 
 | ||||
|     public function getId(): ?int | ||||
|     { | ||||
|         return $this->id; | ||||
|     } | ||||
| 
 | ||||
|     public function getEmail(): ?string | ||||
|     { | ||||
|         return $this->email; | ||||
|     } | ||||
| 
 | ||||
|     public function setEmail(string $email): self | ||||
|     { | ||||
|         $this->email = $email; | ||||
| 
 | ||||
|         return $this; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * A visual identifier that represents this user. | ||||
|      * | ||||
|      * @see UserInterface | ||||
|      */ | ||||
|     public function getUsername(): string | ||||
|     { | ||||
|         return (string) $this->email; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @see UserInterface | ||||
|      */ | ||||
|     public function getRoles(): array | ||||
|     { | ||||
|         $roles = $this->roles; | ||||
|         // guarantee every user at least has ROLE_USER
 | ||||
|         $roles[] = 'ROLE_USER'; | ||||
| 
 | ||||
|         return array_unique($roles); | ||||
|     } | ||||
| 
 | ||||
|     public function setRoles(array $roles): self | ||||
|     { | ||||
|         $this->roles = $roles; | ||||
| 
 | ||||
|         return $this; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @see UserInterface | ||||
|      */ | ||||
|     public function getPassword(): string | ||||
|     { | ||||
|         return (string) $this->password; | ||||
|     } | ||||
| 
 | ||||
|     public function setPassword(string $password): self | ||||
|     { | ||||
|         $this->password = $password; | ||||
| 
 | ||||
|         return $this; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Returning a salt is only needed, if you are not using a modern | ||||
|      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml. | ||||
|      * | ||||
|      * @see UserInterface | ||||
|      */ | ||||
|     public function getSalt(): ?string | ||||
|     { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @see UserInterface | ||||
|      */ | ||||
|     public function eraseCredentials() | ||||
|     { | ||||
|         // If you store any temporary, sensitive data on the user, clear it here
 | ||||
|         // $this->plainPassword = null;
 | ||||
|     } | ||||
| } | ||||
							
								
								
									
										67
									
								
								src/Repository/UserRepository.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/Repository/UserRepository.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,67 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace App\Repository; | ||||
| 
 | ||||
| use App\Entity\User; | ||||
| use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | ||||
| use Doctrine\Persistence\ManagerRegistry; | ||||
| use Symfony\Component\Security\Core\Exception\UnsupportedUserException; | ||||
| use Symfony\Component\Security\Core\User\PasswordUpgraderInterface; | ||||
| use Symfony\Component\Security\Core\User\UserInterface; | ||||
| 
 | ||||
| /** | ||||
|  * @method User|null find($id, $lockMode = null, $lockVersion = null) | ||||
|  * @method User|null findOneBy(array $criteria, array $orderBy = null) | ||||
|  * @method User[]    findAll() | ||||
|  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | ||||
|  */ | ||||
| class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface | ||||
| { | ||||
|     public function __construct(ManagerRegistry $registry) | ||||
|     { | ||||
|         parent::__construct($registry, User::class); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Used to upgrade (rehash) the user's password automatically over time. | ||||
|      */ | ||||
|     public function upgradePassword(UserInterface $user, string $newEncodedPassword): void | ||||
|     { | ||||
|         if (!$user instanceof User) { | ||||
|             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user))); | ||||
|         } | ||||
| 
 | ||||
|         $user->setPassword($newEncodedPassword); | ||||
|         $this->_em->persist($user); | ||||
|         $this->_em->flush(); | ||||
|     } | ||||
| 
 | ||||
|     // /**
 | ||||
|     //  * @return User[] Returns an array of User objects
 | ||||
|     //  */
 | ||||
|     /* | ||||
|     public function findByExampleField($value) | ||||
|     { | ||||
|         return $this->createQueryBuilder('u') | ||||
|             ->andWhere('u.exampleField = :val') | ||||
|             ->setParameter('val', $value) | ||||
|             ->orderBy('u.id', 'ASC') | ||||
|             ->setMaxResults(10) | ||||
|             ->getQuery() | ||||
|             ->getResult() | ||||
|         ; | ||||
|     } | ||||
|     */ | ||||
| 
 | ||||
|     /* | ||||
|     public function findOneBySomeField($value): ?User | ||||
|     { | ||||
|         return $this->createQueryBuilder('u') | ||||
|             ->andWhere('u.exampleField = :val') | ||||
|             ->setParameter('val', $value) | ||||
|             ->getQuery() | ||||
|             ->getOneOrNullResult() | ||||
|         ; | ||||
|     } | ||||
|     */ | ||||
| } | ||||
		Reference in a new issue