|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Application\Form\Type; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Form\AbstractType; |
|
6
|
|
|
use Symfony\Component\Form\FormInterface; |
|
7
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
|
9
|
|
|
use Silex\Application; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Borut Balažek <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class UserType extends AbstractType |
|
15
|
|
|
{ |
|
16
|
|
|
protected $app; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param Application $app |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(Application $app) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->app = $app; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param FormBuilderInterface $builder |
|
28
|
|
|
* @param $options |
|
29
|
|
|
*/ |
|
30
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
31
|
|
|
{ |
|
32
|
|
|
$builder->add( |
|
33
|
|
|
'profile', |
|
34
|
|
|
new ProfileType(), |
|
35
|
|
|
array( |
|
36
|
|
|
'label' => false, |
|
37
|
|
|
) |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
$builder->add('username', 'text', array( |
|
41
|
|
|
'required' => false, |
|
42
|
|
|
)); |
|
43
|
|
|
$builder->add('email', 'email'); |
|
44
|
|
|
$builder->add('plainPassword', 'repeated', array( |
|
45
|
|
|
'type' => 'password', |
|
46
|
|
|
'first_name' => 'password', |
|
47
|
|
|
'second_name' => 'repeatPassword', |
|
48
|
|
|
'required' => false, |
|
49
|
|
|
'invalid_message' => 'The password fields must match.', |
|
50
|
|
|
'first_options' => array( |
|
51
|
|
|
'label' => 'New password', |
|
52
|
|
|
'attr' => array( |
|
53
|
|
|
'class' => 'password-meter-input', |
|
54
|
|
|
), |
|
55
|
|
|
), |
|
56
|
|
|
'second_options' => array( |
|
57
|
|
|
'label' => 'Repeat new Password', |
|
58
|
|
|
), |
|
59
|
|
|
)); |
|
60
|
|
|
|
|
61
|
|
|
$rolesChoices = $this->app['user_system_options']['roles']; |
|
62
|
|
|
if (!$this->app['security']->isGranted('ROLE_SUPER_ADMIN')) { |
|
63
|
|
|
// Only the super admin should be able to set other users to admins and super admins! |
|
64
|
|
|
unset($rolesChoices['ROLE_SUPER_ADMIN']); |
|
65
|
|
|
unset($rolesChoices['ROLE_ADMIN']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$builder->add('roles', 'choice', array( |
|
69
|
|
|
'required' => false, |
|
70
|
|
|
'multiple' => true, |
|
71
|
|
|
'expanded' => true, |
|
72
|
|
|
'choices' => $rolesChoices, |
|
73
|
|
|
)); |
|
74
|
|
|
|
|
75
|
|
|
$builder->add('enabled', 'checkbox', array( |
|
76
|
|
|
'label' => 'Is enabled?', |
|
77
|
|
|
'required' => false, |
|
78
|
|
|
)); |
|
79
|
|
|
$builder->add('locked', 'checkbox', array( |
|
80
|
|
|
'label' => 'Is locked?', |
|
81
|
|
|
'required' => false, |
|
82
|
|
|
)); |
|
83
|
|
|
|
|
84
|
|
|
$builder->add('submitButton', 'submit', array( |
|
85
|
|
|
'label' => 'Save', |
|
86
|
|
|
'attr' => array( |
|
87
|
|
|
'class' => 'btn-primary btn-lg btn-block', |
|
88
|
|
|
), |
|
89
|
|
|
)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param OptionsResolverInterface $resolver |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setDefaultOptions(OptionsResolverInterface $resolver) |
|
96
|
|
|
{ |
|
97
|
|
|
$resolver->setDefaults(array( |
|
98
|
|
|
'data_class' => 'Application\Entity\UserEntity', |
|
99
|
|
|
'validation_groups' => function (FormInterface $form) { |
|
100
|
|
|
$user = $form->getData(); |
|
101
|
|
|
$validationGroups = array(); |
|
102
|
|
|
|
|
103
|
|
|
if ($user->isLocked()) { |
|
104
|
|
|
$validationGroups[] = 'isLocked'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($user->getId()) { |
|
108
|
|
|
$validationGroups[] = 'edit'; |
|
109
|
|
|
} else { |
|
110
|
|
|
$validationGroups[] = 'new'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $validationGroups; |
|
114
|
|
|
}, |
|
115
|
|
|
'csrf_protection' => true, |
|
116
|
|
|
'csrf_field_name' => 'csrf_token', |
|
117
|
|
|
'cascade_validation' => true, |
|
118
|
|
|
)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getName() |
|
125
|
|
|
{ |
|
126
|
|
|
return 'user'; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|