Assign role at runtime for RegistrationForm control?

Posted by Community Admin on 03-Aug-2018 15:28

Assign role at runtime for RegistrationForm control?

All Replies

Posted by Community Admin on 29-Apr-2011 00:00

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);
    

Posted by Community Admin on 02-May-2011 00:00

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.

Posted by Community Admin on 03-May-2011 00:00

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

Posted by Community Admin on 05-May-2011 00:00

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();
        
    

This thread is closed