简体   繁体   English

插件的 Strapi v4 扩展服务器 API 不起作用

[英]Strapi v4 Extending Server API for Plugins does not work

I am trying to follow the Strapi v4.0.0 guide on https://docs.strapi.io/developer-docs/latest/developer-resources/plugin-api-reference/server.html#entry-file for extending the users-permission plugin to add a custom route/controller, but so far have been unsuccessful.我正在尝试按照https://docs.strapi.io/developer-docs/latest/developer-resources/plugin-api-reference/server.html#entry-file上的 Strapi v4.0.0 指南扩展用户 - permission plugin 来添加自定义路由/控制器,但目前为止一直没有成功。 I add the custom files as stated in the docs, but there is no change in the UI.我按照文档中的说明添加了自定义文件,但 UI 没有变化。

I managed to get this to work for normal API highlighted in yellow, but was unable to do so for the users-permission plugin我设法让它在黄色突出显示的正常 API 上工作,但无法为用户权限插件这样做

在此处输入图像描述

In the previous version 3.6.8 this functionality was allowed through the extensions folder.在以前的版本 3.6.8 中,此功能是通过扩展文件夹允许的。

Am I missing something from the new guide, I even tried copying the files from node_modules > @strapi > plugin-users-permission and adding a new route and method to the exiting controller file but it still does not reflect the change in the section where we assign different route permission to roles.我是否遗漏了新指南中的某些内容,我什至尝试从node_modules > @strapi > plugin-users-permission复制文件并将新的路由和方法添加到现有的 controller 文件中,但它仍然没有反映其中的部分中的更改我们为角色分配不同的路由权限。 The user-permission plugin still shows the original routes, with no change.用户权限插件仍然显示原始路由,没有任何变化。

在此处输入图像描述

Thanks,谢谢,

When exporting routes you need to export the type, either content-api or admin-api.导出路由时,您需要导出类型,content-api 或 admin-api。 Look at the Strapi email plugin in node_modules for example, change the folder and file structure in your routes folder to match that and then you will be able to set permissions in the admin panel.例如,查看 node_modules 中的 Strapi email 插件,更改路由文件夹中的文件夹和文件结构以匹配,然后您将能够在管理面板中设置权限。

I ran into this thread while researching pretty much the same issue, and I wanted to share my solution.我在研究几乎相同的问题时遇到了这个线程,我想分享我的解决方案。

First of all, I found this portion of the documentation more useful than the one you referenced: https://docs.strapi.io/developer-docs/latest/development/plugins-extension.html首先,我发现这部分文档比您引用的文档更有用: https://docs.strapi.io/developer-docs/latest/development/plugins-extension.html

My goal was the write a new route to validate JWT tokens based on the comment made here: https://github.com/strapi/strapi/issues/3601#issuecomment-510810027 but updated for Strapi v4.我的目标是根据此处的评论编写一条新路线来验证 JWT 令牌: https://github.com/strapi/strapi/issues/3601#issuecomment-510810027但已针对 Strapi v4 更新。

The solution turned out to be simple:解决方案很简单:

  1. Create a new folder structure: ./src/extensions/user-permissions if it does not exist.创建一个新的文件夹结构: ./src/extensions/user-permissions如果它不存在。
  2. Create a new file ./src/extensions/user-permissions/strapi-server.js if it does not exist.如果不存在,则创建一个新文件./src/extensions/user-permissions/strapi-server.js
  3. Add the following to the file:将以下内容添加到文件中:
module.exports = (plugin) => {
  plugin.controllers.<controller>['<new method>'] = async (ctx) => {
    // custom logic here
  }

  plugin.routes['content-api'].routes.push({
    method: '<method>',
    path: '/your/path',
    handler: '<controller>.<new method>',
    config: {
      policies: [],
      prefix: '',
    },
  });

  return plugin;
};

If you're unsure what controllers are available, you can always check the API documentation or console.log(plugin) or console.log(plugin.controllers) .如果您不确定可用的控制器,您可以随时查看 API 文档或console.log(plugin)console.log(plugin.controllers)

After the admin server restarts, you should see your new route under the user-permissions section as you would expect, and you can assign rights to it as you see fit.管理服务器重新启动后,您应该会在用户权限部分下看到您所期望的新路由,并且您可以根据需要为其分配权限。

My full strapi-server.js file including the logic to validate JWT:我的完整strapi-server.js文件包括验证 JWT 的逻辑:

module.exports = (plugin) => {
  plugin.controllers.auth['tokenDecrypt'] = async (ctx) => {
    // get token from the POST request
    const {token} = ctx.request.body;

    // check token requirement
    if (!token) {
      return ctx.badRequest('`token` param is missing')
    }

    try {
      // decrypt the jwt
      const obj = await strapi.plugin('users-permissions').service('jwt').verify(token);

      // send the decrypted object
      return obj;
    } catch (err) {
      // if the token is not a valid token it will throw and error
      return ctx.badRequest(err.toString());
    }
  }

  plugin.routes['content-api'].routes.push({
    method: 'POST',
    path: '/token/validation',
    handler: 'auth.tokenDecrypt',
    config: {
      policies: [],
      prefix: '',
    },
  });

  return plugin;
};

If your Strapi server is using Typescript, make sure that you name your extension files accordingly.如果您的 Strapi 服务器使用 Typescript,请确保相应地命名您的扩展文件。 So instead of strapi-server.js , you would need to name your file strapi-server.ts .因此,您需要将文件strapi-server.ts而不是strapi-server.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM