如何在 MVC 中处理基于角色的视图?

发布于 2024-09-14 07:47:27 字数 449 浏览 9 评论 0原文

无论使用哪种语言或MVC框架,我应该如何处理基于角色的不同视图?

例如(伪代码):

views/post/show:

<% show post here %>
if (role.isAdmin or role.isModerator) {
   <% show moderation tools %>
}
<% show rest of content %>

我不太喜欢将太多业务逻辑放入视图中的想法,但似乎没有其他选择。有吗?

一旦您拥有更多角色或不同级别的权限,情况就会变得越来越混乱。以这个网站为例。代表数超过 200 的用户看到的广告较少。代表数超过 500 的用户有一个重新标记按钮。然后,您可以在 2000 处获得编辑按钮,在 3000 处获得关闭按钮,在 10k 处获得审核工具,如果您是“明星”版主,还可以获得更多功能。

Regardless of the language or MVC framework used, how should I handle different views based on roles?

For example (pseudo code):

views/post/show:

<% show post here %>
if (role.isAdmin or role.isModerator) {
   <% show moderation tools %>
}
<% show rest of content %>

I don't quite like the idea of putting too much business logic into the view, but it doesn't seem like there're other options. Are there?

This gets messier and messier once you have more roles, or different levels of permissions. Take this site for example. Users with more than 200 rep see less ads. Users with more than 500 rep have a retag button. Then you get an edit button at 2000, a close button at 3000, moderation tools at 10k, and more functions if you are a "star" moderator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲性难收 2024-09-21 07:47:27

您可以通过使用带有名为 ShowModerationTools 的布尔属性的自定义 ViewModel 来使此过程变得更简洁。在您的控制器中,您执行检查以查看当前用户是否可以根据其角色查看帖子,并将 ShowModerationTools 设置为 truefalse.然后,您将自定义 ViewModel 返回到视图。这样您就可以在您的视图中执行此操作:

<% show post here %>
if (Model.ShowModerationTools) {
   <% show moderation tools %>
}
<% show rest of content %>

这也意味着如果您的业务规则发生变化(例如您需要引入另一个条件),您只需更改控制器而无需更改您的视图。

You can make this a little neater by having a custom ViewModel with a boolean property called ShowModerationTools. In your controller you perform the checks to see whether the current user, based on their roles, can see the post and set ShowModerationTools to either true or false. You then return the custom ViewModel to the view. That way you can then just do this in your view:

<% show post here %>
if (Model.ShowModerationTools) {
   <% show moderation tools %>
}
<% show rest of content %>

It also means if your business rules change (for instance you need to introduce another condition) you just change the controller and don't need to alter your view.

菊凝晚露 2024-09-21 07:47:27

这并没有什么问题。这不是业务逻辑。这是表示逻辑。

if 回答的问题是:我们应该展示审核工具吗?
它不像:“普通用户是否应该能够删除整个付款历史记录”之类的。

There is nothing wrong with this. It's not business logic. It's presentation logic.

Question that this if answers is: should we show moderation tools?
It's not like: 'should regular user be able to delete whole history of payments' or something.

夜无邪 2024-09-21 07:47:27

我同意 Arnis L. - 但会添加以下内容。

你可以这样做......

<% show post here %>
<% Html.RenderAction<MyControllerName>(m => m.ModerationTools()); %>
<% show rest of content %>

这有几个好处。

  1. 它将审核工具所需的模型和视图封装在单个操作中。

  2. 它可能会消除您在作为示例发布的页面中的模型上拥有角色的需要

  3. 它可以在其他页面上重复使用。

I agree with Arnis L. - but will add the following.

You could do this instead...

<% show post here %>
<% Html.RenderAction<MyControllerName>(m => m.ModerationTools()); %>
<% show rest of content %>

This has several benefits.

  1. It encapsulates the Model and View required for moderation tools in a single Action.

  2. It will probably remove the need for you to even have the role on the Model in the page you've posted as an example

  3. It can be re-used on other pages.

梦情居士 2024-09-21 07:47:27

就我个人而言,我认为这里更重要的是好和足够好之间的区别......

虽然从技术上讲我认为这属于业务逻辑,但对于这样一个简单的情况,我没有看到将其包含在视图中的问题。

现在,如果您有更复杂的逻辑,我建议为每个“逻辑分支”创建一个新视图,并在控制器中在它们之间进行选择。

Personally, I think what's more important here is the difference between good and good enough...

While technically I think that classifies as business logic, for such a simple case I don't see a problem including it in the view.

Now, if you had more complex logic, I'd suggest creating a new view for each "logic branch", and choosing between them in the controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文