【问题标题】:Why all users in Strapi have access to update all users profile?为什么 Strapi 中的所有用户都有权更新所有用户配置文件?
【发布时间】:2023-01-27 04:39:12
【问题描述】:

I added a new filed called config (type: json) to the User model. I use built-in swagger of Strapi local document. The problem is that I can update another user config (data) with put method.

  1. First, I authorized by POST /auth/local and get my token and my user id (in this cast it's 5)
  2. I add my token to swagger Authorize button.
  3. Then, I use PUT /user/{id} in this case id is 5.
  4. Calling api http://localhost:1337/api/users/4 returns 200!

I expect that I get 403 error! Because I should not able to change other user profiles!!! Is it normal? If yes, tell me a solution to fix this.

  • The link to the documentation is a link to localhost. I don't have this documentation on my system.
  • I mean that I use default swagger. @jebaa

标签: javascript node.js backend strapi


【解决方案1】:

这是因为 Strapi 只有两个默认角色:

  • 公开
  • 已验证

因此,默认情况下,当您设置权限时,无论当前的身份验证状态是什么,用户都可以相应地访问所有内容(例如,公开到仅公开,已验证到已验证)

要使用它,并限制 auth 范围内的用户操作,您必须使用中间件或策略,因此由于这是在用户权限范围内,让我们将策略添加到用户权限:

斯特拉皮 4.5.3

yarn strapi generate

? Strapi Generatos
>policy

? Policy name 
isOwner

? Where do you want to add this policy?
> Add policy to root of project

下一步是在您的 /src/extensions 文件夹中,您必须创建文件夹 users-permissions,并在此文件夹中创建包含以下内容的文件 strapi-server.js

/src/extensions/users-permissions/strapi-server.js

module.exports = (plugin) => {
  for (let i = 0; i < plugin.routes["content-api"].routes.length; i++) {
    const route = plugin.routes["content-api"].routes[i];
    if (
      route.method === "GET" &&
      route.path === "/users/:id" &&
      route.handler === "user.findOne"
    ) {
      console.log(route);
      plugin.routes["content-api"].routes[i] = {
        ...route,
        config: {
          ...route.config,
          policies: route.config.policies
            ? [...route.config.policies, "global::isOwner"] // tests if policies were defined
            : ["global::isOwner"],
        },
      };
    }
  }

  return plugin;
};

如果您在 strapi 服务器控制台中执行了正确的步骤,您必须看到: info: In isOwner policy. 如果您向 /api/users/:id 发送获取请求

下一步是我们将像这样修改策略文件: /src/policies/isOwner.js

"use strict";

/**
 * `isOwner` policy
 */

module.exports = async (policyContext, config, { strapi }) => {
  strapi.log.info("In isOwner policy.");
  const { user, auth } = policyContext.state;
  const { params } = policyContext;

  // this case the userId is the same as the id we are requesting
  // other cases would need more extensive validation...
  const canDoSomething = user.id == params.id;

  if (canDoSomething) {
    return true;
  }

  return false;
};

哇哦:

{
    "data": null,
    "error": {
        "status": 403,
        "name": "PolicyError",
        "message": "Policy Failed",
        "details": {}
    }
}

如果我们尝试获取其他用户配置文件

【讨论】:

    【解决方案2】:

    @antokhio

    我遵循了您的解决方案,但它不起作用(我仍然可以访问任何 id 成员)。我不知道为什么...

    我对此也有疑问:

    • 如果我们通过 /api/users?filter (例如,在 id 或电子邮件上)访问,我认为这不起作用?

    • 为了确保 PUT,我只需要修改 strapi-server.js ?

      route.method === "获取" ||路线.方法===“放”?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多