Skip to content

Commit 3cf18af

Browse files
committed
feature #457 Add fullName property to User entity (yceruto)
This PR was squashed before being merged into the master branch (closes #457). Discussion ---------- Add fullName property to User entity Implements #446 Others changes: * Show full author name in all templates (#439 (comment)) * Use "Jane Doe" instead of "Anna" [RFC] ! * Updated fixtures and user commands. | Posts Index | Post Comments | | --- | --- | | ![index](https://cloud.githubusercontent.com/assets/2028198/22749352/fe23ceb6-edfa-11e6-9667-a60d28c51c5c.png) | ![comments](https://cloud.githubusercontent.com/assets/2028198/22749387/2656157e-edfb-11e6-98ce-9cc7150cbb68.png) | Commits ------- 4f590dd Add fullName property to User entity
2 parents 40ae1a8 + 4f590dd commit 3cf18af

File tree

12 files changed

+76
-22
lines changed

12 files changed

+76
-22
lines changed

app/Resources/views/admin/blog/show.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

1313
<div class="well">

app/Resources/views/blog/index.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

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

1919
{{ post.summary|md2html }}

app/Resources/views/blog/post_show.html.twig

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

1313
{{ post.content|md2html }}
@@ -39,7 +39,7 @@
3939
<div class="row post-comment">
4040
<a name="comment_{{ comment.id }}"></a>
4141
<h4 class="col-sm-3">
42-
<strong>{{ comment.author.email }}</strong> {{ 'post.commented_on'|trans }}
42+
<strong>{{ comment.author.fullName }}</strong> {{ 'post.commented_on'|trans }}
4343
{# it's not mandatory to set the timezone in localizeddate(). This is done to
4444
avoid errors when the 'intl' PHP extension is not available and the application
4545
is forced to use the limited "intl polyfill", which only supports UTC and GMT #}

app/Resources/views/security/login.html.twig

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<td><code>ROLE_USER</code> ({{ 'help.role_user'|trans }})</td>
5454
</tr>
5555
<tr>
56-
<td>anna_admin</td>
56+
<td>jane_admin</td>
5757
<td>kitten</td>
5858
<td><code>ROLE_ADMIN</code> ({{ 'help.role_admin'|trans }})</td>
5959
</tr>
@@ -97,7 +97,7 @@
9797
// in a real application, hardcoding the user/password would be idiotic
9898
// but for the demo application it's very convenient to do so
9999
if (!usernameEl.val() && !passwordEl.val()) {
100-
usernameEl.val('anna_admin');
100+
usernameEl.val('jane_admin');
101101
passwordEl.val('kitten');
102102
}
103103
});

src/AppBundle/Command/AddUserCommand.php

+35-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ protected function configure()
6161
->addArgument('username', InputArgument::OPTIONAL, 'The username of the new user')
6262
->addArgument('password', InputArgument::OPTIONAL, 'The plain password of the new user')
6363
->addArgument('email', InputArgument::OPTIONAL, 'The email of the new user')
64+
->addArgument('full-name', InputArgument::OPTIONAL, 'The full name of the new user')
6465
->addOption('admin', null, InputOption::VALUE_NONE, 'If set, the user is created as an administrator')
6566
;
6667
}
@@ -90,7 +91,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
9091
*/
9192
protected function interact(InputInterface $input, OutputInterface $output)
9293
{
93-
if (null !== $input->getArgument('username') && null !== $input->getArgument('password') && null !== $input->getArgument('email')) {
94+
if (null !== $input->getArgument('username') && null !== $input->getArgument('password') && null !== $input->getArgument('email') && null !== $input->getArgument('full-name')) {
9495
return;
9596
}
9697

@@ -163,6 +164,19 @@ protected function interact(InputInterface $input, OutputInterface $output)
163164
} else {
164165
$output->writeln(' > <info>Email</info>: '.$email);
165166
}
167+
168+
// Ask for the full name if it's not defined
169+
$fullName = $input->getArgument('full-name');
170+
if (null === $fullName) {
171+
$question = new Question(' > <info>Full Name</info>: ');
172+
$question->setValidator([$this, 'fullNameValidator']);
173+
$question->setMaxAttempts(self::MAX_ATTEMPTS);
174+
175+
$fullName = $console->ask($input, $output, $question);
176+
$input->setArgument('full-name', $fullName);
177+
} else {
178+
$output->writeln(' > <info>Full Name</info>: '.$fullName);
179+
}
166180
}
167181

