added form to edit user roles #4 (#18)

added voter to handle user profile access
using Security annotation to handle access authorization
This commit is contained in:
Kevin Papst
2018-01-05 14:10:08 +01:00
committed by GitHub
parent ea030ca199
commit 96de95b7c9
8 changed files with 384 additions and 104 deletions

View File

@@ -15,11 +15,12 @@ use AppBundle\Entity\User;
use AppBundle\Form\Type\LanguageType;
use AppBundle\Form\Type\YesNoType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Defines the form used to create and manipulate Users.
* Defines the form used to edit the profile of a User.
*
* @author Kevin Papst <kevin@kevinpapst.de>
*/
@@ -33,20 +34,22 @@ class UserEditType extends AbstractType
{
$builder
// string - length 160
->add('alias', null, [
->add('alias', TextType::class, [
'label' => 'label.alias',
'required' => false,
])
// string - length 50
->add('title', null, [
//'attr' => ['autofocus' => true],
->add('title', TextType::class, [
'label' => 'label.title',
'required' => false,
])
// string - length 255
->add('avatar', null, [
->add('avatar', TextType::class, [
'label' => 'label.avatar',
'required' => false,
])
// string - length 160
->add('email', null, [
->add('email', TextType::class, [
'label' => 'label.email',
])
// string - length 5
@@ -57,7 +60,6 @@ class UserEditType extends AbstractType
->add('active', YesNoType::class, [
'label' => 'label.active',
])
// TODO roles - see ProfileController::getRoles()
;
}

View File

@@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Kimai package.
*
* (c) Kevin Papst <kevin@kevinpapst.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AppBundle\Form;
use AppBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Defines the form used to set roles for a User.
*
* @author Kevin Papst <kevin@kevinpapst.de>
*/
class UserRolesType extends AbstractType
{
/**
* @var string[]
*/
protected $roles = [];
/**
* UserRolesType constructor.
* @param string[] $roles
*/
public function __construct(array $roles)
{
$this->roles = $roles;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach ($this->roles as $key => $value) {
$roles[$key] = $key;
foreach ($value as $value2) {
$roles[$value2] = $value2;
}
}
$builder
// string[]
->add('roles', ChoiceType::class, [
'label' => 'label.roles',
'multiple' => true,
'choices' => $roles,
])
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
'csrf_protection' => true,
'csrf_field_name' => '_token',
'csrf_token_id' => 'edit_user_roles',
]);
}
}