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

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

问题描述

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


  • 管理

  • 普通用户

  • 餐厅

每个这些用户中有它自己的能力和访问权限。这意味着,为他们每个人的看法应该是不同的。

我创建了型号为正常和餐厅。我想知道我怎么可以修改现有的结构,以支持此功能。

 公共类用户:IUSER
{
    公共用户()
        :这(的String.Empty)
    {
    }    公共用户(用户名字符串)
    {
        用户名=用户名;
        ID = Guid.NewGuid()的ToString()。
    }    [键]
    公共字符串ID {搞定;组; }    [需要]
    公共字符串用户名{获得;组; }    [需要]
    公共字符串名字{获得;组; }    [需要]
    公共字符串名字{获得;组; }    公共字符串电话{搞定;组; }
    公共字符串MobilePhone {搞定;组; }    [需要]
    [数据类型(DataType.EmailAddress)
    公共字符串电子邮件{获得;组; }    公共虚拟的IList< UserAddress>地址{搞定;组; }
}公共类餐厅
{
    [键]
    公众诠释ID {搞定;组; }    [需要]
    公共字符串名称{;组; }    公共虚拟的IList< RestaurantAddress>地址{搞定;组; }    公共虚拟的IList< RestaurantFood>菜单{搞定;组; }    公共虚拟的IList<回顾与GT;评论{搞定;组; }    [数据类型(DataType.Url)
    公共字符串网站{搞定;组; }    [数据类型(DataType.PhoneNumber)
    公共字符串电话{搞定;组; }    [数据类型(DataType.PhoneNumber)
    公共字符串传真{搞定;组; }    [数据类型(DataType.EmailAddress)
    公共字符串电子邮件{获得;组; }    公众诠释座椅{搞定;组; }    公共双AverageRating {搞定;组; }
    公共双AveragePrice {搞定;组; }
}


解决方案

我不知道,我已经得到了正确的Q,但是如果你使用的是互联网应用的模板,你可以将应用的访问控制使用简单管理角色管理。

首先,添加一些角色的分贝 webpages_Roles 表。

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

  Role.AddUserToRole(role1上);

现在,用于过滤的内容,你只需要做两份工作:

1)使用[授权] ATTRIB以适当的角色过滤器的要求

  [授权(角色=role1上,基于role2,...)]

2)渲染相应的内容的相对用户。第一检索当前用户的角色:

  VAR角色= Roles.GetRolesForUser(User.Identity.Name);

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

 布尔hasRole1 = roles.Contain(role1上)| roles.Contain(管理);
// ...
@if(hasRole1)
{
    //显示为role1上用户的内容...
}

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

  • Admin
  • Normal users
  • Restaurants

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; }
}

解决方案

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.

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) filter controller request to appropriate roles by use of [Authorize] attrib:

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

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天全站免登陆