0

In an MVC app, administrator has a CRUD controller for managing users. Now, the functionality of the edit part needs to be extended and it involves adding a number role dependent tabs. They depend on the role of the viewed user, rather than on roles of the administrator who is viewing them. The easiest way for achieving this, would be getting all roles of that user as a array of strings (or similar), but how do I actually go about obtain those.

Is there a preferred method of getting all roles of a single user in SimpleMembership (based on his UserId) or do I just have to patch up a stored function in the database and pull those through it?

Writing the function is not a big deal, but this problem doesn't sound like something I should have to make workarounds for.

jahu
  • 5,427
  • 3
  • 37
  • 64
  • Have you seen this post: http://stackoverflow.com/questions/20858599/how-to-get-roles-from-using-simplemembership – David Tansey Mar 03 '15 at 14:23
  • @DavidTansey Thanks. `GetRolesForUser` looks like it might do the trick, but I'll have to check it further if it really does what I need. – jahu Mar 03 '15 at 14:25

2 Answers2

3

Use the Roles.GetRolesForUser() method https://msdn.microsoft.com/en-us/library/8h930x07(v=vs.110).aspx

string[] rolesArray = Roles.GetRolesForUser("username"); 

With the string being the User Name of the user as contained in the aspnetdb.

If you want to find by using a guid, you could try the following:

Guid userId; // ID of user - you can populate this somehow
MembershipUser memUser = Membership.GetUser(userId);
string[] roles = Roles.GetRolesForUser(memUser.UserName);
user1666620
  • 4,800
  • 18
  • 27
  • Sounds like the thing. If I had to be picky, I'd say that I wanted to find the roles by UserId and not the name\email, but I guess this will have to do ;) – jahu Mar 03 '15 at 14:27
  • @jahu I'm pretty sure the UserName property in aspnetdb has to be unique. If you want to find by the primary key of the user I will add an edit. – user1666620 Mar 03 '15 at 14:35
  • Unfortunately UserId is already used in the CRUD controller, so switching now to the name is not a good idea, but your answer does answer my question (as I wanted to know the Simple Membership way to get those roles). – jahu Mar 03 '15 at 14:39
  • @jahu .NET `IPrincipal`/`IIdentity` interfaces haven't ids at all. `Membership` users can haven't ids. F.e. some of users can be declared in `web.config` file without any id. So, `RoleProvider` uses `name`, not `id`. And, yes, this name must be unique. – Mark Shevchenko Mar 03 '15 at 14:44
0

Here is the stored procedure I mentioned in the question:

CREATE FUNCTION GetUserRoles
(
    @UserId int
)
RETURNS TABLE 
AS
RETURN 
(
    SELECT
        r.RoleName
    FROM
        dbo.webpages_UsersInRoles uir
        JOIN dbo.webpages_Roles r ON (r.RoleId = uir.RoleId)
    WHERE
        uir.UserId = @UserId
)
GO

The only reason to go with this instead than the answer by user1666620, would be if you wanted to skip one unnecessary query to the DB. The preferred method to use this solution would be to add this function to your dbml (or it's EF equivalent). Obviously this first needs to be added in the database.

jahu
  • 5,427
  • 3
  • 37
  • 64