0

I have different types of Roles to take into account when authorizing a user. For example: John must have Position Manager and be part of the Office Supplies Department to order a new computer.

Problem with Roles is Roles.GetRolesForUser("John") can only return a string array.

Should I go with a custom roleProvider and custom roleManager? or should I develop a custom ProfileManager to add methods like GetUsersWithProfileProperties()?

Any suggestion is welcome!

Thibaut

EDIT: the above example is simplified I could have a much as 4 types of roles which are 4 different collections.

EDIT: I found a very similar question

Community
  • 1
  • 1
teebot
  • 1,089
  • 2
  • 12
  • 25
  • 1
    I see this method exists Roles.GetUsersInRole("") – Saar Nov 24 '09 at 11:31
  • I'm failing to understand the problem - the example you give is about testing whether a user has a given role but you're asking about methods to return a list users for a given role - these two don't seem to connect? – Murph Nov 24 '09 at 11:33
  • I edited my post as it wasn't really clear. The core of the problem is really that I have different kinds of roles. Maybe I could achieve that with multpile role providers but it looks complicated to implement. – teebot Nov 24 '09 at 12:13

4 Answers4

2

From what you write; I believe that everything you need is currently available out of the box:

    // Return all Users in a  Role
    string[] users;
    users = Roles.GetUsersInRole("RoleName");
    // Return all Roles for a User
    string[] roles;
    roles = Roles.GetRolesForUser();
    // Search through Membership store locating users with a role
    MembershipUserCollection mu;
    mu = Membership.GetAllUsers();
    // Loop through all membership users looking for users in a role

    foreach(MembershipUser m in mu){
        if(Roles.IsUserInRole(m.UserName, "Role Name")){
            // Do something

            // We can even nest to x levels
            if (Roles.IsUserInRole(m.UserName, "Another Role")){

                // Do something else
            }
        }
    }

Please clarify if I have misunderstood your question.

Mick Walker
  • 3,862
  • 6
  • 47
  • 72
  • I would then have to make the array flat (having "manager" and "office supplies" at the same level). While this could work, this feels a bit like a hack. – teebot Nov 24 '09 at 12:08
  • also what if there's a third type of role I would have to add another iterator. – teebot Nov 24 '09 at 12:11
  • Roles are user defined, so I cannot ever see them becoming strongly typed I am afraid. – Mick Walker Nov 24 '09 at 12:14
  • the problem is not that it's not strongly typed, it's really how to query different collections from a common provider interface – teebot Nov 24 '09 at 12:40
  • ASP.Net allows the flexibility to roll your own custom provider. The out of the box functionality is as far as I gather, simply a base for you to extend upon. Yes there are scenarios where the out of the box model will fit the developers needs, but in real world situations this is rarely the case. – Mick Walker Nov 26 '09 at 10:53
1

why not create a "CompositeRoleProvider" with a Path-To-Level typew convention for accessing each subordinate role provider. You will still have to create multiple role providers, but your Composite or Top-Level Provider does all of the work for you. I plan to do a similar thing with ProfileProvider

Enzoaeneas
  • 103
  • 2
  • 8
0

I'm studying how to solve a pretty similar problem and I've come to a conclusion that the best thing to do is to implement a custom role provider.

I'm using this (http://msdn.microsoft.com/en-us/library/317sza4k(v=vs.100).aspx) as a base and I will implement my methods like (IsManager, GetDepartment, ecc).

Data will be stored in custom tables that are joined to the aspnet_users table.

Hope it may help someone in the future :)

Nicola Cassolato
  • 193
  • 1
  • 11
-1

I think there is a method GetUsersInRole. http://msdn.microsoft.com/en-us/library/system.web.security.roles.getusersinrole.aspx

A G
  • 21,087
  • 11
  • 87
  • 112