Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Resources/views/admin/blog/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<p class="post-metadata">
<span class="metadata"><i class="fa fa-calendar"></i> {{ post.publishedAt|localizeddate('long', 'medium', null, 'UTC') }}</span>
<span class="metadata"><i class="fa fa-user"></i> {{ post.author.email }}</span>
<span class="metadata"><i class="fa fa-user"></i> {{ post.author.fullName }}</span>
</p>

<div class="well">
Expand Down
2 changes: 1 addition & 1 deletion app/Resources/views/blog/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<p class="post-metadata">
<span class="metadata"><i class="fa fa-calendar"></i> {{ post.publishedAt|localizeddate('long', 'medium', null, 'UTC') }}</span>
<span class="metadata"><i class="fa fa-user"></i> {{ post.author.email }}</span>
<span class="metadata"><i class="fa fa-user"></i> {{ post.author.fullName }}</span>
</p>

{{ post.summary|md2html }}
Expand Down
4 changes: 2 additions & 2 deletions app/Resources/views/blog/post_show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<p class="post-metadata">
<span class="metadata"><i class="fa fa-calendar"></i> {{ post.publishedAt|localizeddate('long', 'medium', null, 'UTC') }}</span>
<span class="metadata"><i class="fa fa-user"></i> {{ post.author.email }}</span>
<span class="metadata"><i class="fa fa-user"></i> {{ post.author.fullName }}</span>
</p>

{{ post.content|md2html }}
Expand Down Expand Up @@ -39,7 +39,7 @@
<div class="row post-comment">
<a name="comment_{{ comment.id }}"></a>
<h4 class="col-sm-3">
<strong>{{ comment.author.email }}</strong> {{ 'post.commented_on'|trans }}
<strong>{{ comment.author.fullName }}</strong> {{ 'post.commented_on'|trans }}
{# it's not mandatory to set the timezone in localizeddate(). This is done to
avoid errors when the 'intl' PHP extension is not available and the application
is forced to use the limited "intl polyfill", which only supports UTC and GMT #}
Expand Down
4 changes: 2 additions & 2 deletions app/Resources/views/security/login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<td><code>ROLE_USER</code> ({{ 'help.role_user'|trans }})</td>
</tr>
<tr>
<td>anna_admin</td>
<td>jane_admin</td>
<td>kitten</td>
<td><code>ROLE_ADMIN</code> ({{ 'help.role_admin'|trans }})</td>
</tr>
Expand Down Expand Up @@ -97,7 +97,7 @@
// in a real application, hardcoding the user/password would be idiotic
// but for the demo application it's very convenient to do so
if (!usernameEl.val() && !passwordEl.val()) {
usernameEl.val('anna_admin');
usernameEl.val('jane_admin');
passwordEl.val('kitten');
}
});
Expand Down
38 changes: 35 additions & 3 deletions src/AppBundle/Command/AddUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected function configure()
->addArgument('username', InputArgument::OPTIONAL, 'The username of the new user')
->addArgument('password', InputArgument::OPTIONAL, 'The plain password of the new user')
->addArgument('email', InputArgument::OPTIONAL, 'The email of the new user')
->addArgument('full-name', InputArgument::OPTIONAL, 'The full name of the new user')
->addOption('admin', null, InputOption::VALUE_NONE, 'If set, the user is created as an administrator')
;
}
Expand Down Expand Up @@ -90,7 +91,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (null !== $input->getArgument('username') && null !== $input->getArgument('password') && null !== $input->getArgument('email')) {
if (null !== $input->getArgument('username') && null !== $input->getArgument('password') && null !== $input->getArgument('email') && null !== $input->getArgument('full-name')) {
return;
}

Expand Down Expand Up @@ -163,6 +164,19 @@ protected function interact(InputInterface $input, OutputInterface $output)
} else {
$output->writeln(' > <info>Email</info>: '.$email);
}

// Ask for the full name if it's not defined
$fullName = $input->getArgument('full-name');
if (null === $fullName) {
$question = new Question(' > <info>Full Name</info>: ');
$question->setValidator([$this, 'fullNameValidator']);
$question->setMaxAttempts(self::MAX_ATTEMPTS);

$fullName = $console->ask($input, $output, $question);
$input->setArgument('full-name', $fullName);
} else {
$output->writeln(' > <info>Full Name</info>: '.$fullName);
}
}

/**
Expand All @@ -176,13 +190,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
$username = $input->getArgument('username');
$plainPassword = $input->getArgument('password');
$email = $input->getArgument('email');
$fullName = $input->getArgument('full-name');
$isAdmin = $input->getOption('admin');

// make sure to validate the user data is correct
$this->validateUserData($username, $plainPassword, $email);
$this->validateUserData($username, $plainPassword, $email, $fullName);

// create the user and encode its password
$user = new User();
$user->setFullName($fullName);
$user->setUsername($username);
$user->setEmail($email);
$user->setRoles([$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER']);
Expand Down Expand Up @@ -244,7 +260,22 @@ public function emailValidator($email)
return $email;
}

private function validateUserData($username, $plainPassword, $email)
/**
* This internal method should be private, but it's declared as public to
* maintain PHP 5.3 compatibility when using it in a callback.
*
* @internal
*/
public function fullNameValidator($fullName)
{
if (empty($fullName)) {
throw new \Exception('The full name can not be empty.');
}

return $fullName;
}

