不同能力的用户 [英] Users with different capabilities

查看:27
本文介绍了不同能力的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 ASP.Net MVC 5 应用程序.在我的网站中,存在 3 种不同类型的用户.

I'm creating an ASP.Net MVC 5 application. In my website, 3 different types of users exist.

  • 管理员
  • 普通用户
  • 餐厅

这些用户中的每一个都有自己的能力和访问权限.意思是,他们每个人的观点应该不同.

Each of these users have its own capabilities and access rights. Meaning, the view for each of them should be different.

我已经为普通餐厅和餐厅创建了模型.我想知道如何修改现有结构以支持此功能.

I have created the models for both of the normal and restaurant. I was wondering how I can modify my existing structure to support this functionality.

public class User : IUser
{
    public User()
        : this(String.Empty)
    {
    }

    public User(string userName)
    {
        UserName = userName;
        Id = Guid.NewGuid().ToString();
    }

    [Key]
    public string Id { get; set; }

    [Required]
    public string UserName { get; set; }

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    public string Phone { get; set; }
    public string MobilePhone { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    public virtual IList<UserAddress> Addresses { get; set; }
}

public class Restaurant
{
    [Key]
    public int ID { get; set; }

    [Required]
    public string Name { get; set; }

    public virtual IList<RestaurantAddress> Addresses { get; set; }

    public virtual IList<RestaurantFood> Menu { get; set; }

    public virtual IList<Review> Reviews { get; set; }

    [DataType(DataType.Url)]
    public string Website { get; set; }

    [DataType(DataType.PhoneNumber)]
    public string Phone { get; set; }

    [DataType(DataType.PhoneNumber)]
    public string Fax { get; set; }

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    public int Seats { get; set; }

    public double AverageRating { get; set; }
    public double AveragePrice { get; set; }
}

推荐答案

我不确定我的 Q 是否正确,但是如果您使用的是 Internet 应用程序模板,则可以使用以下命令简单地管理应用程序的访问控制角色管理.

I'm not sure that I've got your Q correctly, But if you are using internet application template, you can simply manage access controls of your app using role management.

首先,在你的数据库的webpages_Roles表中添加一些角色.

First of all, add some roles to the webpages_Roles table of your db.

然后,只需将用户添加到这些角色:

Then, simply add users to those roles:

Role.AddUserToRole("role1");

现在,为了过滤内容,你只需要做两件事:

Now, for filtering contents, you just need to do two jobs:

1) 使用 [Authorize] 属性过滤控制器对适当角色的请求:

1) filter controller request to appropriate roles by use of [Authorize] attrib:

[Authorize(Roles = "role1, role2, ...")]

2) 将适当的内容呈现给相关用户.首先检索当前用户的角色:

2) Render the appropriate content to the relative user. First retrieve the roles of the current user:

var roles = Roles.GetRolesForUser(User.Identity.Name);

然后,根据他/她的角色,为他/她渲染内容:

Then, according to his/her roles, Render contents for him/her:

bool hasRole1 = roles.Contain("role1") | roles.Contain("admin");
// ...
@if (hasRole1)
{
    // Show content for role1 users...
}

这篇关于不同能力的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