Commit 25203b89 authored by Emma's avatar Emma 😻

page for adding moderators

parent b36cd46d
......@@ -6,6 +6,7 @@ use Raddit\AppBundle\Entity\Forum;
use Raddit\AppBundle\Entity\Moderator;
use Raddit\AppBundle\Entity\User;
use Raddit\AppBundle\Form\ForumType;
use Raddit\AppBundle\Form\ModeratorType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
......@@ -166,4 +167,39 @@ final class ForumController extends Controller {
'moderators' => $moderators,
]);
}
/**
* @ParamConverter("forum", options={"mapping": {"forum_name": "name"}})
* @Security("is_granted('ROLE_ADMIN')")
*
* @param Forum $forum
* @param Request $request
*
* @return Response
*/
public function addModeratorAction(Forum $forum, Request $request) {
$moderator = new Moderator();
$moderator->setForum($forum);
$form = $this->createForm(ModeratorType::class, $moderator);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($moderator);
$em->flush();
$this->addFlash('success', 'add_moderator.notice');
return $this->redirectToRoute('raddit_app_forum_moderators', [
'forum_name' => $forum->getName(),
]);
}
return $this->render('@RadditApp/add_moderator.html.twig', [
'form' => $form->createView(),
'forum' => $forum,
]);
}
}
......@@ -13,7 +13,6 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
/**
......
......@@ -3,10 +3,14 @@
namespace Raddit\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity()
* @ORM\Table(name="moderators")
*
* @UniqueEntity(fields={"forum", "user"}, errorPath="user")
*/
class Moderator {
/**
......@@ -30,6 +34,8 @@ class Moderator {
* @ORM\JoinColumn(nullable=false)
* @ORM\ManyToOne(targetEntity="User", inversedBy="moderatorTokens")
*
* @Assert\NotNull(message="No such user.")
*
* @var User
*/
private $user;
......
......@@ -3,10 +3,9 @@
namespace Raddit\AppBundle\Form;
use Raddit\AppBundle\Entity\Ban;
use Raddit\AppBundle\Entity\User;
use Raddit\AppBundle\Form\DataTransformer\UserTransformer;
use Raddit\AppBundle\Repository\UserRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
......@@ -42,16 +41,9 @@ final class BanType extends AbstractType {
'label' => 'ban_form.ban',
]);
$builder->get('user')->addModelTransformer(new CallbackTransformer(
function ($user) {
return $user instanceof User ? $user->getUsername() : null;
},
function ($username) {
return strlen($username) > 0
? $this->userRepository->loadUserByUsername($username)
: null;
}
));
$builder->get('user')->addModelTransformer(
new UserTransformer($this->userRepository)
);
}
/**
......
<?php
namespace Raddit\AppBundle\Form\DataTransformer;
use Raddit\AppBundle\Entity\User;
use Raddit\AppBundle\Repository\UserRepository;
use Symfony\Component\Form\DataTransformerInterface;
/**
* Transforms usernames into {@link User} objects and vice versa.
*/
class UserTransformer implements DataTransformerInterface {
/**
* @var UserRepository
*/
private $userRepository;
public function __construct(UserRepository $userRepository) {
$this->userRepository = $userRepository;
}
/**
* {@inheritdoc}
*/
public function transform($value) {
if ($value instanceof User) {
return $value->getUsername();
}
return null;
}
/**
* {@inheritdoc}
*/
public function reverseTransform($value) {
if (strlen($value) > 0) {
return $this->userRepository->loadUserByUsername($value);
}
return null;
}
}
<?php
namespace Raddit\AppBundle\Form;
use Raddit\AppBundle\Entity\Moderator;
use Raddit\AppBundle\Form\DataTransformer\UserTransformer;
use Raddit\AppBundle\Repository\UserRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ModeratorType extends AbstractType {
/**
* @var UserRepository
*/
private $userRepository;
public function __construct(UserRepository $userRepository) {
$this->userRepository = $userRepository;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('user', TextType::class)
->add('submit', SubmitType::class)
;
$builder->get('user')->addModelTransformer(
new UserTransformer($this->userRepository)
);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'data_class' => Moderator::class,
'label_format' => 'moderator_form.%name%',
]);
}
}
......@@ -52,3 +52,8 @@ raddit_app_forum_moderators:
defaults: { _controller: RadditAppBundle:Forum:moderators }
path: /{forum_name}/moderators
methods: [GET]
raddit_app_add_moderator:
defaults: { _controller: RadditAppBundle:Forum:addModerator }
path: /{forum_name}/add_moderator
methods: [GET, POST]
......@@ -18,6 +18,12 @@ services:
tags:
- { name: form.type }
Raddit\AppBundle\Form\ModeratorType:
class: Raddit\AppBundle\Form\ModeratorType
arguments: ['@Raddit\AppBundle\Repository\UserRepository']
tags:
- { name: form.type }
Raddit\AppBundle\Form\SubmissionType:
class: Raddit\AppBundle\Form\SubmissionType
arguments: ["@security.authorization_checker"]
......
add_moderator:
title: Add moderator to %forum%
notice: The user has been promoted to moderator.
ban_add:
title: Ban an IP address
banned_notice: The IP address has been banned.
......@@ -69,6 +73,7 @@ forum:
subscribe: Subscribe
unsubscribe: Unsubscribe
subscriber_count: '{0} No subscribers|{1} %count% subscriber|[1,Inf[ %count% subscribers'
add_moderator: Add moderator
forum_form:
name: Name
......@@ -109,6 +114,10 @@ login_form:
markdown_type:
help: Syntax reference
moderator_form:
user: Username
submit: Add as moderator
pagination:
next: Next
previous: Previous
......
add_moderator:
title: Legg til moderator for %forum%
notice: Brukeren har blitt lagt til som moderator.
ban_add:
title: Utesteng en IP-adresse
banned_notice: IP-adressen ble utestengt.
......@@ -69,6 +73,7 @@ forum:
subscribe: Abonnere
unsubscribe: Avabonnere
subscriber_count: '{0} Ingen abonnenter|{1} %count% abonnent|[1,Inf[ %count% abonnenter'
add_moderator: Legg til moderator
forum_form:
name: Navn
......@@ -109,6 +114,10 @@ login_form:
markdown_type:
help: Hjelp med formatering
moderator_form:
user: Brukernavn
submit: Legg til som moderator
pagination:
next: Neste
previous: Forrige
......
'The comment must not be empty.': 'The comment must not be empty.'
'The name must contain only contain letters, numbers, and underscores.': 'The name must contain only contain letters, numbers, and underscores.'
'No such user.': 'No such user.'
'The comment must not be empty.': 'Kommentaren ikke være tom.'
'The name must contain only contain letters, numbers, and underscores.': 'Navnet inneholde kun bokstaver, tall og understreker.'
'No such user.': 'Ingen slik bruker.'
{% extends '@RadditApp/forum_base.html.twig' %}
{% form_theme form '@RadditApp/forms/standard_form_theme.html.twig' %}
{% block page_classes %}
add-moderators-page
{% endblock %}
{% block title %}
{{ 'add_moderator.title'|trans({'%forum%': forum.name}) }}
{% endblock %}
{% block body %}
<h1>{{ block('title') }}</h1>
{{ form(form) }}
{% endblock %}
......@@ -32,6 +32,9 @@
<h1>{{ 'forum.manage'|trans }}</h1>
<ul>
<li><a href="{{ path('raddit_app_edit_forum', {forum_name: forum.name}) }}">{{ 'forum.edit'|trans }}</a></li>
{% if is_granted('ROLE_ADMIN') %}
<li><a href="{{ path('raddit_app_add_moderator', {forum_name: forum.name}) }}">{{ 'forum.add_moderator'|trans }}</a></li>
{% endif %}
</ul>
</section>
{% endif %}
......
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