Reputation: 38
Hi all and thansk for your time.
I'm working with Symfony 3.0.2
and in a Class i'm trying to get the service security.authorization_checker in order to see the ROLE_ of who is connected to my application
but i have this error :
Attempted to call an undefined method named "get" of class "AppBundle\Form\RegistrationType". Did you mean to call e.g. "getBlockPrefix", "getName" or "getParent"?
class RegistrationType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options){
$roles = new LoadRoles();
// $container = new Container();
// $this->denyAccessUnlessGranted('ROLE_ADMIN');
//LOAD ROLES FOR SUPER ADMIN
if($this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')){
$roles->getRolesForAdmin($builder, $options);
}
//LOAD ROLES FOR ADMIN
if($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')){
$roles->getRolesForManager($builder, $options);
}
//LOAD ROLES FOR ASSOCIATION
if($this->get('security.authorization_checker')->isGranted('ROLE_ASSOCIATION')){
$roles->getRolesForAssociation($builder, $options);
}
}
The probblem is located here : $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')
The function does not recognize the get() function since it does not extends Controller
I'm wondering if there is a way to access this service ? I'm new with Symfony please forgive my
Sincerly
Upvotes: 0
Views: 2146
Reputation: 1491
From your controller:
$form = $this->createForm($form, $item, array(
'authService' => $this->get('security.authorization_checker')
));
In your form:
public function configureOptions(OptionsResolver $resolver)
{
$resolver
//..
->setRequired('authService')
;
}
You can now access your service in the buildForm function:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$authService = $options['authService'];
//...
}
Upvotes: 2