How to add user to user group in code

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
10/31/2011 10:52:12 AM
Gravatar
Total Posts 41

How to add user to user group in code

Hi,

I need to be able to have an authenticated user add themselves to a specific user group (role) once they've entered an access code which I am authenticating elsewhere. 

How can I do this in code?  Is there a method I can use to add a user to a role programmatically?

Thanks,

 

Michael

10/31/2011 11:05:13 AM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: How to add user to user group in code

I just implemented something similar--our users are authenticating via Active Directory, and I created a custom User Registered handler to auto-assign default role(s) to newly registered users. To add a user to a role, call Role.AddUser() with appropriate parameters. To refresh the user's roles on the fly (so they don't have to log out and back in), call mojoRoleProvider.ResetCurrentUserRolesCookie().

You can find examples of Role.AddUser() in many places in the source code.

Jamie

 

10/31/2011 11:15:08 AM
Gravatar
Total Posts 41

Re: How to add user to user group in code

Hi Jamie,

That's excellent - thanks very much for the quick response!  For some reason I had not spotted the Role class - I don't know how.  Anyway, that, and the cookie reset method, will do exactly what I need to do, so thanks again.

Michael

10/31/2011 1:22:59 PM
Gravatar
Total Posts 41

Re: How to add user to user group in code

Hi again,

Sorry, but are we talking about mojoPortal.Business.Role?  I don't see a AddUser() method in that class.  There are methods like CountOfUsers() and HasUsers() but nothing relating to adding new ones that I can see.  I don't have the source code at the moment so I can't check in there (but I will download it now)...

Michael

10/31/2011 1:33:50 PM
Gravatar
Total Posts 41

Re: How to add user to user group in code

Oh, sorry, it is there but it's a static method!  How strange.... anyway, I can use it...

10/31/2011 1:38:21 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: How to add user to user group in code

Yes, that's the one. When you have the source, check out Web\Components\mojoSetup.cs to see it in action:

Role.AddUser(adminRole.RoleId, adminUser.UserId, adminRole.RoleGuid, adminUser.UserGuid);

There may be alternative methods to add users to roles, but this is the one that I am using.

Jamie

You must sign in to post in the forums. This thread is closed to new posts.