Assign role at runtime for RegistrationForm control?
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);
I think I need to do something like this:
...
If (//DO CUSTOM VALIDATIONS THEN)
this
.Roles(???);
base
.AssignRolesToUser(user);
...
Hello Basem,
You can use ItemId property of the role info (ItemInfoDefinition) to get the role.
Greetings,
Ivan Dimitrov
the Telerik team
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();