How to Add a User to a Role Programmatically
Jun
25
Written by:
6/25/2009
There may come a time when you will want to programmatically add a user to an existing role in DotNetNuke®. It’s actually quite easy. If you take a look at how the core code in DNN does it, just emulate that. This method of using and reusing the existing DNN core code is what DNN development is supposed to be about, so make sure you don’t reinvent the wheel.
In the source code for DotNetNuke®, there is a controller class for nearly everything that you want to do with the various objects that you work with in DNN every day. Since I am familiar with the DNN source, I knew where to go. But many people are not familiar with the source code. In those cases, you need to either do some searches in the Object Browser, or browse around until you find the code you’re looking for. Thinking it through logically, you can usually browse right to the code you’re looking for since the core team has done a fantastic job of keeping the code files organized.
Keep all of that in mind, I immediately gravitated to the Security folder of the Components directory. I did this because roles are part of the security framework in DNN, and all of the entities and objects are in the Components folder. Inside this folder, I saw and opened the RolesController.vb file. To make this clear, I opened the following file in the DNN source code:
~/Library/Components/Security/Roles/RoleController.vb
The controller classes are usually what we are looking for when we want to utilize the existing logic in DNN. So, if I want to manage a role by putting someone into it, I naturally wanted to look for a Role Controller already – before I even opened the source code solution.
One of the methods of the RoleController class is AddUserRole(), and it has a few overloads. We are only concerned about the root method right now. This method accepts a few arguments, and when successfully called, it adds an existing user to an existing role.
RoleController.AddUserRole(integer PortalID, integer UserId, _
integer RoleId, date EffectiveDate, date ExpiryDate)
Using this method may look self-explanatory, but I will explain it anyway. When you intend to use this method, you should already have access to an existing UserInfo object, and a RoleInfo object. Both are regularly available through standard module development.
- PortalID (integer) – This is the id number representing the portal where the user and role exist. The SqlDataProvider will use this value to correctly match up the objects.
- UserId (integer) – This is the id number matching the user that you want to add to a role. Most often, you would simply access this value from the PortalModuleBase class that your module inherits.
- RoleId (integer) – The id number of the role can be found from any number of ways. We can only assume that you already have access to this right now.
- EffectiveDate (date) – This is the date that you want the role assignment to begin. Usually, the current date is what you’re intending to pass to this argument. This argument accepts a null value.
- ExpiryDate (date) – This should be the date that you want the role assignment to expire, and as a result, the user will be removed from it. This argument accepts a null value.
In more recent versions of DNN, there is also another value which we do not pass to the method.
- CreatedByUserID (integer) – This is a value implemented for auditing purposes, to let us know who added the person to the role.
Let’s assume that we have a UserID of 123 and a RoleID of 10. Going further, we will also assume that this is a first instance of a portal, equaling a PortalID of 0. The most common role assignments do not pass an EffectiveDate or an ExpiryDate. We will use this as an example as well. Here is how we would call the method:
' we are assuming that you already have an intRoleId from your logic
RoleController.AddUserRole(Me.PortalID, Me.UserId, intRoleId)
' ... or ...
' Here we assume a role assignment for 30 days, beginning now
RoleController.AddUserRole(Me.PortalID, Me.UserId, intRoleId, DateTime.Now, DateTime.Now.AddDays(30))
That’s all there is to it! :) Easy, right?
8 comment(s) so far...
Re: How to Add a User to a Role Programmatically
Hi Will,
I have an external application and I'm trying to add a role to DNN user. I followed some example from the internet and here's my code.
var container = new DotNetNuke.ComponentModel.SimpleContainer("UserTest");
DotNetNuke.ComponentModel.ComponentFactory.Container = container;
DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();
DotNetNuke.Entities.Users.UserInfo ui = DotNetNuke.Entities.Users.UserController.GetUserById(0, UserID);
ui.Roles = (string[])rc.GetUserRoles(Contants.AMedPortal, UserID).ToArray(typeof(string));
if (!ui.IsInRole(rc.GetRole(roleID, 0).RoleName))
{
rc.AddUserRole(0, UserID, roleID, DateTime.Now, DotNetNuke.Common.Utilities.Null.NullDate);
}
I'm using the Install version of DNN 05.04.01 and every time I access RoleController or UserController, I get error message "Object reference not set to an instance of an object.". The reason is inside UserController.vb, this line can't seem to get a hold of a valid instance to the membershipProvider.
Private Shared memberProvider As DotNetNuke.Security.Membership.MembershipProvider = DotNetNuke.Security.Membership.MembershipProvider.Instance()
Do you have any insight? Do I have to register any component to the container?
By KNguyen on
6/30/2010
|
Re: How to Add a User to a Role Programmatically
Hey there. This really isn't the easiest way to help troubleshoot your code - especially something like this. I'd have to set-up a test environment to test it out myself. Do you have the actual (complete) error message?
By Will on
6/30/2010
|
Re: How to Add a User to a Role Programmatically
Hey merry Christmas,
How can we add the default role if we're adding the user programmatically? how do we get the userid of the registered user for example? i have used the default function available in the core AddUser in UserController. But the role isn't being added with it nor the membership as it seems.
By Mazen on
1/4/2011
|
Re: How to Add a User to a Role Programmatically
@Mazen: Any default roles are automatically assigned in DNN, that is unless this is a custom security role you're referring to. In this case, you would need to also find that security role in your user creation process, using the DotNetNuke.Security.Roles.RoleController class.
By Will on
1/4/2011
|
Re: How to Add a User to a Role Programmatically
Will or KNguyen did you figure out what was need for the third party application setup? I am trying to do something simaliar and recive the same error. I am sure just missing a few lines of code to get his working.
By Chris on
2/1/2011
|
Re: How to Add a User to a Role Programmatically
@Chris: Usually that error in providers relates to the provider not properly being named, or the provider values in the class not properly being named. When this happens, reflection is looking for an object that doesn't exist when loading the provider.
By Will on
2/1/2011
|
Re: How to Add a User to a Role Programmatically
Hi! I am rather new to "coding" IN dnn, so it may be the cause why it didn't work for me. I'm using DNN 05.06.01. Actually Intellisense always "shows" a different set of parameters for that method. This is the "new" AddUserRole:
AddUserRole(objUserInfo, objRole, PortalSettings, effDate, expDate, userID, notifyUser) Being... objUserInfo As DotNetNuke.Entities.Users.UserIfo objRole As DotNetNuke.Security.Roles.RoleInfo PortalSettings As DotNetNuke.Entities.Potals.PortalSettings effDate As Date expDate As Date userID As Integer notifyUser As Boolean
I wanted to use it, but I cannot find a way to find the objRole (As DotNetNuke.Security.Roles.RoleInfo). I'd need a method like getRoleInfo(RoleName As String) or getRoleInfo(RoleId As Integer) :)
Thanks indeed!
By Adrián on
3/7/2011
|
Re: How to Add a User to a Role Programmatically
@Adrian: This blog post was written for an older version of DNN. As such, the parameters do change over time. You can get role info from the RoleController class. There is also the possibility that passing Nothing or null is acceptable. I haven't looked at that source in a while.
By Will on
3/7/2011
|