Set the role for the new created user

Topics: Customizing Orchard
Feb 20, 2011 at 7:03 AM

If the new user is created/registried from the front end, it seems not be assigned a role, right?

I want to know if there has been a plan for support the above feature? So once a user is registried, he can login the dashboard and post his blog, and so on.

Feb 20, 2011 at 8:29 PM
Edited Feb 20, 2011 at 8:29 PM

Yes, the user doesn't have any roles attached by default, but the feature you are describing can be easily doable, two-fold:

  • Implement the IUserEventHandler interface and add the appropriate role inside the Created method. This is the preferred way, I think.
  • In one of your ContentHandlers use OnCreated<UserPart>{(ctx, part) => { <add appropriate role here> });


Feb 20, 2011 at 9:45 PM

I was wrong in one point, sorry.

Registered user doesn't have any roles explicitely assigned by default, but... Every authenticated user has the Authenticated role attached at runtime after logon. Thus you can add necessary permissions to this role and achieve your goal this way:) Remember though that this role is assigned to every user, so be careful.

Feb 20, 2011 at 10:46 PM

I wrote a tutorial on this topic: How to assign default role(s) to new user in Orchard. Hope you find it useful!

Feb 21, 2011 at 12:55 PM

Greate! Thanks very much!

Feb 21, 2011 at 1:19 PM

Cool, nice I could help:)

Apr 23, 2013 at 9:39 PM
Great tutorial, but I have a quick question if you have a moment...

Where should the file that has the DefaultRolesHandler class you described in your post reside? Should it be placed in the Handlers folder of Orchard.Users or would it be bad practice to add a file to one of the core projects? Can it be placed in my custom module if putting it in one of the core projects is not a good location? Would I just have to put it in a Handlers folder in my custom module then, or some other location?

Also, if I'm using the Contrib.Profile module and I have a boolean field that I'm having the user set true or false on registration, is there a way to pick that up in the class file you described and only add the user to the role if that field value is true? In another module I have, I can get to the ProfilePart by accessing the user as a ContentItem. Can I get to the user ContentItem from the class file you described.

Thanks so much for the help!
Apr 25, 2013 at 7:20 AM
I figured most of it out...created the proper file and dropped it in an Events folder in my module. It worked without the ProfilePart condition I spoke of in the 2nd paragraph of my response, but when I add the ProfilePart condition, it doesn't seem to be picking up the value of the field.

I created a boolean field for the ProfilePart named Professional. This is how I attempted to check for it
        /// <summary>
        /// Called after a user has been created
        /// </summary>
        public void Created(UserContext context) {
            dynamic contentItem = context.User.ContentItem;

            if (contentItem.ProfilePart.Professional.Value == true) {
                var role = _roleService.GetRoleByName("Administrator");
                if (role != null) {
                        new UserRolesPartRecord {
                            UserId = context.User.Id,
                            Role = role
I don't really know what UserContext is, but it did let me get back to the User.ContentItem. If you have any suggestions, they'd be greatly appreciated. Thanks.
Apr 25, 2013 at 7:24 AM
You may also use the new workflow for this. No coding.
Apr 25, 2013 at 4:34 PM
Nice to know...thanks. This project won't be launching for a little while, so I might just wait for that.