Skip to content
Open

HW3 #302

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: 2 additions & 0 deletions config/packages/easy_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ easy_admin:
- 'username'
- { property: 'roles', template: 'admin/user/roles-type-read.html.twig' }
- { property: 'homepage', type: 'url' }
- { property: 'linkedin', type: 'url' }
- { property: 'password', template: 'admin/user/password-read.html.twig' }
form:
fields:
- { property: 'email', type: 'email' }
- { property: 'roles', type: 'collection' }
- { property: 'plainPassword', type: 'password' }
- { property: 'homepage', type: 'url' }
- { property: 'linkedin', type: 'url' }

user:
name_property_path: "email"
70 changes: 31 additions & 39 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php

namespace App\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
Expand All @@ -18,55 +16,50 @@ class User implements UserInterface
* @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;

/**
* @var null|\DateTime When password was changed
* @var null|DateTime When password was changed
* @ORM\Column(type="datetime", nullable=true)
*/
private $passwordChanged = null;

/** @var bool Does plain password was entered */
private $passwordWasChanged = false;

/**
* @var null|string Link to Personal Website
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $homepage = "";
/**
* @var null|string Link to Personal Linkedin
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $linkedin = "";

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.
*
Expand All @@ -76,7 +69,6 @@ public function getUsername(): string
{
return (string)$this->email;
}

/**
* @see UserInterface
*/
Expand All @@ -85,66 +77,54 @@ 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;
}

/** Virtual method for EasyAdminBundle */
public function getPlainPassword(): string
{
return ''; // We store passwords hashed, it is impossible to regenerate back
}

/** Virtual method for EasyAdminBundle */
/** Virtual metFixedhod for EasyAdminBundle */
public function setPlainPassword($password): self
{
if (!$password) {
return $this; // For usability: Empty password means do not change password
}

$this->passwordWasChanged = true;
$hash = password_hash($password, PASSWORD_ARGON2I);
return $this->setPassword($hash);
}

/**
* @return bool
*/
public function isPasswordWasChanged(): bool
{
return $this->passwordWasChanged;
}

/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
return (string)$this->password;
}

public function setPassword(string $password): self
{
$this->password = $password;

return $this;
}

/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}

/**
* @see UserInterface
*/
Expand All @@ -153,38 +133,50 @@ public function eraseCredentials()
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}

/**
* @return \DateTime|null
* @return DateTime|null
*/
public function getPasswordChanged(): ?\DateTime
public function getPasswordChanged(): ?DateTime
{
return $this->passwordChanged;
}

/**
* @param \DateTime|null $passwordChanged
* @param DateTime|null $passwordChanged
*/
public function setPasswordChanged(?\DateTime $passwordChanged): void
public function setPasswordChanged(?DateTime $passwordChanged): void
{
$this->passwordChanged = $passwordChanged;
}

/**
* @return string|null
*/
public function getHomepage(): ?string
{
return $this->homepage;
}

/**
* @param string|null $homepage
* @return User
*/
public function setHomepage(?string $homepage): self
{
$this->homepage = $homepage;

return $this;
}
}
/**
* @return string|null
*/
public function getLinkedin(): ?string
{
return $this->linkedin;
}
/**
* @param string|null $linkedin
* @return User
*/
public function setLinkedin(?string $linkedin): self
{
$this->linkedin = $linkedin;
return $this;
}
}
1 change: 1 addition & 0 deletions src/Form/RegistrationFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
],
])
->add('homepage', UrlType::class, ['required' => false])
->add('linkedin', UrlType::class, ['required' => false])
->add('agreeTerms', CheckboxType::class, [
'mapped' => false,
'constraints' => [
Expand Down
35 changes: 35 additions & 0 deletions src/Migrations/Version20191125133334.php
Original file line number Diff line number Diff line change
@@ -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 Version20191125133334 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->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE user ADD linkedin VARCHAR(255) DEFAULT NULL, CHANGE roles roles JSON NOT NULL, CHANGE homepage homepage VARCHAR(255) DEFAULT NULL, CHANGE password_changed password_changed DATETIME DEFAULT NULL');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE user DROP linkedin, CHANGE roles roles LONGTEXT NOT NULL COLLATE utf8mb4_bin, CHANGE password_changed password_changed DATETIME DEFAULT \'NULL\', CHANGE homepage homepage VARCHAR(255) DEFAULT \'NULL\' COLLATE utf8mb4_unicode_ci');
}
}
9 changes: 6 additions & 3 deletions templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
<li class="nav-item">
<a class="nav-link" href="{{ path('app_logout') }}">Atsijungti</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ path('easyadmin') }}">Administravimas</a>
</li>

{% if is_granted('ROLE_ADMIN') %}
<li class="nav-item">
<a class="nav-link" href="{{ path('easyadmin') }}">Administravimas</a>
</li>
{% endif %}

{% else %}

Expand Down
1 change: 1 addition & 0 deletions templates/registration/register.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{{ form_start(registrationForm) }}
{{ form_row(registrationForm.email) }}
{{ form_row(registrationForm.plainPassword) }}
{{ form_row(registrationForm.linkedin) }}
{{ form_row(registrationForm.homepage) }}
{{ form_row(registrationForm.agreeTerms) }}

Expand Down
3 changes: 2 additions & 1 deletion templates/security/profile.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<span class="badge badge-secondary">{{ role|lower }}</span>
{% endfor %}
<div class="mb-2">
<span class="badge badge-primary">Website</span> <a href="{{ user.homepage }}"></a>{{ user.homepage }}
<span class="badge badge-primary">Website</span> <a href="{{ user.homepage }}"></a>{{ user.homepage }}<br />
<span class="badge badge-primary">LinkedIn</span> <a href="{{ user.linkedin }}"></a>{{ user.linkedin }}
</div>
</div>
{% endblock %}