diff --git a/composer.json b/composer.json index 0b841718c..263222165 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,8 @@ "yiisoft/var-dumper": "^3.0@dev", "yiisoft/view": "^3.0@dev", "yiisoft/yii-console": "^3.0@dev", - "yiisoft/yii-web": "^3.0@dev" + "yiisoft/yii-web": "^3.0@dev", + "yiisoft/yii-cycle": "^3.0@dev" }, "require-dev": { }, diff --git a/config/common.php b/config/common.php index d1a36e6b7..f34319a28 100644 --- a/config/common.php +++ b/config/common.php @@ -4,6 +4,7 @@ use App\Factory\MailerFactory; use App\Parameters; use Psr\Log\LoggerInterface; +use Yiisoft\Aliases\Aliases; use Yiisoft\Cache\ArrayCache; use Yiisoft\Cache\Cache; use Yiisoft\Cache\CacheInterface; @@ -16,6 +17,9 @@ */ return [ + Aliases::class => $params['aliases'], + Psr\SimpleCache\CacheInterface::class => ArrayCache::class, + CacheInterface::class => Cache::class, Parameters::class => static function () use ($params) { return new Parameters($params); }, diff --git a/config/params.php b/config/params.php index b2553a987..c96032651 100644 --- a/config/params.php +++ b/config/params.php @@ -1,5 +1,7 @@ [ 'host' => 'smtp.example.com', @@ -20,5 +22,39 @@ 'session' => [ 'options' => ['cookie_secure' => 0], - ] + ], + + 'commands' => [ + 'user/create' => CreateCommand::class, + ], + + // cycle DBAL config + 'cycle.dbal' => [ + 'default' => 'default', + 'aliases' => [], + 'databases' => [ + 'default' => ['connection' => 'sqlite'] + ], + 'connections' => [ + 'sqlite' => [ + 'driver' => \Spiral\Database\Driver\SQLite\SQLiteDriver::class, + 'connection' => 'sqlite:@runtime/database.db', + 'username' => '', + 'password' => '', + ] + ], + ], + // cycle common config + 'cycle.common' => [ + 'entityPaths' => [ + '@src/Entity' + ], + ], + // cycle migration config + 'cycle.migrations' => [ + 'directory' => '@root/migrations', + 'namespace' => 'App\\Migration', + 'table' => 'migration', + 'safe' => false, + ], ]; diff --git a/src/Command/User/CreateCommand.php b/src/Command/User/CreateCommand.php new file mode 100644 index 000000000..26edf5dd0 --- /dev/null +++ b/src/Command/User/CreateCommand.php @@ -0,0 +1,59 @@ +orm = $orm; + } + + public function configure(): void + { + $this + ->setDescription('Creates a user') + ->setHelp('This command allows you to create a user') + ->addArgument('login', InputArgument::REQUIRED, 'Login') + ->addArgument('password', InputArgument::REQUIRED, 'Password'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + $login = $input->getArgument('login'); + $password = $input->getArgument('password'); + + $user = new User(); + $user->setLogin($login); + $user->setPassword($password); + + try { + $transaction = new Transaction($this->orm); + $transaction->persist($user); + $transaction->run(); + $io->success('User created'); + } catch (\Throwable $t) { + $io->error($t->getMessage()); + return self::EXIT_CODE_FAILED_TO_PERSIST; + } + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php index a99417b28..db396fb79 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -1,20 +1,55 @@ id = $id; - $this->login = $login; + if (!isset($this->token)) { + $this->resetToken(); + } } public function getId(): ?string @@ -27,9 +62,9 @@ public function getToken(): ?string return $this->token; } - public function setToken(string $token): void + public function resetToken(): void { - $this->token = $token; + $this->token = Random::string(128); } public function getLogin(): string @@ -37,6 +72,11 @@ public function getLogin(): string return $this->login; } + public function setLogin(string $login): void + { + $this->login = $login; + } + public function validatePassword(string $password): bool { if ($this->passwordHash === null) { diff --git a/src/Factory/AppRouterFactory.php b/src/Factory/AppRouterFactory.php index e17693a06..158e630c7 100644 --- a/src/Factory/AppRouterFactory.php +++ b/src/Factory/AppRouterFactory.php @@ -25,6 +25,7 @@ public function __invoke(ContainerInterface $container) Route::get('/test/{id:\w+}') ->to(new ActionCaller(SiteController::class, 'testParameter', $container)) ->name('site/test'), + Route::methods([Method::GET, Method::POST], '/login') ->to(new ActionCaller(AuthController::class, 'login', $container)) ->name('site/login'), diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index f1cc8a7c1..3f9d99e75 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -3,43 +3,26 @@ namespace App\Repository; use App\Entity\User; +use Cycle\ORM\ORMInterface; +use Cycle\ORM\Select; use Yiisoft\Auth\IdentityInterface; use Yiisoft\Auth\IdentityRepositoryInterface; -class UserRepository implements IdentityRepositoryInterface +class UserRepository extends Select\Repository implements IdentityRepositoryInterface { - private const IDENTITIES = [ - [ - 'id' => '1', - 'token' => 'test1', - 'login' => 'samdark', - 'password' => 'qwerty', - ], - [ - 'id' => '2', - 'token' => 'test2', - 'login' => 'hiqsol', - 'password' => 'qwerty', - ], - ]; + public function __construct(ORMInterface $orm, $role = User::class) + { + parent::__construct(new Select($orm, $role)); + } private function findIdentityBy(string $field, string $value): ?IdentityInterface { - foreach (self::IDENTITIES as $identity) { - if ($identity[$field] === $value) { - $user = new User($identity['id'], $identity['login']); - $user->setToken($identity['token']); - $user->setPassword($identity['password']); - return $user; - } - } - - return null; + return $this->findOne([$field => $value]); } public function findIdentity(string $id): ?IdentityInterface { - return $this->findIdentityBy('id', $id); + return $this->findByPK($id); } public function findIdentityByToken(string $token, string $type): ?IdentityInterface @@ -47,7 +30,7 @@ public function findIdentityByToken(string $token, string $type): ?IdentityInter return $this->findIdentityBy('token', $token); } - public function findByLogin(string $login): ?User + public function findByLogin(string $login): ?IdentityInterface { return $this->findIdentityBy('login', $login); }