Oftentimes, basic web development requirements include custom user profile logic and information to be stored, along with basic user functionality. Yet, registration and configuration of users who access your website do not directly match up with the core Orchard user functionality; as a web development agency, we’ve learned to extend it. In this blog post, we’ll show you how to add custom user types to your Orchard CMS setup.
Orchard CMS is great because the core includes user registration methods, user moderation functionality, and security around these functions as well. Orchard comes with the Orchard.Users module and the Orchard.Roles module as well. These are great extension points for the development of any new website requiring a bit more flexibility.
Assigning a new user type will require you to create a custom module. This will allow you to respond to actions for Brokers and build out your new content part.
After creating your new module, you will need to begin laying out scaffolding in the Migration.cs in order to wire things up. The migrations run on module install and are needed for updates to them. Add the following code to the Migrations.cs file:
So what is all this doing? First, we are creating our new content part record for storage. Creating this record allows us to persist Broker information to the database and give it the characteristics of a content item. Next, we declare our new content item as a part that is not attachable via the UI. This restricts people from attaching it to other content items. Next, we create a new content item called "Broker," which is not able to be created via the UI and has the three content parts web development teams require for functionality. The UserPart gives us all the functionality and storage required for any user in the site. Secondly, the UserRolesPart gives us the ability to assign/retrieve roles associated with this user. Lastly, we assign our new BrokerPart so we can store information required for our users.
The last two lines of this code block create a role in the system for our new user of Broker and assign permissions to it. We do this by injecting a dependency into the Migrations class (IRoleService roleService). This will give you access to the functionality you need from the RoleService. You can learn about how to create a Permissions.cs file in the module here: http://docs.orchardproject.net/Documentation/custom-permissions. You don't need to create permissions or a separate role, but it will be nice to distinguish these users versus other users in the system. Especially if you intend to expand the user base types.
Now that we have our new broker content type created, we want new brokers to be able to sign-up via the website. We created our own registration page that directs to a custom controller in our module to process the registration. We did this for flexibility. We could have just processed the Editor templates of our BrokerPart as normal, but we wanted to takeover the process of registering a user on the site completely. We mainly ported much of the code from the Orchard.Users AccountController.cs in order to do this and then added our own custom code as well. Having a custom page with a custom controller is just easier, in my opinion, than overriding all the specific Broker templates in the theme and adding any other custom hooks for backend processing we wanted to add. (We had a bunch of other things needing to be processed.)
If you look at some of our code for the Register Action on the ProfileController in our module, you will see the general idea of what we are doing.
We are validating the user information just like in the Orchard.Users module but then running some custom code in our service layer to create our Broker. Lastly, we return the Email Address Challenge to the user. This code is very similar to the code in the Register method inside Orchard.Users as well. Let's look at the code to create a broker:
So, this is the guts of the user creation process. We basically extract our values from our view model and set the UserPart and BrokerPart information. Lastly, we assign
the Broker role to the new Broker. This code shows some of the logic behind how Orchard manages content. As you can see, it is not as scary as you might think and is a pretty straightforward web development process.
Now that we have our Broker added to our system, how do we enable the user to manage their information? The same way we do it in MVC/Orchard in any other situation. By simply adding a controller with an action that will return the broker information when they arrive to the webpage. You can retrieve the currently logged-in broker's information like this:
We can then render our broker information in a template inside Views\Broker.cshtml for example. We then can process changes as needed in our custom form.
The good news: it is relatively easy to add more flexibility and functionality to the core Orchard user system. This example is meant to lay out the various pieces required to do this. There are some items left out, but it should take some of the mystery out of it. You can extend the default Editor templates for the Broker so you can view and add this custom logic in the Admin dashboard as well by adding templates to \Editor Templates\Parts\Broker.cshtml. This will let administrators add and update Broker information as well. As you saw in step 3 above, we preferred to just let users modify their own broker information for now.
I hope this sheds some light on ways to customize Orchard for your website! If you have any questions, just comment below or contact us for help with web development and design.
1. Handlers/BrokerPartHandler.cs
2. Models/BrokerPart.cs
3. Models/BrokerPartRecord.cs
4. ViewModels/BrokerViewModel.cs
5. Routes.cs
You will want to edit this as new routes are needed to provide functionality.
Sammi has a great mix of Content Management Experience and .NET development skills. With over 4 years of development experience concentrated on ASP.NET MVC, Sitecore, and Orchard CMS he brings a very valuable skillset to the team. Graduating from UMASS Boston with a degree in MSIS he continued on to develop and architect highly complex web applications for a variety of clients.