168182
/**
@@ -176,13 +190,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
176190
$username = $input->getArgument('username');
177191
$plainPassword = $input->getArgument('password');
178192
$email = $input->getArgument('email');
193+
$fullName = $input->getArgument('full-name');
179194
$isAdmin = $input->getOption('admin');
180195

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

184199
// create the user and encode its password
185200
$user = new User();
201+
$user->setFullName($fullName);
186202
$user->setUsername($username);
187203
$user->setEmail($email);
188204
$user->setRoles([$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER']);
@@ -244,7 +260,22 @@ public function emailValidator($email)
244260
return $email;
245261
}
246262

247-
private function validateUserData($username, $plainPassword, $email)
263+
/**
264+
* This internal method should be private, but it's declared as public to
265+
* maintain PHP 5.3 compatibility when using it in a callback.
266+
*
267+
* @internal
268+
*/
269+
public function fullNameValidator($fullName)
270+
{
271+
if (empty($fullName)) {
272+
throw new \Exception('The full name can not be empty.');
273+
}
274+
275+
return $fullName;
276+
}
277+
278+
private function validateUserData($username, $plainPassword, $email, $fullName)
248279
{
249280
$userRepository = $this->entityManager->getRepository(User::class);
250281

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

262294
// check if a user with the same email already exists.
263295
$existingEmail = $userRepository->findOneBy(['email' => $email]);

src/AppBundle/Command/ListUsersCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9393

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

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

108108
$table = new Table($bufferedOutput);
109109
$table
110-
->setHeaders(['ID', 'Username', 'Email', 'Roles'])
110+
->setHeaders(['ID', 'Full Name', 'Username', 'Email', 'Roles'])
111111
->setRows($usersAsPlainArrays)
112112
;
113113
$table->render();

src/AppBundle/DataFixtures/ORM/PostFixtures.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function load(ObjectManager $manager)
5454
// "References" are the way to share objects between fixtures defined
5555
// in different files. This reference has been added in the UserFixtures
5656
// file and it contains an instance of the User entity.
57-
$post->setAuthor($this->getReference('anna-admin'));
57+
$post->setAuthor($this->getReference('jane-admin'));
5858
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));
5959

6060
$this->addRandomTags($post);

src/AppBundle/DataFixtures/ORM/UserFixtures.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,21 @@ public function load(ObjectManager $manager)
4040
{
4141
$passwordEncoder = $this->container->get('security.password_encoder');
4242

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

5556
$johnUser = new User();
57+
$johnUser->setFullName('John Doe');
5658
$johnUser->setUsername('john_user');
5759
$johnUser->setEmail('[email protected]');
5860
$encodedPassword = $passwordEncoder->encodePassword($johnUser, 'kitten');

src/AppBundle/Entity/User.php

+20
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class User implements UserInterface
2929
*/
3030
private $id;
3131

32+
/**
33+
* @var string
34+
*
35+
* @ORM\Column(type="string")
36+
*/
37+
private $fullName;
38+
3239
/**
3340
* @var string
3441
*
@@ -62,6 +69,19 @@ public function getId()
6269
return $this->id;
6370
}
6471

72+
/**
73+
* @param string $fullName
74+
*/
75+
public function setFullName($fullName)
76+
{
77+
$this->fullName = $fullName;
78+
}
79+
80+
public function getFullName()
81+
{
82+
return $this->fullName;
83+
}
84+
6585
public function getUsername()
6686
{
6787
return $this->username;

tests/AppBundle/Controller/Admin/BlogControllerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getUrlsForRegularUsers()
5959
public function testAdminUsers($httpMethod, $url, $statusCode)
6060
{
6161
$client = static::createClient([], [
62-
'PHP_AUTH_USER' => 'anna_admin',
62+
'PHP_AUTH_USER' => 'jane_admin',
6363
'PHP_AUTH_PW' => 'kitten',
6464
]);
6565

@@ -78,7 +78,7 @@ public function getUrlsForAdminUsers()
7878
public function testBackendHomepage()
7979
{
8080
$client = static::createClient([], [
81-
'PHP_AUTH_USER' => 'anna_admin',
81+
'PHP_AUTH_USER' => 'jane_admin',
8282
'PHP_AUTH_PW' => 'kitten',
8383
]);
8484

var/data/blog.sqlite

-84 KB
Binary file not shown.

var/data/blog_test.sqlite

-84 KB
Binary file not shown.

0 commit comments

Comments
 (0)