Question

Let's say I have multiple restaurants entities in my application. Each restaurant can define their own manager, financier, cook, etc.

Example:

  • user1 is manager in restaurant1
  • user2 is financier in restaurant1
  • user3 is cook in in restaurant1

and

  • user1 is financier in restaurant2

What I would like to call in the RoleProvider is: IsUserInRole(user1, manager, restaurant1). The first 2 parameters are supported but not the last one.

Can this scenario be solved by the .NET RoleProvider?

Was it helpful?

Solution

The Syntax of RoleProvider's IsUserInRole method is:

public abstract bool IsUserInRole(
    string username,
    string roleName
)

So while overriding, you cannot include a third parameter.

Why not define your own custom method say ( extra 'S'):

IsUserInRoles(string username, string roleName1, string roleName2)

OR a more better way :

IsUserInRoles(string username, string[] roles)

and the body will like be:

protected bool IsUserInRoles(string username, string[] rolenames)
{


 if (username == null || username == "")
    throw exception;

  if (rolenames == null || rolenames.Length==0)
    throw exception;

 //code to check if user exists in all roles
 // you can call even the default IsUserInRole() method one by one for all roles
  bool userInRoles=true;
 foreach (string role  in roles )
  {
    if( !UserIsInRole(role))
            // set the boolean value to false
               userInRoles = false;
   }

  return userInRoles;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top