private function validateUserData($username, $plainPassword, $email, $fullName)
{
$userRepository = $this->entityManager->getRepository(User::class);

Expand All @@ -258,6 +289,7 @@ private function validateUserData($username, $plainPassword, $email)
// validate password and email if is not this input means interactive.
$this->passwordValidator($plainPassword);
$this->emailValidator($email);
$this->fullNameValidator($fullName);

// check if a user with the same email already exists.
$existingEmail = $userRepository->findOneBy(['email' => $email]);
Expand Down
4 changes: 2 additions & 2 deletions src/AppBundle/Command/ListUsersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

// Doctrine query returns an array of objects and we need an array of plain arrays
$usersAsPlainArrays = array_map(function (User $user) {
return [$user->getId(), $user->getUsername(), $user->getEmail(), implode(', ', $user->getRoles())];
return [$user->getId(), $user->getFullName(), $user->getUsername(), $user->getEmail(), implode(', ', $user->getRoles())];
}, $users);

// In your console commands you should always use the regular output type,
Expand All @@ -107,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$table = new Table($bufferedOutput);
$table
->setHeaders(['ID', 'Username', 'Email', 'Roles'])
->setHeaders(['ID', 'Full Name', 'Username', 'Email', 'Roles'])
->setRows($usersAsPlainArrays)
;
$table->render();
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/DataFixtures/ORM/PostFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function load(ObjectManager $manager)
// "References" are the way to share objects between fixtures defined
// in different files. This reference has been added in the UserFixtures
// file and it contains an instance of the User entity.
$post->setAuthor($this->getReference('anna-admin'));
$post->setAuthor($this->getReference('jane-admin'));
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));

$this->addRandomTags($post);
Expand Down
18 changes: 10 additions & 8 deletions src/AppBundle/DataFixtures/ORM/UserFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,21 @@ public function load(ObjectManager $manager)
{
$passwordEncoder = $this->container->get('security.password_encoder');

$annaAdmin = new User();
$annaAdmin->setUsername('anna_admin');
$annaAdmin->setEmail('[email protected]');
$annaAdmin->setRoles(['ROLE_ADMIN']);
$encodedPassword = $passwordEncoder->encodePassword($annaAdmin, 'kitten');
$annaAdmin->setPassword($encodedPassword);
$manager->persist($annaAdmin);
$janeAdmin = new User();
$janeAdmin->setFullName('Jane Doe');
$janeAdmin->setUsername('jane_admin');
$janeAdmin->setEmail('[email protected]');
$janeAdmin->setRoles(['ROLE_ADMIN']);
$encodedPassword = $passwordEncoder->encodePassword($janeAdmin, 'kitten');
$janeAdmin->setPassword($encodedPassword);
$manager->persist($janeAdmin);
// In case if fixture objects have relations to other fixtures, adds a reference
// to that object by name and later reference it to form a relation.
// See https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures
$this->addReference('anna-admin', $annaAdmin);
$this->addReference('jane-admin', $janeAdmin);

$johnUser = new User();
$johnUser->setFullName('John Doe');
$johnUser->setUsername('john_user');
$johnUser->setEmail('[email protected]');
$encodedPassword = $passwordEncoder->encodePassword($johnUser, 'kitten');
Expand Down
20 changes: 20 additions & 0 deletions src/AppBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class User implements UserInterface
*/
private $id;

/**
* @var string
*
* @ORM\Column(type="string")
*/
private $fullName;

/**
* @var string
*
Expand Down Expand Up @@ -62,6 +69,19 @@ public function getId()
return $this->id;
}

/**
* @param string $fullName
*/
public function setFullName($fullName)
{
$this->fullName = $fullName;
}

public function getFullName()
{
return $this->fullName;
}

public function getUsername()
{
return $this->username;
Expand Down
4 changes: 2 additions & 2 deletions tests/AppBundle/Controller/Admin/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getUrlsForRegularUsers()
public function testAdminUsers($httpMethod, $url, $statusCode)
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'anna_admin',
'PHP_AUTH_USER' => 'jane_admin',
'PHP_AUTH_PW' => 'kitten',
]);

Expand All @@ -78,7 +78,7 @@ public function getUrlsForAdminUsers()
public function testBackendHomepage()
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'anna_admin',
'PHP_AUTH_USER' => 'jane_admin',
'PHP_AUTH_PW' => 'kitten',
]);

Expand Down
Binary file modified var/data/blog.sqlite
Binary file not shown.
Binary file modified var/data/blog_test.sqlite
Binary file not shown.