STRAPI - 与集合自身属性的关系

问题描述 投票:0回答:1

我正在构建一个社交媒体应用程序,类似于 Instagram,我正在尝试向该应用程序添加固定帖子功能。此功能应该有效,以便用户可以将他们的 3 个帖子固定在他们的个人资料中首先显示。

我现在的问题是用户可以固定任何帖子,即使是那些不是由他们制作的帖子。因此,在 Strapi 中,我希望能够与用户自己的帖子创建关系,这样他们就无法固定他们想要的任何帖子。是否可以创建与创建者自己的帖子列表的关系,或者使用组件或集合的其他方式来创建此功能?

**In Strapi I have collections:**
**User**
- name
- description
- ...
- posts - relation to posts
- pinned - relation to posts

**Posts**
- title
- description
- ...
- creator - relation to user

尝试将固定的内容添加为关系和组件,但不确定如何仅固定用户自己的帖子。

content-management-system strapi
1个回答
0
投票

您需要使用策略来确定用户是否是该帖子的所有者,如果是,则让他们固定该帖子,如果不是,则返回错误。

这是一个基本政策示例:

export default (policyContext, config, { strapi }) => {
  if (policyContext.state.user) { 

    // you can write your logic here to check id the user that is
    // trying to pin the post is the owner if so this would return true

    // if a session is open
    // go to next policy or reach the controller's action
    return true;
  }

  return false; // if user is not the owner you would return false which would block for an unauthorized user being able to pin post that they don't own.
};

您可以在此处了解有关使用政策的更多信息

strapi 文档 政策

© www.soinside.com 2019 - 2024. All rights reserved.