Developer forum

Forum » Development » Assign user to group

Assign user to group

Anders Ebdrup
Anders Ebdrup
Reply

Hello guys,

 

I am looking for a way to assign a user to an extranet group from the frontend, when the user buys a specific product. I have tried with this approach:

                user.AddToGroup(groupId);
                using (var supress = new NotificationContext(NotificationContext.NotificationState.SuppressNotifications))
                {
                    user.Save();
                }

But it does not seems fully to do the trick. Any help is appreciated.

 

Best regards, Anders


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Try user.Groups.Add() instead. The other method seems related to frontend additions where the user have to approve the new group belongings first.

 
Anders Ebdrup
Anders Ebdrup
Reply

Dear Nicolai,

 

The Groups-property seems to be a ReadOnlyGroupCollection, with no option to set the property. So do you have another suggestion? :-)

 

Best regards, Anders

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply

Hi Anders,

Your code looks good!  Could you describe what is wrong?
Alternative way here is to add user(s) into a group:

   group.AddUser(user.Id);
   group.save();

 

Best regards,
Vladimir

 

 

 

 

 
Anders Ebdrup
Anders Ebdrup
Reply

Dear Vladimir,

 

I can see that it is somehow working, but the permissions on the site for the User does not seems to be updated, so the user can see the new pages in the navigation before logout/login.

Do you have an idea on how to get the permissions for the user updated, so we can avoid having the user to logout/login before getting access to the new pages?

 

Best regards, Anders

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply

I suppose we can try to clean user permissions:

            var permissionService= new UnifiedPermissionService();
            permissionService.ClearUserCache($"{userId}");

However I'm not sure 100% - could you give me your setup and detail steps how to reproduce the issue please?

Best regards,
Vladimir 

 
Anders Ebdrup
Anders Ebdrup
Reply

Dear Vladimir,

Thanks for helping me out here. Me issue was that I only assigned the user to the group in a "CheckoutDoneOrderIsComplete"-observer, which is called in a Callback from the payment provider, so this information was only stored in the database, and not the session. So my fix is also to set the information in a "BeforeRenderingNewStep"-observer, when the order is complete.

 

Best regards, Anders

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply

Hi Anders,
thank you for sharing your solution here!
I'm sorry that my help wasn't very big - but it is hard without knowing full context info.. at least to me :)

Best regards,
Vladimir

 

You must be logged in to post in the forum