user194076
user194076

Reputation: 9017

Filter users by roles

I have a regular MVC app with a standard account controller. I've added couple fields to UserProfile class, so now it looks like this:

 public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public string FullName { get { return FirstName + " " + LastName; } }
        public int GroupId { get; set; }
        [ForeignKey("GroupId")]
        public ICollection<Group> Groups { get; set; }
        }   

So, basically saying that User can be assigned to multiple groups. (And also in Group class I say that Group can contain multiple users, obviously).

Now, each user must be in a specific role. (Admin, Teacher, Student etc.)

I've added roles support by following a tutorial online like this:

<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
      <providers>
        <clear/>
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
      </providers>
    </roleManager>
    <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <clear/>
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
      </providers>
    </membership>   

Now I want to filter all of the users by RoleName. But I do not have any way to do that.
IE. something like this. Users.Where(x=>x.RoleName=="Admin").Select(x=>x.FullName)
(Users is a property of a Group class of type ICollection<Users>). How do I get to the roles without having a class for the roles?

Upvotes: 0

Views: 1395

Answers (2)

amhed
amhed

Reputation: 3659

Here's an example based on Luke's suggestion.

Your UsersContext class constructor should look like this:

 public class UsersContext : DbContext
    {
        public UsersContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<RoleModel> Roles { get; set; }
        public DbSet<UsersInRole> UsersInRoles { get; set; }
    }

Notice I added two additional DbSets. These are already on the membership database, this is just our way of mapping them.

"RoleModel" and "UsersInRole" models are declared below:

 [Table("webpages_Roles")]
public class RoleModel
{
    [Key]
    public int RoleId { get; set; }
    public string RoleName { get; set; }
}

[Table("webpages_UsersInRoles")]
public class UsersInRole
{
    [Key, Column(Order=0)]
    public int UserId { get; set; }
    public virtual UserProfile User { get; set; }

    [Key, Column(Order = 1)]
    public int RoleId { get; set; }
    public RoleModel Role { get; set; }
}

After that you could add a new navigational property to your UserProfile Model like so:

[Table("UserProfile")]
public class UserProfile
{
   [Key]
   [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
   public int UserId { get; set; }
   public string UserName { get; set; }
   public virtual ICollection<UsersInRole> UsersInRole { get; set; }
}

And then call it with something like:

var context = new UsersContext();
context.UserProfiles.First(d => d.UserName == "username").UsersInRole.Select(d => d.Role);

But in all honesty, probably using the GetUsersInRole method mentioned by @awright18 is easier.

Upvotes: 0

awright18
awright18

Reputation: 2333

The asp.net membership has a static Roles class with a GetUsersInRole method, also the CurrentUser in HttpContext can tell you the roles the active user is in.

http://msdn.microsoft.com/en-us/library/system.web.security.roles.getusersinrole.aspx

http://msdn.microsoft.com/en-us/library/system.web.security.membershipuser.aspx

Upvotes: 1

Related Questions