Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
Merge pull request #78 from Senjutsumo/add-roles
Browse files Browse the repository at this point in the history
Add roles
  • Loading branch information
Senjutsuu authored Nov 27, 2018
2 parents a42f176 + b114769 commit ac8b9ea
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 3 deletions.
14 changes: 13 additions & 1 deletion DataTransferObject/BaseUserDataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SumoCoders\FrameworkMultiUserBundle\DataTransferObject;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces\UserDataTransferObject;
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User;
use SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser;
Expand All @@ -26,6 +28,9 @@ class BaseUserDataTransferObject implements UserDataTransferObject
/** @var User */
protected $user;

/** @var Collection|null */
public $roles;

public static function fromUser(User $user): UserDataTransferObject
{
$baseUserTransferObject = new static();
Expand All @@ -34,6 +39,7 @@ public static function fromUser(User $user): UserDataTransferObject
$baseUserTransferObject->userName = $user->getUsername();
$baseUserTransferObject->displayName = $user->getDisplayName();
$baseUserTransferObject->email = $user->getEmail();
$baseUserTransferObject->roles = $user->getRolesAsCollection();
if ($user->hasPlainPassword()) {
$baseUserTransferObject->plainPassword = $user->getPlainPassword();
}
Expand All @@ -53,7 +59,8 @@ public function getEntity(): User
$this->userName,
$this->plainPassword,
$this->displayName,
$this->email
$this->email,
$this->roles instanceof Collection ? $this->roles : new ArrayCollection()
);
}

Expand Down Expand Up @@ -81,4 +88,9 @@ public function getPlainPassword(): ?string
{
return $this->plainPassword;
}

public function getRoles(): ?Collection
{
return $this->roles;
}
}
3 changes: 3 additions & 0 deletions DataTransferObject/Interfaces/UserDataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces;

use Doctrine\Common\Collections\Collection;
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User;

interface UserDataTransferObject
Expand All @@ -19,4 +20,6 @@ public function getPlainPassword(): ?string;
public static function fromUser(User $user): self;

public function getEntity(): User;

public function getRoles(): ?Collection;
}
21 changes: 19 additions & 2 deletions Entity/BaseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SumoCoders\FrameworkMultiUserBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Serializable;
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces\UserDataTransferObject;
Expand Down Expand Up @@ -82,11 +84,19 @@ class BaseUser implements User, Serializable, EquatableInterface
*/
protected $plainPassword;

/**
* @var Collection
*
* @ORM\ManyToMany(targetEntity="SumoCoders\FrameworkMultiUserBundle\Entity\UserRole")
*/
protected $roles;

/**
* @param string $username
* @param string $plainPassword
* @param string $displayName
* @param string $email
* @param Collection $roles
* @param int $id
* @param PasswordResetToken $token
*/
Expand All @@ -95,13 +105,15 @@ public function __construct(
string $plainPassword,
string $displayName,
string $email,
Collection $roles,
int $id = null,
PasswordResetToken $token = null
) {
$this->username = $username;
$this->plainPassword = $plainPassword;
$this->displayName = $displayName;
$this->email = $email;
$this->roles = $roles;
$this->id = $id;

// set the default status to active
Expand All @@ -112,9 +124,14 @@ public function __construct(
}
}

public function getRoles(): array
public function getRoles(): ?array
{
return $this->roles->toArray();
}

public function getRolesAsCollection(): ?Collection
{
return ['ROLE_USER'];
return $this->roles;
}

public function getPassword(): string
Expand Down
94 changes: 94 additions & 0 deletions Entity/UserRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace SumoCoders\FrameworkMultiUserBundle\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\Role;

/**
* @ORM\Entity
* @ORM\Table
* @ORM\HasLifecycleCallbacks
*/
class UserRole extends Role
{
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;

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

/**
* @var DateTime
*
* @ORM\Column(type="datetime")
*/
private $createdOn;

/**
* @var DateTime
*
* @ORM\Column(type="datetime")
*/
private $editedOn;

public function __construct(string $roleName)
{
parent::__construct($roleName);

$this->roleName = $roleName;
}

public function getId(): int
{
return $this->id;
}

public function getRole(): string
{
return $this->roleName;
}

public function getCreatedOn(): DateTime
{
return $this->createdOn;
}

public function getEditedOn(): DateTime
{
return $this->editedOn;
}

/**
* @ORM\PrePersist
*/
public function onPrePersist(): void
{
$this->createdOn = new DateTime();
$this->editedOn = new DateTime();
}

/**
* @ORM\PreUpdate
*/
public function onPreUpdate(): void
{
$this->editedOn = new DateTime();
}

public function __toString(): string
{
return $this->roleName;
}
}
28 changes: 28 additions & 0 deletions Form/AddBaseUserType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
namespace SumoCoders\FrameworkMultiUserBundle\Form;

use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\BaseUserDataTransferObject;
use SumoCoders\FrameworkMultiUserBundle\Entity\UserRole;
use SumoCoders\FrameworkMultiUserBundle\Form\Interfaces\FormWithDataTransferObject;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;

class AddBaseUserType extends AbstractType implements FormWithDataTransferObject
{
/**
* @var TranslatorInterface
*/
private $translator;

public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
Expand All @@ -38,6 +51,21 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
[
'type' => PasswordType::class,
]
)->add(
'roles',
EntityType::class,
[
'attr' => [
'class' => 'select2',
],
'class' => UserRole::class,
'choice_label' => function (UserRole $userRole) {
return $this->translator->trans($userRole);
},
'required' => false,
'multiple' => true,
'placeholder' => '',
]
);
}

Expand Down
28 changes: 28 additions & 0 deletions Form/EditBaseUserType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
namespace SumoCoders\FrameworkMultiUserBundle\Form;

use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\BaseUserDataTransferObject;
use SumoCoders\FrameworkMultiUserBundle\Entity\UserRole;
use SumoCoders\FrameworkMultiUserBundle\Form\Interfaces\FormWithDataTransferObject;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;

class EditBaseUserType extends AbstractType implements FormWithDataTransferObject
{
/**
* @var TranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
Expand Down Expand Up @@ -41,6 +54,21 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'type' => PasswordType::class,
'required' => false,
]
)->add(
'roles',
EntityType::class,
[
'attr' => [
'class' => 'select2',
],
'class' => UserRole::class,
'choice_label' => function (UserRole $userRole) {
return $this->translator->trans($userRole);
},
'required' => false,
'multiple' => true,
'placeholder' => '',
]
);
}

Expand Down
4 changes: 4 additions & 0 deletions Resources/config/forms.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
services:
multi_user_form_add_base_user:
class: SumoCoders\FrameworkMultiUserBundle\Form\AddBaseUserType
arguments:
- "@translator"
tags:
- { name: form.type }

multi_user_form_edit_base_user:
class: SumoCoders\FrameworkMultiUserBundle\Form\EditBaseUserType
arguments:
- "@translator"
tags:
- { name: form.type }
1 change: 1 addition & 0 deletions Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ change:
second: repeat password
submit: Set new password
user:
roles: User roles
header:
title:
login: Login
1 change: 1 addition & 0 deletions Resources/translations/messages.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ change:
second: herhaal wachtwoord
submit: Stel nieuw wachtwoord in
user:
roles: Gebruikersrollen
header:
title:
login: Login
3 changes: 3 additions & 0 deletions User/InMemoryBaseUserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SumoCoders\FrameworkMultiUserBundle\User;

use Doctrine\Common\Collections\ArrayCollection;
use SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser;
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken;
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User;
Expand All @@ -20,6 +21,7 @@ public function __construct(EncoderFactory $encoderFactory)
'test',
'Wouter Sioen',
'[email protected]',
new ArrayCollection(['ROLE_USER']),
1
);
$user->encodePassword($encoderFactory->getEncoder($user));
Expand All @@ -31,6 +33,7 @@ public function __construct(EncoderFactory $encoderFactory)
'reset',
'reset',
'[email protected]',
new ArrayCollection(['ROLE_USER']),
2,
PasswordResetToken::generate()
);
Expand Down
3 changes: 3 additions & 0 deletions User/Interfaces/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SumoCoders\FrameworkMultiUserBundle\User\Interfaces;

use Doctrine\Common\Collections\Collection;
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces\UserDataTransferObject;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
Expand All @@ -21,4 +22,6 @@ public function hasPlainPassword(): bool;
public function getPlainPassword(): string;

public function encodePassword(PasswordEncoderInterface $encoder): void;

public function getRolesAsCollection(): ?Collection;
}

0 comments on commit ac8b9ea

Please sign in to comment.