开发者

Data based on user role

开发者 https://www.devze.com 2023-03-06 04:49 出处:网络
I have a product with array of prices. I want that based on the user group the corresponding price will be displayed.

I have a product with array of prices. I want that based on the user group the corresponding price will be displayed.

As far i created roles that are describing these groups

  1. Admin
  2. UserPriceA
  3. UserPriceB
  4. UserPriceC

I would like somehow to get current users role.

Someth开发者_JS百科ing like this:

public decimal Price {
    get
    {
        var price = Prices.First(p => p.PriceType.Name == Something.CurentUser.Role);
        return price; 
    }
}


You can write a conditional statement based on the name of each role. Based on if the user is a member of the role, execute a different LINQ statement.

if (User.IsInRole("Admin")) {
    // ...
}
else if (User.IsInRole("UserPriceA")) {
   // ...
}
else if (User.IsInRole("UserPriceB")) {
   // ...
}
else if (User.IsInRole("UserPriceC")) {
   // ...
}


You can't do it quite like that because a user can have multiple roles but you could do

Prices.Where( p=> Membership.GetRoles().Contains(p.PriceType.Name))
typing on this phone isn't fun... Sorry for the brevity

0

精彩评论

暂无评论...
验证码 换一张
取 消