This project is read-only.

Authorization module that extends Orchard.Users

Topics: Writing modules
Nov 28, 2011 at 5:14 PM

Hi, recently I've started to work with Orchard CMS and one of my tasks is to create module that provides some authorization work.

I've found Orchard.Users modules, that almost fill my needs, but I want to add some additional fields to UserPartRecord (e.g. company name) and do additional validation with new fields.

How can I correctly do this work?

Nov 28, 2011 at 10:40 PM

You might want to look at the code for the profile modules that are available on the gallery.

Nov 29, 2011 at 1:05 PM

Thanks for answer. I've reviewed some modules from user category and if I understanding rightly - for implementing custom registration with company name field I must:

1. Create module with my record and part, that contains company name field.

2. Create migration file, where I'm creating table for new record and attaching my part to User through ContentDefinitionManager.AlterTypeDefinition.

3. Then I'm implementing service that saves registration information in DB through _repository and IMembershipService.

Is it right?

Nov 29, 2011 at 2:11 PM

That's the process for extending content normally, but the registration page works a bit differently, it's kind of fixed. However you can then use the following module to extend the page:

Nov 29, 2011 at 3:44 PM
Edited Nov 29, 2011 at 4:13 PM

Yeah, I've looked through ExtendedRegistration module.

Maybe I have mistake, but I think, that it's repeating code from Orchard.Users AccountController Register with appending ProfilePart content, that's why it depends on Profiles. 

And I really not understanding what's the purpose of Profile module and why do I need to use Profile module, why can't I just create my ContentPart and append it to UserContentType (like in ExtendedRegistration module, but instead ProfilePart insert mine)?



Have some progress...

I have read these forum discussions: and, I have some simular problems...

Now I've tried to use OrchardSuppressDependency attribute and it's working... I will try to implement custom registration page.



One more question was emerged...

I noticed that condition in Orchard.Users module:


if (!registrationSettings.UsersCanRegister)
    return HttpNotFound();
How can I permit users to register on site?
Nov 29, 2011 at 4:39 PM
Edited Nov 29, 2011 at 4:40 PM

The Profile module lets you display *any* parts attached to the user. So, that module then extends Profile so the same system also works for the Registration page. Meaning you can then attach a part or field to the User content item to implement your company name data (as you previously described) and it'll appear on the Registration page without having to do anything else.

Edit: Your other question - it's in the Settings area of admin.

Nov 29, 2011 at 5:20 PM

Thank you so much for help, Pete

Dec 2, 2011 at 9:27 AM

Extanding of registration got stuck.

In my task I need to extend standard user record because I have some additional fields. So I decided to do it through creating relation between Orchard_Users_UserPartRecord and my table 

I've created my custom record:

using Orchard.Users.Models;
public class ActionUserPartRecord : ContentPartRecord {
        public virtual UserPartRecord UserPartRecord { get; set; } //for connection with UserRecord table
        public virtual string CompanyName{ get; set; }
        public virtual string PhoneNumber{ get; set; }
        public virtual Guid ActionUserId{ get; set; }
        public virtual Guid Adviser{ get; set; }

After it I've created migration to DB:

        public int Create() {
	SchemaBuilder.CreateTable("ActionUserPartRecord", table => table
                .Column("CompanyName", DbType.String)
				.Column("PhoneNumber", DbType.String)
                .Column("ActionUserId", DbType.Guid)
                .Column("Adviser", DbType.Guid)
                .Column("UserPartRecord_Id", DbType.Int32)

                cfg => cfg

            return 1;

And in controller I'm creating user and bind him to my table:

	IUser newUser = _membershipService.CreateUser(new CreateUserParams(model.Name, model.Password, model.Email, null, null, true)); //User created rightly

        UserPartRecord userPart = newUser.As<UserPart>().Record;
        ActionUserPartRecord userRecord = new ActionUserPartRecord() {
               PhoneNumber = model.Phone,
               CompanyName = model.CompanyName,
               UserPartRecord = userPart
        _actionUserRepository.Create(userRecord); //going Exception

The problem lay in creating actionPartRecord through repository.

I've got IdentifierGenerationException (attempted to assign id from null one-to-one property: ContentItemRecord).

Help me please with any advice.

Dec 2, 2011 at 4:50 PM

The problem is you don't need to create that relationship. ActionUserPartRecord already has a relationship with UserPartRecord because they're both ContentParts.

If you use santiago's Extended Registration module then all you have to do is write a ContentPartDriver for your ActionUserPart, and in the driver you can emit the right templates and populate the module. You don't need your own controller at all.

Dec 5, 2011 at 4:29 PM

Thanks, I've looked through the santiago's module and I am not really understanding mechanism of working with ContentManager and filling ContentItem with information, but I want to clarify it.

I have User type with my custom ActionUserPart, and when I'm calling var user = _orchardServices.ContentManager.New("User"); I'm getting ContentItem with UserPart and my custom ActionUserPart...

As I understand for saving this ContentItem I should fill it with data and call


But I don't know how to fill ContentItem with information (how to flood two my content parts with appropriate data and to push its in my ContentItem?)

But maybe I'm wandered off the course and understanding something wrong... Please, take me on right direction.


Dec 5, 2011 at 4:51 PM

So you've added your ActionUserPart to User ... now just install santiago's module and look at the registration page. Your part should be there, and filling in the form will save your data. You don't need to call ContentManager at all.

Dec 5, 2011 at 5:30 PM

ok, how can I get companyName for appropriate user?

Dec 5, 2011 at 5:43 PM


Dec 6, 2011 at 11:29 AM

ok, thanks, I also see RolePart in my User/Account/Register page, how may I remove it?

Dec 6, 2011 at 11:36 AM

one more question: How may I change style and fields order of registration form?

Dec 6, 2011 at 1:13 PM

Hmm, seeing RolePart sounds to me like a bug. You don't want users to decide their own roles :)

You can use to change the positioning and appearance of the form. If you haven't used it before there's documentation on Enable the Designer Tools Shape Tracing feature and that will let you explore the page to find the names of parts, so you can position them with placement. You can also specify alternate template names there.

Technically you can hide the roles part with placement, but I think there would be a security hole because it would still respond to POST data.

Dec 6, 2011 at 5:47 PM

RolePart showed because I was signed in by admin (when I'm going through anonimous user - it's absent).

The flow goes through UserRolesPartDriver, but this lines 

if (!_authorizationService.TryCheckAccess(StandardPermissions.SiteOwner, _authenticationService.GetAuthenticatedUser(), userRolesPart))
                return null;

settle it ok.


I've done the custom form registration, and it works with these lines of code (I excepted validations and some checking, that will be added after): 

public ActionResult Registration() {

            var user = _orchardServices.ContentManager.New("User").As<UserPart>();

            dynamic shape = _orchardServices.ContentManager.BuildEditor(user.ContentItem);

            return View((object)shape);
        public ActionResult Registration(string name, string email, string password, string confirmPassword)

            var shape = _orchardServices.New.Register();

            var user = _membershipService.CreateUser(new CreateUserParams(name, password, email, null, null, false));

            _orchardServices.ContentManager.UpdateEditor(user.ContentItem, this);

            return new ShapeResult(this, shape);


But I've encountered with next problem... my task now is to do custom registration with Ajax pop up form and I need method like this:

public JsonResult Register(string name, string email, string password, string confirmPassword)
            try {

                var shape = _orchardServices.New.Register();

                var user = _membershipService.CreateUser(new CreateUserParams(name, password, email, null, null, false));

                _orchardServices.ContentManager.UpdateEditor(user.ContentItem, this);

                return Json(new { success = true });
            catch (Exception e)

                return Json(new { success = false});

but it's not working...

P.S. The form for data I'm inserting to master page by 


and all parameters of JsonResult Register method passing rightly, but when flow goes in ActionUserPartDriverEditor - custom parameters not passing there...

Help me please.

Dec 6, 2011 at 6:33 PM

You would have to be posting the entire registration form data to that ajax method for UpdateEditor to work. Although, it's possible to return the content editor from an ajax call.

Dec 7, 2011 at 4:39 PM
Edited Dec 7, 2011 at 4:55 PM

Yeah! It's working, I had mistake with incorrect writting name and id attributes of input... It's because I rewriting site from ASP.NET MVC to Orchard CMS and doing some naming changes... but never mind... thank you for you patience, attention and really nice help with Orchard insight. You're the best!