Commit 2c0bcacd authored by Emma's avatar Emma 😻

check for uniqueness when adding moderators

parent 8ac817b4
Pipeline #12488699 passed with stage
in 7 minutes and 6 seconds
......@@ -271,12 +271,12 @@ final class ForumController extends Controller {
* @return Response
*/
public function addModeratorAction(EntityManager $em, Forum $forum, Request $request) {
$data = new ModeratorData();
$data = new ModeratorData($forum);
$form = $this->createForm(ModeratorType::class, $data);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$forum->addModerator($data->toModerator($forum));
$forum->addModerator($data->toModerator());
$em->flush();
......
......@@ -4,16 +4,12 @@ namespace Raddit\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity()
* @ORM\Table(name="moderators", uniqueConstraints={
* @ORM\UniqueConstraint(name="moderator_forum_user_idx", columns={"forum_id", "user_id"})
* })
*
* @UniqueEntity(fields={"forum", "user"}, errorPath="user")
*/
class Moderator {
/**
......@@ -36,8 +32,6 @@ class Moderator {
* @ORM\JoinColumn(nullable=false)
* @ORM\ManyToOne(targetEntity="User", inversedBy="moderatorTokens")
*
* @Assert\NotNull(message="No such user.")
*
* @var User
*/
private $user;
......
......@@ -5,9 +5,23 @@ namespace Raddit\AppBundle\Form\Model;
use Raddit\AppBundle\Entity\Forum;
use Raddit\AppBundle\Entity\Moderator;
use Raddit\AppBundle\Entity\User;
use Raddit\AppBundle\Validator\Constraints\Unique;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Unique({"forum", "user"}, entityClass="Raddit\AppBundle\Entity\Moderator",
* message="That user is already a moderator.", errorPath="user")
*/
class ModeratorData {
/**
* @var Forum
*/
private $forum;
public function __construct(Forum $forum) {
$this->forum = $forum;
}
/**
* @Assert\NotBlank()
*
......@@ -15,8 +29,12 @@ class ModeratorData {
*/
private $user;
public function toModerator(Forum $forum): Moderator {
return new Moderator($forum, $this->user);
public function toModerator(): Moderator {
return new Moderator($this->forum, $this->user);
}
public function getForum(): Forum {
return $this->forum;
}
/**
......
......@@ -17,3 +17,4 @@
'A forum by that name already exists.': 'A forum by that name already exists.'
'No such theme.': 'No such theme.'
'That theme cannot be extended.': 'That theme cannot be extended.'
'That user is already a moderator.': 'That user is already a moderator.'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment