Add new roles into the role_hierarchy automatically - symfony

I made a form for an administrator to add users and give them roles, so I added this line in the RegistrationFormType
//....
->add('roles', 'choice', array(
'choices' => array(
'ROLE_ADMIN' => 'ROLE_ADMIN',
'ROLE_USER' => 'ROLE_USER',
'ROLE_AUTEUR' => 'ROLE_AUTEUR',
'ROLE_MODERATEUR' => 'ROLE_MODERATEUR',
),
'required' => false,
'empty_value' => 'Choisissez un ou plusieurs roles',
'empty_data' => null,
'multiple' => true,
'expanded' => false,
))
//.....
The code works and the roles are added in the database.
My question is, if in the future, an administrator wants to add other new roles, so how to can add them into the role_hierarchy automatically?

I think this make not really sense then when you add a new Role this has no effect if you don't use the role in your application. So you should define the role structure before and make it so granular you need.
But you don't need to define the roles in your FormType. You can implement that the roles are loaded from an database. So you can manage your roles in your database and define the role hierarchy in your configuration file.
http://symfony.com/doc/current/cookbook/security/entity_provider.html#managing-roles-in-the-database
Here is the section in the documentation to manage roles from the database. I think this could help you.
Edit:
I have found something for you
http://symfony.com/doc/current/components/security/authorization.html
You can define voters in your case you could try a RoleHierarchyVoter or RoleVoter but thats a lot of work to implement it.

Related

Add roles in FOSUserBundle

I feel the same as a forum user who posted this:
I implemented FOSUserBundle, and I want to add to RegisrationFormType roles that are taken from a table. When I had it like this:
->add('roles', 'choice', array('label' => 'Rol', 'required' => true,
'choices' => array( 'ROLE_ADMIN' => 'ADMINISTRADOR','ROLE_SUPERADMIN' => 'SUPERADMINISTRADOR',
'ROLE_USER' => 'USUARIO'), 'multiple' => true))
And it works! But they must leave the BD, I can not put the Entity field because roles should be an array, not an object. How I can generate the array with the roles taken from a table? In FosUSerbundle as you would add roles?
Thanks ....
I write because that user had no answer. I followed [the steps of official documentation] (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md) and adding the above lines in the register of FOSUserBundle works, but I want to work this from the database.
And then I used to create groups this. Two additional tables were created and even now joined a group or role in the list, but not how to show the login to register a new user.
Has anyone solved it?
So you have the roles in a table? You could inject the EntityManager in the form type and use it to fetch the choices.
->add('roles', 'choice', array(
'label' => 'Rol',
'choices' => $this->getRoles(),
'multiple' => true,
'required' => true,
))
Then create a method which gives you the data the way you need.
Something like:
public function getRoles()
{
$roles = array();
$er = $this->em->getRepository('AppBundle:Role');
$results = $er->createQueryBuilder('r')
// conditions here
->getQuery()
->getResult();
// process the array?
foreach ($results as $role) {
$roles[$role->getId()] = $role->getLabel();
}
return $roles;
}

Is it possible to add multi role in wordpress?

I am building an education site, in that I want to add Teacher and Student as different user with some capabilities. I tried many times, but only default role is working. How can I assign different role for student and teacher....?
Tried many plugin also... At the time of new user registration, the role can select.
I tried these also
/* Add member role to the site */
add_role('member', 'Member', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
/* Add snypher role to the site */
add_role('snypher', 'Snypher', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
/* remove the unnecessary roles */
remove_role('subscriber');
remove_role('editor');
remove_role('author');
remove_role('contributor');
Is it possibile to add multi role in wordpresss?
How can hide the default role?
please help me..
Thanks...
Yes it is possible to create multi-role in WordPress. You can use User Role Editor for creating different roles with different capabilities.

ManyToMany nullable not allowed

I have a scenario like this:
tracks ManyToMany instruments
I create the form in this way:
$builder
->add('instruments', 'entity_hidden', array(
'attr' => array('class' => 'one_req'),
'label' => 'form.instrument',
'translation_domain' => 'label',
'required' => false,
'class' => 'Acme\DemoBundle\Entity\Instrument'
))
"hidden_entity" is given by a custom transformer that you can find here: gist
although I believe that is not the problem.
The problem is that the user may not even choose any instrument (such asamong other specified with "required" => false).
When I go to save my returns this error:
Neither the property 'instruments' nor one of the methods 'setInstruments()',
'__set()' or '__call()' exist and have public access in
class 'Acme\DemoBundle\Entity\SoundtrackVersion'.
I do not understand why ..
Where am I doing wrong?
It sounds very obvious, but error can not lie. Check that Acme\DemoBundle\Entity\SoundtrackVersion entity has property instruments and methods setInstruments() and getInstruments().

Wordpress add custom roles as well as remove default roles

I need to customize the default roles as I need only 3 roles - administrator, buyer, seller.
Then I need to add the buyer, seller and remove all other default roles. What should I do?
If there is any ready made code which I can paste in and it will work?
Paste this code in your themes function.php file and customize as your need. This is from my own code library. So it will definitely work.
/* Add member role to the site */
add_role('member', 'Member', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
/* Add snypher role to the site */
add_role('snypher', 'Snypher', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
/* remove the unnecessary roles */
remove_role('subscriber');
remove_role('editor');
remove_role('author');
remove_role('contributor');

How to add user-fields programmatically in Drupal 7

I'm trying to create a .install for a module that I'm migrating from Drupal 6. It requires two 'profile fields', which, in drupal 6, it checked for and created automatically.
To upgrade it to drupal 7 I'm trying to do this with fields! Easy enough right?
So far I have
if(!field_info_field('user_fullname')) {
$field = array(
'field_name' => 'user_fullname',
'type' => 'text',
'settings' => array(
'required' => TRUE,
),
);
field_create_field($field);
$instance = array(
'field_name' => 'user_fullname',
'entity_type' => 'user',
'label' => 'The user\'s full name',
'bundle' => 'additional_info',
'required' => true,
'widget' => array(
'type'=>'options_select',
)
);
field_create_instance($instance);
}
Which, sure enough, creates the field, but it's not visible in the user's profile?
Do I need something additional for that? If so, What?
Many Thanks.
SOLVED: It was due to the bundle (not entirely sure what a bundle is really), I changed the bundle to 'user' and it appeared!
bundle is pretty much the same as content type. But since in D7 users are entities too, but they are not content, using the term 'content type' didn't make sense. Reference: Barry Jaspan's DrupalCon Paris 2009 Session: Intro to the Field API for Module Developers.
Intro to the Field API for Module Developers
It was due to the bundle (not entirely sure what a bundle is really), I changed the bundle to 'user' and it appeared!
As I know in D7 the bundles are something like a model for an entity and it can have fields. The default drupal bundles are node, user and taxonomy but the new API provides the developer to create custom bundles too. Every field needs to belong to a bundle.
A bundle in Drupal is a subset of the entity. In this case the entity type is User and there is only one type of User so the bundle is User.
In Taxonomy: the Taxonomy is the Entity and the Vocabularies are the bundles.
In Nodes: Nodes are the Entity and Content Types are the bundles.
No field can be attached to an entity, properties are attached to the entity (published, sticky, etc). Fields are attached to the Bundles.
I don't have enough rep to comment, so here is my answer for those you find this via google. As I did.

Resources