# PostGuardian can\_delete\_post 中的空指针解引用

**URL:** <https://meta.discourse.org/t/nil-dereference-in-postguardian-can-delete-post/303223>\
**Category:** Bug\
**Created:** [2024年四月10日 12:18 UTC](https://meta.discourse.org/t/nil-dereference-in-postguardian-can-delete-post/303223 "2024-04-10T12:18:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)\
**Post date:** [2024年四月10日 12:18 UTC](https://meta.discourse.org/t/nil-dereference-in-postguardian-can-delete-post/303223/1 "2024-04-10T12:18:37Z")

</div>

`PostGuardian.can_delete_post` 包含一个 `user` 解引用，当没有用户登录时调用会崩溃。它正在从插件中调用，所以也许目前在核心中不会发生这种情况。

它发生在 [这里](https://github.com/discourse/discourse/blob/b7a2d29b7bc509dbd4de6bd4121b2397150f5a61/lib/guardian/post_guardian.rb#L210)：

`return true if user.in_any_groups?(SiteSetting.delete_all_posts_and_topics_allowed_groups_map)`

建议使用安全导航运算符 `&.` 或将其更改为 `return true if user && user.in_any_groups? ...`

---

<div class="post-metadata">

**Author:** ![Firepup650](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/firepup650/32/465200_2.png) [@Firepup650](https://meta.discourse.org/u/Firepup650)\
**Post date:** [2024年四月10日 12:58 UTC](https://meta.discourse.org/t/nil-dereference-in-postguardian-can-delete-post/303223/3 "2024-04-10T12:58:25Z")

</div>

您能否为此使用 @system 作为用户，因为它应该始终拥有权限？（另外，核心不也是这样做的吗？）

---

<div class="post-metadata">

**Author:** ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)\
**Post date:** [2024年四月10日 14:12 UTC](https://meta.discourse.org/t/nil-dereference-in-postguardian-can-delete-post/303223/4 "2024-04-10T14:12:33Z")

</div>

我不确定你是否理解。

没有用户不是问题（并且“没有用户”应该等于“没有权限”）。问题在于代码假定存在用户而没有进行检查，因此它会尝试查看“空”是否属于任何组。
