Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Commit f42dee6

Browse files
committed
Added blog admin part and authentication module
1 parent 723b3e8 commit f42dee6

File tree

32 files changed

+582
-67
lines changed

32 files changed

+582
-67
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: User
5+
* Date: 03/02/2019
6+
* Time: 04:52
7+
*/
8+
9+
namespace App\AuthModule\Actions;
10+
11+
use Keiryo\Renderer\RendererInterface;
12+
use Keiryo\Security\Authentication\AuthenticationManagerInterface;
13+
use Keiryo\Validation\Validator;
14+
use Rakit\Validation\Validation;
15+
use Symfony\Component\HttpFoundation\RedirectResponse;
16+
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
19+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
20+
21+
class AccessAction
22+
{
23+
24+
/**
25+
* @var RendererInterface
26+
*/
27+
private $view;
28+
29+
/**
30+
* @var Validator
31+
*/
32+
private $validator;
33+
34+
/**
35+
* @var AuthenticationManagerInterface
36+
*/
37+
private $auth;
38+
39+
/**
40+
* AccessAction constructor.
41+
*
42+
* @param RendererInterface $view
43+
* @param Validator $validator
44+
* @param AuthenticationManagerInterface $auth
45+
*/
46+
public function __construct(RendererInterface $view, Validator $validator, AuthenticationManagerInterface $auth)
47+
{
48+
$this->view = $view;
49+
$this->validator = $validator;
50+
$this->auth = $auth;
51+
}
52+
53+
/**
54+
* @param Request $request
55+
* @return string
56+
*/
57+
public function loginForm(Request $request)
58+
{
59+
if ($this->auth->authenticate($request)) {
60+
return new RedirectResponse('/');
61+
}
62+
63+
return new Response($this->view->render('@auth/login'));
64+
}
65+
66+
/**
67+
* @param Request $request
68+
* @param UrlGeneratorInterface $router
69+
* @return RedirectResponse
70+
*/
71+
public function attemptLogin(Request $request, UrlGeneratorInterface $router)
72+
{
73+
$credentials = $this->validate($request->request->all())->getValidData();
74+
$remember = in_array($credentials['remember'], ['on', 1]);
75+
/** @var FlashBagInterface $flash */
76+
$flash = $request->getSession()->getFlashBag();
77+
78+
if ($this->auth->login($request, $credentials, $remember)) {
79+
$flash->add('info', "You've been successfully logged on");
80+
$path = $request->getSession()->get('auth.referer', '/');
81+
$request->getSession()->remove('auth.referer');
82+
83+
return new RedirectResponse($path, 301);
84+
}
85+
86+
$flash->add('error', "Invalid credentials");
87+
return new RedirectResponse($router->generate('auth_login'));
88+
}
89+
90+
/**
91+
* @param array $input
92+
* @return Validation
93+
*/
94+
private function validate(array $input)
95+
{
96+
return $this->validator
97+
->validate($input, [
98+
'login' => 'required',
99+
'password' => 'required',
100+
'remember' => 'default:off|in:on,off'
101+
]);
102+
}
103+
104+
/**
105+
* @param Request $request
106+
* @param UrlGeneratorInterface $router
107+
* @return RedirectResponse
108+
*/
109+
public function logout(Request $request, UrlGeneratorInterface $router)
110+
{
111+
$this->auth->logout($request);
112+
$request->getSession()
113+
->getFlashBag()
114+
->add('success', "You've successfully been logged out");
115+
116+
return new RedirectResponse($router->generate('auth_login'));
117+
}
118+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: User
5+
* Date: 03/02/2019
6+
* Time: 04:49
7+
*/
8+
9+
namespace App\AuthModule;
10+
11+
use Keiryo\Renderer\RendererInterface;
12+
use Steins\Bundle\Extension\AbstractModule;
13+
use Steins\Bundle\Routing\RouteCollection;
14+
15+
class AuthModuleProvider extends AbstractModule
16+
{
17+
/**
18+
* Get module short name for searching in config
19+
*
20+
* @return string
21+
*/
22+
public function getName(): string
23+
{
24+
return 'auth';
25+
}
26+
27+
/**
28+
* @param RendererInterface $renderer
29+
*/
30+
public function registerTemplates(RendererInterface $renderer)
31+
{
32+
$renderer->addPath(__DIR__ . '/views', 'auth');
33+
}
34+
35+
/**
36+
* @param RouteCollection $collection
37+
*/
38+
public function registerRoutes(RouteCollection $collection): void
39+
{
40+
$collection->add('auth_login', '/login')
41+
->controller('App\AuthModule\Actions\AccessAction:loginForm')
42+
->methods(['GET']);
43+
$collection->add('POST_login', '/login')
44+
->controller('App\AuthModule\Actions\AccessAction:attemptLogin')
45+
->methods(['POST']);
46+
$collection->add('auth_logout', '/logout')
47+
->controller('App\AuthModule\Actions\AccessAction:logout')
48+
->methods(['POST']);
49+
}
50+
51+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: User
5+
* Date: 14/07/2019
6+
* Time: 20:50
7+
*/
8+
9+
namespace App\AuthModule\Command;
10+
11+
use App\AuthModule\Entity\User;
12+
use App\AuthModule\Provider\DatabaseUserProvider;
13+
use Keiryo\Validation\Validator;
14+
use Keiryo\Helper\Str;
15+
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
17+
use Symfony\Component\Console\Input\InputArgument;
18+
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Question\Question;
21+
22+
class CreateUserCommand extends Command
23+
{
24+
25+
/**
26+
* @var DatabaseUserProvider
27+
*/
28+
private $provider;
29+
/**
30+
* @var Validator
31+
*/
32+
private $validator;
33+
34+
public function __construct(DatabaseUserProvider $provider, Validator $validator)
35+
{
36+
parent::__construct('auth:user-create');
37+
$this->provider = $provider;
38+
$this->validator = $validator;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function configure()
45+
{
46+
$this->setDefinition([
47+
new InputArgument('username', InputArgument::REQUIRED, 'User username'),
48+
new InputArgument('email', InputArgument::REQUIRED, 'User email'),
49+
new InputArgument('password', InputArgument::REQUIRED, 'User password'),
50+
])
51+
->setDescription('Create a new admin user');
52+
}
53+
54+
/**
55+
* @param InputInterface $input
56+
* @param OutputInterface $output
57+
*/
58+
protected function interact(InputInterface $input, OutputInterface $output)
59+
{
60+
$helper = new SymfonyQuestionHelper();
61+
$username = $helper->ask($input, $output, (new Question('Username'))
62+
->setValidator(function (string $username) {
63+
$validation = $this->validator
64+
->validate(compact('username'), ['username' => 'alpha_num']);
65+
return $validation->passes()
66+
? $validation->getValidData()['username']
67+
: null;
68+
}));
69+
70+
$email = $helper->ask($input, $output, (new Question('Email'))
71+
->setValidator(function (string $email) {
72+
$validation = $this->validator
73+
->validate(compact('email'), ['email' => 'email']);
74+
return $validation->passes()
75+
? $validation->getValidData()['email']
76+
: null;
77+
}));
78+
79+
$password = $helper->ask($input, $output, (new Question('Password'))
80+
->setHidden(true)
81+
->setValidator(function (string $password) {
82+
$validation = $this->validator
83+
->validate(compact('password'), ['password' => 'min:8']);
84+
return $validation->passes()
85+
? $validation->getValidData()['password']
86+
: null;
87+
}));
88+
89+
$helper->ask($input, $output, (new Question('Confirm password'))
90+
->setHidden(true)
91+
->setValidator(function (string $confirm) use ($password) {
92+
return $password === $confirm ? true : null;
93+
}));
94+
95+
$input->setArgument('username', $username);
96+
$input->setArgument('email', $email);
97+
$input->setArgument('password', password_hash($password, PASSWORD_BCRYPT));
98+
}
99+
100+
/**
101+
* @param InputInterface $input
102+
* @param OutputInterface $output
103+
* @return int|void|null
104+
* @throws \Exception
105+
*/
106+
protected function execute(InputInterface $input, OutputInterface $output)
107+
{
108+
$token = Str::random(28);
109+
$user = new User($name = $input->getArgument('username'), $token, $input->getArgument('password'));
110+
$user->setEmail($input->getArgument('email'));
111+
$output->writeln("Creating user '$name'");
112+
$this->provider->insert($user);
113+
$output->writeln("User '$name' successfully created");
114+
}
115+
}

app/AuthModule/Entity/User.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: User
5+
* Date: 14/07/2019
6+
* Time: 21:21
7+
*/
8+
9+
namespace App\AuthModule\Entity;
10+
11+
use Keiryo\Security\Authentication\User\User as BaseUser;
12+
13+
class User extends BaseUser
14+
{
15+
16+
/**
17+
* @var string
18+
*/
19+
protected $email;
20+
21+
/**
22+
* @return string
23+
*/
24+
public function getEmail(): string
25+
{
26+
return $this->email;
27+
}
28+
29+
/**
30+
* @param string $email
31+
*/
32+
public function setEmail(string $email): void
33+
{
34+
$this->email = $email;
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: User
5+
* Date: 14/07/2019
6+
* Time: 21:13
7+
*/
8+
9+
namespace App\AuthModule\Provider;
10+
11+
use App\AuthModule\Entity\User;
12+
use Keiryo\Security\Authentication\Provider\DatabaseUserProvider as BaseUserProvider;
13+
14+
class DatabaseUserProvider extends BaseUserProvider
15+
{
16+
17+
/**
18+
* @param User $user
19+
* @return int
20+
*/
21+
public function insert(User $user)
22+
{
23+
return $this->builder
24+
->insert([
25+
'username' => $user->getUsername(),
26+
'session_token' => $user->getToken(),
27+
'password' => $user->getPassword(),
28+
'email' => $user->getEmail()
29+
]);
30+
}
31+
}

app/AuthModule/views/login.twig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% extends 'layout.twig' %}
2+
3+
{% block title %}Login{% endblock %}
4+
5+
{% block content %}
6+
<div class="card flex-centered">
7+
<div class="card-header">
8+
<div class="panel-title h2">Login</div>
9+
</div>
10+
{{ form_open({action: url('auth_login'), method: 'POST', class: 'col-5'}) }}
11+
<div class="card-body">
12+
{% for error in app.flashes('error') %}
13+
<div class="text-error text-center">{{ error }}</div>
14+
{% endfor %}
15+
{{ csrf_field() }}
16+
{{ form_widget('email', 'login', 'Email : ') }}
17+
{{ form_widget('password', 'password', 'Password : ') }}
18+
{{ form_widget('checkbox', 'remember', 'Remember me', null, {switch: true}) }}
19+
</div>
20+
<div class="card-footer">
21+
{{ form_submit('Login', {class: 'flex-centered'}) }}
22+
</div>
23+
{{ form_close() }}
24+
</div>
25+
{% endblock %}

0 commit comments

Comments
 (0)