1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Assign role at runtime for RegistrationForm control?

Assign role at runtime for RegistrationForm control?

4 posts, 0 answered
  1. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    29 Apr 2011
    Link to this post
    I am doing some validation when visitors are registering through the beta Registration Wizard control. I would like to intercept the area where users are assigned a role. Based on my validation, I would like to conditionally assign roles to the user. How can I do that?

    Below is my attempt to inherit and override from the RegistrationForm control, but am stuck on how to make the control use my role for assignment instead:

    public class CustomRegistrationForm : Telerik.Sitefinity.Security.Web.UI.RegistrationForm
    {
        protected override void AssignRolesToUser(Telerik.Sitefinity.Security.Model.User user)
        {
            UserProfileManager userManager = UserProfileManager.GetManager();
     
            //GET USER PROFILE
            var profile = userManager.GetUserProfile(user, typeof(SitefinityProfile).Name);
            string company = profile.GetValue<string>("Company");
     
            //GET ACCOUNT STATUS FROM EXTERNAL SOURCE
            string status = null;
            using (ApiService api = new ApiService())
            {
                status = api.QueryStatus<Account>(company);
            }
     
            //ADD TO ROLE BASED ON STATUS
            switch (status)
            {
                case "Active":
                    //APPROVED, ASSIGN ROLE 1
                    break;
                default:
                    //NOT APPROVED, ASSIGN ROLE 2
                    break;
            }
     
            base.AssignRolesToUser(user);
        }
    }
  2. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    02 May 2011
    Link to this post
    I think I need to do something like this:

    ...
    If (//DO CUSTOM VALIDATIONS THEN)
      this.Roles(???);
     
    base.AssignRolesToUser(user);
    ...

    How do I add my own roles to this.Roles? It looks like it only accepts a ItemInfoDefinition type.
  3. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    03 May 2011
    Link to this post
    Hello Basem,

    You can use ItemId property of the role info (ItemInfoDefinition) to get the role.

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  4. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    05 May 2011
    Link to this post
    I just realize this.Roles is read-only, plus constructing an ItemInfoDefinition from a role name was getting awkward. Here's what I ended up doing:

    public class CustomRegistrationForm : RegistrationForm
    {
        protected override void InitializeControls(GenericContainer container)
        {
            base.InitializeControls(container);
            this.RolesAssignedToUser += new UserOperationInvokedEventHandler(CustomRegistrationForm_RolesAssignedToUser);
        }
     
        protected void CustomRegistrationForm_RolesAssignedToUser(object sender, UserOperationInvokedEventArgs e)
        {
            if (something)
            {
                var roleManager = this.GetRoleManager(this.RoleProvider);
                bool suppressSecurityChecks = roleManager.Provider.SuppressSecurityChecks;
                roleManager.Provider.SuppressSecurityChecks = true;
                var role = roleManager.GetRole(item);
                roleManager.AddUserToRole(user, role);
                roleManager.Provider.SuppressSecurityChecks = suppressSecurityChecks;
                roleManager.SaveChanges();
            }
        }
    }
4 posts, 0 answered