首页 > 解决方案 > 根据角色和某些条件在 mvc 视图中渲染特定部分

问题描述

I have a requirement to render 4 buttons (Add, Delete, Read, Update) in a view based on the following conditions:

i) Each user can have multiple roles. 
ii) Each roles will have specific permissions for each pages. 

以下是我设计的表格。

Table Page: 
   i) PageId 
  ii) Page Name 
 iii) Controller Name 
  iv) Action Name

Table Role 
   i) RoleId 
  ii) Role Name

Table UserRole 
   i) UserId 
  ii) RoleId

Table Permission 
   i) PermissionId 
  ii) RoleId 
 iii) PageId 
  iv) CanRead <--- Boolean
   v) CanAdd  <--- Boolean
  vi) CanDelete <--- Boolean
 vii) CanUpdate <--- Boolean
viii) Controller Name 
  ix) Action Name




  UserId               RoleId
    -------------------- -----------
    2                    1
    2                    2
    4                    2
    5                    2

存储在 UserRole 表中的数据

Id          RoleId      PageId      CanView CanInsert CanUpdate CanDelete
    ----------- ----------- ----------- ------- --------- --------- ---------
    1           1           1           1       0         1         1
    2           1           2           1       0         0         1
    3           1           3           0       0         1         1
    4           2           1           0       0         0         0
    5           2           2           1       1         0         0
    6           2           3           0       0         1         1



Data Stored in permission table.

例如:角色管理员可以在用户页面中拥有读取、删除、添加或更新的权限。同一个角色可能只是在员工页面中只有读取权限。无论用户在哪里浏览到某个页面,都应根据他为该特定页面设置的角色和权限来呈现 4 个按钮。此外,用户可以同时拥有管理员和用户角色。

我希望我是有道理的,并正确地传达了我的要求。

标签: c#asp.net-mvcauthorizationasp.net-identityroles

解决方案


推荐阅读