Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesProgrammatically adding User Roles Programmatically adding User Roles
Previous
 
Next
New Post
8/25/2014 5:16 PM
 

Hi Folks,

I'm having a little trouble with programmatically adding User Roles, and being able to get a decent handle on the current User Roles after adding.

I'm able to add a Role to a User just fine, but it's not until the second page refresh that my "output" textbox has the expiryDate as I expect.

In stepping through the following code in debug; after adding the Role - my UserInfo (cUser in this case) - is not that in that role, until I refresh the page again.

Running DNN 7.3

try

            {

                if (HttpContext.Current.Session["trialUser"] != null)

                    sTrialStatus = HttpContext.Current.Session["trialUser"].ToString();

 

                if (sTrialStatus == "Trial")

                {

                    bTrialUser = true;

                }

                else

                {

                    bTrialUser = false;

                }

                if (Request.IsAuthenticated && !_currentUser.IsInRole("Trial") && bTrialUser == true)

                {

                    RoleController ctrRoles = new RoleController();

                    RoleInfo oCurrentRole = ctrRoles.GetRoleByName(this.PortalId, "Trial");

                    uRole = ctrRoles.GetUserRole(this.PortalId, _currentUser.UserID, oCurrentRole.RoleID);

                    ctrRoles.AddUserRole(this.PortalId, _currentUser.UserID, oCurrentRole.RoleID, System.DateTime.Now, System.DateTime.Now.AddDays(1));

                    DotNetNuke.Security.PortalSecurity.ClearRoles();

                }

 

                if (!Page.IsPostBack)

                {

                  UserInfo cUser = new UserInfo();

                    cUser = UserController.GetCurrentUserInfo();

 

                    if (!UserInfo.IsInRole(PortalSettings.AdministratorRoleName.ToString()) && cUser.IsInRole("Trial"))

                    {

                        DotNetNuke.Security.Roles.RoleController rolesController = new DotNetNuke.Security.Roles.RoleController();

                        UserRoleInfo role = rolesController.GetUserRole(0, _currentUser.UserID, rolesController.GetRoleByName(0, "Trial").RoleID);

                        expiryDate = role.ExpiryDate;

                        output.Visible = true;

                        output.Text = expiryDate.ToString();

                    }

                }

            }

            catch (Exception ex)

            {

                DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, ex.ToString(), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);

            }

 
New Post
8/26/2014 4:00 AM
 

Hi

After AddUserRole method you can try for UserLogin method again.
You can refer to this link (http://dnnsunil.blogspot.in/2013/01/clearing-login-user-role-from-cache.html)

Thanks
Sibabrata Dash
Mindfire Solutions

 
New Post
8/26/2014 1:35 PM
 

Hi Sunil,

Thanks, although that doesn't seem to be working for me.

I switched this out: DotNetNuke.Security.PortalSecurity.ClearRoles();

For this: UserController.UserLogin(this.PortalId, objUser, PortalSettings.PortalName, this.Request.UserHostAddress, false);

I still only have the two roles until a second page refresh.

I wonder if this is a timing issue?

 
New Post
8/27/2014 6:12 AM
 
Could it be that you haver pseudo code that works like this...

control.DataBind(); // therefore showing the OLD values
object.UpdateRolesInSomeWay() // Adding the new role - but too late for it to get shown on the screen

That could cause it to show up on the next time around.

Best wishes,
- Richard
Agile Development Consultant, Practitioner, and Trainer
www.dynamisys.co.uk
 
Previous
 
Next
HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesProgrammatically adding User Roles Programmatically adding User Roles


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.

Content Layout

Subscribe to DNN Digest

Subscribe to DNN Digest

DNN Digest is our monthly email newsletter. It highlights news and content from around the DNN ecosystem, such as new modules and themes, messages from leadership, blog posts and notable tweets. Keep your finger on the pulse of the ecosystem by subscribing.  


Copyright 2017 by DNN Corp Terms of Use Privacy
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out