【问题标题】:Create User Profile Piece创建用户资料片
【发布时间】:2021-06-05 02:49:51
【问题描述】:

我想创建一个用户个人资料片段,它加入撇号用户,以便管理员可以为特定用户制作公开个人资料。我知道不建议这样做,但我认为这是让这一方的人有可能拥有公共个人资料并能够登录和编辑他们的个人资料的唯一选择。否则,管理员需要为数百人更改用户配置文件,这将导致全职工作。现在不需要用户注册,但将来可能需要,所以我的模块应该与 apsotrophe-signup 兼容。

开头很简单:

// lib/modules/profiles/index.js
module.exports = {
  extend: 'apostrophe-pieces',
  name: 'profile',
  label: 'Profile',
  pluralLabel: 'Profiles',
  slugPrefix: 'profile-',
  addFields: [
    {
        name: '_user',
        label: 'User',
        type: 'joinByOne',
        withType: 'apostrophe-user',
        idField: 'userId',
        withJoins: true
    }
  ]
};

这是碎片小部件:

// lib/modules/profiles-widgets/views/widget.html

{% for piece in data.widget._pieces %}
  <li class="collection-item avatar">

    {{ piece._user.username }}

    {{ piece._user.title }}

    {{ piece._user.firstName }}

    {{ piece._user.lastName }}

    {{ piece._user.email }}

    <img class="circle grey"
      src="{{ apos.attachments.url(piece._user._thumbnail.attachment, { size: data.options.size or 'one-sixth' }) }}"
      srcset="{{ apos.images.srcset(piece._user._thumbnail.attachment) }}"
      sizes="{{ '8vw' }}"
      alt="{{ piece._user._thumbnail.title }}"
    >

    {{ apos.area(piece._user, 'bio', { edit: false }) }}

  </li>
{% endfor %}

我添加到apostrophe-user的字段_thumbnailbio

// lib/modules/apostrophe-users/index.js
module.exports = {
  beforeConstruct: function(self, options) {
      options.addFields = [
        {
          name: '_thumbnail',
          type: 'joinByOne',
          withType: 'apostrophe-image',
          label: 'Profile Picture',
          help: 'Choose profile picture',
          filters: {
            projection: {
              attachment: true,
              description: true,
              title: true
            }
          }
        },
        {
          name: 'bio',
          label: 'Biography',
          type: 'area',
          help: 'Choose Biography',
          options: {
            limit: 1,
            widgets: {
              'apostrophe-rich-text': {
                toolbar: [ 'Bold' ],
                controls: {
                  movable: false,
                  cloneable: false,
                  removable: true,
                  position: 'top-right'
                }
              }
            }
          }
        }
      ].concat(options.addFields || []);
  }
};

登录后,我可以看到widget.html 中定义的所有字段,但是如果我退出,我什么也看不到。我预计这个原因 apostrophe-users 未由 default 发布。 我尝试将published 字段添加到apostrophe-user,但这没有成功。

// lib/modules/apostrophe-users/index.js
...

        {
          type: 'boolean',
          name: 'published',
          label: 'Published',
          def: true,
        },
...

我做错了什么?

作为第二个问题,我想知道是否可以允许登录用户编辑他自己的个人资料?

【问题讨论】:

    标签: apostrophe-cms


    【解决方案1】:

    在 Apostrophe 2 中不直接支持将用户用作公开可用数据。一种常见的解决方法是创建一个“个人资料”片段类型或类似的东西,然后添加一个连接以将其连接到用户,如果这有助于组织。

    在用户模块上设置adminOnly: false,您可以直接配置权限,但它会使围绕用户的安全问题成为您职责的一部分。

    更新: 在这样的项目中写入lib/modules/apostrophe-users/index.js 会将published 字段添加回用户:

    module.exports = {
      defaultRemoveFields: [],
      defaultRemoveFilters: [],
      defaultRemoveColumns: []
    };
    
    

    默认情况下,这些数组包括 'published',从 UI 中隐藏发布选项。

    一旦发布,它们将在注销时可见。同样,请确保在执行此操作时考虑到安全隐患。

    【讨论】:

    • “然后添加连接以将其连接到用户”这正是我所做的。更具体地说,我只需要帮助将已发布的字段添加回用户,就像我做的那样不起作用。您还在文档中说我可以将其添加回来,但这不起作用,至少不是我这样做的方式。请你再看看我的问题。
    • Thx 'defaultRemoveFields' 为我做的,我不知道这个选项,并打算从 this 声明中,我需要将它们添加为正常字段,如上所示。也许这有点误导,可以根据您的解释进行更新。
    • 当然。我已经公关了文档以使其更清晰。
    猜你喜欢
    • 2020-10-09
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多