MVC 框架、API 层和身份验证/权限

发布于 2024-10-19 07:36:29 字数 615 浏览 2 评论 0原文

我之前问了一个问题,但它可能太模糊或广泛以保证答案。从那时起,我重新考虑了我的方法和我的问题。

我正在开发一个考虑到特定应用程序的框架,但显然我将可重用性放在首位。该框架的关键要素是公开 API 层,用于 JSON 驱动的远程交互。

我在计划这个问题时遇到的问题是身份验证/权限如果我在设计时考虑了 MVC 风格的架构,但又想允许通过 API 层使用,那么身份验证/权限检查应该在哪里进行?

Web 访问层本质上与 API 相同层,唯一的区别是输出方法(HTML 与 JSON),因此我认为两者的通用业务逻辑层是有意义的。请求将被标准化为数据结构,并发送到业务逻辑。业务逻辑最终会产生一个结果数据结构(查询结果、失败、成功等),该结构要么馈送到模板引擎,要么序列化为 JSON。

在我看来,权限检查需要在公共业务逻辑层中执行,但是是否有更好的地方将其合并到 MVC/API 框架中?

我现在不知道还能在哪里详细说明,所以请随时询问更多详细信息,我会在想到后提供。

I asked a question earlier, however it may have been too vague or broad to warrant an answer. I've since reconsidered my approach and my question.

I'm working on a framework with specific application in mind, but obviously am keeping re-usability at the forefront. The key element of this framework is exposing an API layer, for JSON driven remote interaction.

My issue in planning this out comes with authentication/permissions. If I'm designing with an MVC style architecture in mind, yet want to permit usage via the API layer, where should the authentication/permissions check take place?

The web access layer is essentially the same as API layer, the only difference being the output method (HTML vs. JSON), so I figured a common business logic layer for the two would make sense. Requests would be normalized to a data structure, and sent to the business logic. The business logic ultimately kicks out a result data structure (query results, failure, success, etc.) that is either fed to the Template engine, or serialized to JSON.

It appears to me that the permissions check needs to be performed in the common business logic layer, but is there a better place to incorporate this into an MVC/API framework?

I don't know where else to elaborate at the moment, so feel free to ask for more details and I'll provide as I come up with them.

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

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

发布评论

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

评论(2

千纸鹤带着心事 2024-10-26 07:36:29

您打算如何允许用户访问 API?

以下是我如何进行基于角色的授权:

  • 每个控制器通常有多个前端可以访问的操作(方法),例如
    • 函数action_get($id)
    • 函数action_delete($id)
  • 每个控制器都有一个类属性,指定每个操作需要哪些角色,例如
    受保护的$access = array(
        '得到' => NULL, // 每个人都可以访问;不过这条线不是必需的
        '删除' => array('admin') // 只有管理员可以访问
    )
  • before() 方法在执行操作之前被触发。它根据 $access 列表进行授权检查。
  • 每个操作负责确定要输出的内容,例如,
public function action_get($id) {
      // Business logic... build data structure
      # ...

      if (Request::is_ajax()) {
          // Output JSON
      }
      else {
          // Output HTML
      }
  }

该系统可以轻松扩展以允许 API 调用。要么扩展该方法的输出部分,检测它是否是 API 请求,要么只是专门为 API 调用创建另一个操作。如果您选择后一种方法并希望保持代码干燥,请将数据结构的业务逻辑移至辅助方法(例如受保护的函数_get())。可能还有更好的方法来提供对 API 的访问和处理 API。这实际上取决于您希望如何允许访问以及您希望使其变得多么灵活/干燥。

How do you plan on allowing users to access the API?

Here's how I do role-based authorization:

  • Each controller typically has multiple actions (methods) that are accessible to the frontend, e.g.
    • function action_get($id)
    • function action_delete($id)
  • Each controller has a class property that specifies which roles are required for each action, e.g.
    protected $access = array(
        'get' => NULL,  // everybody can access; this line isnt necessary though
        'delete' => array('admin')  // only admins can access
    )
  • A before() method is fired before executing an action. It does the authorization check based on the $access list.
  • Each action is responsible for determining what to output, e.g.
public function action_get($id) {
      // Business logic... build data structure
      # ...

      if (Request::is_ajax()) {
          // Output JSON
      }
      else {
          // Output HTML
      }
  }

That system could easily be expanded to allow for API calls. Either expand on the output portion of the method, detecting if its an API request, or simply create another action specifically for the API call. If you choose the latter method and wish to keep your code DRY, move the business logic for the data structure to a helper method (e.g. protected function _get()). There may be better ways of providing access to and handling the API as well.. it really depends on how you want to allow access and how flexible/dry you want to make it.

╰沐子 2024-10-26 07:36:29

一般来说,身份验证是在控制器级别执行的(通常在控制器类的构造函数中,因此该控制器中的每个方法都会调用它)。当然,模型中会有一些用户/角色数据,但控制器会将请求与权限等进行比较,并允许或不允许该方法(然后该方法将决定输出 html、json、或其他)。

In general, authentication is performed at the controller level (usually in a constructor of the controller class so it's called for every method in that controller). You would of course have some of the user/role data in models, but the controller is what would compare the request to the permissions, etc. and either allow the method or not (and then the method would decide to output html, json, or whatever).

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