Symfony2 User Roles (BETA2) - symfony

What's the appropriate way to add a user to a role.. for every new user, do I have to do:
$em->getRepository('MyBundle:Role')->findOneBy(array('name' => 'ROLE_USER'))
every time?
I'm not too much of a fan of how large UserBundle is.... and I don't use XML. I'm using YML/Annotations, so the UserBundle is pretty hard to follow for certain things.
So yes, what's the best / cleanest way to do a user signup and associate him to a default role?

The simplest way I've found is just to define roles as a field of type array on your User object. Then, when you're creating the user (on registration or whatnot), it's as simple as
$roles = array('ROLE_USER');
$user->setRoles($roles);
I've put together a mockup of my user registration process in this gist. It's not fully functional (I can flesh it out later if needed), but it should get you pointed in the right direction.

I wrote a couple of blog posts on roles, a simple solution http://blog.jmoz.co.uk/symfony2-fosuserbundle-roles and http://blog.jmoz.co.uk/symfony2-fosuserbundle-role-entities

Related

Change node author on user deletion

Is there a way to reassign all nodes associated with a user to another user when the first one is deleted instead of it going to anonymous?
To ask another way, I when I go to delete johnsmith, I am looking for the option to reassign all of johnsmith's content to janesmith.
I know this functionality exists within WordPress.
I do not know of any way to do this without coding.
In code I would look at
https://api.drupal.org/api/drupal/modules%21user%21user.api.php/function/hook_user_cancel_methods_alter/7.x
to define and add a new method when cancelling a user. And
https://api.drupal.org/api/drupal/modules%21user%21user.api.php/function/hook_user_cancel/7.x
to act on the new method.

Symfony2 best way of removing business logic from controller and correct usage of model

I'm in searching of the best way of removing business logic from controller and correct usage of model(and maybe services).
Some details below.
Actually, my project is more complicated, but as example I will use Simple Blog application.
I have created my application (Simple Blog) in next steps:
created bundle
generated entities(Topic, Post, Comment)
generated controller for each entity, using doctrine:generate:crud
installed FOSUserBundle and generated User entity
So, I have all needed methods and forms in my controllers. But now I have some troubles:
Admin need to be able see all topics and posts, when simple User can only see
topic and posts where he is owner.
Currently there are indexAction, that return findAll common for any user. As solution, I can check in action, if ROLE_USER or ADMIN and return find result for each condition. But this variant keep some logic at action.
I also can generate action for each role, but what happened if roles amount will increase?
What is the best way to solve this problem with result for each role?
I need to edit some parameters before saving.
For example, I have some scheduler, where I create date in some steps, using features of DateTime.
Before saving I need to do some calculations with date.
I can do it in controller using service or simple $request->params edit.
What is the best way to edit some $request parameters before saving?
My questions I have marked with bold.
Thanks a lot for any help!
What I would do is to create a query which fetches the topics. Afterwards I would have a method argument which specifies if the query should select only the topics for a certain user or all topics. Something like this should do the work in your TopicRepository:
public function findTopics($userId = false)
{
$query = $this->createQueryBuilder('topic');
if($userId) {
$query->join('topic.user', 'user')
->where('user.id = :user_id')
->setParameter(':user_id', $userId)
;
}
return $query->getQuery()->getResult();
}
So, whenever you need to get the topics only by a user, you would pass a $userId to the method and it would return the results only for that user. In your controller you'd have something similar to this code (Symfony 2.6+):
$authorizationChecker = $this->get('security.authorization_checker');
if($authorizationChecker->isGranted('ROLE_ADMIN')){
$results = $this->get('doctrine.orm.entity_manager')->getRepository('TopicRepository')->findTopics();
} else {
$results = $this->get('doctrine.orm.entity_manager')->getRepository('TopicRepository')->findTopics($this->getUser()->getId());
}
You can try using Doctrine Events and create a PreUpdate depending on your case. See the documentation for more information. If you have a TopicFormType, you could also try the form events.
You are not supposed to "edit" a $request, which is why you can't directly do that. You can, however, retrieve a value, save it as a $variable and then do whatever you want with it. You can always create a new Request if you really need it. Could you be more specific what you want to do here and why is this necessary?

Plone, extend portalmembership.getMemberInfo method

I made an extended user schema in my Plone 4.3 site with the collective.example.userdata.
My problem is when I try to display my new fields in author.cpt page.
I used to get my user object with the getMemberById from membershiptool and then use getProperty on the userobject but it requires a "manager" permission.
Maybe a solution would be to extend the getMemberInfo with my new fields but I don't know how to do such a thing.
Anyone ?
Thanks
I had the same problem and ended up monkey patching Products.PlonePAS.tools.membership.MembershipTool.getMemberInfo so it supply more data to the caller. The method getMemberInfo can be called with/from the Anonymous role.

Password required when editing

I'm trying to create/edit users and I'm having a slight problem with the password field.
When creating a user, it is fair to assume that the password field is required, however, I don't want it required when editing a user
Is there an easy way to do this?
I'm using the SonataAdminBundle to manage the User Form (NOT SonataUserBundle)
I think you can do it in two ways:
Use inheritance. Create a base FormType and then inherit it to create CreateUserType and EditUserType that add custom password settings
Use same form and leave password as not required, then in the "create user" controller check if password field has been filled and throw an exception if it is empty
Maybe there are more approaches to your problem. I usually use the second one but choose the one fits best to you.

Having multiple registration form with fosuser bundle

I'm new to symfony2 & I'm writing an application that requires to have 2 different user types.
one is normal user & the other is the one which has same fields as user plus some other extra fields.
we design it with foreign key & one-to-one relationship.
Now I don't understand how to build two different registration forms with the functionality of FOSUserBundle.i mean i don't know how to override RegistrationController for this purpose
thanks for your answer :)
I would recommend using the PugxMultiUserBundle. I think that is exactly what you are after, I have been using it for one of my projects and it works like a charm!
You can use RollerworksMultiUserBundle:
https://github.com/rollerworks/RollerworksMultiUserBundle

Resources